|
上周做的单纯的利用一些简单的模块实现pwm调速和红外避障的小车,
#include<reg52.h>
unsigned char PWM;
sbit ENA=P1^0;
sbit IN1=P1^1;
sbit IN2=P1^2;
sbit IN3=P1^3;
sbit IN4=P1^4;
sbit ENB=P1^5;
sbit a=P0^0;
void delayed(unsigned int x);
void front();
void back();
void right();
void left();
void init()
{
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
PWM=0;
}
void temp0() interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
PWM++;
if(PWM<4)
{
IN2=1;
IN4=1;
}
else
{
IN2=0;
IN4=0;
}
if(PWM>10)
PWM=0;
}
void main()
{
int i;
init();
front();
while(1)
{
if(a==0)
{
ENA=0;
ENB=0;
for(i=1;i>0;i--)
{
delayed(1000);
}
right();
for(i=1;i>0;i--)
{
delayed(240);
}
front();
}
}
}
void delayed(unsigned int x)
{
unsigned int i,j;
for(i = x; i > 0; i--)
{
for(j = 113; j >0; j--);
}
}
void front()
{
ENA = 1;
ENB = 1;
IN1 = 0;
IN2 = 1;
IN3 = 0;
IN4 = 1;
}
void back()
{
ENA = 1;
ENB = 1;
IN1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;
}
void right()
{
ENA = 1;
ENB = 1;
IN1 = 0;
IN2 = 1;
IN3 = 1;
IN4 = 0;
}
void left()
{
ENA = 1;
ENB = 1;
IN1 = 0;
IN2 = 0;
IN3 = 0;
IN4 = 1;
}
|
|