[原创] 【RTT&英飞凌PSoC6评估板(带触摸)】定时器测试

qinyunti   2023-6-18 00:05 楼主

前言

这一篇来进行定时器的实现与测试,因为后面很多功能都需要用到定时器。通过定时器中断中翻转IO使用示波器测量来确定时间准确度。

 

代码

在前面https://bbs.eeworld.com.cn/thread-1247060-1-1.html的基础上进行。

 

定时器初始化

 void timer_init(void)

 {

    cy_rslt_t result;

 

    const cyhal_timer_cfg_t led_blink_timer_cfg =

    {

        .compare_value = 0,                 /* Timer compare value, not used */

        .period = LED_BLINK_TIMER_PERIOD,   /* Defines the timer period */

        .direction = CYHAL_TIMER_DIR_UP,    /* Timer counts up */

        .is_compare = false,                /* Don't use compare mode */

        .is_continuous = true,              /* Run timer indefinitely */

        .value = 0                          /* Initial value of counter */

    };

 

    /* Initialize the timer object. Does not use input pin ('pin' is NC) and

     * does not use a pre-configured clock source ('clk' is NULL). */

    result = cyhal_timer_init(&led_blink_timer, NC, NULL);

 

    /* timer init failed. Stop program execution */

    if (result != CY_RSLT_SUCCESS)

    {

        CY_ASSERT(0);

    }

 

    /* Configure timer period and operation mode such as count direction,

       duration */

    cyhal_timer_configure(&led_blink_timer, &led_blink_timer_cfg);

 

    /* Set the frequency of timer's clock source */

    cyhal_timer_set_frequency(&led_blink_timer, LED_BLINK_TIMER_CLOCK_HZ);

 

    /* Assign the ISR to execute on timer interrupt */

    cyhal_timer_register_callback(&led_blink_timer, isr_timer, NULL);

 

    /* Set the event on which timer interrupt occurs and enable it */

    cyhal_timer_enable_event(&led_blink_timer, CYHAL_TIMER_IRQ_TERMINAL_COUNT,

                              7, true);

 

    /* Start the timer with the configured settings */

    cyhal_timer_start(&led_blink_timer);

 }

 

其中

#define LED_BLINK_TIMER_CLOCK_HZ          (10000) 定义定时器时钟频率

 

 

#define LED_BLINK_TIMER_PERIOD            (99) 定义周期,即多少个时钟产生中断

如果时钟频率为10000则一个时钟1/10000秒, 设置周期99即100个时钟100/10000秒,即1/100秒,10mS。

 

注册了回调函数isr_timer

 

中断中翻转IO

static void isr_timer(void *callback_arg, cyhal_timer_event_t event)

{

    (void) callback_arg;

    (void) event;

 

    /* Set the interrupt flag and process it from the main while(1) loop */

    timer_interrupt_flag = true;

    cyhal_gpio_toggle(CYBSP_USER_LED);

}

 

IO的初始化如下

    result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT,

                             CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);

其中CYBSP_USER_LED为P11_1

 

image-20230618000511-1.png  

image-20230618000511-2.png  

注释掉以下代码

        /* Check if timer elapsed (interrupt fired) and toggle the LED */

        //if (timer_interrupt_flag)

        //{

            /* Clear the flag */

        //    timer_interrupt_flag = false;

 

            /* Invert the USER LED state */

        //    cyhal_gpio_toggle(CYBSP_USER_LED);

        //}

时间准确度测试

设置1mS中断

#define LED_BLINK_TIMER_PERIOD            (9)

实测0.99mS

image-20230618000511-3.jpeg  

设置10mS中断

#define LED_BLINK_TIMER_PERIOD            (99)

实测9.92

image-20230618000511-4.jpeg  

 

考虑到测量误差,上述结果可以看到时间是准确的。

 

总结

使用官方的库函数,HAL接口进行外设操作比较简单,比如定时器几个接口就完成了操作,非常方便。

回复评论 (1)

定时器中断中翻转IO使用示波器测量来确定时间准确度,用官方的库函数,HAL接口进行外设操作确实是比较简单

点赞  2023-6-18 10:57
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复