包含Proteus仿真和源码
单片机源程序如下:
- #include <reg52.h>
- sbit beep = P2^3;
- unsigned char timer0H, timer0L, time;
- //单片机晶振采用11.0592MHz
- code unsigned char FREQH[] = {
- 0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音1234567
- 0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i
- 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音 234567
- 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567
- code unsigned char FREQL[] =
- {
- 0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音1234567
- 0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i
- 0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音 234567
- 0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16 //超高音 1234567
- };
-
- code unsigned char song[] =
- {
- 6, 1, 2, 1, 2, 4, 6, 1, 2, 3, 2, 4, 2, 2, 2, 2, 2, 2, 3, 2, 1, 2, 2, 1, 1, 1, 6, //你挑着担
- 7, 1, 2, 6, 1, 4, 7, 1, 2, 2, 2, 4, 3, 2, 2, 1, 2, 2, //我牵着马
- 6, 1, 4, 6, 1, 6, 3, 2, 6, 6, 2, 5, 3, 2, 2, 6, 2, 5,
- 5, 2, 2, 4, 2, 2, 3, 2, 6, 1, 2, 5, 2, 2, 2, 3, 2, 6,
- 4, 2, 2, 3, 2, 2, 2, 2, 10, //迎来日出,送走晚霞
- 6, 1, 4, 3, 2, 4, 2, 2, 2, 3, 2, 2, 6, 1, 4, 1, 2, 8,
- 3, 2, 4, 2, 2, 2, 7, 1, 4, 3, 2, 2, 2, 2, 2, 6, 1, 2, 1, 2, 2, 2, 2, 2, 3, 2, 10, //踏平坎坷成大道
- 3, 2, 6, 6, 2, 4, 3, 2, 2, 6, 2, 4, 5, 2, 2, 4, 2, 2, 3, 2, 6, 5, 2, 2, 2, 2, 4,
- 4, 2, 2, 3, 2, 2, 2, 2, 2, 1, 2, 4, 2, 2, 8, 3, 2, 4, //斗罢艰险又出发
- 2, 2, 2, 7, 1, 4, 3, 2, 2, 7, 1, 2, 6, 1, 2, 5, 1, 4, 6, 1, 8,
- 3, 2, 4, 5, 2, 8, 3, 2, 2, 5, 2, 2, 6, 2, 5,
- 1, 3, 2, 7, 2, 2, 6, 2, 2, 5, 2, 4, 6, 2, 10, //啦 啦啦 啦 啦 啦啦啦
- 1, 3, 6, 7, 2, 5, 6, 2, 2, 6, 2, 4, 5, 2, 6, 5, 2, 2, 6, 2, 2, 3, 2, 10, //一番番春秋冬夏
- 1, 3, 6, 7, 2, 5, 6, 2, 2, 6, 2, 4, 5, 2, 6, 6, 2, 2, 3, 2, 10, //一番番酸甜苦辣
- 6, 1, 5, 1, 2, 2, 3, 2, 5, 1, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 8, //敢问路在何方
- 2, 2, 2, 7, 3, 4, 2, 2, 2, 7, 1, 2, 6, 1, 2, 5, 1, 4, 6, 1, 10, //路在脚下
- 6, 1, 5, 1, 2, 2, 3, 2, 5, 1, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 8, //敢问路在何方
- 3, 2, 4, 5, 2, 6, 3, 2, 4, 7, 2, 5,
- 1, 3, 2, 7, 2, 2, 6, 2, 2, 5, 2, 4, 6, 2, 10, 6, 2, 10, 0, 0, 0 //路在脚下
- };
- void t0int() interrupt 1 //T0中断程序,控制发音的音调
- {
- TR0 = 0; //先关闭T0
- beep = !beep; //输出方波, 发音
- TH0 = timer0H; //下次的中断时间, 这个时间, 控制音调高低
- TL0 = timer0L;
- TR0 = 1; //启动T0
- }
- void delay(unsigned char t) //延时程序,控制发音的时间长度
- {
- unsigned char t1;
- unsigned long t2;
- for(t1 = 0; t1 < t; t1++) //双重循环, 共延时t个半拍
- for(t2 = 0; t2 < 4000; t2++); //延时期间, 可进入T0中断去发音
- TR0 = 0; //关闭T0, 停止发音
- }
- void sing() //演奏一个音符
- {
- TH0 = timer0H; //控制音调
- TL0 = timer0L;
- TR0 = 1; //启动T0, 由T0输出方波去发音
- delay(time); //控制时间长度
- }
- void main(void)
- {
- unsigned char k, i;
- TMOD = 1; //置T0定时工作方式1
- ET0 = 1; //开T0中断
- EA = 1; //开总中断
- while(1)
- {
- i = 0;
- time = 1;
- while(time)
- {
- k = song[i] + 7 * song[i + 1] - 1;//第i个是音符, 第i+1个是第几个八度
- timer0H = FREQH[k]; //从数据表中读出频率数值
- timer0L = FREQL[k]; //实际上, 是定时的时间长度
- time = song[i + 2]; //读出时间长度数值
- i += 3;
- sing(); //发出一个音符
- }
- }
- }
复制代码
所有资料51hei附件下载:
MP3 - 副本.rar
(34.76 KB, 下载次数: 13)
|