用到的MCU是STC15W204S,主要是体积小,蓝牙模块HC–04,然后180度舵机。手机上下载HC蓝牙助手。
#include <REG51.H>
unsigned char receive_data;
unsigned char PWM,compare;
sbit steering=P3^2;
void Uart_Init(void)
{
SCON = 0x50;
AUXR |= 0x01;
AUXR &= 0xFB;
T2L = 0xE8;
T2H = 0xFF;
AUXR |= 0x10;
EA = 1;
ES = 1;
}
void Uart_Routine() interrupt 4
{
if(RI)
{
receive_data=SBUF;
RI=0;
SBUF=receive_data;
}
if(TI)
{
TI=0;
}
}
void Timer0_Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x66;
TH0 = 0xEA;
TF0 = 0;
TR0 = 1;
ET0 = 1;
}
void Timer0_Routine() interrupt 1
{
PWM++;
if(PWM>=40) PWM=0;
if(PWM<compare) steering=1;
else steering=0;
}
void main()
{
Uart_Init();
Timer0_Init();
steering=0;
while(1)
{
if(receive_data=='0') compare=1;
if(receive_data=='1') compare=5;
}
}
|