|
лл´óÉñ£¬»¹ÓÐÎÒÏÖÔÚÒ»¸öÑ°ºÚÏߣ¬¾ÍÊÇÑ°¼£µÄ³ÌÐò×ÜÊǸ㲻³öÀ´°ïÎÒ¿´¿´£¬ÓÐËĸöÑ°¼£ºìÍ⣬ÖмäÓÐÁ½¸ö¡£
ÎÒдµÄ³ÌÐòÊÇÕâÑùµÄ£¬´óÉñ°ïÎÒ¸ÄÒ»¸ÄÊÔһϣ¬¶àл´óÉñ#include<AT89X52.H> //reg52.h
#define uchar unsigned char
#define uint unsigned int
sbit QL1 = P1^0;
sbit QL2 = P1^1; // Ç°Ãæ×ó±ß Ç°½ø£¨Õýת£©
sbit HL1 = P1^2; // Ç°Ãæ×ó±ß ºóÍË£¨·´×ª£©
sbit HL2 = P1^3;
sbit QR1 = P1^4;
sbit QR2 = P1^5;
sbit HR1 = P1^6;
sbit HR2 = P1^7;
sbit EN1 = P2^0; // ¸ßµçƽʹÄÜ
sbit EN2 = P2^1; // ¸ßµçƽʹÄÜ
sbit EN3 = P2^2; // ¸ßµçƽʹÄÜ
sbit EN4 = P2^3; // ¸ßµçƽʹÄÜ
#define Left_go {QL1=1,QL2=0,HL1=1,HL2=0;} //×ó±ßµç»úÏòÇ°×ß
#define Left_back {QL1=0,QL2=1,HL1=0,HL2=1;} //×ó±ßµç»úÏòºó×ß
#define Left_Stop {QL1=1,QL2=1,HL1=1,HL2=1;} //×ó±ßµç»úͣת
#define Right_go {QR1=1,QR2=0,HR1=1,HR2=0;} //Óұߵç»úÏòÇ°×ß
#define Right_back {QR1=0,QR2=1,HR1=0,HR2=1;} //Óұߵç»úÏòºó×ß
#define Right_Stop {QR1=1,QR2=1,HR1=1,HR2=1;} //Óұߵç»úͣת
#define XL P2_3 // ×󴫸ÐÆ÷
#define XML P2_4 // ÖÐ×󴫸ÐÆ÷
#define XMR P2_5 // ÖÐÓÒ´«¸ÐÆ÷
#define XR P2_6 // ÓÒ´«¸ÐÆ÷
#define Left_pwm1 P2_0 //PWMÐźŶË
#define Left_pwm2 P2_1 //PWMÐźŶË
#define Right_pwm1 P2_2 //PWMÐźŶË
#define Right_pwm2 P2_3 //PWMÐźŶË
uchar pwmleft =0;//±äÁ¿¶¨Òå
uchar pushleft1 =0;// ×óµç»ú1Õ¼¿Õ±ÈN/20
uchar pushleft2 =0;// ×óµç»ú2Õ¼¿Õ±ÈN/20
uchar pwmright =0;
uchar pushright1=0;// ÓÒµç»ú1Õ¼¿Õ±ÈN/20
uchar pushright2=0;// ÓÒµç»ú2Õ¼¿Õ±ÈN/20
bit Right_stop=1;
bit Left_stop =1;
uint time=0;
//ÑÓʱº¯Êý
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
//go
void run(void)
{
pushleft1=7; //Ëٶȵ÷½Ú±äÁ¿ 0-20,0×îС£¬20×î´ó
pushleft2=7;
pushright1=7;
pushright2=7;
Left_go ; //×óµç»úÍùÇ°×ß
Right_go ; //ÓÒµç»úÍùÇ°×ß
}
//left
void leftrun(void)
{
pushleft1=7;
pushleft2 =7;
pushright1=7;
pushright2=7;
Right_go ; //ÓÒµç»úÍùÇ°×ß
Left_Stop ; //×óµç»úÍ£Ö¹
}
//right
void rightrun(void)
{
pushleft1=7;
pushleft2=7;
pushright1=7;
pushright2=7;
Left_go; //×óµç»úÍùÇ°×ß
Right_Stop; //ÓÒµç»úÍùÇ°×ß
}
// ×óµç»úµ÷ËÙ
//µ÷½Úpush_val_leftµÄÖµ¸Ä±äµç»úתËÙ,Õ¼¿Õ±È
void pwm_out_left(void)
{
if(Left_stop&&Left_stop)
{
if(pwmleft<=pushleft1&&pwmleft<=pushleft2)
{
Left_pwm1=1;
Left_pwm2=1;
}
else
{
Left_pwm1=0;
Left_pwm2=0;
}
if(pwmleft>=20)
pwmleft=0;
}
else
{
Left_pwm1=0;
Left_pwm2=0;
}
}
// ÓÒµç»úµ÷ËÙ
void pwm_out_right(void)
{
if(Right_stop)
{
if(pwmright<=pushright1&&pwmright<=pushright2)
{
Right_pwm1=1;
Right_pwm2=1;
}
else
{
Right_pwm1=0;
Right_pwm2=0;
}
if(pwmright>=20)
pwmright=0;
}
else
{
Right_pwm1=0;
Right_pwm2=0;
}
}
void main(void)
{
P1=0X00;
delay(100);
run();
TMOD=0X01;
TH0= 0XFc; //1ms¶¨Ê±
TL0= 0X18;
TR0= 1;
ET0= 1;
EA = 1; //¿ª×ÜÖжÏ
while(1)
{
//ÓÐÐźÅΪ0 ûÓÐÐźÅΪ1
if(XL==0&&XR==0&&XML==0&&XMR==0)
run();
else
{
if(XL==1&&XR==0) //×ó±ß¼ì²âµ½ºÚÏß
{
leftrun();
}
if(XR==1&&XL==0) //Óұ߼ì²âµ½ºÚÏß
{
rightrun();
}
}
}
}
//TIMER0ÖжϷþÎñ×Óº¯Êý²úÉúPWMÐźÅ
void timer0()interrupt 1 using 2
{
TH0=0XFc; //1Ms¶¨Ê±
TL0=0X18;
time++;
pwmleft++;
pwmright++;
pwm_out_left();
pwm_out_right();
} C:\Documents and Settings\Administrator\×ÀÃæ |
|