单片机小车

C51单片机小车,能够前进后退左转右转,循迹,避障,跟随,超声波测距
main.c

#include
#include
#include "serial.h"
#include "cargogogo.h"
#include "time.h"
#include "track.h"
#include "infrared.h"
#include "follow.h"
#include "chaosheng.h"
#define uchar unsigned char
#define uint unsigned int
extern bit command_finish;
extern unsigned char recbuf[20];
extern unsigned char command_team[3][15];//extern 在另外一个文件里使用就用extern
extern unsigned char cnt;

void delayms(uchar i);
unsigned char speed;
main(){
Timer1_Init();
Timer0_Init();
serial_init();

while(1){                                 // DF 1.2=1       1DB .3=0  后退
if(command_finish){
                                            //1.6=0   1.7=1  
command_finish=0;
Command_Token(recbuf);

}
if(strcmp("control",command_team[1])==0) {
 if(command_team[2][0]=='1') {

forward();
 }
 else if(command_team[2][0]=='2'){

back();

 }
 else if(command_team[2][0]=='3') {
 
left();
 
 
 }
 else if(command_team[2][0]=='4'){

right();
 
 
 }
 else if(command_team[2][0]=='0'){

stop();
}
}
else if(strcmp("speed",command_team[1])==0){
    
speed =(command_team[2][0]-48)*10+(command_team[2][1]-48);
push_val_left=speed;
push_val_right=speed;
}

else if(strcmp("track",command_team[1])==0){
    
if(LED_Track_l==1&&LED_Track_r==0){
    push_val_left=18;
    push_val_right=18;
    
}
Track_Car();
}
else if(strcmp("inrafred",command_team[1])==0){
if(command_team[2][0]=='1')
    infrared_track();
    else
        stop();
}
else if(strcmp("ultrasonic",command_team[1])==0){

  if(command_team[2][0]=='1'){

delay_start();
Timer1_Count();
distance();
}

else if(command_team[2][0]=='0')
stop();
}

else if(strcmp("follow",command_team[1])==0){
    infrared_follow();
}
}

 }

 
void delayms(uchar i){
   uchar j;
  for(;i>0;i--)
  for(j=112;j>0;j--);
}

serial.c

#include
#include
#include "serial.h"
#include "cargogogo.h"
#include "time.h"
#include "track.h"
#include "infrared.h"
#include "follow.h"
#include "chaosheng.h"
#define uchar unsigned char
#define uint unsigned int
extern bit command_finish;
extern unsigned char recbuf[20];
extern unsigned char command_team[3][15];//extern 在另外一个文件里使用就用extern
extern unsigned char cnt;

void delayms(uchar i);
unsigned char speed;
main(){
Timer1_Init();
Timer0_Init();
serial_init();

while(1){                                 // DF 1.2=1       1DB .3=0  后退
if(command_finish){
                                            //1.6=0   1.7=1  
command_finish=0;
Command_Token(recbuf);

}
if(strcmp("control",command_team[1])==0) {
 if(command_team[2][0]=='1') {

forward();
 }
 else if(command_team[2][0]=='2'){

back();

 }
 else if(command_team[2][0]=='3') {
 
left();
 
 
 }
 else if(command_team[2][0]=='4'){

right();
 
 
 }
 else if(command_team[2][0]=='0'){

stop();
}
}
else if(strcmp("speed",command_team[1])==0){
    
speed =(command_team[2][0]-48)*10+(command_team[2][1]-48);
push_val_left=speed;
push_val_right=speed;
}

else if(strcmp("track",command_team[1])==0){
    
if(LED_Track_l==1&&LED_Track_r==0){
    push_val_left=18;
    push_val_right=18;
    
}
Track_Car();
}
else if(strcmp("inrafred",command_team[1])==0){
if(command_team[2][0]=='1')
    infrared_track();
    else
        stop();
}
else if(strcmp("ultrasonic",command_team[1])==0){

  if(command_team[2][0]=='1'){

delay_start();
Timer1_Count();
distance();
}

else if(command_team[2][0]=='0')
stop();
}

else if(strcmp("follow",command_team[1])==0){
    infrared_follow();
}
}

 }

 
void delayms(uchar i){
   uchar j;
  for(;i>0;i--)
  for(j=112;j>0;j--);
}

cargogogo.c

#include 
#include "cargogogo.h"
extern unsigned char pwm_val_left;       
extern unsigned char pwm_val_right;       
unsigned char push_val_left;
unsigned char push_val_right;
bit left_pwm =1;
bit right_pwm =1;

sbit ENL=P1^4;
sbit ENR=P1^5;
sbit INL_F=P1^2;
sbit INL_B=P1^3;
sbit INR_F=P1^6;
sbit INR_B=P1^7;

#define L_go      {INL_F=0,INL_B=1;} //左轮前进
#define L_back    {INL_F=1,INL_B=0;} //左轮后退    
#define L_stop    {INL_F=0,INL_B=0;} //左轮停止
#define R_go      {INR_F=1,INR_B=0;} //右轮前进
#define R_back    {INR_F=0,INR_B=1;} //右轮后退
#define R_stop    {INR_F=0,INR_B=0;} //右轮停止
#define PUSH_VAL_LEFT_MAX 80
#define PUSH_VAL_RIGHT_MAX 80



void Left_Moto()
{
    if(left_pwm){
    if(pwm_val_left<=18)
    ENL=1;
    
    
    else
        ENL=0;
    if(pwm_val_left==PUSH_VAL_LEFT_MAX)
        pwm_val_left=0;
    }
    else{
        ENL=0;
    }
}
void Right_Moto()
{
    if(right_pwm){
    if(pwm_val_right<=18)
        ENR=1;
    
    else
        ENR=0;
    if(pwm_val_right==PUSH_VAL_RIGHT_MAX)
        pwm_val_right=0;
    }
    else{
        ENR=0;
    }
}

void forward(){
    L_go
R_go
}
void back(){
    L_back
R_back
}
void left(){
    R_go
L_stop
}
void right(){
    L_go
R_stop
}
void stop(){
    L_stop
R_stop
}

time.c

#include 
#include"time.h"
#include"cargogogo.h"
unsigned char cnt;
unsigned char pwm_val_left;        //左轮PWM占空比值
unsigned char pwm_val_right;        //右轮PWM占空比值
void Timer0_Init(void)
{
    TMOD = 0x01;    //设定定时计数器0:工作方式1,16位计数
//  TH0 = (65536 - SYSCLK/12/FCLK)/256;//设置计数初值高8位
//  TL0 = (65536 - SYSCLK/12/FCLK)%256;//设置计数初值低8位
    TH0 = 0xFF;                        //0.25Ms定时      
    TL0 = 0x1A;
    EA = 1;         //开总中断
    ET0 = 1;
    TR0 = 1;
        //TCON=0x10,启动定时计数器0
pwm_val_left=0;
pwm_val_right=0;

}
void Timer0_Isr(void) interrupt 1
{
    TH0=0XFF;
    TL0=0X1A;
    pwm_val_left++;
    pwm_val_right++;
    Left_Moto();
    Right_Moto();

}

infrared.c

#include 
#include "track.h"
#include "cargogogo.h"
#include "serial.h"
#include "infrared.h"

sbit    LED_infrared_l=P3^4;
sbit    LED_infrared_r=P3^5;
void infrared_track(void)
{
    if(LED_infrared_l==0&&LED_infrared_r==0)
     forward();
    else if(LED_infrared_l==1&&LED_infrared_r==0)
     right();
    else if(LED_infrared_l==0&&LED_infrared_r==1)
     left();
    else if(LED_infrared_l==1&&LED_infrared_r==1)
     stop();
    
}

track.c

#include
#include"track.h"
#include"cargogogo.h"

void Track_Car(){
if(LED_Track_l==0&&LED_Track_r==0){
forward();
}
else if(LED_Track_l==1&&LED_Track_r==0){
left();
}
else if(LED_Track_l==0&&LED_Track_r==1){
right();
}
else if(LED_Track_l==1&&LED_Track_r==1){
left();
}

                 }

follow.c

#include 
#include "cargogogo.h"
#include "serial.h"
#include "follow.h"

sbit    LED_follow_l=P3^4;
sbit    LED_follow_r=P3^5;
void infrared_follow(void)
{
    if(LED_follow_l==0&&LED_follow_r==0)
     stop();
    else if(LED_follow_l==1&&LED_follow_r==0)
     left();
    else if(LED_follow_l==0&&LED_follow_r==1)
     right();
    else if(LED_follow_l==1&&LED_follow_r==1)
     forward();
    
}

chaosheng.c

#include
#include"chaosheng.h"
#include"cargogogo.h"
#include"serial.h"


unsigned int time;
unsigned int s;
sbit LED_F=P1^0;


void delay10ms(void);

void delay_start(void){
TRIG=1;
delay10ms();
TRIG=0;
}


void Timer1_Init(){
TMOD=0x10|TMOD;
TH1=0;
TL1=0;
EA=1;
ET1=1;
//TR1=1;
}

void Timer1_Count(){
while(!ECHO);     //接受是高电平的时候 开启定时器
TR1=1;
while(ECHO);
TR1=0;
}

void Timer1_Isr(void) interrupt 3{
//TH0=0xFC;
//TL0=0x18;
ECHO=0;

}

void distance(void){
time=TH1*256+TL1;
TH1=0;
TL1=0;
LED_F=0;
s=time*0.17;

if(s<=1500){

left();
//delay10ms();
//car_L();
}
else if(s>1500){
 
forward();
}        

}

void delay10ms(void)
{
        unsigned char i,j,k;
         for(i=5;i>0;i--)
         for(j=4;j>0;j--)
         for(k=248;k>0;k--);
}

你可能感兴趣的:(单片机小车)