RT-Thread 15. list_timer与软定时器

1. 代码
c 复制代码
void rt_thread_usr1_entry(void *parameter)
{
    /* set LED2 pin mode to output */
    rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
    while (1)
    {
        rt_pin_write(LED2_PIN, PIN_HIGH);
        rt_thread_mdelay(2000);
        rt_pin_write(LED2_PIN, PIN_LOW);
        rt_thread_mdelay(3000);
    } 
}

int main(void)
{
  
    rt_thread_init(&usr1_thread,
                 USR1_THREAD_NAME,
                 rt_thread_usr1_entry,
                 RT_NULL,
                 &usr1_thread_stack[0],
                 sizeof(usr1_thread_stack),
                 RT_USR1_THREAD_PRIO,
                 20);
    rt_thread_startup(&usr1_thread);
  
    /* set LED1 pin mode to output */
    rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);

    while (1)
    {
        rt_pin_write(LED1_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED1_PIN, PIN_LOW);
        rt_thread_mdelay(600);
    }
}
2. list_timer结果
3. 解释
c 复制代码
usr1     0x0000012c 0x00000dad activated   one shot

0x0000012c: 300个时钟周期,即处于rt_thread_mdelay(3000);

c 复制代码
main     0x00000032 0x00000d17 activated   one shot

0x00000032: 50个时钟周期,即处于rt_thread_mdelay(500);

4.增加软定时器

//usrtimer.c

c 复制代码
#include <rtthread.h>

/* 定时器的控制块 */
static struct rt_timer timer1;
static struct rt_timer timer2;
static int cnt = 0;

/* 定时器 1 超时函数 */
static void timeout1(void* parameter)
{
    rt_kprintf("periodic timer is timeout\n");
    /* 运行 10 次 */
    if (cnt++>= 9)
    {
        rt_timer_stop(&timer1);
    }
}

/* 定时器 2 超时函数 */
static void timeout2(void* parameter)
{
    rt_kprintf("one shot timer is timeout\n");
}

int timer_static_sample(void)
{
    /* 初始化定时器 */
    rt_timer_init(&timer1, 
                    "timer1",  /* 定时器名字是 timer1 */
                    timeout1, /* 超时时回调的处理函数 */
                    RT_NULL, /* 超时函数的入口参数 */
                    100, /* 定时长度,以 OS Tick 为单位,即 10 个 OS Tick */
                    RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
    rt_timer_init(&timer2, 
                    "timer2",   /* 定时器名字是 timer2 */
                    timeout2, /* 超时时回调的处理函数 */
                    RT_NULL, /* 超时函数的入口参数 */
                    300, /* 定时长度为 30 个 OS Tick */
                    RT_TIMER_FLAG_ONE_SHOT); /* 单次定时器 */
  

    /* 启动定时器 */
    rt_timer_start(&timer1);
    rt_timer_start(&timer2);
    return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(timer_static_sample, timer_static sample);
5. MSH命令开启软件定时器
bash 复制代码
msh > timer_static_sample

timer_static_sample()会执行,timer1和timer2会开始工作。输入list_timer,发现多了timer1、timer2

相关推荐
范纹杉想快点毕业1 天前
ZYNQ PS 端 UART 接收数据数据帧(初学者友好版)嵌入式编程 C语言 c++ 软件开发
c语言·笔记·stm32·单片机·嵌入式硬件·mcu·51单片机
电子科技圈2 天前
芯科科技FG23L无线SoC现已全面供货,为Sub-GHz物联网应用提供最佳性价比
科技·嵌入式硬件·mcu·物联网·制造·智能硬件·交通物流
意法半导体STM322 天前
运行 Ux_Host_HUB_HID_MSC 通过 Hub 连接 U 盘读写不稳定问题分析 LAT1511
mcu·usb·st·hub·usb_otg_hs·filex·u 盘
路弥行至4 天前
从0°到180°,STM32玩转MG996R舵机
c语言·数据库·stm32·单片机·嵌入式硬件·mcu·mongodb
索迪迈科技5 天前
基于野火F407开发板实现电源管理-停止模式
c语言·stm32·单片机·嵌入式硬件·mcu
方案开发PCBA抄板芯片解密5 天前
MCU、CPLD、DSP、FPGA 有什么区别,该如何选择?
mcu
蜀黍@猿5 天前
【GD32】存储器架构介绍
单片机·mcu
伴杯猫5 天前
【ESP32-IDF】基础外设开发2:系统中断矩阵
c语言·单片机·嵌入式硬件·mcu·物联网·github
【ql君】qlexcel6 天前
MCU上电到运行的全过程
单片机·嵌入式硬件·mcu·启动过程
今日待办8 天前
Arduino Nano33 BLESense Rev2【室内空气质量检测语音识别蓝牙调光台灯】
c语言·单片机·嵌入式硬件·mcu·语音识别·ardunio·arduinonano33