用51单片机红外遥控舵机,用到外部中断0、定时器0、定时器1;定时器0用来红外解码,定时器1用来产生舵机的PWM,两个部分都会了,但是最后一步的使用红外遥控舵机在同一个程序里没反应,我想是定时器之间是不是有什么问题,有懂得吗?代码如下:
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table[]={ //段选数组定义
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- uchar code aable[]={ //位选数组定义
- 0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
- uchar irtimer;
- uchar irdata[33]; //接收红外发送的数据
- uchar bitnum;
- uchar ircode[4];
- uchar disp[8];
- uchar duoji_timer;
- uchar state = 0;
- sbit PWM1 = P2^0;
- sbit K1=P2^7;
- sbit K2=P2^6;
- sbit K3=P2^5;
- bit IRstart_Flag; //红外发送标志位
- bit IR_rec_ok_Flag; //接收红外发送的数据完毕标志位
- bit IR_code_ok_Flag; //数据解码完成标志位
- void delay(uint t)
- {
- uchar i,j;
- for(i=t;i>0;i--)
- for(j=110;j>0;j--);
- }
- void INT0_Init() //外部中断0初始化
- {
- IT0 = 1; //下降沿触发外部中断0
- EX0 = 1; //打开外部中断0
- EA = 1; //打开总中断
- }
- void Timer0_Init() //定时器0初始化
- {
- TMOD |= 0x02;
- TH0 = 0x00;
- TL0 = 0x00;
- ET0 = 1;
- EA = 1;
- TR0 = 1;
- }
- void Timer1_Init()
- {
- TMOD = 0x10;
- TH1 = 0xff;
- TL1 = 0x1a; //0.25ms
- ET1 = 0;
- TR1 = 1;
- }
- void irdata_pros() //红外数据判断处理函数,逻辑“0” OR 逻辑“1”
- {
- uchar i,j,n;
- uchar value;
- n = 1;
- for(j=0;j<4;j++)
- {
- for(i=0;i<8;i++)
- {
- value = value >>1;
- if(irdata[n] > 6) //判断是逻辑“0” OR 逻辑“1”,根据两者间的中间值6来判断
- {
- value |= 0x80;
- }
- n++;
- }
- ircode[j] = value;
- }
- IR_code_ok_Flag = 1;
- }
- void ir_work()
- {
- disp[0] = ircode[0]/16; //用户码
- disp[1] = ircode[0]%16;
- disp[2] = ircode[1]/16; //用户反码
- disp[3] = ircode[1]%16;
- disp[4] = ircode[2]/16; //数据码
- disp[5] = ircode[2]%16;
- disp[6] = ircode[3]/16; //数据反码
- disp[7] = ircode[3]%16;
- }
- void display()
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- P1=table[disp]; //段选
- P0=aable; //位选
- delay(10);
- }
- }
- void turn()
- {
- if(K1==0)
- {
- delay(20);
- state = -1;
- }
- if(K2==0)
- {
- delay(20);
- state = 0;
- }
- if(K3==0)
- {
- delay(20);
- state = 1;
- }
-
- switch(state)
- {
- case -1 :
- {
- if(duoji_timer <= 1)
- {
- PWM1 = 1;
- }
- else
- {
- PWM1 = 0;
- }
- break;
- }
- case 0 :
- {
- if(duoji_timer <= 5)
- {
- PWM1 = 1;
- }
- else
- {
- PWM1 = 0;
- }
- break;
- }
- case 1 :
- {
- if(duoji_timer <= 9)
- {
- PWM1 = 1;
- }
- else
- {
- PWM1 = 0;
- }
- break;
- }
- }
- }
- void main()
- {
- if(K1)
- {
- INT0_Init();
- Timer0_Init();
- while(1)
- {
-
- if(IR_rec_ok_Flag == 1)
- {
- irdata_pros();
- IR_rec_ok_Flag = 0;
- }
- if(IR_code_ok_Flag == 1)
- {
- ir_work();
- IR_code_ok_Flag = 0;
- }
- display();
- break;
- }
- }
- else
- {
- Timer1_Init();
- while(1)
- {
- turn();
- break;
- }
- }
- }
- void INT0_ISP() interrupt 0 //外部中断0服务函数
- {
- if(IRstart_Flag) //红外遥控开始发送
- {
- if(irtimer > 30) // 9ms/277.8us = 32 开头的引导码
- {
- bitnum = 0;
- }
- irdata[bitnum] = irtimer;//接收红外发送的数据
- irtimer = 0;
- bitnum++;
- if(bitnum==33) //接收红外发送的数据完毕
- {
- bitnum = 0;
- IR_rec_ok_Flag = 1;
- IRstart_Flag = 0;
- }
-
- }
- else
- {
- IRstart_Flag = 1;
- irtimer = 0;
- }
- }
- void Timer0_ISP()interrupt 1
- {
- irtimer++; //每隔(256*12/11.0592)加一 277.8us
- }
- void Timer1_ISP() interrupt 3 //定时器1服务函数
- {
- TH1 = 0xff;
- TL1 = 0x1A; //0.25ms
- duoji_timer++;
- if(duoji_timer == 80) //20ms
- {
- duoji_timer = 0;
- }
- }
复制代码
此帖出自
小平头技术问答
另外不能确定是硬件还是软件的问题,所以看了半天可能白费时间。
建议你调试起来跑一下,把问题范围缩小,才好。
一周热门 更多>