|
用串口發(fā)送數(shù)據(jù),然后用串口助手接收數(shù)據(jù),但接收到的數(shù)據(jù)和程序里寫的數(shù)據(jù)不一樣,請(qǐng)問這是怎么回事?串口發(fā)送的數(shù)據(jù):uchar send[8]={0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B}
這是串口助手接收到的數(shù)據(jù):跟程序里的數(shù)據(jù)不一樣,請(qǐng)問大家這是哪里錯(cuò)了?
捕獲1.PNG (11.8 KB, 下載次數(shù): 57)
下載附件
2020-12-28 14:07 上傳
后來我又把發(fā)送的數(shù)據(jù)全部改成了0X00,結(jié)果如下
捕獲2.PNG (7.06 KB, 下載次數(shù): 61)
下載附件
2020-12-28 14:48 上傳
結(jié)果接收到的是這個(gè)
捕獲3.PNG (4.67 KB, 下載次數(shù): 63)
下載附件
2020-12-28 14:48 上傳
這是完整程序:
- #include <stc12c5a.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar rec[9];//接收
- uchar send[8]={0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B};//發(fā)送數(shù)據(jù)
- uchar num=0;
- int i;
- void delay(uint z)//延時(shí)函數(shù)
- {
- uint x,y;
- for(x=z;x>0;x--);
- for(y=110;y>0;y--);
- }
- void init() //系統(tǒng)初始化
- {
- TMOD |=0X20;//定時(shí)器T1,方式2,波特率由PCON寄存器的SMOD決定
- SCON=0x50; //REN RI TI,RI為0,TI為0
- //串行口1方式1 SCON是串行口1的串行控制寄存器,REN為1,允許接收
- PCON=0x00;//各工作方式波特率加倍
- TH1=0xfD;//9600bps@11.0592
- TL1=0xfD;
- TR1=1; //定時(shí)器1中斷打開
- EA=1;//cpu總中斷允許位,1為開放中斷
- ES=1;// 1允許串行口中斷
- }
- void main()
- {
- uchar m=0;
- init();
- while(1)
- {
- EA=0;//關(guān)中斷
-
- for(i=0;i<8;i++)
- {
- SBUF=send[i];//發(fā)送數(shù)據(jù)
- while(TI==0);
- {
- } //數(shù)據(jù)發(fā)送結(jié)束時(shí)TI自動(dòng)置1
- TI=0;
- }
- EA=1;//開中斷
- delay(1000);
- }
- }
- void UART_1() interrupt 4
- {
- RI=0; //RI置0
- rec[num]=SBUF;
- if(rec[0]==0x01)
- {
- num++;
- if(num>=9)
- {
- num=0;
- }
- }
- }
復(fù)制代碼 |
|