定时器的高级用法

全局变量一共用4次 声明+创建+开始+结束

time_ops_type   timer =
{
    .creat = timer_register_isr,
    .stop = timer_stop_time , 
    .start = timer_start_time,
};

然后先看简单的

uint8_t timer_stop_time(uint8_t handle)
{
    time_type *priv = time;
    
    while( priv != NULL )
    {
        if( priv->handle == handle)
        {
            priv->start = false;
            priv->cnt = 0;
            return true;
        }
        priv = priv->next;     
    }  
    
    return false;
}

uint8_t timer_start_time(uint8_t handle)
{
    time_type *priv = time;
    
    while( priv != NULL )
    {
        if( priv->handle == handle)
        {
            priv->start = true;
            return true;
        }
        priv = priv->next;     
    }  
    
    return false;
}

基本是一样的 开始和结束 就是在链表里面去找这个handle 找到以后就设置它true/false

看创建

uint8_t timer_register_isr(  uint32_t time_out ,uint8_t start, time_call_back call_back)
{
    time_type *priv;
    time_type    *this;

    this = (time_type *)timer_malloc(sizeof(time_type));
    if( this != NULL)
    {

        this->cnt = 0;
        this->start = start;
        this->handle = timerTaskId++;
        this->time_out = time_out;
        this->fun = call_back;
        this->next = NULL;
        if( time == NULL)
        {
             time = this;
        }
        else
        {
            priv = time;
            while( priv->next != NULL )    priv = priv->next;
            priv->next = this;
        }    
    }
    else
    {
        return 0xFF;
    }


    return (this->handle);

}
这是一个链表 并且每次都是尾巴插入 做成一个链表呀

全局变量 //osal time queue
time_type            *time = NULL;

是头 后面都是尾巴插入

最后怎么用呢?

链表做好了 时驱函数一样的 需要一个后台的程序在定时器不停的扫描

start为1就是开启的 为0就是关闭的

handle就是编号

cnt tinmeout 是变量和常量的比较

next指针

还有一个回调函数

猜你喜欢

转载自blog.csdn.net/weixin_42381351/article/details/81166744