|
我通過藍(lán)牙串口軟件給單片機(jī)傳輸數(shù)字,返回輸入值卻出現(xiàn)亂碼,這是為什么呢?通訊頻率也沒問題,字符集是utf-8,我是不太清楚51單片機(jī)內(nèi)部數(shù)據(jù)怎么運(yùn)算的,按理說 receiveData=SBUF;
RI=0;
SBUF=receiveData;
這一步寫的也沒錯(cuò),前后腳變量都沒變怎么就不行了呢?
單片機(jī)程序如下
#include <reg52.h>
#define LED P2
typedef unsigned int u16;
typedef unsigned char u8;
u8 receiveData;
void set_put();
void UsartInit() //初始化
{
SCON=0X50;
TMOD=0X20;
PCON=0X80;
TH1=0XFD;
TL1=0XFD;
ES=1;
EA=1;
TR1=1;
}
void main() //主函數(shù)
{
UsartInit();
while(1);
}
void Usart() interrupt 4//串口中斷
{
receiveData=SBUF;
RI=0;
SBUF=receiveData;
while(!TI);
TI=0;
set_put();
}
void set_put() //數(shù)據(jù)處理
{
switch(receiveData)
{
case('A'):LED=0xaa;break;//前進(jìn)
case('B'):LED=0x99;break;//右轉(zhuǎn)
case('C'):LED=0x55;break;//后退
case('D'):LED=0x66;break;//左轉(zhuǎn)
case('E'):LED=0xff;break;//停止
default:break;
}
}
|
|