匠人求助 串口多字节发送 需要复位才显示?

[复制链接]
2034|6
 楼主| 16777216 发表于 2013-6-11 20:30 | 显示全部楼层 |阅读模式
本帖最后由 16777216 于 2013-6-11 20:46 编辑

我在做串口多字节发送的时候,pc给单片机发送好后,单片机不显示,复位之后才显示,一般这是哪里出问题了

问题更新 现在使用匠人的串口猎人的高级发码ok 但是用普通方式 或者是别的软件串口助手就需要重启单片机才能正确显示上次发送的数据?

这是源代码
  1. main.c

  2. #include "uart.h"
  3. #include "lcd1602.h"
  4. #include "stc.h"
  5. //extern unsigned char  receive;
  6. unsigned char hello[]={"ello,boy\n"};
  7. void main()
  8. {
  9.         Lcdstart();
  10.         UartInit();
  11.         Uart_send_char('h');
  12.         Uart_send_str(hello);
  13.         while(1)
  14.         {
  15.                 if(uartflag)
  16.                 {        ES=0;
  17.                         LCDclear();
  18.                         LCD_write_num(2,2,receive);
  19.                         ES=1;
  20.                 }

  21.                 uartflag=0;        
  22.         }        
  23. }


  24. uart.c

  25. #include "uart.h"
  26. #include "stc.h"
  27. //#include "lcd1602.h"
  28. unsigned long int  receive;
  29. bit uartflag=0;

  30. unsigned char recdata[6];
  31. unsigned char cnt=0;

  32. void Uartnit(void);
  33. void Uart_send_char(unsigned char ch);
  34. void Uart_send_str(unsigned char  *str);




  35. void interrupt_uart() interrupt 4                        //中断接收 receive
  36. {
  37.         if(TI)
  38.         {
  39.                 TI = 0;
  40.                 REN = 1;
  41.         }




  42.         if(RI)
  43.         {
  44.                 RI = 0;
  45.                
  46.                 recdata[cnt]=SBUF;
  47.         cnt++;
  48.         if(recdata[0] == 0xaa)//0xaa为起始字节               
  49.         {
  50.                 if(cnt>=6)
  51.                 {
  52.                         //这里处理你的数据,以更新显示。
  53.                                                  receive=recdata[1]+recdata[2]*10+recdata[3]*100+recdata[4]*1000+recdata[5]*10000;
  54.                                                  uartflag=1;
  55.                                                  cnt=0;
  56.                                                       
  57.                 }        
  58.         }else
  59.         {
  60.                 cnt=0;//没有接收到字头,设置下一个可能为字头,
  61.                                 uartflag=0;
  62.         }
  63.                 //receive= SBUF;
  64.                 //uartflag=1;
  65.         }
  66. }












  67. /***********************************************************************/
  68. /***************************通用函数***********************************/
  69. void Uartinit(void)                //9600bps@11.0592MHz
  70. {
  71.         PCON &= 0x7F;                //波特率不倍速
  72.         SCON = 0x50;                //8位数据,可变波特率
  73.         AUXR &= 0xFB;                //独立波特率发生器时钟为Fosc/12,即12T
  74.         BRT = 0xFD;                //设定独立波特率发生器重装值
  75.         AUXR |= 0x01;                //串口1选择独立波特率发生器为波特率发生器
  76.         AUXR |= 0x10;                //启动独立波特率发生器
  77.         EA=1;
  78.         ES=1;
  79. }

  80. void Uart_send_char(unsigned char ch)
  81. {
  82.          ES = 0;
  83.          TI = 0;
  84.          SBUF = ch;
  85.          while(!TI);
  86.          TI = 0;
  87.          ES = 1;
  88. }

  89. void Uart_send_str(unsigned char  *str)
  90. {
  91.         while(*str != 0)
  92.         {
  93.                 Uart_send_char(*str);
  94.                 str++;               
  95.         }
  96. }
































dqyubsh 发表于 2013-6-11 21:44 | 显示全部楼层
直观感觉,首先要把receive=那行从中断里拿出来,移到主程序里处理。或者把这行去掉,看看运行情况。至少不要在中断里做乘法转换。

还有,串行程序写这么简单,很可能会遭遇各种各样的问题,或者说,在工业上很难使用。建议参考MODBUS协议的分帧方法,看一下国外的开源代码。
pangencao 发表于 2013-6-12 14:54 | 显示全部楼层
高级发码与普通方式分别怎么操作的?
 楼主| 16777216 发表于 2013-6-12 16:14 | 显示全部楼层
pangencao 发表于 2013-6-12 14:54
高级发码与普通方式分别怎么操作的?

高级发码就是 第一组 aa  第二组 01 第三组 02 第四组01 第五组02 第六组01  第七组校验和07
普通发码就是一个个发的

估计是软件bug把 我俩单片机双机通信已经ok了
 楼主| 16777216 发表于 2013-6-13 15:33 | 显示全部楼层
dqyubsh 发表于 2013-6-11 21:44
直观感觉,首先要把receive=那行从中断里拿出来,移到主程序里处理。或者把这行去掉,看看运行情况。至少不 ...

能提供下具体的思路么  或者一些伪代码
dqyubsh 发表于 2013-6-13 20:56 | 显示全部楼层
16777216 发表于 2013-6-13 15:33
能提供下具体的思路么  或者一些伪代码

http://www.freemodbus.org/
ningling_21 发表于 2013-6-13 21:30 | 显示全部楼层
dqyubsh 发表于 2013-6-13 20:56
http://www.freemodbus.org/

网页打不开....
您需要登录后才可以回帖 登录 | 注册

本版积分规则

61

主题

399

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部