[技术问答] 华大mcu(hcl136k8ta)串口可以发送数据,但是接收不到数据!

[复制链接]
1628|10
 楼主| lalhdo 发表于 2022-7-25 17:22 | 显示全部楼层 |阅读模式
今天在做一个华大的串口配置的时候,一直卡在了这个问题上,串口发送没有问题(单个字节和字符串都没有问题),以下是串口的配置,串口助手调试如下图:
  1. /***************************

  2. 串口配置

  3. ***************************/

  4. #include "usart.h"



  5. _UART_Rx_Data UART1_Rx_Data;



  6. void Uart1RC_CallBackFun(void)

  7. {

  8.     if(Uart_GetStatus(UARTCH1,UartRC))

  9.     {

  10.         Uart_ClrStatus(UARTCH1,UartRC);

  11.         Tim3_M0_Cnt16Set(Timer3_Count);

  12.         Tim3_M0_Run();//开始计时

  13.         if(UART1_Rx_Data.UART_Rx_Len < 512)

  14.         {

  15.             UART1_Rx_Data.UART_Rx_Buf[UART1_Rx_Data.UART_Rx_Len++] = Uart_ReceiveData(UARTCH1);

  16.             printf("buf:%s\r\n",UART1_Rx_Data.UART_Rx_Buf);

  17.         }

  18.     }

  19. }



  20. /************************

  21. 函数名称:USART_Config

  22. 函数作用:串口初始化

  23. 函数入口:brr 波特率

  24. 函数出口:无

  25. ************************/

  26. void USART_Config(uint32_t brr)

  27. {

  28.     uint16_t u16Scnt = 0;

  29.     //开启外设时钟

  30.     Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1,TRUE);///<使能uart1模块时钟

  31.    

  32.     //串口物理层配置

  33.     stc_gpio_config_t GPIO_InitStructure;

  34.     DDL_ZERO_STRUCT(GPIO_InitStructure);

  35.    

  36.     GPIO_InitStructure.enDir = GpioDirOut; //Tx

  37.     Gpio_Init(GpioPortD,GpioPin0,&GPIO_InitStructure);

  38.     Gpio_SetAfMode(GpioPortD,GpioPin0,GpioAf3);

  39.    

  40.     GPIO_InitStructure.enDir = GpioDirIn;  //Rx

  41.     Gpio_Init(GpioPortD,GpioPin1,&GPIO_InitStructure);

  42.     Gpio_SetAfMode(GpioPortD,GpioPin1,GpioAf3);

  43.    

  44.     //串口协议层配置

  45.     stc_uart_config_t  UART_InitStructure;//总体配置

  46.     stc_uart_irq_cb_t UartIrq_InitStructure;//发送接收中断处理函数接口

  47.     stc_uart_multimode_t UartMulti_InitStructure;//多主机模式及从机地址和地址掩码配置

  48.     stc_uart_baud_t UartBaud_InitStructure;//通道波特率计算参数

  49.    

  50.     DDL_ZERO_STRUCT(UART_InitStructure);

  51.     DDL_ZERO_STRUCT(UartIrq_InitStructure);

  52.     DDL_ZERO_STRUCT(UartMulti_InitStructure);

  53.     DDL_ZERO_STRUCT(UartBaud_InitStructure);

  54.    

  55.     UartMulti_InitStructure.enMulti_mode = UartNormal;//正常工作模式

  56.    

  57.     UartIrq_InitStructure.pfnRxIrqCb = Uart1RC_CallBackFun;//接受中断服务函数

  58.    

  59.     UartBaud_InitStructure.enRunMode = UartMode1;//模式1

  60.     UartBaud_InitStructure.u32Baud = brr;

  61.     UartBaud_InitStructure.u32Pclk = Sysctrl_GetPClkFreq();

  62.    

  63.     UART_InitStructure.bTouchNvic = TRUE;//中断使能

  64.     UART_InitStructure.enRunMode = UartMode1;//模式1

  65.     UART_InitStructure.enStopBit = Uart1bit;//一个停止位

  66.     UART_InitStructure.pstcIrqCb = &UartIrq_InitStructure;

  67.     UART_InitStructure.pstcMultiMode = &UartMulti_InitStructure;

  68.     Uart_Init(UARTCH1,&UART_InitStructure);//初始化

  69.     Uart_SetClkDiv(UARTCH1,Uart8Or16Div);//设置分频

  70.    

  71.     u16Scnt = Uart_CalScnt(UARTCH1,&UartBaud_InitStructure);//计算uart1波特率

  72.     Uart_SetBaud(UARTCH1,u16Scnt);//设置波特率

  73.    

  74.     Uart_ClrIsr(UARTCH1);//清中断

  75.     Uart_ClrStatus(UARTCH1,UartTC);//清发送完成标志位

  76.     Uart_ClrStatus(UARTCH1,UartRC);//清接收完成标志位

  77.     Uart_EnableIrq(UARTCH1,UartRxIrq);//使能接收中断

  78.     Uart_EnableFunc(UARTCH1,UartRx);//使能中断服务函数

  79.     EnableNvic(UART1_IRQn,IrqLevel1,TRUE);//中断优先级配置

  80. }



  81. /************************

  82. 函数名称:USART_SendByte

  83. 函数作用:串口发送字节

  84. 函数入口:byte 发送数据

  85. 函数出口:无

  86. ************************/

  87. void USART_SendByte(uint8_t byte)

  88. {

  89.     Uart_SendData(UARTCH1,byte);

  90. }



  91. /************************

  92. 函数名称:USART_RecByte

  93. 函数作用:串口接收字节

  94. 函数入口:无

  95. 函数出口:byte 接收数据

  96. ************************/

  97. uint8_t USART_RecByte(void)

  98. {

  99.     uint8_t byte = 0;

  100.     while(Uart_GetStatus(UARTCH1,UartRC) == FALSE); //直到接收完成

  101.     byte = Uart_ReceiveData(UARTCH1);

  102.     return byte;

  103. }



  104. /************************

  105. 函数名称:USART_SendStr

  106. 函数作用:USART发送字符串--查询

  107. 函数入口:str 发送字符串

  108.           Length 长度

  109. 函数出口:无

  110. ************************/

  111. void USART_SendStr(char* str,uint32_t Length)

  112. {

  113.     uint8_t i = 0;

  114.     for(i=0;i<Length;i++)

  115.     {

  116.         Uart_SendData(UARTCH1,*str);

  117.         str++;

  118.     }

  119.         

  120. }



  121. /************************

  122. 函数名称:USART_Echo

  123. 函数作用:USART回显

  124. 函数入口:无

  125. 函数出口:无

  126. ************************/

  127. void USART_Echo(void)

  128. {

  129.     uint8_t byte;

  130.     byte = USART_RecByte();

  131.     USART_SendByte(byte);

  132. }



  133. /************************

  134. 函数名称:fputc

  135. 函数作用:printf重定义

  136. 函数入口:无

  137. 函数出口:无

  138. ************************/

  139. int fputc(int c, FILE *stream)

  140. {

  141.     USART_SendByte(c);

  142.     return c;

  143. }

下图1
把回显函数放到主函数中运行没有任何现象,(一般在调试串口的时候我都会写一个回显函数来判断串口是否调通)
debug调试如下图(断点在回显之前,还没进入函数)
下图2
debug下一步进入函数(串口助手上发送了一个 2 ),此时现象如下图
下图3
按照常理来说,2 应该在上位机上显示出来才对呀,为什么没有显示呢?发送函数是通的,说明是接收出了问题,然后debug单步运行,好像是卡在了这里


tpgf 发表于 2022-8-2 16:45 | 显示全部楼层
能进入接收中断吗
coshi 发表于 2022-8-2 16:57 | 显示全部楼层
谁是接收端啊
qcliu 发表于 2022-8-2 17:10 | 显示全部楼层
使用的是什么接收模式啊
drer 发表于 2022-8-2 17:19 | 显示全部楼层
能用示波器监测到是吗
kxsi 发表于 2022-8-2 17:30 | 显示全部楼层
串口助手是上位机的是吗
wiba 发表于 2022-8-2 17:45 | 显示全部楼层
标志位一直没有变化是吗
caigang13 发表于 2022-8-2 19:37 来自手机 | 显示全部楼层
串口配置是否有问题
newwei 发表于 2022-8-3 09:03 | 显示全部楼层
看看波特率是不是配对了
tbbt 发表于 2022-8-7 23:16 | 显示全部楼层
本帖最后由 tbbt 于 2022-8-7 23:19 编辑

我和这位大哥问题类似,之前用内部4M时钟,收发都误码,现在使用内部24M时钟,是可以收正确了,但发送误码
https://bbs.21ic.com/icview-3244744-1-1.html
百里素雪 发表于 2022-11-22 23:20 | 显示全部楼层
是不是配置错了,时隙误码什么的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

22

主题

48

帖子

0

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