用单片机做毕业设计《无线遥控小车》
此贴本人之前的硬件配置求助已经搞定,现在还有谁在做,我们可以互相探讨,就是实现,前进、后退、左转、右转的功能,最好能控制器速度!!!
诚信交流,非诚勿扰!!
QQ:354073268
#include <reg51.h>
#define TH0C 0xff
#define TL0C 0xf6
unsigned int dr_l,dr_r,times; //duty ratio 是占空比的意思。
unsigned char flag;
sbit leftv=P1^0;
sbit leftg=P1^1;
sbit rightv =P1^2;
sbit rightg =P1^3;
sbit ena=P1^4;
sbit enb=P1^5;
sbit key0=P2^5;
sbit key_a=P2^0;
sbit key_r=P2^1;
sbit key_l=P2^2;
sbit key_b=P2^3;
void beforeint(void);
void main()
{
while(1)
{
P2=0xff;
if(key0)
{
ena=1;
enb=1;
if(key_a)
{
flag=1;
dr_l=50;
dr_r=50;
leftv=1;
rightv=1;
beforeint();
}
if(key_r)
{
flag=2;
dr_l=99;
dr_r=1;
leftv=1;
rightg=1;
beforeint();
}
if(key_l)
{
flag=3;
dr_l=1;
dr_r=99;
leftg=1;
rightv=1;
beforeint();
}
if(key_b)
{
flag=4;
dr_l=50;
dr_r=50;
leftg=1;
rightg=1;
beforeint();
}
}
else
{
ena=0;
enb=0;
flag=0;
P1=0xff;
continue;
}
}
}
void beforeint(void)
{
TMOD=0x01;
TH0=TH0C;
TL0=TL0C;
TR0=1;
ET0=1;
EA=1;
}
void time0(void) interrupt 1 using 2
{
times=0;
TH0=TH0C;
TL0=TL0C;
times+=1;
if(times>99)
{
times=0;
}
if(flag==1)
{
if(times<=dr_l)
{
leftg=0;
}
else
{
leftg=1;
}
if(times<=dr_r)
{
rightg=0;
}
else
{
rightg=1;
}
}
else if(flag==2)
{
if(times<=dr_l)
{
leftg=0;
}
else
{
leftg=1;
}
if(times<=dr_r)
{
rightv=0;
}
else
{
rightv=1;
}
}
else if(flag==3)
{
if(times<=dr_l)
{
leftv=0;
}
else
{
leftv=1;
}
if(times<=dr_r)
{
rightg=0;
}
else
{
rightg=1;
}
}
else if(flag==4)
{
if(times<=dr_l)
{
leftv=0;
}
else
{
leftv=1;
}
if(times<=dr_r)
{
rightv=0;
}
else
{
rightv=1;
}
}
}
|