如想了解接线的请私聊作者
由于小车的情况比较复杂要考虑的东西很多,有时候接线不合适都跑不起来
话不多说直接上代码吧:
//以下为代码段
#include"reg51.h"
#include "intrins.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit HIN1=P1^4;// P1.0Êä³öpwm
sbit HIN2=P3^0;// P1.1Êä³öpwm
sbit HIN3=P1^6;// P1.2Êä³öpwm
sbit HIN4=P1^7;// P1.3Êä³öpwm
sbit QIN1=P1^0;// P1.4Êä³öpwm
sbit QIN2=P1^1;// P1.5Êä³öpwm
sbit QIN3=P1^2;// P1.6Êä³öpwm
sbit QIN4=P1^3;// P1.7Êä³öpwm
sbit IRIN=P3^2; // ºìÍâ¿ÚΪ3.2 ÖжÏ0
sbit k1=P2^0; //Ñ°¼£Ä£¿ék1λ
sbit k2=P2^1; //Ñ°¼£Ä£¿ék2λ
sbit k3=P2^2; //Ñ°¼£Ä£¿ék3λ
sbit k4=P2^4; //Ñ°¼£Ä£¿ék4λ
sbit k5=P2^5; //Ñ°¼£Ä£¿ék5λ
u8 time,i;
u16 count,count2,count3,count4; //pwmÕ¼¿Õ±È£¨ËٶȴóС£©
//u8 z=0, x=0;
/********»ù±¾ÑÓʱº¯Êý*******/
void delay(int u)
{
while(u--);
}
void minrun()
{
count=30;
count2=30;
count3=0;
count4=0;
}
void run()
{
count=35;
count2=35;
count3=0;
count4=0;
}
void right()
{
count=0;//ÓÒת
count2=40;//zouÕý´«
count3=60;//you·´
count4=0;
}
void Lefet()
{
count=30;
count2=0;
count3=0;
count4=50;
}
void back()
{
count=0;
count2=0;
count3=50;
count4=50;
}
void stop()
{
count=0;
count2=0;
count3=0;
count4=0;
}
void Lefet1()
{
count=60;
count2=0;
count3=0;
count4=40;
}
void lefet_1()
{
count=60;//
count2=0;
count3=0;
count4=40;
}
void right_1()
{
count=0;
count2=60;
count3=40;
count4=0;
}
void lefet_2()
{
count=65;
count2=0;
count3=0;
count4=40;
}
void right_2()
{
count=0;
count2=40;
count3=60;
count4=0;
}
void xunji()
{
u8 m;
if(k1==1&&k2==1&&k4==1&&k4==1&&k5==1)//ûÓмì²âµ½ºÚÏß
{m=0;}
if(k1==1&&k2==1&&k3==0&&k4==1&&k5==1)//Öмä¼ì²âµ½ºÚÏß
{m=1;}
if(k1==1&&k2==0&&k3==1&&k4==1&&k5==1)//΢µ÷ ×ó
{m=2;}
if(k1==1&&k2==1&&k3==1&&k4==0&&k5==1)//΢µ÷ ÓÒ
{m=3;}
if(k1==0&&k2==1&&k3==1&&k4==1&&k5==1)//
{m=4;}
if(k1==1&&k2==1&&k3==1&&k4==1&&k5==0)//´ó·ùÓÒת
{m=5;}
if(k1==0&&k3==0&&k4==1&&k5==1)//Ö±½Ç ×ó
{m=6;}
if(k1==1&&k2==1&&k3==0&&k5==0)//Ö±½Ç ÓÒ
{m=7;}
/*
if(k1==0&&k2==0&&k3==1&&k4==1&&k5==1)
{m=8;}
if(k1==1&&k2==1&&k3==1&&k4==0&&k5==0)
{m=9;}
if(k1==1&&k2==1&&k3==1&&k4==1&&k5==0)
{m=10;}
*/
switch(m)
{
//case (0):lefet_2();break;
case (1):run();break;
case (2):Lefet();delay(200);break;//΢µ÷
case (3):right();delay(200);break;//΢µ÷
//case (4):Lefet1();delay(900);break;//´ó·ù×óת
//case (5):right();delay(900);break;//´ó·ù¶ÈÓÒת
case (6):lefet_1();delay(900);break;//Ö±½Ç ×óת
case (7):right_1();delay(900);break;//Ö±½Ç ÓÒת
//case (8):lefet_2();delay(200);break;
//case (9):right_2();delay(200);break;
//case (10):right_2();delay(900);break;
//default :run();delay(500);stop();break;
}
}
/*******************
* ¹¦ÄÜ£º´ò¿ªÐèÒªÓõ½µÄÖжÏ
* Óõ½µÄÖжϣº¶¨Ê±Æ÷0¡¢1 ¹¤×÷·½Ê½1 16λ¶¨Ê±Æ÷ģʽ
* ÍⲿÖÕ¶Ë0
*********************/
void timer()
{
TMOD=0x11;//¶¨Ê±Æ÷0¹¤×÷·½Ê½1
TH0=0xff;//(65536-10)/256;//¸³³õÖµ¶¨Ê±
TL0=0xf7;//(65536-10)%256;//0.01ms
TH1=0;
TL1=0;
EA=1;//¿ª×ÜÖжÏ
ET0=1;//¿ª¶¨Ê±Æ÷0ÖжÏ
ET1=1;
TR0=1;//Æô¶¯¶¨Ê±Æ÷0
IT0=1;
EX0=1;
IRIN=1;
}
/*******Ö÷º¯Êý********/
void main()
{
timer(); //µ÷ÓÃÖжϴò¿ªº¯Êý
while(1)
{
xunji();
//back();
//run();
//Lefet();
//right();
// delay(3000);
//xunji();
}
}
/****¶¨Ê±Æ÷0Òç³öÀûÓÃpwmµ÷4¸öµç»úµÄËÙ¶È***/
void tim0() interrupt 1
{
//TR0=0;//¸³³õֵʱ£¬¹Ø±Õ¶¨Ê±Æ÷
TH0=0xff;//(65536-10)/256;//¸³³õÖµ¶¨Ê±
TL0=0xf7;//(65536-10)%256;//0.01ms£¬ÒòΪһ¸öÖÜÆÚ1us
//TR0=1;//´ò¿ª¶¨Ê±Æ÷
i++;
if(i>=100) //1khz
i=0;
if(i<=count) //Õ¼¿Õ±È%30,¿É¸Ä
{
QIN2=0;
HIN4=0;
}
else
{
QIN2=1;
HIN4=1;
}
if(i<=count2) //Õ¼¿Õ±È%30,¿É¸Ä
{
QIN4=0;
HIN2=0;
}
else
{
QIN4=1;
HIN2=1;
}
if(i<=count3) //Õ¼¿Õ±È%30,¿É¸Ä
{
QIN1=0;
HIN3=0;
}
else
{
QIN1=1;
HIN3=1;
}
if(i<=count4) //Õ¼¿Õ±È%30,¿É¸Ä
{
QIN3=0;
HIN1=0;
}
else
{
QIN3=1;
HIN1=1;
}
}
注释出现乱码了,然后下面我在以文本的形式编一下:
#include"reg51.h"
#include "intrins.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit HIN1=P1^4;// P1.0Êä³öpwm
sbit HIN2=P3^0;// P1.1Êä³öpwm
sbit HIN3=P1^6;// P1.2Êä³öpwm
sbit HIN4=P1^7;// P1.3Êä³öpwm
sbit QIN1=P1^0;// P1.4Êä³öpwm
sbit QIN2=P1^1;// P1.5Êä³öpwm
sbit QIN3=P1^2;// P1.6Êä³öpwm
sbit QIN4=P1^3;// P1.7Êä³öpwm
sbit IRIN=P3^2; // ºìÍâ¿ÚΪ3.2 ÖжÏ0
sbit k1=P2^0; //Ñ°¼£Ä£¿ék1λ
sbit k2=P2^1; //Ñ°¼£Ä£¿ék2λ
sbit k3=P2^2; //Ñ°¼£Ä£¿ék3λ
sbit k4=P2^4; //Ñ°¼£Ä£¿ék4λ
sbit k5=P2^5; //Ñ°¼£Ä£¿ék5λ
u8 time,i;
u16 count,count2,count3,count4; //pwmÕ¼¿Õ±È£¨ËٶȴóС£©
//u8 z=0, x=0;
/********»ù±¾ÑÓʱº¯Êý*******/
void delay(int u)
{
while(u--);
}
void minrun()
{
count=30;
count2=30;
count3=0;
count4=0;
}
void run()
{
count=35;
count2=35;
count3=0;
count4=0;
}
void right()
{
count=0;//ÓÒת
count2=40;//zouÕý´«
count3=60;//you·´
count4=0;
}
void Lefet()
{
count=30;
count2=0;
count3=0;
count4=50;
}
void back()
{
count=0;
count2=0;
count3=50;
count4=50;
}
void stop()
{
count=0;
count2=0;
count3=0;
count4=0;
}
void Lefet1()
{
count=60;
count2=0;
count3=0;
count4=40;
}
void lefet_1()
{
count=60;//
count2=0;
count3=0;
count4=40;
}
void right_1()
{
count=0;
count2=60;
count3=40;
count4=0;
}
void lefet_2()
{
count=65;
count2=0;
count3=0;
count4=40;
}
void right_2()
{
count=0;
count2=40;
count3=60;
count4=0;
}
void xunji()
{
u8 m;
if(k1==1&&k2==1&&k4==1&&k4==1&&k5==1)//ûÓмì²âµ½ºÚÏß
{m=0;}
if(k1==1&&k2==1&&k3==0&&k4==1&&k5==1)//Öмä¼ì²âµ½ºÚÏß
{m=1;}
if(k1==1&&k2==0&&k3==1&&k4==1&&k5==1)//΢µ÷ ×ó
{m=2;}
if(k1==1&&k2==1&&k3==1&&k4==0&&k5==1)//΢µ÷ ÓÒ
{m=3;}
if(k1==0&&k2==1&&k3==1&&k4==1&&k5==1)//
{m=4;}
if(k1==1&&k2==1&&k3==1&&k4==1&&k5==0)//´ó·ùÓÒת
{m=5;}
if(k1==0&&k3==0&&k4==1&&k5==1)//Ö±½Ç ×ó
{m=6;}
if(k1==1&&k2==1&&k3==0&&k5==0)//Ö±½Ç ÓÒ
{m=7;}
/*
if(k1==0&&k2==0&&k3==1&&k4==1&&k5==1)
{m=8;}
if(k1==1&&k2==1&&k3==1&&k4==0&&k5==0)
{m=9;}
if(k1==1&&k2==1&&k3==1&&k4==1&&k5==0)
{m=10;}
*/
switch(m)
{
//case (0):lefet_2();break;
case (1):run();break;
case (2):Lefet();delay(200);break;//΢µ÷
case (3):right();delay(200);break;//΢µ÷
//case (4):Lefet1();delay(900);break;//´ó·ù×óת
//case (5):right();delay(900);break;//´ó·ù¶ÈÓÒת
case (6):lefet_1();delay(900);break;//Ö±½Ç ×óת
case (7):right_1();delay(900);break;//Ö±½Ç ÓÒת
//case (8):lefet_2();delay(200);break;
//case (9):right_2();delay(200);break;
//case (10):right_2();delay(900);break;
//default :run();delay(500);stop();break;
}
}
/*******************
* ¹¦ÄÜ£º´ò¿ªÐèÒªÓõ½µÄÖжÏ
* Óõ½µÄÖжϣº¶¨Ê±Æ÷0¡¢1 ¹¤×÷·½Ê½1 16λ¶¨Ê±Æ÷ģʽ
* ÍⲿÖÕ¶Ë0
*********************/
void timer()
{
TMOD=0x11;//¶¨Ê±Æ÷0¹¤×÷·½Ê½1
TH0=0xff;//(65536-10)/256;//¸³³õÖµ¶¨Ê±
TL0=0xf7;//(65536-10)%256;//0.01ms
TH1=0;
TL1=0;
EA=1;//¿ª×ÜÖжÏ
ET0=1;//¿ª¶¨Ê±Æ÷0ÖжÏ
ET1=1;
TR0=1;//Æô¶¯¶¨Ê±Æ÷0
IT0=1;
EX0=1;
IRIN=1;
}
/*******Ö÷º¯Êý********/
void main()
{
timer(); //µ÷ÓÃÖжϴò¿ªº¯Êý
while(1)
{
xunji();
//back();
//run();
//Lefet();
//right();
// delay(3000);
//xunji();
}
}
/****¶¨Ê±Æ÷0Òç³öÀûÓÃpwmµ÷4¸öµç»úµÄËÙ¶È***/
void tim0() interrupt 1
{
//TR0=0;//¸³³õֵʱ£¬¹Ø±Õ¶¨Ê±Æ÷
TH0=0xff;//(65536-10)/256;//¸³³õÖµ¶¨Ê±
TL0=0xf7;//(65536-10)%256;//0.01ms£¬ÒòΪһ¸öÖÜÆÚ1us
//TR0=1;//´ò¿ª¶¨Ê±Æ÷
i++;
if(i>=100) //1khz
i=0;
if(i<=count) //Õ¼¿Õ±È%30,¿É¸Ä
{
QIN2=0;
HIN4=0;
}
else
{
QIN2=1;
HIN4=1;
}
if(i<=count2) //Õ¼¿Õ±È%30,¿É¸Ä
{
QIN4=0;
HIN2=0;
}
else
{
QIN4=1;
HIN2=1;
}
if(i<=count3) //Õ¼¿Õ±È%30,¿É¸Ä
{
QIN1=0;
HIN3=0;
}
else
{
QIN1=1;
HIN3=1;
}
if(i<=count4) //Õ¼¿Õ±È%30,¿É¸Ä
{
QIN3=0;
HIN1=0;
}
else
{
QIN3=1;
HIN1=1;
}
}
有问题请私聊博主哦