zoukankan      html  css  js  c++  java
  • 蜂鸣器类代码

    /*
    标题:蜂鸣器的使用
    电路:P1.0口接P11
    
    */
    #include "sys.h"//包含延时函数头文件
    #include <reg52.h>
    #include "beep1.h"
    
    sbit beez = P2^1;//位声明,P2端口的第一位为蜂鸣器所用
    void beep1()     //非空函数+函数名
    {
        while(1)       //while死循环
        {
            beez = 1;      //赋高电平
            delays(100);  //调用延时函数
            beez = 0;      //赋低电平
            delays(100);  //同上
        }
    }
    
    
    //----------------------------------------------
    /*
    if not defined的简写为ifndef,c语言在对程序进行
    编译时,会先根据预处理命令进行“预处理”。C语言
    编译系统包括预处理,编译和链接等部分。
    */
    #ifndef _BEEP1_H_                 //先测试函数是否被宏定义过 
    #define _BEEP1_H_ 
    
    void beep1();                     //如果程序没有被定义过,定义函数
                                      //并编译程序段beep1
    
    #endif                            //终止if,条件指示符#endif的最主要目的是
                                      //防止头文件的重复包含和编译。
    /*
    标题:蜂鸣器发出不同的音调
    电路:p2.1口接P11
    
    */
    
    #include <reg52.h>
    #include "sys.h"
    #include "beep2.h"
    sbit beez = P2^1;
    
    void beep2(){
        int i;
        while(1){
        for(i = 0;i<1000;i++){
                beez = 0;
                delays(100);
                beez = 1;
                delays(100);
            }
            
            for(i = 0;i<1000;i++){
                beez = 0;
                delays(200);
                beez = 1;
                delays(200);
            }
            
            for(i = 0;i<1000;i++){
                beez = 0;
                delays(50);
                beez = 1;
                delays(50);
            }
            
            for(i = 0;i<1000;i++){
                beez = 0;
                delays(150);
                beez = 1;
                delays(150);
            }
        
        }
            
    
    }
    
    
    
    //----------------------------------------------
    #ifndef _BEEP2_H_
    #define _BEEP2_H_
    
    void beep2();
    
    #endif
    /*
    标题:蜂鸣器播放音乐
    电路:p2.1口接P11
    
    */
    
    
    #include <reg52.h>
    #include "beep3.h"
    #include "sys.h"
    
    //八月桂花
    unsigned char code music_code[] ={   
    0x18, 0x30, 0x1C , 0x10, //格式为:频率常数、节拍常数、频率常数、节拍常数   
    0x20, 0x40, 0x1C , 0x10,   
    0x18, 0x10, 0x20 , 0x10,   
    0x1C, 0x10, 0x18 , 0x40,   
    0x1C, 0x20, 0x20 , 0x20,   
    0x1C, 0x20, 0x18 , 0x20,   
    0x20, 0x80, 0xFF , 0x20,   
    0x30, 0x1C, 0x10 , 0x18,   
    0x20, 0x15, 0x20 , 0x1C,   
    0x20, 0x20, 0x20 , 0x26,   
    0x40, 0x20, 0x20 , 0x2B,   
    0x20, 0x26, 0x20 , 0x20,   
    0x20, 0x30, 0x80 , 0xFF,   
    0x20, 0x20, 0x1C , 0x10,   
    0x18, 0x10, 0x20 , 0x20,   
    0x26, 0x20, 0x2B , 0x20,   
    0x30, 0x20, 0x2B , 0x40,   
    0x20, 0x20, 0x1C , 0x10,   
    0x18, 0x10, 0x20 , 0x20,   
    0x26, 0x20, 0x2B , 0x20,   
    0x30, 0x20, 0x2B , 0x40,   
    0x20, 0x30, 0x1C , 0x10,   
    0x18, 0x20, 0x15 , 0x20,   
    0x1C, 0x20, 0x20 , 0x20,   
    0x26, 0x40, 0x20 , 0x20,   
    0x2B, 0x20, 0x26 , 0x20,   
    0x20, 0x20, 0x30 , 0x80,   
    0x20, 0x30, 0x1C , 0x10,   
    0x20, 0x10, 0x1C , 0x10,   
    0x20, 0x20, 0x26 , 0x20,   
    0x2B, 0x20, 0x30 , 0x20,   
    0x2B, 0x40, 0x20 , 0x15,   
    0x1F, 0x05, 0x20 , 0x10,   
    0x1C, 0x10, 0x20 , 0x20,   
    0x26, 0x20, 0x2B , 0x20,   
    0x30, 0x20, 0x2B , 0x40,   
    0x20, 0x30, 0x1C , 0x10,   
    0x18, 0x20, 0x15 , 0x20,   
    0x1C, 0x20, 0x20 , 0x20,   
    0x26, 0x40, 0x20 , 0x20,   
    0x2B, 0x20, 0x26 , 0x20,   
    0x20, 0x20, 0x30 , 0x30,   
    0x20, 0x30, 0x1C , 0x10,   
    0x18, 0x40, 0x1C , 0x20,   
    0x20, 0x20, 0x26 , 0x40,   
    0x13, 0x60, 0x18 , 0x20,   
    0x15, 0x40, 0x13 , 0x40,   
    0x18, 0x80, 0x00   
    };   
    
    unsigned char* pin;//频率
    unsigned char* pai;//节拍
    sbit beez = P2^1;
    
    void beep3()
    {
        int i,j;
        play://为goto所用,当程序执行到goto play时,重回此
        pin = music_code;
        pai = pin+1;
        beez = 0;
        while(1)
            {
            if(pin == 0x00)//if条件函数
                {
                //遇到结束符
                goto play;
              }
            if(pai == 0xff)
                {
                //遇到休止符,等待一段时间
                delays(200000);
              }
            for(i=0;i<(*pai)*10;i++)//for嵌套
                {
                
                for(j=0;j<(*pin);j++)
                    {
                    beez = ~beez;
                  }
            
              }
            pin += 2;
            pai += 2;
            
        }
    
    }
    
    
    
    //------------------------------------------------
    #ifndef _BEEP3_H_
    #define _BEEP3_H_
    
    void beep3();
    
    #endif
  • 相关阅读:
    CF 444B(DZY Loves FFT-时间复杂度)
    摆弄【Nhibernate 协会制图--导乐陪伴分娩】
    固定的报文统计报告的规定
    CSS——(2)与标准流盒模型
    自动复制转换StringBuffer
    IM信息网
    Oracle Redo Log
    【转载】有哪些省时小技巧,是每个Linux用户都应该知道的
    Linux snmp
    MySQL zabbix
  • 原文地址:https://www.cnblogs.com/kinson/p/6838136.html
Copyright © 2011-2022 走看看