基于单片机智能音乐闹钟源代码0财富.docx
《基于单片机智能音乐闹钟源代码0财富.docx》由会员分享,可在线阅读,更多相关《基于单片机智能音乐闹钟源代码0财富.docx(21页珍藏版)》请在冰豆网上搜索。
![基于单片机智能音乐闹钟源代码0财富.docx](https://file1.bdocx.com/fileroot1/2023-3/19/8e2aa3d5-4ce6-4423-bef1-fdb2587b60d8/8e2aa3d5-4ce6-4423-bef1-fdb2587b60d81.gif)
基于单片机智能音乐闹钟源代码0财富
基于单片机智能音乐闹钟源代码
/*******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodefont[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};//ucharcodetab[]={0xc40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};
uintcodenote_value[][14]={
{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相对应的定时器初值
};
uintcodesong[][67]={
/*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},
/*11Nokia经典铃声*/{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}
};
sbitkey_select=P1^0;//功能键
sbitkey_inc=P1^1;//增大建
sbitkey_dec=P1^2;//减小键&强行关闹钟和歌曲键
sbitkey_alarm_or_song=P1^3;//调闹钟键&点歌键
sbitbuzzer=P0^0;
bitmusic_break=0;
ucharweek=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;//定义数组display[6]作为显示缓冲区,n为数组下标,flag_display用于设置时间或闹钟时的闪烁显示标志,flag_alarm用于闹钟键按下的标志
uchardisplay_music[6]={0x10,0x10,0,0,0x10,0x10},display_alarm[6]={0,0,0,0,0,0},alarm[][3]={{12},{12,1},{12,3},{12,5},{1},{2},{3},{4},{5},{6}},alarm_num=0,song_num=0;
uintinitial_value,count_display=0,count=0;//count_display用于设置时间或闹钟时的闪烁显示记时变量
voiddelay_ms(uintt)//延时1ms
{
uinti,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
voiddi()//按键发声函数
{
buzzer=0;
delay_ms(100);
buzzer=1;
}
voidmusic(ucharsong_number)
{
uchari,section,basic_note,changed_note,pitch,duration,note_num=0;//分别定义i,音区,基本音级,变化音级,音高,时值,所选歌曲的音符的编号
uintnote;//定义音符变量
ET1=1;
music_break=1;
note=song[song_number][note_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_value[section][pitch];
if(initial_value!
=0)
{
TH1=initial_value/256;
TL1=initial_value%256;
TR1=1;
}
else
TR1=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=song[song_number][note_num];
if(key_dec==0)//强行关闭闹钟
{
delay_ms(10);
if(key_dec==0)
{
while(!
key_dec);
break;
}
}
}
music_break=0;
TR1=0;
}
voidalarm_()
{
for(alarm_num=1;alarm_num<10;alarm_num++)//添加闹钟时需要修改的地方!
!
!
!
!
!
!
!
!
!
if(hour==alarm[alarm_num][0]&&minute==alarm[alarm_num][1]&&second==alarm[alarm_num][2])
{
display_alarm[0]=alarm[alarm_num][0]/10;
display_alarm[1]=alarm[alarm_num][0]%10;
display_alarm[2]=alarm[alarm_num][1]/10;
display_alarm[3]=alarm[alarm_num][1]%10;
display_alarm[4]=0x10;
display_alarm[5]=alarm_num;
music(alarm_num);
}
}
/*voidalarm_()//闹钟发声函数
{
if(hour==alarm[0]&&minute==alarm[1]&&second==alarm[2])
{
uintk;
for(k=10;k>0;k--)
{
buzzer=0;
delay_ms(100);
buzzer=1;
}
}
}
*/
voidscan_key()//按键扫描函数
{
bitflag_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_alarm[0]=alarm[alarm_num][0]/10;
display_alarm[1]=alarm[alarm_num][0]%10;
display_alarm[2]=alarm[alarm_num][1]/10;
display_alarm[3]=alarm[alarm_num][1]%10;
display_alarm[4]=0x10;
display_alarm[5]=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)/