[技术问答] 关于n76e003单片机串口接收,接收字符串会在发完所有字符后...

[复制链接]
1354|11
 楼主| 新塘初级用户 发表于 2020-7-24 09:20 | 显示全部楼层 |阅读模式
本帖最后由 新塘初级用户 于 2020-7-24 09:36 编辑
  1. #include "n76e003.h"
  2. #include "system.h"
  3. #include "tm1650.h"
  4. #include "stdio.h"
  5. #include "BC26.h"
  6. #include "string.h"

  7. #define   CHARTAST     1               
  8. #define   NUMTAST       2               
  9. #define   MENUTAST     3               

  10. #define BUFLEN 32              //字符串长度


  11. typedef struct _UART_BUF
  12. {
  13.     char buf [BUFLEN+1];               
  14.     unsigned int index ;
  15. }UART_BUF;


  16. bit HaveTast=0;                             
  17. unsigned char Uart0Cmd;           
  18. int SET_BIT4=0X10;
  19. int CLR_BIT4=0XEF;
  20. UART_BUF buf_uart2;      //给字符串数组分配空间



  21. void Uart0_Init(void);
  22. void Uart0_Process(void);
  23. void Uart0_SendChar(unsigned char Udat);
  24. void Uart0_SendString(unsigned char *PBuf);

  25.    void Clear_Buffer(void)          //数组清空
  26. {
  27.    
  28.     Delay_ms(30);
  29.     buf_uart2.index=0;
  30.     memset(buf_uart2.buf,1,BUFLEN);
  31. }


  32. void nbiot_receive_process_event(unsigned char ch )          //将字符依次存入数组
  33. {
  34.      if(buf_uart2.index >= BUFLEN)
  35.     {
  36.         buf_uart2.index = 0 ;
  37.     }
  38.       else{
  39.                                 buf_uart2.buf[buf_uart2.index] = ch;
  40.               buf_uart2.index++;
  41.          }
  42. }

  43. void USART2_IRQHandler()   interrupt 4  
  44. {
  45.         char temp;
  46. temp=SBUF;
  47. nbiot_receive_process_event(temp); 将SUBF依次存入数组
  48.         RI=0;        //串口中断标志位  置0

  49. }

  50. void main(void)
  51. {
  52.         System_Init();                  
  53.         TM1650_Init();                  
  54.         ToDisplay__();                       
  55.          Uart0_Init();                             
  56.   MODIFY_HIRC_166();   //调整波特率
  57.          while(1)
  58.         {


  59.         Uart0_SendString(buf_uart2.buf); //打印字符串内容
  60.                 Delay_ms(1000);
  61.         
  62.                
  63.                
  64.                
  65. }
  66. }

  67. void Uart0_Init(void)
  68. {
  69.          SCON=0x50;                                                      //模式一,定时器,八位自动重装                  
  70.         ClrBits(TMOD,T1_GATE|T1_CT|T1_M0);
  71.         SetBits(TMOD,T1_M1);      
  72.         SetBits(PCON,SMOD);                                    
  73.         SetBits(CKCON,T1M);                                       
  74.         ClrBits(T3CON,BRCK);                                
  75.         TH1=256 - (1000000/115200+1);        //波特率115200  16.6MHz        
  76.         TR1=1;                                                                                 
  77.         ES=1;                                                                                          
  78.         EA=1;                                                                                                                                                                                                                              
  79.         P0_Quasi_Mode(PIN_6|PIN_7);        
  80. }

  81. void Uart0_SendChar(unsigned char Udat) //串口发送单个字符
  82. {
  83.         SBUF=Udat;               
  84.         while(!TI);               
  85.   TI=0;                     
  86. }

  87. void Uart0_SendString(unsigned char *PBuf)   //串口发送字符串
  88. {
  89.   while(*PBuf!='\0')            
  90.   {
  91.           Uart0_SendChar(*PBuf);
  92.           PBuf++;                                                
  93.   }
  94. }

  95. void MODIFY_HIRC_166(void)  //调整时钟
  96. {
  97. unsigned char hircmap0,hircmap1;
  98. unsigned int trimvalue16bit;
  99. if ((PCON&SET_BIT4)==SET_BIT4)
  100. {
  101. hircmap0 = RCTRIM0;
  102. hircmap1 = RCTRIM1;
  103. trimvalue16bit = ((hircmap0<<1)+(hircmap1&0x01));
  104. trimvalue16bit = trimvalue16bit - 15;
  105. hircmap1 = trimvalue16bit&0x01;
  106. hircmap0 = trimvalue16bit>>1;
  107. TA=0XAA;
  108. TA=0X55;
  109. RCTRIM0 = hircmap0;
  110. TA=0XAA;
  111. TA=0X55;
  112. RCTRIM1 = hircmap1;
  113. PCON &= CLR_BIT4;
  114. }
  115. }
  116. [img][/img]

7_串口0接收发送字符串测试实验定时器1.zip

95.89 KB, 下载次数: 14

程序源码

评论

process和isr这两个函数没有调用,不用管它  发表于 2020-7-24 09:23
不知为何,复制过来的代码注释是乱码,不好意思  发表于 2020-7-24 09:22
643757107 发表于 2020-7-24 09:30 | 显示全部楼层
没看明白是什么问题,
643757107 发表于 2020-7-24 09:31 | 显示全部楼层
楼主是要问的啥,没懂。
 楼主| 新塘初级用户 发表于 2020-7-24 09:41 | 显示全部楼层
643757107 发表于 2020-7-24 09:30
没看明白是什么问题,

意思就是,我用单片机接收从PC串口下发的字符串,收到这个字符串,把他存到数组里面打印出来,不知为何会在发回我接收到的字符串的所有字符后,会一直打印这个字符串的最后一个字符,比如我发给单片机GJKV,他回GJKVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
 楼主| 新塘初级用户 发表于 2020-7-24 09:47 | 显示全部楼层
323725f1a3dad01a25.png

评论

发送给单片机GJKV,他回GJKVVVVVVVV,按理说我打印的是个数组,他会一直回最后一个字母,应该是不知道什么时候又跳进中断,但是没有数据给他,SBUF就一直存着最后一个字符,但我不知道是哪个环节出了问题  发表于 2020-7-24 09:51
 楼主| 新塘初级用户 发表于 2020-7-24 10:27 | 显示全部楼层
我整出来了,原来是没有清除TI
598330983 发表于 2020-7-24 17:56 | 显示全部楼层
新塘初级用户 发表于 2020-7-24 10:27
我整出来了,原来是没有清除TI

喔,就是没清除中断,一直在重复。。。。。
598330983 发表于 2020-7-24 18:00 | 显示全部楼层
多谢楼主分享经验。
643757107 发表于 2020-7-24 21:29 | 显示全部楼层
这个经验宝贵。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

36

帖子

0

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