[STM32U5] 【NUCLEO-U575ZI-Q测评】串口控制LED灯

[复制链接]
959|15
 楼主| lulugl 发表于 2023-2-25 12:53 | 显示全部楼层 |阅读模式
本次采用USART1不定长中断接收,来控制板截LED_RED的开与关。
1、串口图形化配置、打开串口全局中断:
截屏2023-02-25 11.42.23.png
2、添加串口重定向:
  1. /* USER CODE BEGIN 1 */

  2. #include "stdio.h"

  3. #ifdef __GNUC__

  4. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  5. //#define GETCHAR_PROTOTYPE int __io_getchar(FILE *f)

  6. PUTCHAR_PROTOTYPE
  7. {

  8.   HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
  9.   return ch;
  10. }

  11. //GETCHAR_PROTOTYPE
  12. //{
  13. //        uint8_t ch = 0;
  14. //        HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
  15. //        return ch;
  16. //}
  17. #endif
3、添加串口中断函数,中断函数中,如果接收到数据,更新串口接收数据标志为1,以便其他进程判,并处理数据。
  1. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  2. {
  3.   /* Prevent unused argument(s) compilation warning */
  4.   UNUSED(huart);
  5.   /* NOTE: This function Should not be modified, when the callback is needed,
  6.            the HAL_UART_TxCpltCallback could be implemented in the user file
  7.    */

  8.         if(rxtx_it_usart.huart == huart)
  9.         {
  10.                 rxtx_it_usart.rx_flage = 1;
  11.         }
  12. }
4、添加自定义串口空闲中断回调函数
  1. //自定义空闲中断处理函数
  2. void HAL_UART_IDLE_HANDLER(UART_HandleTypeDef *huart)
  3. {
  4.    uint32_t isrflags   =  READ_REG(huart->Instance->ISR);
  5.    if((USART_ISR_IDLE & isrflags) != RESET && ( huart->RxXferCount > 0))
  6.    {
  7.        printf("idle\r\n");
  8.        __HAL_UART_CLEAR_IDLEFLAG(huart);
  9.       /* Disable the UART Data Register not empty Interrupt */
  10.       __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  11.       /* Disable the UART Parity Error Interrupt */
  12.       __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  13.       /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  14.       __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  15.       /* Rx process is completed, restore huart->RxState to Ready */
  16.       huart1.RxState = HAL_UART_STATE_READY;
  17.       HAL_UART_RxCpltCallback(huart);
  18.    }else if((USART_ISR_IDLE & isrflags) != RESET && ( huart->RxXferCount == 0 ))
  19.    {
  20.        __HAL_UART_CLEAR_IDLEFLAG(huart);

  21.    }
  22. }
5、添加初始化空闲中断,在main初始化时加载来启用空闲中断的接收。
  1. //初始化空闲中断
  2. void uart1_user_init(void)
  3. {
  4.         __HAL_UART_ENABLE_IT(rxtx_it_usart.huart, UART_IT_IDLE);
  5.         __HAL_UART_CLEAR_IDLEFLAG(rxtx_it_usart.huart);
  6.         HAL_UART_Receive_IT(rxtx_it_usart.huart,rxtx_it_usart.rx_buf,RXBUFFSIZE);
  7. }
6、在main中添加串口接收数,判是串口接收数据标志是否更新,如果有更新,判断符合LED命令,则相应做出LED灯的动作。数据处理后,重置接收标志,清空数据接收缓存。
  1. if(rxtx_it_usart.rx_flage == 1)
  2.                  {
  3.                             ret = strstr((char *)rxtx_it_usart.rx_buf,(char *)"LED_RED_ON");
  4.                     if(0 != ret)
  5.                     {
  6.                             printf("LED_ON \r\n");
  7.                             HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
  8.                     }
  9.                     ret = strstr((char *)rxtx_it_usart.rx_buf,(char *)"LED_RED_OFF");
  10.                     if(ret != 0)
  11.                         {
  12.                             printf("LED_OFF  \r\n");
  13.                                 HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
  14.                         }
  15.                          rxtx_it_usart.rx_flage = 0;
  16.                          //回显接收到的数据
  17. //                         printf("\r\nrcv done:\r\n");
  18.                          //printf("%s\r\n", rxtx_it_usart.rx_buf);
  19.                          //重新开始接收20个字节数据
  20.                          memset(rxtx_it_usart.rx_buf,0,sizeof(rxtx_it_usart.rx_buf));
  21.                          HAL_UART_Receive_IT(rxtx_it_usart.huart,rxtx_it_usart.rx_buf,RXBUFFSIZE);
  22.                  }
7、实验效果:
截屏2023-02-25 12.50.44.png
【小结】这次实验实现了,不定长度的串口中断接收,并完成对接收数据的判断,做出相应的LED的控制动作。
jason1011 发表于 2023-3-1 17:42 | 显示全部楼层
saservice 发表于 2023-5-14 20:45 | 显示全部楼层
现在开发单片机都是hal库了。              
sheflynn 发表于 2023-5-14 20:49 | 显示全部楼层
不定长怎么计算超时呢?              
adolphcocker 发表于 2023-5-14 21:26 | 显示全部楼层
使用了idle的中断了吗              
MessageRing 发表于 2023-5-14 22:39 | 显示全部楼层
saservice 发表于 2023-5-14 20:45
现在开发单片机都是hal库了。

有些型号不给提供标准库了都
 楼主| lulugl 发表于 2023-5-15 08:04 | 显示全部楼层
adolphcocker 发表于 2023-5-14 21:26
使用了idle的中断了吗

对的,这个就是使用空闲中断来判断接收命令结束来实现的。
 楼主| lulugl 发表于 2023-5-15 08:05 | 显示全部楼层
MessageRing 发表于 2023-5-14 22:39
有些型号不给提供标准库了都

对的,hal库相比标准库,移植更加方便!
vivilyly 发表于 2023-5-18 14:04 | 显示全部楼层
这个单片机的性能怎么样              
yeates333 发表于 2023-5-18 16:07 | 显示全部楼层
为什么不使用定时器判断呢              
 楼主| lulugl 发表于 2023-5-19 09:04 | 显示全部楼层
yeates333 发表于 2023-5-18 16:07
为什么不使用定时器判断呢

这个可以试一下。
LLGTR 发表于 2023-5-19 14:06 | 显示全部楼层
串口控制LED灯,这个Demo还是比较简单的!
朝生 发表于 2023-5-19 14:07 | 显示全部楼层
感觉这算是比较简单的设计了!测评必备。
软核硬核 发表于 2023-5-19 14:08 | 显示全部楼层
虽然看起来很简单,但是用到的技术还是不错的。
fengm 发表于 2023-5-21 15:01 | 显示全部楼层
这个单片机的性能怎么样              
beacherblack 发表于 2023-5-21 17:14 | 显示全部楼层
为什么不使用定时器判断呢              
您需要登录后才可以回帖 登录 | 注册

本版积分规则

188

主题

843

帖子

12

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