基于单片机智能乐闹钟源代码财富.doc
基于单片机智能音乐闹钟源代码/*/#include<reg52.h>#include<intrins.h>#define uchar unsigned char #define uint unsigned intuchar code font=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf; /uchar codetab=0xc40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e;uint code note_value14=0,63628,63835,64021,64103,64260,64400,64524,63731,63928,0,64185,64331,64463, /低音1,2,3,4,5,6,7,#1,#2,0(#3在此程序中无需设),#4,#5,#6 相对应的定时器初值0,64580,64684,64777,64820,64898,64968,65030,64633,64732,0,64860,64934,64994, /中音1,2,3,4,5,6,7,#1,#2,0(#3在此程序中无需设),#4,#5,#6 相对应的定时器初值0,65058,65110,65157,65178,65217,65252,65283,65085,65134,0,65198,65235,65268 /高音1,2,3,4,5,6,7,#1,#2,0(#3在此程序中无需设),#4,#5,#6 相对应的定时器初值 ;uint code song67=/* 0 嘀(按键时的嘀嗒声)*/0x2701,0x0,/* 1 茉莉花 */ 0x1304,0x1302,0x1502,0x1602,0x2102,0x2102,0x1602,0x1504,0x1502,0x1602,0x1507,0x1001,/* 0x1304,0x1302,0x1502,0x1602,0x2102,0x2102,0x1602,0x1504,0x1502,0x1602,0x1507,0x1001,*/ 0x1504,0x1504,0x1504,0x1302,0x1502,0x1604,0x1604,0x1507,0x1001, 0x1304,0x1202,0x1302,0x1504,0x1302,0x1202,0x1104,0x1102,0x1202,0x1107,0x1001, 0x1302,0x1202,0x1102,0x1302,0x1204,0x1002,0x1302,0x1504,0x1602,0x2102,0x1507,0x1001, 0x1202,0x1302,0x1502,0x1202,0x1302,0x1102,0x0602,0x0507,0x1001,0x0604,0x0602,0x1102,0x1206,0x1302,0x1102,0x1202,0x1102,0x0602,0x050d,0x0, /* 2 莫斯科郊外的晚上 */ 0x0602,0x1102,0x1302,0x1102,0x1204,0x1102,0x0702,0x1304,0x1204,0x0607,0x1001, 0x1102,0x1302,0x1502,0x1502,0x1604,0x1502,0x1402,0x1307,0x1001, 0x1414,0x1514,0x1702,0x1602,0x1304, 0x1002,0x0704,0x0602,0x1302,0x1202,0x1404, 0x1004,0x1502,0x1402,0x1304,0x1202,0x1102,0x1304,0x1204,0x060c, 0x0,/* 3 祝你生日快乐 */ 0x0502,0x0502,0x0604,0x0504,0x1104,0x0708,0x0502,0x0502,0x0604,0x0504,0x1204,0x1108,0x0502,0x0502,0x1504,0x1304,0x1104,0x0704,0x0604,0x1402,0x1402,0x1304,0x1104,0x1204,0x1108,0x0,/* 4 铃儿响叮当 */0x1302,0x1302,0x1303,0x1001, 0x1302,0x1303,0x1001, 0x1302,0x1502,0x1102,0x1202,0x1307,0x1001, 0x1402,0x1402,0x1402,0x1402,0x1402,0x1302,0x1302,0x1302,0x1302,0x1202,0x1202,0x1302,0x1204,0x1503,0x1001, 0x1302,0x1302,0x1303,0x1001, 0x1302,0x1303,0x1001, 0x1302,0x1502,0x1102,0x1202,0x1307,0x1001, 0x1402,0x1402,0x1402,0x1402,0x1402,0x1302,0x1302,0x1302,0x1502,0x1502,0x1402,0x1202,0x1108,0x0,/* 5 喀秋莎 */ 0x0606,0x0702,0x1106,0x0602,0x1102,0x1102,0x0702,0x0602,0x0704,0x1002, 0x0706,0x1102,0x1206,0x0702,0x1202,0x1202,0x1102,0x0702,0x0607,0x1001, 0x1304,0x1604,0x1504,0x1602,0x1502,0x1402,0x1402,0x1302,0x1202,0x1304,0x0604, 0x1002,0x1404,0x1202,0x1306,0x1102,0x0702,0x0302,0x1102,0x0702,0x0608,0x0,/* 6 欢乐颂 */0x1304,0x1304,0x1404,0x1504,0x1504,0x1404,0x1304,0x1204,0x1104,0x1104,0x1204,0x1304,0x1306,0x1202,0x1207,0x1001, 0x1304,0x1304,0x1404,0x1504,0x1504,0x1404,0x1304,0x1204,0x1104,0x1104,0x1204,0x1304,0x1206,0x1102,0x1107,0x1001, 0x0,/* 7 四季歌 */ 0x1304,0x1302,0x1202,0x1104,0x1102,0x0702,0x0604,0x0604,0x0607,0x1001, 0x1404,0x1402,0x1302,0x1202,0x1102,0x1202,0x1402,0x130f,0x1001, 0x1404,0x1402,0x1302,0x1204,0x1202,0x1402,0x1304,0x1302,0x1102,0x0604,0x0604,0x0704,0x1304,0x1302,0x1102,0x0702,0x1102,0x060f,0x0,/* 8 月亮代表我的心 */ 0x150a,0x1302,0x1202,0x1102,0x130f,0x160a,0x1402,0x1202,0x1102,0x070c,0x1002,0x0502,0x1106,0x1302,0x1506,0x1102,0x0706,0x1302,0x1504,0x1002,0x1502,0x1606,0x1702,0x2106,0x1604,0x150a,0x1302,0x1202,0x1106,0x1102,0x1104,0x1302,0x1202,0x1106,0x1102,0x1104,0x1202,0x1302,0x1206,0x1102,0x0606,0x1201,0x1301,0x120c,0x1002,0x0502,0x1106,0x1302,0x1506,0x1102,0x0706,0x1302,0x1504,0x1002,0x1502,0x1606,0x1702,0x2106,0x1604,0x150a,0x1302,0x1202,0x1106,0x1102,0x1104,0x1302,0x0,/0x1202,0x1106,0x1102,0x1104,0x1202,0x1302,0x1206,0x0602,0x0704,0x1102,011202,0x110c,0x1302,0x1502,0x1306,0x1202,0x1104,0x1504,0x070c,0x0602,0x0702,0x0606,0x0702,0x0604,0x0504,0x130c,0x1504,0x1306,0x1202,0x1104,0x1504,0x070c,0x0602,0x0702,0x1106,0x1102,0x1104,0x1202,0x1302,0x120c,0x1002,0x0502,0x1106,0x1302,0x1506,0x1102,0x0706,0x1302,0x1504,0x1002,0x1502,0x1606,0x1702,0x2106,0x1604,0x150a,0x1302,0x1202,0x1106,0x1102,0x1104,0x1302,0x1202,0x1106,0x1102,0x1104,0x1202,0x1302,0x1206,0x0602,0x0704,0x1102,0x1202,0x1104,0x1502,0x1402,0x1304,0x1202,0x1102,0x1104,0x0702,0x0602,0x0518,0x050f,0x0,/* 9 献给爱丽丝 */ 0x2302,0x2212,0x2302,0x2212,0x2302,0x1702,0x2202,0x2102,0x1604, 0x1002,0x1102,0x1302,0x1602,0x1704, 0x1002,0x1302,0x1512,0x1702,0x2104, 0x1002,0x1302,0x2302,0x2212,0x2302,0x2212,0x2302,0x1702,0x2202,0x2102,0x1604, 0x1002,0x1102,0x1302,0x1602,0x1704, 0x1002,0x1202,0x2102,0x1702,0x1604, 0x1002,0x1702,0x2102,0x2202,0x2305,0x1001, 0x1502,0x2402,0x2302,0x2205,0x1001, 0x1402,0x2302,0x2202,0x2105,0x1001, 0x1302,0x2202,0x2102,0x1704, 0x1002,0x1302,0x2302,0x1002, 0x0,/ 0x2212,0x2302, 0x1002,0x1002,0x2212,0x2302,0x2212,0x2302,0x2212,0x2302,0x1702,0x2202,0x2102,0x1604, 0x1002,0x1102,0x1302,0x1602,0x1704, 0x1002,0x1302,0x1512,0x1702,0x2104, 0x1002,0x1302,0x2302,0x2212,0x2302,0x2212,0x2302,0x1702,0x2202,0x2102,0x1604, 0x1002,0x1102,0x1302,0x1602,0x1704, 0x1002,0x1202,0x2102,0x1702,0x1604,0x1004,0x0,/* 10 小星星,亮晶晶 */ 0x1104,0x1104,0x1504,0x1504,0x1604,0x1604,0x1507,0x1001, 0x1404,0x1404,0x1304,0x1304,0x1204,0x1204,0x1107,0x1001, 0x1504,0x1504,0x1404,0x1404,0x1304,0x1304,0x1207,0x1001, 0x1504,0x1504,0x1404,0x1404,0x1304,0x1304,0x1207,0x1001, 0x1104,0x1104,0x1504,0x1504,0x1604,0x1604,0x1507,0x1001, 0x1404,0x1404,0x1304,0x1304,0x1204,0x1204,0x1107,0x1001, 0x0,/* 11 Nokia经典铃声 */ 0x2502,0x2402,0x1604,0x1704,0x2302,0x2202,0x1404,0x1504,0x2202,0x2102,0x1304,0x1504,0x210c, 0x2502,0x2402,0x1604,0x1704,0x2302,0x2202,0x1404,0x1504,0x2202,0x2102,0x1304,0x1504,0x210c,0x0/* 12 两只老虎 */0x1104,0x1204,0x1304,0x1104,0x1104,0x1204,0x1304,0x1104,0x1304,0x1404,0x1508,0x1304,0x1404,0x1508,0x1502,0x1602,0x1502,0x1402,0x1304,0x1104,0x1502,0x1602,0x1502,0x1402,0x1304,0x1104,0x1304,0x0504,0x1108,0x1304,0x0504,0x1108,0x0 ;sbit key_select=P10; /功能键sbit key_inc=P11; / 增大建sbit key_dec=P12; / 减小键&强行关闹钟和歌曲键sbit key_alarm_or_song=P13; /调闹钟键&点歌键sbit buzzer=P00;bit music_break=0; uchar week=1,hour=12,minute=0,second=0,alarm_num,display=1,2,0,0,0,0,n=0,bit_code=0xfb,alarm_break=1,flag_display=0,flag_alarm=0; /定义数组display6作为显示缓冲区,n为数组下标,flag_display用于设置时间或闹钟时的闪烁显示标志,flag_alarm用于闹钟键按下的标志 uchar display_music6=0x10,0x10,0,0,0x10,0x10,display_alarm6=0,0,0,0,0,0,alarm3=12,12,1,12,3,12,5,1,2,3,4,5,6,alarm_num=0,song_num=0;uint initial_value,count_display=0,count=0; /count_display用于设置时间或闹钟时的闪烁显示记时变量void delay_ms(uint t) /延时1ms uint i,j;for(i=t;i>0;i-)for(j=110;j>0;j-); void di() /按键发声函数 buzzer=0;delay_ms(100); buzzer=1; void music(uchar song_number) uchar i,section,basic_note,changed_note,pitch,duration,note_num=0; /分别定义i,音区,基本音级,变化音级,音高,时值,所选歌曲的音符的编号uint note; /定义音符变量 ET1=1; music_break=1; note=songsong_numbernote_num;while(note!=0&&music_break)section=note/0x1000;basic_note=note/0x100%0x10;changed_note=note/0x10%0x10;duration=note%0x10;if(changed_note=1)pitch=basic_note+7;pitch=basic_note;initial_value=note_valuesectionpitch;if(initial_value!=0)TH1=initial_value/256;TL1=initial_value%256;TR1=1;elseTR1=0;if(key_alarm_or_song=0)delay_ms(10);if(key_alarm_or_song=0)break; for(i=duration;i>0;i-) /音符延时函数delay_ms(125);note_num+;note=songsong_numbernote_num; if(key_dec=0) /强行关闭闹钟 delay_ms(10);if(key_dec=0) while(!key_dec); break; music_break=0;TR1=0;void alarm_()for(alarm_num=1;alarm_num<10;alarm_num+)/添加闹钟时需要修改的地方!if(hour=alarmalarm_num0&&minute=alarmalarm_num1&&second=alarmalarm_num2)display_alarm0=alarmalarm_num0/10;display_alarm1=alarmalarm_num0%10;display_alarm2=alarmalarm_num1/10;display_alarm3=alarmalarm_num1%10;display_alarm4=0x10;display_alarm5=alarm_num;music(alarm_num);/*void alarm_ () /闹钟发声函数if(hour=alarm0&&minute=alarm1&&second=alarm2) uint k; for(k=10;k>0;k-) buzzer=0;delay_ms(100); buzzer=1; */ void scan_key() /按键扫描函数 bit flag_break=0;/flag_break作为跳出按键扫描循环程序的标志位if(key_select=0)delay_ms(10);if(key_select=0) / 确认功能键被按下while(!key_select); /释放确认di();while(1)/时按键处理flag_display=1;if(flag_break=1) alarm_num=0;break;if(key_alarm_or_song=0)delay_ms(10);if(key_alarm_or_song=0) flag_alarm=1;if(flag_break=1)alarm_num=0;break; alarm_num+;if(alarm_num=10)/添加闹钟时需要修改的地方!alarm_num=1;display_alarm0=alarmalarm_num0/10;display_alarm1=alarmalarm_num0%10;display_alarm2=alarmalarm_num1/10;display_alarm3=alarmalarm_num1%10;display_alarm4=0x10;display_alarm5=alarm_num%10; while(!key_alarm_or_song);di(); if(key_select=0)delay_ms(10);if(key_select=0) /确认功能键改时被按下while(!key_select); /释放确认di();while(1)/分按键处理flag_display=2;if(flag_break=1)break; if(key_select=0)delay_ms(10);if(key_select=0) /确认功能键改分被按下while(!key_select); /释放确认di(); while(1) /秒按键处理flag_display=3;if(key_select=0)delay_ms(10);if(key_select=0) /确认功能键改秒被按下while(!key_select); /此处等待按键释放非常重要!di();flag_alarm=0;flag_break=1;break;if(key_inc=0&&flag_alarm=0)delay_ms(10);if(key_inc=0) if(flag_alarm=0) second+;if(second=60)second=0;display4=second/10;display5=second%10; while(!key_inc); else alarmalarm_num2+;if(alarmalarm_num2=60)alarmalarm_num2=0;display_alarm4=0x10;display_alarm5=alarm_num;while(!key_inc); di();if(key_dec=0)delay_ms(10);if(key_dec=0) if(flag_alarm=0) second-;if(second=255)second=59;display4=second/10; display5=second%10;while(!key_dec); else alarmalarm_num2-;if(alarmalarm_num2=255)alarmalarm_num2=59;display_alarm4=0x10;display_alarm5=alarm_num;while(!key_dec);di();if(key_inc=0)delay_ms(10);if(key_inc=0) if(flag_alarm=0) minute+; if(minute=60)minute=0;display2=minute/10;display3=minute%10;while(!key_inc); else alarmalarm_num1+;if(alarmalarm_num1=60)alarmalarm_num1=0;display_alarm2=alarmalarm_num1/10;display_alarm3=alarmalarm_num1%10;while(!key_inc); di();if(key_dec=0)delay_ms(10);if(key_dec=0) if(flag_alarm=0)minute-;if(minute=255)minute=59;display2=minute/10;display3=minute%10;while(!key_dec); else alarmalarm_num1-;if(alarmalarm_num1=255)alarmalarm_num1=59;display_alarm2=alarmalarm_num1/10;display_alarm3=alarmalarm_num1%10;while(!key_dec); di();if(key_inc=0)delay_ms(10);if(key_inc=0) if(flag_alarm=0) hour+;if(hour=24)hour=0;display0=hour/10;display1=hour%10;while(!key_inc); else alarmalarm_num0+;if(alarmalarm_num0=24)alarmalarm_num0=0;display_alarm0=alarmalarm_num0/10;display_alarm1=alarmalarm_num0%10;while(!key_inc); di();if(key_dec=0)delay_ms(10);if(key_dec=0) if(flag_alarm=0) hour-; if(hour=255)hour=23; display0=hour/10;display1=hour%10;while(!key_d