• RT-Thread STM32F407 定时器


    定时器简介

    硬件定时器一般有 2 种工作模式,定时器模式和计数器模式。不管是工作在哪一种模式,实质都是通过内部计数器模块对脉冲信号进行计数。下面是定时器的一些重要概念。

    计数器模式:对外部输入引脚的外部脉冲信号计数。

    定时器模式:对内部脉冲信号计数。定时器常用作定时时钟,以实现定时检测,定时响应、定时控制。

    计数器:计数器可以递增计数或者递减计数。16位计数器的最大计数值为65535,32位的最大值为4294967295。

    计数频率:定时器模式时,计数器单位时间内的计数次数,由于系统时钟频率是定值,所以可以根据计数器的计数值计算出定时时间,定时时间 = 计数值 / 计数频率。例如计数频率为 1MHz,计数器计数一次的时间则为 1 / 1000000, 也就是每经过 1 微秒计数器加一(或减一),此时 16 位计数器的最大定时能力为 65535 微秒,即 65.535 毫秒。

    • 第一步,首先在 RT-Thread Settings中进行配置
      在这里插入图片描述
    • 第二步,退出RT-Thread Settings,进入board.h,定义宏
      在这里插入图片描述
    • 第三步,进入tim_config.h,增加或修改定时器相关宏
      在这里插入图片描述
    • 第四步,进入stm32f4xx_hal_conf.h 使能#define HAL_TIM_MODULE_ENABLED
      在这里插入图片描述
    • 第五步,回到main.c,使能定时器时钟,并找到 打开
    __HAL_RCC_TIM3_CLK_ENABLE();
    rt_device_t hw_dev = rt_device_find("timer3");
    rt_err_t ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
    
    • 1
    • 2
    • 3
    • 第六步,设置超时回调函数、计数频率和计数模式、超时值并其开启定时器
    rt_device_set_rx_indicate(hw_dev, timeout_cb);     /* 设置超时回调函数 */    rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);  /* 设置计数频率(若未设置该项,默认为1Mhz 或 支持的最小计数频率) */
    mode = HWTIMER_MODE_PERIOD;    /* 设置模式为周期性定时器(若未设置,默认是HWTIMER_MODE_ONESHOT)*/
    ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
    timeout_s.sec = 2;      /* 秒 */
    timeout_s.usec = 0;     /* 微秒 */
    rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 第七步,编写超时回调函数
    static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
    {
        rt_kprintf("tick is :%d !\n", rt_tick_get());    //获取当前系统的滴答计数值
        rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));     /* 读取定时器当前值 */
        rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    综上,main.c

    #include 
    #include 
    #include 
    #include 
    #include "tim_config.h"
    
    #define DBG_TAG "main"
    #define DBG_LVL DBG_LOG
    
    #define HWTIMER_DEV_NAME   "timer3"     /* 定时器名称 */
    
    rt_err_t ret = RT_EOK;
    rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
    rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
    rt_hwtimer_mode_t mode;         /* 定时器模式 */
    rt_uint32_t freq = 10000;               /* 计数频率 */
    
    /* 定时器超时回调函数 */
    static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
    {
        //rt_kprintf("this is hwtimer timeout callback fucntion!\n");
        rt_kprintf("tick is :%d !\n", rt_tick_get());    //获取当前系统的滴答计数值
    
        //rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
        //rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
    
        return 0;
    }
    
    int main(void)
    {
        // 使用前必须先手动打开时钟
        __HAL_RCC_TIM3_CLK_ENABLE();
    
        /* 查找定时器设备 */
        hw_dev = rt_device_find(HWTIMER_DEV_NAME);
        if (hw_dev == RT_NULL)
        {
          rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
          return RT_ERROR;
        }
    
        /* 以读写方式打开设备 */
        ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
        if (ret != RT_EOK)
        {
          rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
          return ret;
        }
    
        /* 设置超时回调函数 */
        rt_device_set_rx_indicate(hw_dev, timeout_cb);
    
        /* 设置计数频率(若未设置该项,默认为1Mhz 或 支持的最小计数频率) */
        rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
        /* 设置模式为周期性定时器(若未设置,默认是HWTIMER_MODE_ONESHOT)*/
        mode = HWTIMER_MODE_PERIOD;
        ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
        if (ret != RT_EOK)
        {
          rt_kprintf("set mode failed! ret is :%d\n", ret);
          return ret;
        }
    
        /* 设置定时器超时值为5s并启动定时器 */
        timeout_s.sec = 2;      /* 秒 */
        timeout_s.usec = 0;     /* 微秒 */
        if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
        {
            rt_kprintf("set timeout value failed\n");
            return RT_ERROR;
        }
        while(1)
        {
        }
        return RT_EOK;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    在这里插入图片描述

  • 相关阅读:
    C++ 线段树的实现总结
    技术的“核心引擎”
    swift 事件
    QT中多线程QThread使用解疑实例
    Java 接口
    基础-JavaSE面试题目
    微服务框架 SpringCloud微服务架构 20 RestClient 操作索引库 20.5 删除和判断索引库
    STM32第十一课:ADC采集光照
    互联网真实赚钱平台有哪些?分享六大类正规靠谱的线上平台
    设计模式-单例模式 (Singleton)
  • 原文地址:https://blog.csdn.net/Dustinthewine/article/details/134407635