单片机遥控音乐灯闪烁等功能程序.docx
- 文档编号:5863184
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:24
- 大小:19.31KB
单片机遥控音乐灯闪烁等功能程序.docx
《单片机遥控音乐灯闪烁等功能程序.docx》由会员分享,可在线阅读,更多相关《单片机遥控音乐灯闪烁等功能程序.docx(24页珍藏版)》请在冰豆网上搜索。
单片机遥控音乐灯闪烁等功能程序
#include
#defineuintunsignedint//重定义无符号整数类型
#defineucharunsignedchar//重定义无符号字符类型
ucharcodeLedShowData[]={0x03,0x9F,0x25,0x0D,0x99,//定义数码管显示数据
0x49,0x41,0x1F,0x01,0x19};//0,1,2,3,4,5,6,7,8,9
ucharcodeRecvData[]={0x07,0x0A,0x1B,0x1F,0x0C,0x0D,0x0E,0x00,0x0F,0x19};
ucharIRCOM[7];//接收数据缓冲区
staticunsignedintLedFlash;//定义闪动频率计数变量
unsignedcharRunFlag=0;//定义运行标志位
/***********完成基本数据变量定义**************/
sbitLeftLed=P2^0;//定义前方左侧指示灯端口
sbitRightLed=P0^7;
sbitLed=P1^0;//定义前方右侧指示灯端口
sbitM1A=P0^0;//定义电机1正向端口
sbitM1B=P0^1;//定义电机1反向端口
sbitM2A=P0^2;//定义电机2正向端口
sbitM2B=P0^3;
sbitMUSIC=P0^6;
sbitIRIN=P3^3;
sbitLED1=P1^1;
sbitLED2=P1^2;
sbitLED3=P1^3;
sbitLED4=P1^4;
sbitLED5=P1^5;
sbitLED6=P1^6;
sbitLED7=P1^7;
sbitLeftIR1=P3^1;//定义前方左侧红外探头端口
sbitRightIR1=P3^4;//定义前方右侧红外探头端口
sbitFontIR1=P3^0;
#defineLEDP1//定义电机2反向端口
/*************完成电机端口定义***************/
typedefstruct{
unsignedcharh;
unsignedcharl;
}FREQ_T;
typedefstruct{
FREQ_T*freq;
unsignedchartime;
}NOTE_T;
#defineTIME_1_321
#defineTIME_1_162
#defineTIME_1_84
#defineTIME_1_48
#defineTIME_1_216
#defineTIME_1_132
externvoidinitialize_music(void);
externFREQ_T*s[];
voidinitialize_music(void);
codeFREQ_TLDO={0xf2,0x4b};
codeFREQ_TLDOR={0xf3,0x09};
codeFREQ_TLRE={0xf3,0xca};
codeFREQ_TLRER={0xf4,0x75};
codeFREQ_TLMI={0xf5,0x20};
codeFREQ_TLFA={0xf5,0xb8};
codeFREQ_TLFAR={0xf6,0x4e};
codeFREQ_TLSO={0xf6,0xd9};
codeFREQ_TLSOR={0xf7,0x5c};
codeFREQ_TLLA={0xf7,0xda};
codeFREQ_TLLAR={0xf8,0x4f};
codeFREQ_TLSI={0xf8,0xbf};
codeFREQ_TDO={0xf9,0x26};
codeFREQ_TDOR={0xf9,0x89};
codeFREQ_TRE={0xf9,0xce};
codeFREQ_TRER={0xfa,0x3f};
codeFREQ_TMI={0xfa,0x92};
codeFREQ_TFA={0xfa,0xe0};
codeFREQ_TFAR={0xfb,0x2b};
codeFREQ_TSO={0xfb,0x71};
codeFREQ_TSOR={0xfb,0xb3};
codeFREQ_TLA={0xfb,0xf1};
codeFREQ_TLAR={0xfc,0x2c};
codeFREQ_TSI={0xfc,0x64};
codeFREQ_THDO={0xfc,0x97};
codeFREQ_THDOR={0xfc,0xc9};
codeFREQ_THRE={0xfc,0xf8};
codeFREQ_THRER={0xfd,0x24};
codeFREQ_THMI={0xfd,0x4d};
codeFREQ_THFA={0xfd,0x75};
codeFREQ_THFAR={0xfd,0x9a};
codeFREQ_THSO={0xfd,0xbd};
codeFREQ_THSOR={0xfd,0xde};
codeFREQ_THLA={0xfd,0xfd};
codeFREQ_THLAR={0xfe,0x1a};
codeFREQ_THSI={0xfe,0x36};
codeFREQ_TPAUSE={0,0};
FREQ_Tfreq;
codeNOTE_Talice[]=
{
{&HMI,TIME_1_16},
{&HRER,TIME_1_16},
{&HMI,TIME_1_16},
{&HRER,TIME_1_16},
{&HMI,TIME_1_16},
{&SI,TIME_1_16},
{&HRE,TIME_1_16},
{&HDO,TIME_1_16},
{&LA,TIME_1_8},
{&PAUSE,TIME_1_16},
{&DO,TIME_1_16},
{&MI,TIME_1_16},
{&LA,TIME_1_16},
{&SI,TIME_1_8},
{&PAUSE,TIME_1_16},
{&MI,TIME_1_16},
{&SOR,TIME_1_16},
{&SI,TIME_1_16},
{&HDO,TIME_1_8},
{&PAUSE,TIME_1_16},
{&MI,TIME_1_16},
{&HMI,TIME_1_16},
{&HRER,TIME_1_16},
{&HMI,TIME_1_16},
{&HRER,TIME_1_16},
{&HMI,TIME_1_16},
{&SI,TIME_1_16},
{&HRE,TIME_1_16},
{&HDO,TIME_1_16},
{&LA,TIME_1_8},
{&PAUSE,TIME_1_16},
{&DO,TIME_1_16},
{&MI,TIME_1_16},
{&LA,TIME_1_16},
{&SI,TIME_1_8},
{&PAUSE,TIME_1_16},
{&MI,TIME_1_16},
{&HDO,TIME_1_16},
{&SI,TIME_1_16},
{&LA,TIME_1_8},
{&PAUSE,TIME_1_16},
{&SI,TIME_1_16},
{&HDO,TIME_1_16},
{&HRE,TIME_1_16},
{&HMI,TIME_1_16},
{&HMI,TIME_1_8},
{&SO,TIME_1_16},
{&HFA,TIME_1_16},
{&HMI,TIME_1_16},
{&HRE,TIME_1_16},
{&HRE,TIME_1_8},
{&FA,TIME_1_16},
{&HMI,TIME_1_16},
{&HRE,TIME_1_16},
{&HDO,TIME_1_16},
{&HDO,TIME_1_8},
{&MI,TIME_1_16},
{&HRE,TIME_1_16},
{&HDO,TIME_1_16},
{&SI,TIME_1_16},
{&SI,TIME_1_8},
{&MI,TIME_1_16},
{&HMI,TIME_1_16},
{&MI,TIME_1_16},
{&HMI,TIME_1_16},
{&MI,TIME_1_16},
{&HMI,TIME_1_16},
{&HRER,TIME_1_16},
{&HMI,TIME_1_16},
{&HRER,TIME_1_16},
{&HMI,TIME_1_16},
{&HRER,TIME_1_16},
{&HMI,TIME_1_16},
{&HRER,TIME_1_16},
};//定义红外接收端口
/*********完成红外接收端口的定义*************/
#defineShowPortP2//定义数码管显示端口
externvoidControlCar(ucharCarType);//声明小车控制子程序
voidinterrupt_timer1(void)interrupt3
{
staticunsignedcharcount=0;
staticunsignedcharcountb=0;
staticunsignedchari=0;
unsignedchark;
TH1=0X70;//20ms
TL1=0X00;
count++;
if(count==5)
{
count=0;
countb++;
if(countb==alice[i].time)
{
countb=0;
k=i+1;
if(k>=sizeof(alice)/sizeof(NOTE_T))k=0;
freq.h=alice[k].freq->h;
freq.l=alice[k].freq->l;
i++;
if(i==(sizeof(alice)/sizeof(NOTE_T)))i=0;
}
}
}
voidinterrupt_timer0(void)interrupt1
{
TH0=freq.h;//60Hz
TL0=freq.l;
if(freq.h||freq.l)MUSIC=!
MUSIC;
}
voidinitialize_music(void)
{TMOD|=0X01;//T016bitMODE;
TMOD|=0X10;
freq.h=alice[0].freq->h;
freq.l=alice[0].freq->l;
TH0=freq.h;
TL0=freq.l;
TF0=0;
TR0=1;//starttimer0
ET0=1;//Timer0interruptenable
TF1=0;
TR1=1;
ET1=1;
EA=1;//interruptenable
}
voiddelay(unsignedcharx)//0.14mS延时程序
{
unsignedchari;//定义临时变量
while(x--)//延时时间循环
{
for(i=0;i<13;i++){}//14mS延时
}
}
voidDelay()//定义延时子程序
{uintDelayTime=30000;//定义延时时间变量
while(DelayTime--);//开始进行延时循环
return;//子程序返回
}
voidDelay1()
{unsignedintDelayTime=50000;//定义可变延时函数
while(DelayTime--);//定义前方左侧指示灯端口
}
voidDelay6(unsignedintDelayTime)
{
while(DelayTime--);
}
voidDelay2()
{unsignedintDelayTime=2000;//定义可变延时函数
while(DelayTime--);
Led=~Led;
if(ShowPort==LedShowData[3])
{RightLed=1;LeftLed=~LeftLed;}
if(ShowPort==LedShowData[4])
{LeftLed=1;RightLed=~RightLed;}//定义前方左侧指示灯端口
return;//延时函数进入倒计时
}
voidDelay5()//定义机器人调转子时间子程序
{unsignedintDelayTime=20000;//定义机器人转弯时间变量
while(DelayTime--);//机器人转弯循环
MUSIC=!
MUSIC;//蜂鸣器闪响
return;
}
voidDelay7()
{unsignedintDelayTime=2000;//定义可变延时函数
while(DelayTime--);
LED1=~LED1;
if(ShowPort==LedShowData[3])
{RightLed=1;LeftLed=~LeftLed;}
if(ShowPort==LedShowData[4])
{LeftLed=1;RightLed=~RightLed;}//定义前方左侧指示灯端口
return;//延时函数进入倒计时
}
voidDelay8()
{unsignedintDelayTime=2000;//定义可变延时函数
while(DelayTime--);
LED2=~LED2;
if(ShowPort==LedShowData[3])
{RightLed=1;LeftLed=~LeftLed;}
if(ShowPort==LedShowData[4])
{LeftLed=1;RightLed=~RightLed;}//定义前方左侧指示灯端口
return;//延时函数进入倒计时
}
voidDelay9()
{unsignedintDelayTime=2000;//定义可变延时函数
while(DelayTime--);
LED3=~LED3;
if(ShowPort==LedShowData[3])
{RightLed=1;LeftLed=~LeftLed;}
if(ShowPort==LedShowData[4])
{LeftLed=1;RightLed=~RightLed;}//定义前方左侧指示灯端口
return;//延时函数进入倒计时
}
voidDelay10()
{unsignedintDelayTime=2000;//定义可变延时函数
while(DelayTime--);
LED4=~LED4;
if(ShowPort==LedShowData[3])
{RightLed=1;LeftLed=~LeftLed;}
if(ShowPort==LedShowData[4])
{LeftLed=1;RightLed=~RightLed;}//定义前方左侧指示灯端口
return;//延时函数进入倒计时
}
voidDelay11()
{unsignedintDelayTime=2000;//定义可变延时函数
while(DelayTime--);
LED5=~LED5;
if(ShowPort==LedShowData[3])
{RightLed=1;LeftLed=~LeftLed;}
if(ShowPort==LedShowData[4])
{LeftLed=1;RightLed=~RightLed;}/定义前方左侧指示灯端口
return;//延时函数进入倒计时
}
voidDelay12()
{unsignedintDelayTime=2000;//定义可变延时函数
while(DelayTime--);
LED6=~LED6;
if(ShowPort==LedShowData[3])
{RightLed=1;LeftLed=~LeftLed;}
if(ShowPort==LedShowData[4])
{LeftLed=1;RightLed=~RightLed;}//定义前方左侧指示灯端口
return;//延时函数进入倒计时
}
voidDelay13()
{unsignedintDelayTime=2000;//定义可变延时函数
while(DelayTime--);
LED7=~LED7;
if(ShowPort==LedShowData[3])
{RightLed=1;LeftLed=~LeftLed;}
if(ShowPort==LedShowData[4])
{LeftLed=1;RightLed=~RightLed;}//定义前方左侧指示灯端口
return;//延时函数进入倒计时
}
voidpao()
{Delay2();
Delay2();
Led=1;
Delay7();
Delay7();
LED1=1;
Delay8();
Delay8();
LED2=1;
Delay9();
Delay9();
LED3=1;
Delay10();
Delay10();
LED4=1;
Delay11();
Delay11();
LED5=1;
Delay12();
Delay12();
LED6=1;
Delay13();
Delay13();
LED7=1;
}
voidFlash1()
{
unsignedintCountData=0;
while
(1)
{
if(CountData<90)
{
Delay6(5000);
}
elseif(CountData<100)
{
CountData=0;
}
CountData=CountData+1;
LeftLed=1;
Led=~Led;
LED1=~LED1;
LED2=~LED2;
LED3=~LED3;
LED4=~LED4;
LED5=~LED5;
LED6=~LED6;
LED7=~LED7;
RightLed=~RightLed;
if(IRIN==0)
{
break;
}
}
}
voidFlash2()
{
unsignedintCountData=0;
while
(1)
{
if(CountData<90)
{
Delay6(5000);
}
elseif(CountData<100)
{
CountData=0;
}
CountData=CountData+1;
Led=~Led;
LED1=~LED1;
LED2=~LED2;
LED3=~LED3;
LED4=~LED4;
LED5=~LED5;
LED6=~LED6;
LED7=~LED7;
LeftLed=~LeftLed;
RightLed=1;
if(IRIN==0)
{
break;
}
}
}
voidbzang()
{LeftLed=LeftIR1;//前方左侧指示灯指示出前方左侧红外探头
RightLed=RightIR1;
M1A=1;//避障子程序
while
(1)
{
if(FontIR1==1&&LeftIR1==0&&
RightIR1==1)
{ControlCar(4);
Delay5();
Delay5();
ControlCar
(1);
MUSIC=1;
}
elseif(FontIR1==1&&LeftIR1==1&&
RightIR1==0)
{ControlCar(3);
Delay5();
Delay5();
ControlCar
(1);
MUSIC=1;
}
elseif(FontIR1==1&&LeftIR1==1&&
RightIR1==1)
{M1A=1;//将电机1正向端口置高
M2A=1;
Delay5();
MUSIC=1;
}
elseif(FontIR1==1&&LeftIR1==0&&
RightIR1==0)
{ControlCar
(1);
Delay5();
MUSIC=1;
}
elseif(FontIR1==0&&LeftIR1==0&&
RightIR1==1)
{ControlCar
(2);
Delay5();
ControlCar(4);
Delay5();
ControlCar
(1);
MUSIC=1;
}
elseif(FontIR1==0&&LeftIR1==1&&
RightIR1==0)
{ControlCar
(2);
Delay5();
ControlCar(3);
Delay5();
ControlCar
(1);
MUSIC=1;
}
elseif(FontIR1==0&&LeftIR1==0&&
RightIR1==0)
{ControlCar
(2);
Delay5();
Delay5();
ControlCar(5);
MUSIC=1;
}
elseif(FontIR1==0&&LeftIR1==1&&
RightIR1==1)
{ControlCar
(2);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 单片机 遥控 音乐 闪烁 功能 程序