打印
[活动专区]

【AT-START-F405测评】2.串口和定时器的使用

[复制链接]
574|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
开发板上的ATLink有串口功能,连接到了F405的PA9和PA10,可以直接用板载的ATLink调试串口功能
打开AT32 Work Bench,在左侧找到USART1,使能它,下面可以直接设置串口波特率、停止位、奇偶校验等参数

会自动分配PA9和PA10作为串口

开启中断

点一下代码预览后再点击生成代码,打开工程,打开at32f402_405_wk_config.c这个文件,找到wk_usart1_init这个方法,生成的代码并没有使能中断,需要自己添加
/**
   * Users need to configure USART1 interrupt functions according to the actual application.
   * 1. Call the below function to enable the corresponding USART1 interrupt.
   *     --usart_interrupt_enable(...)
   * 2. Add the user's interrupt handler code into the below function in the at32f402_405_int.c file.
   *     --void USART1_IRQHandler(void)
   */

  usart_enable(USART1, TRUE);

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

        usart_data_transmit(USART1, dat);
    }

  /* add user code end USART1_IRQ 0 */
  /* add user code begin USART1_IRQ 1 */

  /* add user code end USART1_IRQ 1 */
}
运行效果

接下来实现printf重定向到串口,在keil中实现这个功能很简单,在工程设置中勾选这个

在main.c中包含头文件#include "stdio.h",并增加方法
int fputc(int ch, FILE *f)
{
    while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
    usart_data_transmit(USART1, (uint16_t)ch);
    while(usart_flag_get(USART1, USART_TDC_FLAG) == RESET);
    return ch;
}
在主循环中加入测试代码
printf("雅特力AT32F405 printf test\n");
运行效果

在AT32 Work Bench配置定时器也很方便,找到TMR,选择一个定时器然后勾选启用


在这个界面配置定时器参数可以直接看到最终得到的定时器周期,这里配置了一个1hz的定时器,开启中断

和串口一样生成代码后需要手动开启中断使能
/**
   * Users need to configure TMR6 interrupt functions according to the actual application.
   * 1. Call the below function to enable the corresponding TMR6 interrupt.
   *     --tmr_interrupt_enable(...)
   * 2. Add the user's interrupt handler code into the below function in the at32f402_405_int.c file.
   *     --void TMR6_GLOBAL_IRQHandler(void)
   */

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

  /* add user code end TMR6_GLOBAL_IRQ 0 */
  /* add user code begin TMR6_GLOBAL_IRQ 1 */

  /* add user code end TMR6_GLOBAL_IRQ 1 */
}
运行效果



使用特权

评论回复
沙发
呐咯密密| | 2024-4-25 11:31 | 只看该作者
定时器的配置竟然是图形化的,好爽

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

86

主题

464

帖子

4

粉丝