蓝牙控制小车(含舵机)程序,实际操作时手机发送字符无效,求大神看看程序有问题吗?
时间:10-02
整理:3721RD
点击:
#include <STC12C5A60S2.H>
sbit PWM1 = P2^0;//PWM?
sbit PWM2 = P2^1;
sbit motor_control_1 = P2^7;//?
sbit motor_control_2 = P2^6;//
sbit motor_control_3 = P2^5;
sbit motor_control_4 = P2^4;//
unsigned int PWMCnt1 = 0;
unsigned int cntPWM1 = 60;
unsigned int PWMCnt2 = 0;
unsigned int cntPWM2 = 60;
unsigned char bluetoothdata;
void initial_myself();
void initial_interrupt();
void usart_service(void);
void delay_long(unsigned int time);
void go_forward(void);//
void stop();
void turn_right();
void turn_left();
void back();
void turn_right1();
void turn_left1();
#define uint unsigned int
#define uchar unsigned char
void DelayMs(uchar ms);
void init_PWM();
void PWM0_set(uchar empty);
void DelayMs(uchar ms) //?,?,?
{
uint i;
while(ms--)
{
for(i = 0; i < 850; i++);
}
}
void init_PWM()
{
CCON=0X00;
CH=0;
CL=0;
CMOD=0X02; //0X00:?/12 ?,0X02:?/2,0x08:?
PWM0_set(0Xec);//?,0XC0:?25%,0X80:?50%,0X40:?75% //
PCA_PWM0=0x00; //?0
CCAPM0=0X42; //0X42:8?PWM P1.3?,?;0X53:8?PWM?,?;0X63:?;0X73:?
CR=1; //?
}
void PWM0_set(uchar empty) //?
{
CCAP0L=empty;
CCAP0H=empty;
}
void main()
{ init_PWM();
initial_myself();
delay_long(100);
initial_interrupt();
stop();
while(1)
{
usart_service();
}
}
void usart_service()
{
switch(bluetoothdata)
{ case 'f':go_forward();
delay_long(100);
SBUF = 'f';//?
bluetoothdata = 'a'; break;
case 's':stop();
delay_long(100);
SBUF = 's';
bluetoothdata = 'a'; break;
case 'r':turn_right();
delay_long(100);
SBUF = 'r';
bluetoothdata = 'a'; break;
case 'l':turn_left();
delay_long(100);
SBUF = 'l';
bluetoothdata = 'a'; break;
case 'x':turn_right1();
delay_long(100);
SBUF = 'r';
bluetoothdata = 'a'; break;
case 'y':turn_left1();
delay_long(100);
SBUF = 'l';
bluetoothdata = 'a'; break;
case 'b':back();
delay_long(100);
SBUF = 'b';
bluetoothdata = 'a'; break;
case '1':cntPWM1 = 60;
cntPWM2 = 60;
delay_long(100);
SBUF = '1';
bluetoothdata = 'a'; break;
case '2':cntPWM1 = 100;
cntPWM2 = 100;
delay_long(100);
SBUF = '2';
bluetoothdata = 'a'; break;
case '3':cntPWM1 = 140;
cntPWM2 = 140;
delay_long(100);
SBUF = '3';
bluetoothdata = 'a'; break;
}
}
void usart_receive(void) interrupt 4 //?
{
if(RI == 1)//?
{
RI = 0;//?
bluetoothdata = SBUF;//?
}
if(TI == 1)//?
{
TI = 0;//?
}
}
void T0_time() interrupt 1 //?1?
{
PWMCnt1++;
PWMCnt2++;
if(PWMCnt1 >= 200)
{
PWMCnt1 = 1;
}
if(PWMCnt1 <= cntPWM1) //230
{
PWM1 = 1;
}
else
{
PWM1 = 0;
}
if(PWMCnt2 >= 200)
{
PWMCnt2 = 1;
}
if(PWMCnt1 <= cntPWM2) //230
{
PWM2 = 1;
}
else
{
PWM2 = 0;
}
TH0 = (65536 - 50) / 256;
TL0 = (65536 - 50) % 256;
}
void initial_myself()
{
TMOD = 0x01;//
TH0 = (65536 - 50) / 256;//?50?
TL0 = (65536 - 50) % 256;
SCON = 0x50;
TMOD = 0x21;
TH1 = TL1 = 0xfd;//?9600
IP = 0x10;//?
}
void initial_interrupt()
{
EA = 1;// ?
ES = 1;// ?
ET0 = 1;//?0?
TR0 = 1;//?0
TR1 = 1;// ?1
}
void delay_long(unsigned int time)
{
unsigned int i;
unsigned int j;
for(i = 0; i< time;i++)
{ for(j = 0; j < 500; j++); }
}
void go_forward()
{
PWM0_set(0xec);
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
}
void stop()
{ PWM0_set(0Xec);
motor_control_1 = 0;
motor_control_2 = 0;
motor_control_3 = 0;
motor_control_4 = 0;
}
void turn_left()
{
PWM0_set(0Xe7);
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
}
void turn_right()
{
PWM0_set(0Xf0);
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
}
void back()
{ PWM0_set(0xec);
motor_control_1 = 1;
motor_control_2 = 0;
motor_control_3 = 1;
motor_control_4 = 0;
}
void turn_left1()
{
PWM0_set(0Xe7);
motor_control_1 = 1;
motor_control_2 = 0;
motor_control_3 = 1;
motor_control_4 = 0;
}
void turn_right1()
{
PWM0_set(0Xf0);
motor_control_1 = 1;
motor_control_2 = 0;
motor_control_3 = 1;
motor_control_4 = 0;
}
sbit PWM1 = P2^0;//PWM?
sbit PWM2 = P2^1;
sbit motor_control_1 = P2^7;//?
sbit motor_control_2 = P2^6;//
sbit motor_control_3 = P2^5;
sbit motor_control_4 = P2^4;//
unsigned int PWMCnt1 = 0;
unsigned int cntPWM1 = 60;
unsigned int PWMCnt2 = 0;
unsigned int cntPWM2 = 60;
unsigned char bluetoothdata;
void initial_myself();
void initial_interrupt();
void usart_service(void);
void delay_long(unsigned int time);
void go_forward(void);//
void stop();
void turn_right();
void turn_left();
void back();
void turn_right1();
void turn_left1();
#define uint unsigned int
#define uchar unsigned char
void DelayMs(uchar ms);
void init_PWM();
void PWM0_set(uchar empty);
void DelayMs(uchar ms) //?,?,?
{
uint i;
while(ms--)
{
for(i = 0; i < 850; i++);
}
}
void init_PWM()
{
CCON=0X00;
CH=0;
CL=0;
CMOD=0X02; //0X00:?/12 ?,0X02:?/2,0x08:?
PWM0_set(0Xec);//?,0XC0:?25%,0X80:?50%,0X40:?75% //
PCA_PWM0=0x00; //?0
CCAPM0=0X42; //0X42:8?PWM P1.3?,?;0X53:8?PWM?,?;0X63:?;0X73:?
CR=1; //?
}
void PWM0_set(uchar empty) //?
{
CCAP0L=empty;
CCAP0H=empty;
}
void main()
{ init_PWM();
initial_myself();
delay_long(100);
initial_interrupt();
stop();
while(1)
{
usart_service();
}
}
void usart_service()
{
switch(bluetoothdata)
{ case 'f':go_forward();
delay_long(100);
SBUF = 'f';//?
bluetoothdata = 'a'; break;
case 's':stop();
delay_long(100);
SBUF = 's';
bluetoothdata = 'a'; break;
case 'r':turn_right();
delay_long(100);
SBUF = 'r';
bluetoothdata = 'a'; break;
case 'l':turn_left();
delay_long(100);
SBUF = 'l';
bluetoothdata = 'a'; break;
case 'x':turn_right1();
delay_long(100);
SBUF = 'r';
bluetoothdata = 'a'; break;
case 'y':turn_left1();
delay_long(100);
SBUF = 'l';
bluetoothdata = 'a'; break;
case 'b':back();
delay_long(100);
SBUF = 'b';
bluetoothdata = 'a'; break;
case '1':cntPWM1 = 60;
cntPWM2 = 60;
delay_long(100);
SBUF = '1';
bluetoothdata = 'a'; break;
case '2':cntPWM1 = 100;
cntPWM2 = 100;
delay_long(100);
SBUF = '2';
bluetoothdata = 'a'; break;
case '3':cntPWM1 = 140;
cntPWM2 = 140;
delay_long(100);
SBUF = '3';
bluetoothdata = 'a'; break;
}
}
void usart_receive(void) interrupt 4 //?
{
if(RI == 1)//?
{
RI = 0;//?
bluetoothdata = SBUF;//?
}
if(TI == 1)//?
{
TI = 0;//?
}
}
void T0_time() interrupt 1 //?1?
{
PWMCnt1++;
PWMCnt2++;
if(PWMCnt1 >= 200)
{
PWMCnt1 = 1;
}
if(PWMCnt1 <= cntPWM1) //230
{
PWM1 = 1;
}
else
{
PWM1 = 0;
}
if(PWMCnt2 >= 200)
{
PWMCnt2 = 1;
}
if(PWMCnt1 <= cntPWM2) //230
{
PWM2 = 1;
}
else
{
PWM2 = 0;
}
TH0 = (65536 - 50) / 256;
TL0 = (65536 - 50) % 256;
}
void initial_myself()
{
TMOD = 0x01;//
TH0 = (65536 - 50) / 256;//?50?
TL0 = (65536 - 50) % 256;
SCON = 0x50;
TMOD = 0x21;
TH1 = TL1 = 0xfd;//?9600
IP = 0x10;//?
}
void initial_interrupt()
{
EA = 1;// ?
ES = 1;// ?
ET0 = 1;//?0?
TR0 = 1;//?0
TR1 = 1;// ?1
}
void delay_long(unsigned int time)
{
unsigned int i;
unsigned int j;
for(i = 0; i< time;i++)
{ for(j = 0; j < 500; j++); }
}
void go_forward()
{
PWM0_set(0xec);
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
}
void stop()
{ PWM0_set(0Xec);
motor_control_1 = 0;
motor_control_2 = 0;
motor_control_3 = 0;
motor_control_4 = 0;
}
void turn_left()
{
PWM0_set(0Xe7);
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
}
void turn_right()
{
PWM0_set(0Xf0);
motor_control_1 = 0;
motor_control_2 = 1;
motor_control_3 = 0;
motor_control_4 = 1;
}
void back()
{ PWM0_set(0xec);
motor_control_1 = 1;
motor_control_2 = 0;
motor_control_3 = 1;
motor_control_4 = 0;
}
void turn_left1()
{
PWM0_set(0Xe7);
motor_control_1 = 1;
motor_control_2 = 0;
motor_control_3 = 1;
motor_control_4 = 0;
}
void turn_right1()
{
PWM0_set(0Xf0);
motor_control_1 = 1;
motor_control_2 = 0;
motor_control_3 = 1;
motor_control_4 = 0;
}
首先确定发送的消息确实收到了,其次是你的消息是否被正确处理
你好,我前期的程序可以用手机控制前后和档位,说明蓝牙通信成功, 后来加入了这块芯片的内部产生的pwm波,去控制舵机, 然后再试验就错了,输入字符就没反应了。(程序 0 error)
那就检查检查PWM输出是否正常。
你先别用蓝牙去控制舵机,先事先实现你想要的功能,看能不能实现,然后再和一起
谢谢谢谢,我试验试验