|
求教:我用串口助手发送数据给单片机,但再让单片机把接收到的数据发送给串口助手,但却没有结果,请问是怎么回事呢?
请问这是出了什么问题呢?
以下为源程序:
- #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();//串口发送
- int flag2=0;//接收标志位
- void delay(uint z)//延时函数1ms
- {
- uint x,y;
- for(x=z;x>0;x--);
- for(y=110;y>0;y--);
- }
- void init() //系统初始化
- {
- 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() //串口发送函数
- {
- SBUF=table[0];//发送table[0],串口助手应显示
- while(TI==0);
- {
- } //数据发送结束时TI自动置1
- TI=0;//清除数据传送标志
- delay(500);
- }
- void UART_1interrupt() interrupt 4 //串口1接收函数
- {
- 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个为一组,发送完一组数据
- {
- flag2=1;//接收完一组数据置1
- num=0;
- table[0]=rec[4];//rec[4]给table[0]
- }
- RI=0;
- }
- }
- }
- }
- }
复制代码 |
|