51单片机—用蜂鸣器播放音乐

来源:互联网 发布:如何提高淘宝动态评分 编辑:程序博客网 时间:2024/05/02 01:06
#include <reg52.h>#include "./delay/delay.h"//sbit key1 = P1^0;sbit beep = P2^7;#define SEGPORT P0#define KEYPORT P1sbit bit_select = P2^0;sbit seg_select = P2^1;unsigned char segdata[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};unsigned char TempData[2];unsigned char High,Low;unsigned char code freq[][2]={   0xD8,0xF7,//00440HZ 1     0xBD,0xF8,//00494HZ 2    0x87,0xF9,//00554HZ 3    0xE4,0xF9,//00587HZ 4      0x90,0xFA,//00659HZ 5       0x29,0xFB,//00740HZ 6       0xB1,0xFB,//00831HZ 7        0xEF,0xFB,//00880HZ `1};void display(){static unsigned char i = 0;SEGPORT = 0x0;seg_select = 1;seg_select = 0;SEGPORT = 0xff;bit_select = 1;bit_select = 0; //消重影SEGPORT = TempData[i];seg_select = 1;seg_select = 0;SEGPORT = bitdata[i];bit_select = 1;bit_select = 0;i++;if(2 == i){i = 0;}}void timer0_isr(void) interrupt 1{TH0 = High;TL0 = Low;beep = ~beep;}//0xD8,0xF7void timer0_init(void){EA = 1;TMOD |= 0x01;TH0 = 0;TL0 = 0;ET0 = 1;TR0 = 0;}void main(){     unsigned char num = 0;unsigned char disdata = 0;     timer0_init();       while(1) {   switch(KEYPORT)  {  case 0xfe:num = 1;break;case 0xfd:num = 2;break;case 0xfb:num = 3;break;case 0xf7:num = 4;break;case 0xef:num = 5;break;case 0xdf:num = 6;break;case 0xbf:num = 7;break;case 0x7f:num = 8;break;default:num = 0; break;  }  if(num)  {      High = freq[num - 1][1];Low  = freq[num - 1][0];TR0 = 1;  }  else  {    TR0 = 0;  //关闭定时器  }    }}

1 0
原创粉丝点击