"); //-->
/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-11-30 misonyo first implementation. */ /* * 程序清单:这是一个 hwtimer 设备使用例程 * 例程导出了 hwtimer_sample 命令到控制终端 * 命令调用格式:hwtimer_sample * 程序功能:基于L476 nucleo 计算按键持续时间 非常方便*/ * 主要注意 超时回调函数的作用就是 一个应用场合就是 比如超声波 传感器 设置1s 超时, 说明一直没有接收,来作为一个异常情况来处理*/ #include <rtthread.h> #include <rtdevice.h> #include "drv_gpio.h" #define HWTIMER_DEV_NAME "timer16" /* 定时器名称 */ #define LED0_PIN GET_PIN(A, 5) #define KEY0_PIN_NUM 45 /* PC13 */ rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */ /* 定时器超时回调函数 */ static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size) { rt_kprintf("tick is :%d !\n", rt_tick_get()); int value = 0; value = rt_pin_read(LED0_PIN); rt_pin_write(LED0_PIN, !value); return 0; } void timer_on(void *args) { rt_err_t ret = RT_EOK; rt_hwtimerval_t timeout_s; /* 定时器超时值 */ rt_hwtimerval_t timeout_e; //rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */ rt_hwtimer_mode_t mode; rt_kprintf("turn on timer!\n"); rt_uint32_t value = 0; rt_thread_mdelay(20); value = rt_pin_read(KEY0_PIN_NUM); rt_kprintf("the key value is %d\r\n", value); if ( value == 0) { rt_kprintf("the key down start timer %d\r\n", value); /* 设置定时器超时值为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 failed\n"); return ; } rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s)); rt_kprintf("Start Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec); } else if (value == 1) { rt_kprintf("stop timer\r\n"); //rt_device_close(hw_dev); rt_device_control(hw_dev,HWTIMER_CTRL_STOP,RT_NULL); rt_device_read(hw_dev, 0,&timeout_e, sizeof(timeout_e)); rt_kprintf("Stop Read:Sec = %d, Usec = %d\n", timeout_e.sec, timeout_e.usec); } // rt_pin_write(BEEP_PIN_NUM, PIN_HIGH); } static int hwtimer_sample(int argc, char *argv[]) { rt_err_t ret = RT_EOK; rt_hwtimerval_t timeout_s; /* 定时器超时值 */ // rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */ rt_hwtimer_mode_t mode; /* 定时器模式 */ /* 查找定时器设备 */ 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); /* 按键0引脚为输入模式 */ rt_pin_mode(KEY0_PIN_NUM, PIN_MODE_INPUT_PULLUP); /* 绑定中断,下降沿模式,回调函数名为beep_on */ rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_RISING_FALLING, timer_on, RT_NULL); /* 使能中断 */ rt_pin_irq_enable(KEY0_PIN_NUM, PIN_IRQ_ENABLE); /* 设置模式为周期性定时器 */ mode = HWTIMER_MODE_ONESHOT;//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 = 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 failed\n"); // 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 = %d\n", timeout_s.sec, timeout_s.usec); return ret; } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。