99精品伊人亚洲|最近国产中文炮友|九草在线视频支援|AV网站大全最新|美女黄片免费观看|国产精品资源视频|精彩无码视频一区|91大神在线后入|伊人终合在线播放|久草综合久久中文

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

【英飛凌開發(fā)板模塊評測任務大挑戰(zhàn)】硬件定時器的使用

冬至子 ? 來源:chejia12 ? 作者:chejia12 ? 2023-08-11 17:25 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

4.硬件定時器的使用和學習

這里依然使用mdk的看法環(huán)境,使用mdk編譯程序,下載程序

4.1配置使能硬件定時器2

1.jpg

4.2 編寫定時器的測試函數

/*

程序清單:這是一個 hwtimer 設備使用例程
例程導出了 hwtimer_sample 命令到控制終端
命令調用格式:hwtimer_sample
程序功能:硬件定時器超時回調函數周期性的打印當前tick值,2次tick值之差換算為時間等同于定時時間值。
/
#include
#include
#define HWTIMER_DEV_NAME "time2" /
定時器名稱 /
/
定時器超時回調函數 /
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());
return 0;
}
int hwtimer_sample(void)
{
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; /
計數頻率 /
/
查找定時器設備 /
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 :%dn", ret);
return ret;
}
/
設置定時器超時值為5s并啟動定時器 /
timeout_s.sec = 5; /
/
timeout_s.usec = 0; /
微秒 /
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
{
rt_kprintf("set timeout value failedn");
return RT_ERROR;
}
/
延時3500ms /
rt_thread_mdelay(3500);
/
讀取定時器當前值 /
rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
rt_kprintf("Read: Sec = %d, Usec = %dn", timeout_s.sec, timeout_s.usec);
return ret;
}
/
導出到 msh 命令列表中 */
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);

4.3測試函數,查看運行結果

1.jpg

4.4硬件定時器設備驅動框架學習

使用方法:

4.4.1實現定時器的各個操作函數

/*

  1. 定時器 初始化函數
  2. 定時器起始函數
  3. 定時器停止函數
  4. 定時器的計數值獲取
  5. 定時器的控制函數
    */
    struct rt_hwtimer_ops
    {
    void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
    rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
    void (*stop)(struct rt_hwtimer_device *timer);
    rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
    rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void args);
    };
    4.4.2配置定時器的基本參數
    /
    定時器特征描述 Timer Feature Information /
    struct rt_hwtimer_info
    {
    rt_int32_t maxfreq; /
    最大頻率 the maximum count frequency timer support /
    rt_int32_t minfreq; /
    最小頻率 the minimum count frequency timer support /
    rt_uint32_t maxcnt; /
    最大計數 值counter maximum value /
    rt_uint8_t cntmode; /
    計數方向 count mode (inc/dec) */
    };
    typedef struct rt_hwtimer_device
    {
    struct rt_device parent;//基本設備驅動框架
    const struct rt_hwtimer_ops *ops;//定時器特有的操作函數
    const struct rt_hwtimer_info info;//定時器相關的參數信息
    rt_int32_t freq; /
    用戶設置的計數頻率 counting frequency set by the user /
    rt_int32_t overflow; /
    定時器溢出 timer overflows /
    float period_sec;
    rt_int32_t cycles; /
    溢出后將生成超時事件多少次 how many times will generate a timeout event after overflow /
    rt_int32_t reload; /
    重新加載循環(huán)(使用周期模式) reload cycles(using in period mode) /
    rt_hwtimer_mode_t mode; /
    計時模式(一次/周期) timing mode(oneshot/period) /
    } rt_hwtimer_t;
    4.4.3注冊定時器的設備到驅動框架
    /

    定時器設備注冊函數
    */
    rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);

4.4.4詳細的定時器設備驅動相關

/*

Copyright (c) 2006-2023, RT-Thread Development Team

SPDX-License-Identifier: Apache-2.0

Change Logs:
Date Author Notes
/
#ifndef HWTIMER_H
#define HWTIMER_H
#include
#ifdef __cplusplus
extern "C" {
#endif
/
定時器的控制命令類型 /
typedef enum
{
HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, /
設置技術的頻率set the count frequency /
HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, /
停止定時器stop timer /
HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, /
獲取計時器功能信息 get a timer feature information /
HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 /
設置定時器的工作模式 Setting the timing mode(oneshot/period) /
} rt_hwtimer_ctrl_t;
/
Timing Mode /
typedef enum
{
HWTIMER_MODE_ONESHOT = 0x01,//單次模式
HWTIMER_MODE_PERIOD//周期模式
} rt_hwtimer_mode_t;
/
Time Value /
typedef struct rt_hwtimerval
{
rt_int32_t sec; /
秒 second /
rt_int32_t usec; /
微秒 microsecond /
} rt_hwtimerval_t;
/ 計數的方向 /
#define HWTIMER_CNTMODE_UP 0x01 /
increment count mode /
#define HWTIMER_CNTMODE_DW 0x02 /
decreasing count mode /
struct rt_hwtimer_device;
/

  1. 定時器 初始化函數
  2. 定時器起始函數
  3. 定時器停止函數
  4. 定時器的計數值獲取
  5. 定時器的控制函數
    */
    struct rt_hwtimer_ops
    {
    void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
    rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
    void (*stop)(struct rt_hwtimer_device *timer);
    rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
    rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void args);
    };
    /
    定時器特征描述 Timer Feature Information /
    struct rt_hwtimer_info
    {
    rt_int32_t maxfreq; /
    最大頻率 the maximum count frequency timer support /
    rt_int32_t minfreq; /
    最小頻率 the minimum count frequency timer support /
    rt_uint32_t maxcnt; /
    最大計數 值counter maximum value /
    rt_uint8_t cntmode; /
    計數方向 count mode (inc/dec) */
    };
    typedef struct rt_hwtimer_device
    {
    struct rt_device parent;//基本設備驅動框架
    const struct rt_hwtimer_ops *ops;//定時器特有的操作函數
    const struct rt_hwtimer_info info;//定時器相關的參數信息
    rt_int32_t freq; /
    用戶設置的計數頻率 counting frequency set by the user /
    rt_int32_t overflow; /
    定時器溢出 timer overflows /
    float period_sec;
    rt_int32_t cycles; /
    溢出后將生成超時事件多少次 how many times will generate a timeout event after overflow /
    rt_int32_t reload; /
    重新加載循環(huán)(使用周期模式) reload cycles(using in period mode) /
    rt_hwtimer_mode_t mode; /
    計時模式(一次/周期) timing mode(oneshot/period) /
    } rt_hwtimer_t;
    /

    定時器設備注冊函數
    */
    rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
    / 定時器回調函數 /
    void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
    #ifdef __cplusplus
    }
    #endif
    #endif
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯系本站處理。 舉報投訴
  • 驅動器
    +關注

    關注

    54

    文章

    8695

    瀏覽量

    149930
  • 定時器
    +關注

    關注

    23

    文章

    3298

    瀏覽量

    118898
  • 計時器
    +關注

    關注

    1

    文章

    432

    瀏覽量

    33739
  • 回調函數
    +關注

    關注

    0

    文章

    88

    瀏覽量

    11894
  • RT-Thread
    +關注

    關注

    32

    文章

    1409

    瀏覽量

    41934
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    如何利用STM32L475開發(fā)板去處理定時器捕獲模塊應用程序

    我想在捕獲模塊上進行測試,以找出實際應用中定時器引腳處到達脈沖的脈沖寬度。所以,在這里我想要一個 STM32L475 的開發(fā)板來處理這個應用程序,比如從一個定時器 PWM 生成脈沖并循
    發(fā)表于 12-23 09:08

    【實驗38】定時器定時

    HL配套C實驗例程100例之定時器定時,配合開發(fā)板學習效果更好。
    發(fā)表于 04-11 16:09 ?7次下載

    HL配套C實驗例程定時器

    HL配套C實驗例程定時器,配合開發(fā)板學習效果更好。
    發(fā)表于 04-11 17:04 ?2次下載

    基于MCU的模塊定時器的詳細解析

    在MCU中(M16),定時器是獨立的一個模塊,M16有三個獨立的定時器模塊,即T/C0、T/C1和T/C2;其中T/C0和T/C2都是8位的定時器
    的頭像 發(fā)表于 01-16 09:42 ?1.1w次閱讀

    新唐 NuMaker-M2354模塊評測任務挑戰(zhàn)

    評測任務挑戰(zhàn)活動,讓開發(fā)者小伙伴們互相協(xié)作,對開發(fā)板的每個模塊功能進行
    的頭像 發(fā)表于 11-16 16:42 ?1588次閱讀

    基于硬件定時器的軟件定時器

    概括硬件定時器很精確,軟件定時器無論如何都有延遲,主要用在不需要精確定時的地方,而且軟件定時比較浪費單片機資源。梳理講到
    發(fā)表于 11-25 09:51 ?8次下載
    基于<b class='flag-5'>硬件</b><b class='flag-5'>定時器</b>的軟件<b class='flag-5'>定時器</b>

    基于cubemx的stm32開發(fā)之路(使用正點原子戰(zhàn)艦V3開發(fā)板)——基本定時器的應用

    1hz的閃爍實驗設備正點原子新戰(zhàn)艦V3 STM32F103ZET6開發(fā)板學習st-link燒錄定時器原理F103定時器組成STM32F1
    發(fā)表于 12-08 15:21 ?5次下載
    基于cubemx的stm32<b class='flag-5'>開發(fā)</b>之路(使用正點原子戰(zhàn)艦V3<b class='flag-5'>開發(fā)板</b>)——基本<b class='flag-5'>定時器</b>的應用

    MM32F0140定時器模塊計數定時功能

    本篇筆記主要探討 MM32F0140 定時器模塊的框圖結構、定時器提供的計數定時等功能以及配置定時器的流程,并以 pokt-f0140
    的頭像 發(fā)表于 04-07 16:31 ?3158次閱讀
    MM32F0140<b class='flag-5'>定時器</b><b class='flag-5'>模塊</b>計數<b class='flag-5'>定時</b>功能

    Linux驅動開發(fā)高精度定時器的精度測量評測

    正點原子的示波器能不能支撐嵌入式開發(fā)流程。 Linux高精度定時器說明 其實傳統(tǒng)的低分辨率定時器隨著技術的演進,已經無法滿足開發(fā)需求。而且硬件
    的頭像 發(fā)表于 08-09 11:17 ?2356次閱讀

    【合宙Air105開發(fā)板試用體驗】小小定時器,能有大作用!

    本文來源電子發(fā)燒友社區(qū),作者:HonestQiao, 帖子地址: https://bbs.elecfans.com/jishu_2283057_1_1.html 案例演示(開發(fā)板體驗視頻,詳見作者
    的頭像 發(fā)表于 12-02 15:02 ?1175次閱讀

    N32L40XCL-STB 開發(fā)板模塊評測任務挑戰(zhàn)

    評測任務挑戰(zhàn)活動,讓開發(fā)者小伙伴們互相協(xié)作,對開發(fā)板的每個模塊功能進行
    的頭像 發(fā)表于 03-28 03:25 ?1144次閱讀

    英飛凌開發(fā)板模塊評測任務挑戰(zhàn)

    ?RT-Thread 官方特聯合合作伙伴發(fā)起開發(fā)板評測任務挑戰(zhàn)活動,讓開發(fā)者小伙伴們互相協(xié)作,對開發(fā)板
    的頭像 發(fā)表于 04-11 09:05 ?1185次閱讀

    英飛凌開發(fā)板模塊評測任務挑戰(zhàn)-SPI驅動測試

    使用PSoC? 62 with CAPSENSE? evaluation kit開發(fā)板適配的RTT SPI驅動,做顯示測試。
    發(fā)表于 08-10 15:44 ?917次閱讀
    <b class='flag-5'>英飛凌</b><b class='flag-5'>開發(fā)板</b><b class='flag-5'>模塊</b><b class='flag-5'>評測</b><b class='flag-5'>任務</b>大<b class='flag-5'>挑戰(zhàn)</b>-SPI驅動測試

    定時器中斷程序怎么寫

    達到預定的定時時間時,它會產生一個中斷信號,稱為定時器中斷。在本文中,我們將詳細了解如何編寫定時器中斷程序。 #1. 硬件配置 在開始編寫定時器
    的頭像 發(fā)表于 09-01 10:17 ?2469次閱讀

    英飛凌開發(fā)板模塊評測任務挑戰(zhàn)】mdk開發(fā)板環(huán)境搭建

    在rtt源碼內部生成英飛凌芯片的獨立的工程
    的頭像 發(fā)表于 10-27 12:39 ?1188次閱讀
    【<b class='flag-5'>英飛凌</b><b class='flag-5'>開發(fā)板</b><b class='flag-5'>模塊</b><b class='flag-5'>評測</b><b class='flag-5'>任務</b>大<b class='flag-5'>挑戰(zhàn)</b>】mdk<b class='flag-5'>開發(fā)板</b>環(huán)境搭建