main.c
#include <REGX52.H>
#include "time.h"
#include "motordriver.h"
#include "MoveState.h"
#include "follow.h"
#include "lanya.h"
extern unsigned int PWMR;
extern unsigned int PWML;
unsigned int recive=0;
void main()
{
UART_Init();
if(recive==1)
{
PWML=15;
PWMR=15;
go();
}
if(recive==2)
{
PWML=15;
PWMR=15;
back();
}
if(recive==3)
{
PWML=15;
PWMR=0;
left();
}
if(recive==4)
{
PWML=0;
PWMR=15;
right();
}
if(recive==5)
{
PWML=15;
PWMR=15;
stop();
}
if(recive==6)
{
PWML=15;
PWMR=15;
left1();
}
if(recive==6)
{
PWML=15;
PWMR=15;
right1();
}
if(recive==7)
{
PWML=15;
PWMR=10;
go();
}
if(recive==8)//zuozhuan
{
PWML=10;
PWMR=15;
go();
}
Timer0_Init();
EA=1;
while(1)
{
track();
}
}
void Uart_Routine(void) interrupt 4
{
if(RI==1)
{
RI=0;
recive= SBUF; //½ÓÊÕÊý¾Ý
}
}
time.c
#include <REGX52.H>
sbit ENA=P1^2; //定义左电机
sbit ENB=P1^0; //定义右电机
unsigned int PWMR=0,PWML=0,T0Count=0;
void Timer0_Init()
{
TMOD=0x01; //0000 0001
TH0=(64536-100)/256;
TL0=(64536-100)%256;
TF0=0;
TR0=1;//打开定时器
ET0=1;//打开定时器0中断允许
EA=1;
PT0=0;
}
void Timer0_Routine() interrupt 1 //中断函数
{
TL0 = (64536-100)%256;
TH0 = (64536-100)/256;
T0Count++;
if(T0Count<PWMR)
{
ENB=1;
}
else
{
ENB=0;
}
if(T0Count<PWML)
{
ENA=1;
}
else
{
ENA=0;
}
if(T0Count>=100)
{
T0Count=0;
}
}
time.h
#ifndef __TIME_H__
#define __TIME_H__
void Timer0Init(void);
#endif
motordirver.c
#include <REGX52.H>
sbit IN1=P0^1;
sbit IN2=P0^2;
sbit IN3=P0^4;
sbit IN4=P0^3;
void leftmotorforward()
{IN1=0;IN2=1;}//×óµç»úÕýת
void rightmotorforward()
{IN3=0;IN4=1;}//ÓÒµç»úÕýת
void leftmotorstop()
{IN1=1;IN2=1;}//×óµç»úÍ£Ö¹
void rightmotorstop()
{IN3=1;IN4=1;}//×óµç»úÍ£Ö¹
void leftmotorback()
{IN1=1;IN2=0;}//×óµç»ú·´×ª
void rightmotorback()
{IN3=1;IN4=0;}//ÓÒµç»ú·´×ª
motordirver.h
#ifndef __MOTORDRIVER_H__
#define __MOTORDRIVER_H__
void leftmotorforward();
void rightmotorforward();
void leftmotorstop();
void rightmotorstop();
void leftmotorback();
void rightmotorback();
#endif
follow.c
#include <REGX52.H>
#include "time.h"
#include "Delay.h"
#include "MoveState.h"
sbit d5=P2^0;
sbit d4=P2^1;
sbit d3=P2^2;
sbit d2=P2^3;
sbit d1=P2^4;
extern unsigned int PWMR;
extern unsigned int PWML;
void track()//Ãð1 ÁÁ0 ¼ì²âµ½ºÚÏßΪÃð
{
if(d1==0&&d2==0&&d3==0&&d4==0&&d5==1)
{
PWML=10;
PWMR=10;
go();
}
if(d1==0&&d2==1&&d3==1&&d4==0&&d5==1)
{
PWML=10;
PWMR=10;
go();
}
if(d1==0&&d2==0&&d3==0&&d4==0&&d5==0)
{
PWML=10;
PWMR=10;
go();
}
if(d1==1&&d2==1&&d3==1&&d4==1&&d5==1)
{
PWML=16;
PWMR=10;
right1();
}
if(d1==0&&d2==0&&d3==1&&d4==1&&d5==0)
{
PWML=16;
PWMR=10;
right1();
}
if(d1==1&&d2==1&&d3==0&&d4==0&&d5==0)
{
PWML=10;
PWMR=16;
left1();
}
if(d1==0&&d2==0&&d3==0&&d4==1&&d5==0)
{
PWML=14;
PWMR=0;
go();
}
if(d1==1&&d2==0&&d3==0&&d4==0&&d5==0)
{
PWML=0;
PWMR=14;
go();
}
if(d1==0&&d2==1&&d3==0&&d4==0&&d5==0)
{
PWML=6;
PWMR=13;
go();
}
if(d1==0&&d2==0&&d3==1&&d4==0&&d5==0)
{
PWML=13;
PWMR=6;
go();
}
}
follow.h
#ifndef __DELAY_H__
#define __DELAY_H__
void track();
#endif
movestate.c
#include <REGX52.H>
#include "motordriver.h"
void go()
{leftmotorforward();rightmotorforward();}
void left()
{leftmotorstop();rightmotorforward();}
void right()
{leftmotorforward();rightmotorstop();}
void stop()
{leftmotorstop();rightmotorstop();}
void back()
{leftmotorback();rightmotorback();}
void left1()
{leftmotorback();rightmotorforward();}
void right1()
{leftmotorforward();rightmotorback();}
movestate.h
#ifndef __MOVESTATE_H__
#define __MOVESTATE_H__
void go(void);
void left();
void right();
void stop();
void back();
void leftback();
void rightback();
#endif
lanya.c
#include <REGX52.H>
/**
* @brief ´®¿Ú³õʼ»¯£¬[email protected]
* @param ÎÞ
* @retval ÎÞ
*/
void UART_Init()
{
SCON = 0x50; //·½Ê½1 8λÊý¾Ý ½ÓÊÕµØÖ·Õç±ð½ûÖ¹ ½ÓÊÕʹÄÜ 0101 0000
PCON = 0x00; //²¨ÌØÂʲ»Ôö±¶
TMOD = 0x20; //T1,8λ×Ô¶¯ÖØ×°
TH1 = 0xfd; //²¨ÌØÂÊ9600
TL1 = 0xfd;
EA = 1;
ES = 1;
TR1 = 1;
}
/**
* @brief ´®¿Ú·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý
* @param Byte Òª·¢Ë͵ÄÒ»¸ö×Ö½ÚÊý¾Ý
* @retval ÎÞ
*/
void UART_SendByte(unsigned char Byte)
{
SBUF=Byte;
while(TI==0);
TI=0;
}
///unsigned int recive=0;
//void Uart_Routine(void) interrupt 4
//{
//
// if(RI==1)
// {
// RI=0;
// recive= SBUF; //½ÓÊÕÊý¾Ý
//
// }
//}
lanya.h
#ifndef __LANYA_H__
#define __LANYA_H__
void UART_Init();
void UART_SendByte(unsigned char Byte);
#endif
标签:PWML,__,循迹,51,void,蓝牙,&&,include,PWMR
From: https://blog.csdn.net/2402_84189794/article/details/140417615