单片机红外发射原理与设计程序文档格式.docx
《单片机红外发射原理与设计程序文档格式.docx》由会员分享,可在线阅读,更多相关《单片机红外发射原理与设计程序文档格式.docx(14页珍藏版)》请在冰豆网上搜索。
voidSendIRdata(charp_irdata);
voiddelay();
voidmain(void)
{
count=0;
flag=0;
OP=0;
P3_4=0;
EA=1;
//允许CPU中断
TMOD=0x11;
//设定时器0和1为16位模式1
ET0=1;
//定时器0中断允许
TH0=0xFF;
TL0=0xE6;
//设定时值0为38K也就是每隔26us中断一次
TR0=1;
//开始计数
iraddr1=3;
iraddr2=252;
do{
delay();
SendIRdata(12);
}while
(1);
}
//定时器0中断处理
voidtimeint(void)interrupt1
{
TH0=0xFF;
TL0=0xE6;
//设定时值为38K也就是每隔26us中断一次
count++;
if(flag==1)
{
OP=~OP;
}
else
P3_4=OP;
voidSendIRdata(charp_irdata)
inti;
charirdata=p_irdata;
//发送9ms的起始码
endcount=223;
flag=1;
count=0;
do{}while(count<
endcount);
//发送4.5ms的结果码
endcount=117
flag=0;
//发送十六位地址的前八位
irdata=iraddr1;
for(i=0;
i<
8;
i++)
//先发送0.56ms的38KHZ红外波(即编码中0.56ms的低电平)
endcount=10;
//停止发送红外信号(即编码中的高电平)
if(irdata-(irdata/2)*2)
//判断二进制数个位为1还是0
endcount=41;
//1为宽的高电平
else
endcount=15;
//0为窄的高电平
flag=0;
count=0;
irdata=irdata>
>
1;
//发送十六位地址的后八位
irdata=iraddr2;
endcount=10;
flag=1;
do{}while(count<
if(irdata-(irdata/2)*2)
endcount=41;
//发送八位数据
irdata=p_irdata;
}
irdata=irdata>
//发送八位数据的反码
irdata=~p_irdata;
endcount=15;
voiddelay()
inti,j;
400;
for(j=0;
j<
100;
j++)
制作的实物如下图所示:
步进电机
#include<
reg51.h>
#defineuintunsignedint
#defineucharunsignedchar
#definems*77
//f=12M
#defineLEDLen4
#defineDj_star(){IE=0x81;
pri_dj=0;
#defineDj_stop(){IE=0x00;
pri_dj=1;
P1=0xff;
shache="
0"
;
delay(800ms);
delay(400ms);
shache=1;
#defineChilun_Num8
/*齿轮数8个*/
#defineset_display_num(){LEDBuf[0]=tmp/1000;
LEDBuf[1]=tmp/100%10;
\
LEDBuf[2]=tmp/10%10;
LEDBuf[3]=tmp%10;
ucharLEDBuf[LEDLen]={0,0,0,0};
voidread_num();
/*读播码盘到set_round_num*8*/
voiddisplay();
voiddelay(uintdelay_time){uinti;
for(i=0;
i<
delay_time;
i++);
voidrun();
voidfx_run();
uintround_num=0;
/*记录已转的齿轮数,中断1次加1*/
uintset_round_num=0;
/*播码盘设置圈数*/
uintset_pwm_width=0;
/*播码盘设置步进电机正向速度*/
bitone_round_flg=0;
sbitled_1000=P0^7;
//usefordisplay
sbitled_100=P0^6;
sbitled_10=P0^5;
sbitled_1=P0^4;
sbitkey_start=P3^0;
sbitkey_puse=P3^0;
sbitkey_clear=P3^1;
/*P3^2接齿轮传感器中断*/
sbitbujin_zx_stop=P3^3;
/*接步进电机,正向到位传感器,为0停机*/
sbitbujin_fx_stop=P3^4;
/*接步进电机,反向到位传感器,为0停机*/
sbitshache=P3^5;
/*接刹车控制继电器0电位有效*/
sbitpri_dj=P3^6;
/*接主电机控制继电器0电位有效*/
voidmain(){
TCON=0x01;
display();
while
(1){
IE="
0x00"
round_num=0;
if(bujin_fx_stop)fx_run();
while(key_start);
delay(8ms);
if(!
key_start){
read_num();
//set_round_num=8;
while(!
key_start);
run();
fx_run();
voidrun(){
#defineDelay_time180
/*转一圈50次循环,每循环4步,50*4=200,200*1。
8=360*/
uchari;
P1=0xff;
set_pwm_width=15+set_pwm_width/10;
while
(1){
while(!
shache|!
Dj_star();
for(i="
;
bujin_zx_stop&
!
pri_dj;
i++){
P1=0xf9;
delay(Delay_time);
//bujin_zx_stop=P3^3;
P1=0xfc;
//bujin_fx_stop=P3^4;
delay(Delay_time);
//key_puse=P3^0;
P1=0xf6;
//key_clear=P3^1;
//shache=P3^5;
P1=0xf3;
//pri_dj=P3^6;
if(i==set_pwm_width){P1=0xff;
i=0;
one_round_flg=0;
one_round_flg&
key_puse);
key_puse){delay(4ms);
key_puse)break;
if(pri_dj)break;
if(!
key_puse){
Dj_stop();
key_puse);
//nextpreekey
shache);
while
(1){
while(key_puse&
key_clear);
key_clear){round_num=0;
key_puse)break;
delay(8ms);
voidext_int0(void)interrupt0{/*主电机齿轮中断*/
uinttmp;
EA=0;
if(!
pri_dj){
round_num++;
if(round_num%Chilun_Num==0){
one_round_flg=1;
tmp=round_num/Chilun_Num;
set_display_num();
P0=0xf0;
P0=P0|LEDBuf[0];
led_1000=0;
P0|=0xf0;
P0=P0|LEDBuf[1];
led_100=0;
P0=P0|LEDBuf[2];
led_10=0;
P0=P0|LEDBuf[3];
led_1=0;
if(round_num>
=set_round_num)Dj_stop();
EA=0x81;
voiddisplay(){
uchari;
uinttmp=0;
for(i=0;
LEDLen;
i++){
P0=P0|LEDBuf[i];
if(i==0)led_1000=0;
//P0^4
if(i==1)led_100=0;
//P0^5
if(i==2)led_10=0;
//P0^6
if(i==3)led_1=0;
//P0^7
voidread_num(){
/*读播码盘到set_round_num,set_pwm_width*/
uchartmp;
P2=0xFF;
P2=0xEF;
//11101111
delay(1ms);
tmp=~(P2|0xF0);
P2=0xDF;
//11011111
tmp=(~(P2|0xF0))*10+tmp;
set_round_num=tmp;
P2=0xBF;
//10111111
tmp=(~(P2|0xF0));
P2=0x7F;
//01111111
tmp=(~(P2|0xF0))*10+tmp;
set_round_num=set_round_num+tmp*100;
set_round_num=set_round_num*Chilun_Num;
P1=0xbF;
tmp=~(P2|0xF0);
P1=0xFF;
P1&
=0x7F;
tmp=(~(P2|0xF0))*10+tmp;
set_pwm_width=tmp;
voidfx_run(){
#definef_Delay_time180
while(bujin_fx_stop){/*反向回车直到传感器动作*/
//0011
delay(f_Delay_time);
//0110
//1100
//1001