/*
蓝牙小车程序
T0产生PWM控制舵机,T1用于串口通信
舵机330hz
*/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit PWM=P1^0; //舵机控制端口
sbit in1=P2^0; //电机控制端口1
sbit in2=P2^1; //电机控制端口2
sbit in3=P2^2; //电机控制端口3
sbit in4=P2^3; //电机控制端口4
uint time=0,jd;
uchar a;
void InitUART() //串口初始化
{
TMOD = 0x21;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFD; //T1 波特率9600
TL1 = 0xFD;
TR1 = 1;
TH0 = 0xFF;//T0 0.01ms
TL0 = 0xF6;
TR0 = 1;
ES = 1;
PS = 1;
ET0 = 1;
EA = 1;
}
void main ()
{
InitUART();
while (1);
}
void Time0_Int() interrupt 1 //中断程序 1
{
TR0=0;
TH0=0xFF;
TL0=0xF6;
TR0=1;
time++;
if(time>300)
time=0; //303个周期 330hz
if (time<=jd)
PWM=1;
else
PWM=0;
}
void UARTInterrupt () interrupt 4 //中断程序 4
{
if(RI==1)
{
RI = 0;
a = SBUF;
if (a == '0') //前进
{
in1=1;
in2=0;
in3=1;
in4=0;
}
if (a == '1') //后退
{
in1=0;
in2=1;
in3=0;
in4=1;
}
if (a == '2') //停止
{
in1=1;
in2=1;
in3=1;
in4=1;
}
if(a == '3') //左转
{
jd=22;
}
if(a == '4') //右转
{
jd=34;
}
if(a == '5') //回中
{
jd=28;
}
}
}
|