刚写了一个小车循迹测试程序,板子还在焊接,请高手帮忙看下。。
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar CYCLE,PWM_ON,PWM_Z,i,j;
sbit pwm1=P1^0; //PWM控制小车速度
sbit pwm2=P1^1; //控制小车转弯
sbit xunjizuo=P1^2; //控制口
sbit xunjiyou=P1^3;
sbit xunjizhong=P1^4;
sbit dj10=P1^5; //电机1
sbit dj11=P1^6;
sbit dj20=P1^7; //电机2
sbit dj21=P2^0;
sbit led=P2^1;
void delay(uint z) //延迟1ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void djzhengfan()
{
dj10=1;dj11=0;
PWM_ON=1;
delay(2000);
dj20=1;dj21=0;
PWM_Z=5;
delay(2000);
dj20=0;dj21=1;
PWM_Z=5;
delay(2000);
PWM_ON=3;
delay(2000);
PWM_ON=5;
delay(2000);
PWM_ON=0;
delay(1000);
dj10=0;dj11=1; //反转
PWM_ON=1;
delay(1000);
PWM_ON=3;
delay(2000);
PWM_ON=5;
delay(2000);
PWM_ON=0;
delay(1000);
}
void tingliu()
{
PWM_ON=0;
PWM_Z=0;
for(i=0;i<5;i++)
{
led=0;
delay(500);
led=1;
delay(500);
}
}
void main()
{
TMOD=0x11;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
TH1=(65536-100)/256;
TL1=(65536-100)%256;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
CYCLE = 5; // 时间可以调整 这个是10调整 8位PWM就是256步
tingliu();
while(1)
{
djzhengfan();
}
}
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;
}
}
void tim1(void) interrupt 3
{
static unsigned char count1;
TH1=(65536-100)/256;
TL1=(65536-100)%256; //定时0.1mS
if (count1==PWM_Z)
{
pwm2 = 0;
}
count1++;
if(count1== CYCLE)
{
count1=0;
if(PWM_Z!=0)
pwm2=1;
}
} |