#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar CYCLE,PWM_ON,PWM_Z;
sbit pwm1=P1^0; //小车后轮控制口
sbit pwm2=P1^1; //小车前轮控制
sbit xunjizuo=P1^2; //控制口
sbit xunjizhong=P1^3;
sbit xunjiyou=P1^4;
sbit dj10=P1^5; //电机1
sbit dj11=P1^6;
sbit dj20=P1^7; //电机2
sbit dj21=P2^0;
void xunji()
{
if((xunjizuo==0)&&(xunjizhong==1)&&(xunjiyou==0))
{
pwm2=0;
dj10=1;dj11=0;
PWM_ON=4;
}
if((xunjizuo==1)&&(xunjizhong==0)&&(xunjiyou==0))
{
dj20=1;dj21=0; //左转
pwm2=1;
PWM_ON=3;
if((xunjizuo==0)&&(xunjizhong==1)&&(xunjiyou==0))
{
pwm2=0;
PWM_ON=4;
}
}
if((xunjizuo==0)&&(xunjizhong==0)&&(xunjiyou==1))
{
dj20=0;dj21=1; //右转
pwm2=1;
PWM_ON=3;
if((xunjizuo==0)&&(xunjizhong==1)&&(xunjiyou==0))
{
pwm2=0;
PWM_ON=4;
}
}
if((xunjizuo==1)&&(xunjizhong==1)&&(xunjiyou==1)) //交叉口判断
{
pwm2=0;
PWM_ON=4;
}
if((xunjizuo==0)&&(xunjizhong==0)&&(xunjiyou==0)) //交叉口判断
{
pwm2=0;
PWM_ON=0;
}
}
void init()
{
PWM_ON=0;
PWM_Z=0;
TMOD=0x01;
TH0=(65536-100)/256;
TL0=(65536-100)%256; //定时0.1mS
EA=1;
ET0=1;
TR0=1;
CYCLE = 5; // 时间可以调整 这个是10调整 8位PWM就是256步
}
void main()
{
init();
while(1)
{
xunji();
}
}
void tim(void) interrupt 1
{
static unsigned char count;
TH0=(65536-100)/256;
TL0=(65536-100)%256; //定时0.1mS
if (count==PWM_ON)
{
pwm1 = 0;
}
count++;
if(count == CYCLE)
{
count=0;
if(PWM_ON!=0)
pwm1 = 1;
}
} |