|
求教:我用串口助手發(fā)送數(shù)據(jù)給單片機(jī),但再讓單片機(jī)把接收到的數(shù)據(jù)發(fā)送給串口助手,但卻沒有結(jié)果,請問是怎么回事呢?
捕獲1.PNG (13.82 KB, 下載次數(shù): 25)
下載附件
2021-1-4 15:11 上傳
請問這是出了什么問題呢?
以下為源程序:
- #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[7]={0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- uchar table[2]={0x00,0x00};//接收
- uchar num=0;
- void UART_1Interrupt();//串口1接收字符串
- void UART_1send();//串口發(fā)送
- int flag2=0;//接收標(biāo)志位
- void delay(uint z)//延時函數(shù)1ms
- {
- uint x,y;
- for(x=z;x>0;x--);
- for(y=110;y>0;y--);
- }
- void init() //系統(tǒng)初始化
- {
- TMOD |=0X20;//定時器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; //定時器1中斷打開
- EA=1;//cpu總中斷允許位,1為開放中斷
- ES=1;// 1允許串行口中斷
- }
- void main()
- {
- init();
- while(1)
- {
- if(flag2==1)
- {
- UART_1send();
- }
- }
- }
- void UART_1send() //串口發(fā)送函數(shù)
- {
- SBUF=table[0];//發(fā)送table[0],串口助手應(yīng)顯示
- while(TI==0);
- {
- } //數(shù)據(jù)發(fā)送結(jié)束時TI自動置1
- TI=0;//清除數(shù)據(jù)傳送標(biāo)志
- delay(500);
- }
- void UART_1interrupt() interrupt 4 //串口1接收函數(shù)
- {
- if(RI)
- {
- if(flag2==0)//如果flag2=0,
- {
- if(num==0)
- {
- if(SBUF==0x01)//如果SUBF=0x01
- {
- rec[num++]=SBUF;
- RI=0;
- }
- else
- {
- rec[num++]=SBUF;
- if(num>=6)7個為一組,發(fā)送完一組數(shù)據(jù)
- {
- flag2=1;//接收完一組數(shù)據(jù)置1
- num=0;
- table[0]=rec[4];//rec[4]給table[0]
- }
- RI=0;
- }
- }
- }
- }
- }
復(fù)制代碼 |
-
捕獲1.PNG
(13.67 KB, 下載次數(shù): 70)
下載附件
2021-1-4 14:57 上傳
|