郑汉松 发表于 2019-12-5 19:40
当然可以。不过我觉得你这个红外遥控车,要解码又要PWM控制,定时器应该不够用吧?可以用89c52的芯片有3 ...
我用电脑连接调试就可以,用独立电源调试就接收不了红外信号,程序我用一个定时器把PWM和红外接收写一起,分开两个定时器写的话,只能一次接收一个按键信号,要重启才可以接收另一个,有什么办法解决。程序如下:
#include<reg52.h>
sbit P36=P3^6 ;
sbit P37=P3^7 ;
sbit P24=P2^4;
sbit ENA=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit ENB=P1^3 ;
sbit P14=P1^4;
sbit P15=P1^5;
sbit IR=P3^2;
void fonrt();
void zuozuan();
void tingzhi();
unsigned char irtime;
bit irpro_ok,irok;
unsigned char IRcord[4];
unsigned char irdata[33];
void Ir_work (void);
void Ircordpro(void);
unsigned char pwm_val_left =0;
unsigned char push_val_left =0;
unsigned char pwm_val_right =0;
unsigned char push_val_right=0;
unsigned int time;
void pwm_out_left_moto() ;
void pwm_out_right_moto() ;
bit Left_moto_stop =1; //右电机停转
bit Right_moto_stop=1;
#define Left_moto_Stop {P1^1=0,P1^2=0;}
#define Right_moto_Stop {P1^4=0,P1^5=0;}
sbit Left_moto_pwm= P1^0;
sbit Right_moto_pwm = P1^3 ;
/******************************************************************
** 左电机调速
******************************************************************/
void pwm_out_left_moto()
{
if(pwm_val_left<=push_val_left)
{
ENA=0;
}
else
{
ENA=1;
}
if(pwm_val_left>=20)
ENA=1;
}
/******************************************************************
** 右电机调速
******************************************************************/
void pwm_out_right_moto(void)
{
if(pwm_val_right<=push_val_right)
{
Right_moto_pwm=1;
}
else
{
Right_moto_pwm=0;
}
if(pwm_val_right>=20)
pwm_val_right=0;
}
void tim0_isr(void)interrupt 1 using 1
{
irtime++;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}
void EX0_ISR(void)interrupt 0
{
static unsigned char i;
static bit startflag;
if (startflag)
{if(irtime<63&&irtime>=33)
i=0;
irdata=irtime;
irtime=0;
i++;
if (i==33)
{irok=1;
i=0;
}
}
else
{irtime=0;
startflag=1;}
}
void TIM0init(void)
{TMOD=0x02;
TH0= 0x00; //1ms定时
TL0= 0x00;
ET0=1;
TR0=1;
TR1=0;}
void EX0init(void)
{IT0=1;
EX0=1;
EA=1;}
void Ircordpro (void)
{unsigned char i,j,k;
unsigned char cord,value;
k=1;
for (i=0;i<4;i++)
{for(j=1;j<=8;j++)
{cord=irdata[k];
if(cord>7)
value |=0x80;
if(j<8)
{value>>=1;
}
k++;
}IRcord=value;
value=0;}
irpro_ok=1;
}
void Ir_work(void)
{switch(IRcord[2])
{case 0x18:fonrt();break;
case 0x52:tingzhi( );break;
case 0x08:zuozuan();break;
default:break;}
irpro_ok=0 ;
}
void delay (int xms)
{unsigned int i,j;
for (i=xms;i>0;i--)
for (j=112;j>0;j--);}
void fonrt ()
{ push_val_right=16;
push_val_left =16;
P11=1;
P12=0;
P14=1;
P15=0;
delay(200);
}
void zuozuan()
{
delay(500);
push_val_left=8;
push_val_right=12;
P11=0;
P12=1;
P14=1;
P15=0;
}
void tingzhi()
{P1=0x00;
}
void main()
{ IP=0X02;
EX0init();
TIM0init();
while(1)
{
if(irok)
{Ircordpro();
irok=0;}
if(irpro_ok)
{
Ir_work();
}
}
} |