以下是我超聲波無線傳輸測距從機部分,現(xiàn)在我加了一些串口通訊功能將距離數(shù)據(jù),可是電腦串口收到結果卻是亂碼?
請大家?guī)兔聪麓诓糠钟袩o問題
單片機源程序如下:
- #include <reg52.h>
- #include "eepom52.h"
- #define uchar unsigned char
- #define uint unsigned int
- #include "nrf24l01.H"
- #include "lcd1602.h"
- #include<stdio.h>
- sbit K3 = P3^0; //按鍵定義
- sbit K2 = P3^1;
- sbit K1 = P3^2;
- sbit beep = P1^6; //蜂鳴器IO口定義
- sbit led = P1^7; //無線模塊型號指示燈
- uchar a_a =0;
- unsigned long S=0; //距離
- unsigned char code mcustudio[] ={" CeJu-System "};
- unsigned char code email[] = {" ^_^ "};
- unsigned char code Cls[] = {" "};
- unsigned char code ASCII[15] = {'0','1','2','3','4','5','6','7','8','9','.','-','M'};
- unsigned int Set_d = 144;
- unsigned char disbuff[4] ={ 0,0,0,0}; //顯示數(shù)據(jù)數(shù)組
- bit bSetFlag = 0,bTime_600ms,flag_300ms,bClrFlag;
- bit flag =0;
- /******************把數(shù)據(jù)保存到單片機內(nèi)部eepom中******************/
- void write_eepom()
- {
- SectorErase(0x2000);
- byte_write(0x2000, Set_d % 256);
- byte_write(0x2001, Set_d / 256);
- byte_write(0x2058, a_a);
- }
- /******************把數(shù)據(jù)從單片機內(nèi)部eepom中讀出來*****************/
- void read_eepom()
- {
- Set_d = byte_read(0x2001);
- Set_d <<= 8;
- Set_d |= byte_read(0x2000);
- a_a = byte_read(0x2058);
- }
- /**************開機自檢eepom初始化*****************/
- void init_eepom()
- {
- read_eepom(); //先讀
- if(a_a != 1) //新的單片機初始單片機內(nèi)問EEPOM
- {
- Set_d = 100;
- a_a = 1;
- write_eepom(); //寫
- }
- }
- /*************定時器0初始化程序***************/
- void time_init()
- {
- EA = 1; //開總中斷
- TMOD = 0X01; //定時器0、定時器1工作方式1
- ET0 = 1; //開定時器0中斷
- TR0 = 1; //允許定時器0定時
- }
- void KeyScanf(void) //按鍵處理函數(shù)
- {
- static bit K1_on_off;
- static char add = 1; ///按鍵一次增加的變量
- if(K1 == 0) //設置距離按鍵按下
- {
- if(K1_on_off)
- {
- K1_on_off = 0;
- bSetFlag = ~bSetFlag; //按下標置位取反
- bClrFlag = 1;
- }
- }
- else {K1_on_off = 1;}
- if(bSetFlag) //設置鍵按下
- {
- if(K2 == 0 && bTime_600ms) //設置鍵加按下
- {
- bTime_600ms = 0;
- Set_d += add; //設置值加
- if(Set_d >= 350)Set_d = 350; //設置值最大為3.5m
- write_eepom(); //寫保存
- }
- if(K3 == 0 && bTime_600ms) //設置鍵減鍵按下
- {
- bTime_600ms = 0;
- Set_d -= add; //設置值減
- if(Set_d <= 10)Set_d = 10; //設置值最大為0.1m
- write_eepom(); //寫保存
- }
-
- if ((K3 == 0 || K2 == 0) && bTime_600ms)//有設置加減鍵按下
- {
- bTime_600ms = 0;
- if(++add >= 10)add = 10;
- }
- else if(K3 != 0 && K2 != 0)add = 1; //沒有設置加減鍵按下
- }
- }
- /****************報警函數(shù)***************/
- void clock_h_l()
- {
- static unsigned char value;
- if(S <= Set_d)
- {
- value ++; //消除實際距離在設定距離左右變化時的干擾
- if(value >= 2)
- {
- beep = ~beep; //蜂鳴器報警
- }
- }
- else
- {
- value = 0;
- beep = 1; //取消報警
- }
- }
- void SetDisplay(void)
- {
- DisplayListChar(0,0," Set_DIST: ");
- disbuff[0]=Set_d%1000/100;
- disbuff[1]=Set_d%1000%100/10;
- disbuff[2]=Set_d%1000%10 %10;
- DisplayOneChar(5, 1, ASCII[disbuff[0]]);
- DisplayOneChar(6, 1, ASCII[10]); //顯示點
- DisplayOneChar(7, 1, ASCII[disbuff[1]]);
- DisplayOneChar(8, 1, ASCII[disbuff[2]]);
- DisplayOneChar(9, 1, ASCII[12]); //顯示M
- }
- void DISPLAY(void)
- {
- if(bSetFlag == 0) //按鍵設置標志位
- {
- disbuff[0]=Set_d%1000/100;
- disbuff[1]=Set_d%1000%100/10;
- disbuff[2]=Set_d%1000%10 %10;
- DisplayOneChar(0, 1, 'A');
- DisplayOneChar(1, 1, ':');
- DisplayOneChar(2, 1, ASCII[disbuff[0]]);
- DisplayOneChar(3, 1, ASCII[10]); //顯示點
- DisplayOneChar(4, 1, ASCII[disbuff[1]]);
- DisplayOneChar(5, 1, ASCII[disbuff[2]]);
- DisplayOneChar(6, 1, ASCII[12]); //顯示M
-
- if((S>=700)||flag==1) //超出測量范圍顯示“-”
- {
- flag=0;
- //dd(" A3.45m 2.15m ")
- DisplayOneChar(10, 1, ASCII[11]);
- DisplayOneChar(11, 1, ASCII[10]); //顯示點
- DisplayOneChar(12, 1, ASCII[11]);
- DisplayOneChar(13, 1, ASCII[11]);
- DisplayOneChar(14, 1, ASCII[12]); //顯示M
- }
- else
- {
- disbuff[0]=S%1000/100;
- disbuff[1]=S%1000%100/10;
- disbuff[2]=S%1000%10 %10;
- DisplayOneChar(10, 1, ASCII[disbuff[0]]);
- DisplayOneChar(11, 1, ASCII[10]); //顯示點
- DisplayOneChar(12, 1, ASCII[disbuff[1]]);
- DisplayOneChar(13, 1, ASCII[disbuff[2]]);
- DisplayOneChar(14, 1, ASCII[12]); //顯示M
- }
- }
- }
- /*******************************************************************************
- * 函數(shù)名 :UsartInit()
- * 函數(shù)功能 :設置串口
- * 輸入 : 無
- * 輸出 : 無
- *******************************************************************************/
- void UsartInit()
- {
- SCON=0X50; //設置為工作方式1
- TMOD=0X20; //設置定時器器工作方式2
- PCON=0X80; //波特率加倍
- TH1=0XFD; //定時器初始值設置,波特率是9600的
- TL1=0XFD;
- TR1=1; //打開定時器
- TI=1;
- }
- /********主函數(shù)**********/
- void main()
- {
- LCMInit(); //LCM初始化
- time_init(); //初始化定時器
- UsartInit(); //串口初始化
- DisplayListChar(0, 0, mcustudio);
- DisplayListChar(0, 1, email); //顯示^_^
-
- CE=0;
- SCK=0; //無線模塊初始化
- CSN=1;
- RX_Mode();
- delay_ms(2000);
- DisplayListChar(0, 1, Cls); //清屏
- init_eepom(); //開機自檢eepom
-
- while(1)
- {
- if(flag_300ms == 1) //300ms 處理一次測量程序
- {
- flag_300ms = 0;
- led = 1; //無線模塊的信號指示燈
- if(nRF24L01_RxPacket(Rx_Buf)) //無線模塊的數(shù)據(jù)接收
- {
- led =0 ;
- if(Rx_Buf[0] == 0xa1)
- S = 256 * Rx_Buf[2] + Rx_Buf[1]; //取出距離值
- clock_h_l(); //報警函數(shù)
- }
- DISPLAY(); //顯示距離
- printf("%d\n",S); //串口顯示距離
- if(beep == 1)DisplayListChar(0, 0, mcustudio);
- else DisplayListChar(0,0," Alarm ");
- }
- else
- if(bSetFlag) //按鍵設置標志位
- {
- SetDisplay(); //按鍵設置顯示函數(shù)
- }
-
- if(bClrFlag) //清除第二行顯示標志位
- {
- bClrFlag = 0;
- DisplayListChar(0, 1, Cls); //對第一行清屏
- }
- }
- }
- /*************定時器0中斷服務程序***************/
- void time0_int() interrupt 1
- {
- static uchar value;
- TH0 = 0x3c;
- TL0 = 0xb0; // 50ms
- value ++;
- KeyScanf(); //按鍵處理函數(shù)
- if(value % 6 == 0)
- {
- flag_300ms = 1; //300ms
- }
- if(value == 12)
- {
- value = 0;
- bTime_600ms = 1; //600ms
- }
- }
復制代碼
|