|
為啥一有while循環(huán) 串口中斷就接不到數(shù)據(jù)了
只要前進(jìn) 就停止不了了 只要一前進(jìn) 任何操作都執(zhí)行不了了
求大神幫助
完整程序代碼:
#include<reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
//小車輪子接線方式:in1=p1.2 in2=p1.3 in3=p1.6 in4=p1.7
sbit wleft0=P1^2;sbit wleft1=P1^3;//左輪
sbit wright0=P1^6;sbit wright1=P1^7;//右輪
sbit d0=P1^4;sbit d1=P1^5;//右輪
bit startBit = 0; //串口接收開始標(biāo)志位
bit newLineReceived = 0; //串口一幀協(xié)議包接收完成
unsigned char inputString[50]; //接收數(shù)據(jù)協(xié)議
uchar f=1;//0為前進(jìn) 1為后退
uchar al=0;
uchar ar=0;
uchar l=10; //左輪速度 0~20內(nèi)調(diào)節(jié),控制輸出電壓,參數(shù)過低輪子不動(dòng),注意調(diào)試。
uchar r=10; //右輪速度 0~20內(nèi)調(diào)節(jié),控制輸出電壓,參數(shù)過低輪子不動(dòng),注意調(diào)試。
/******************************************************************/
/* 串口中斷程序*/
/******************************************************************/
void UART_SER () interrupt 4
{
unsigned char n; //定義臨時(shí)變量
static int num = 0;
if(RI) //判斷是接收中斷產(chǎn)生
{
RI = 0; //標(biāo)志位清零
n = SBUF; //讀入緩沖區(qū)的值
//control=n;
if(n == '$')
{
startBit = 1;
num = 0;
}
if(startBit == 1)
{
inputString[num] = n;
}
if (n == '#')
{
newLineReceived = 1;
startBit = 0;
}
num++;
if(num >= 50)
{
num = 0;
startBit = 0;
newLineReceived = 0;
}
}
}
//WIFI、藍(lán)牙初始化
void WifiInit(void)
{
SCON = 0x50; // SCON: 模式1, 8-bit UART, 使能接收
TMOD |= 0x20;
TH1=0xfd; //波特率9600 初值
TL1=0xfd;
TR1= 1;
EA = 1; //開總中斷
ES= 1; //打開串口中斷
}
void delay_ms(uint k)//延時(shí)函數(shù)
{
uint i,j;
for(i=0;i<k;i++)
{
for(j=0;j<113;j++)
{
;
}
}
}
uint a=0;
uint i=0;
void turn_left(){ //2.5v
while(a<15){
wleft0=1;
delay_ms(10);
wleft0=0;
delay_ms(10);
a++;
}
if(wright1==1){
wleft0=1;
}else{
wleft0=0;
}
a=0;
}
void round_left(){//原地左轉(zhuǎn)
P1=0x77;
}
void round_right(){//原地右轉(zhuǎn)
P1=0xbb;
}
void stop(){
P1 = 0xff; //關(guān)閉所有LED和風(fēng)扇
}
void run(uint r){//前進(jìn)
while(r){
P1=0x7b;
}
}
void retreat(){//后退
P1=0xb7;;
}
main(){
P1 = 0xff; //關(guān)閉所有LED和風(fēng)扇
WifiInit();
//循環(huán)接收藍(lán)牙數(shù)據(jù)
while(1){
if (newLineReceived)
{
switch(inputString[1])
{
case '0': run(0);break;//停止
case '1': run(1);break;//前進(jìn)
case '2': retreat();break;//后退
case '3': round_left();break;//原地左轉(zhuǎn)
case '4': round_right();break;//原地右轉(zhuǎn)
case '5': turn_left();break;
case '6': ;break;//開燈
default:P1 = 0xff;break;
}
newLineReceived = 0;
}
}
}
|
|