[技术问答]

关于n76e003单片机串口接收,接收字符串会在发完所有字符后...

[复制链接]
895|11
手机看帖
扫描二维码
随时随地手机跟帖
新塘初级用户|  楼主 | 2020-7-24 09:20 | 显示全部楼层 |阅读模式
本帖最后由 新塘初级用户 于 2020-7-24 09:36 编辑
#include "n76e003.h"
#include "system.h"
#include "tm1650.h"
#include "stdio.h"
#include "BC26.h"
#include "string.h"

#define   CHARTAST     1               
#define   NUMTAST       2               
#define   MENUTAST     3               

#define BUFLEN 32              //字符串长度


typedef struct _UART_BUF
{
    char buf [BUFLEN+1];               
    unsigned int index ;
}UART_BUF;


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



void Uart0_Init(void);
void Uart0_Process(void);
void Uart0_SendChar(unsigned char Udat);
void Uart0_SendString(unsigned char *PBuf);

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


void nbiot_receive_process_event(unsigned char ch )          //将字符依次存入数组
{
     if(buf_uart2.index >= BUFLEN)
    {
        buf_uart2.index = 0 ;
    }
      else{
                                buf_uart2.buf[buf_uart2.index] = ch;
              buf_uart2.index++;
         }
}

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

}

void main(void)
{
        System_Init();                  
        TM1650_Init();                  
        ToDisplay__();                       
         Uart0_Init();                             
  MODIFY_HIRC_166();   //调整波特率
         while(1)
        {


        Uart0_SendString(buf_uart2.buf); //打印字符串内容
                Delay_ms(1000);
        
               
               
               
}
}

void Uart0_Init(void)
{
         SCON=0x50;                                                      //模式一,定时器,八位自动重装                  
        ClrBits(TMOD,T1_GATE|T1_CT|T1_M0);
        SetBits(TMOD,T1_M1);      
        SetBits(PCON,SMOD);                                    
        SetBits(CKCON,T1M);                                       
        ClrBits(T3CON,BRCK);                                
        TH1=256 - (1000000/115200+1);        //波特率115200  16.6MHz        
        TR1=1;                                                                                 
        ES=1;                                                                                          
        EA=1;                                                                                                                                                                                                                              
        P0_Quasi_Mode(PIN_6|PIN_7);        
}

void Uart0_SendChar(unsigned char Udat) //串口发送单个字符
{
        SBUF=Udat;               
        while(!TI);               
  TI=0;                     
}

void Uart0_SendString(unsigned char *PBuf)   //串口发送字符串
{
  while(*PBuf!='\0')            
  {
          Uart0_SendChar(*PBuf);
          PBuf++;                                                
  }
}

void MODIFY_HIRC_166(void)  //调整时钟
{
unsigned char hircmap0,hircmap1;
unsigned int trimvalue16bit;
if ((PCON&SET_BIT4)==SET_BIT4)
{
hircmap0 = RCTRIM0;
hircmap1 = RCTRIM1;
trimvalue16bit = ((hircmap0<<1)+(hircmap1&0x01));
trimvalue16bit = trimvalue16bit - 15;
hircmap1 = trimvalue16bit&0x01;
hircmap0 = trimvalue16bit>>1;
TA=0XAA;
TA=0X55;
RCTRIM0 = hircmap0;
TA=0XAA;
TA=0X55;
RCTRIM1 = hircmap1;
PCON &= CLR_BIT4;
}
}
[img][/img]

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

95.89 KB

程序源码

使用特权

评论回复
评论
新塘初级用户 2020-7-24 09:23 回复TA
process和isr这两个函数没有调用,不用管它 
新塘初级用户 2020-7-24 09:22 回复TA
不知为何,复制过来的代码注释是乱码,不好意思 
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

使用特权

评论回复
评论
新塘初级用户 2020-7-24 09:51 回复TA
发送给单片机GJKV,他回GJKVVVVVVVV,按理说我打印的是个数组,他会一直回最后一个字母,应该是不知道什么时候又跳进中断,但是没有数据给他,SBUF就一直存着最后一个字符,但我不知道是哪个环节出了问题 
新塘初级用户|  楼主 | 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

粉丝