[活动专区] 【AT-START-F405测评】2.串口和定时器的使用

[复制链接]
4851|1
 楼主| yuyy1989 发表于 2024-4-23 19:46 | 显示全部楼层 |阅读模式
开发板上的ATLink有串口功能,连接到了F405的PA9和PA10,可以直接用板载的ATLink调试串口功能
打开AT32 Work Bench,在左侧找到USART1,使能它,下面可以直接设置串口波特率、停止位、奇偶校验等参数
微信截图_20240422192217.png
会自动分配PA9和PA10作为串口
微信截图_20240422192238.png
开启中断
微信截图_20240422192321.png
点一下代码预览后再点击生成代码,打开工程,打开at32f402_405_wk_config.c这个文件,找到wk_usart1_init这个方法,生成的代码并没有使能中断,需要自己添加
  1. /**
  2.    * Users need to configure USART1 interrupt functions according to the actual application.
  3.    * 1. Call the below function to enable the corresponding USART1 interrupt.
  4.    *     --usart_interrupt_enable(...)
  5.    * 2. Add the user's interrupt handler code into the below function in the at32f402_405_int.c file.
  6.    *     --void USART1_IRQHandler(void)
  7.    */

  8.   usart_enable(USART1, TRUE);

  9.   /* add user code begin usart1_init 2 */
  10.     usart_interrupt_enable(USART1,USART_RDBF_INT,TRUE);
  11.   /* add user code end usart1_init 2 */
打开at32f402_405_int.c,找到USART1_IRQHandler,这是默认的中断处理方法,先简单将收到的数据原样返回
  1. void USART1_IRQHandler(void)
  2. {
  3.   /* add user code begin USART1_IRQ 0 */
  4.     uint8_t dat;
  5.     if(usart_interrupt_flag_get(USART1, USART_RDBF_FLAG) != RESET)
  6.     {
  7.         /* read one byte from the receive data register */
  8.         dat = usart_data_receive(USART1);

  9.         usart_data_transmit(USART1, dat);
  10.     }

  11.   /* add user code end USART1_IRQ 0 */
  12.   /* add user code begin USART1_IRQ 1 */

  13.   /* add user code end USART1_IRQ 1 */
  14. }
运行效果
微信截图_20240422194801.png
接下来实现printf重定向到串口,在keil中实现这个功能很简单,在工程设置中勾选这个
微信截图_20240423191515.png
在main.c中包含头文件#include "stdio.h",并增加方法
  1. int fputc(int ch, FILE *f)
  2. {
  3.     while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
  4.     usart_data_transmit(USART1, (uint16_t)ch);
  5.     while(usart_flag_get(USART1, USART_TDC_FLAG) == RESET);
  6.     return ch;
  7. }
在主循环中加入测试代码
  1. printf("雅特力AT32F405 printf test\n");
运行效果
微信截图_20240423191852.png
在AT32 Work Bench配置定时器也很方便,找到TMR,选择一个定时器然后勾选启用

微信截图_20240422201209.png
在这个界面配置定时器参数可以直接看到最终得到的定时器周期,这里配置了一个1hz的定时器,开启中断
微信截图_20240422195312.png
和串口一样生成代码后需要手动开启中断使能
  1. /**
  2.    * Users need to configure TMR6 interrupt functions according to the actual application.
  3.    * 1. Call the below function to enable the corresponding TMR6 interrupt.
  4.    *     --tmr_interrupt_enable(...)
  5.    * 2. Add the user's interrupt handler code into the below function in the at32f402_405_int.c file.
  6.    *     --void TMR6_GLOBAL_IRQHandler(void)
  7.    */

  8.   /* add user code begin tmr6_init 2 */
  9.     tmr_interrupt_enable(TMR6,TMR_OVF_INT,TRUE);
  10.   /* add user code end tmr6_init 2 */
在中断函数中每一秒打印一次
  1. void TMR6_GLOBAL_IRQHandler(void)
  2. {
  3.   /* add user code begin TMR6_GLOBAL_IRQ 0 */
  4.     static uint8_t tim = 0;
  5.     if(tmr_interrupt_flag_get(TMR6, TMR_OVF_FLAG) == SET)
  6.     {
  7.         printf("雅特力F405定时器测试:%d",tim++);
  8.         tmr_flag_clear(TMR6, TMR_OVF_FLAG);
  9.     }

  10.   /* add user code end TMR6_GLOBAL_IRQ 0 */
  11.   /* add user code begin TMR6_GLOBAL_IRQ 1 */

  12.   /* add user code end TMR6_GLOBAL_IRQ 1 */
  13. }
运行效果
微信截图_20240423193858.png


呐咯密密 发表于 2024-4-25 11:31 | 显示全部楼层
定时器的配置竟然是图形化的,好爽
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:同飞软件研发工程师
简介:制冷系统单片机软件开发,使用PID控制温度

168

主题

826

帖子

10

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