电子说
定时器
使用定时器翻转LED电平
使能TIM1
使用硬件定时器,使能定时器后,需要主动添加构建
cyhal_pwm.c/cyhal_tcpwm_common.c/cy_tcpwm_counter.c 三个文件否则会出现函数未定义错误
#define LED_PIN1 GET_PIN(0, 0)
#define LED_PIN2 GET_PIN(0, 1)
#define HWTIMER_DEV_NAME "time1" /* 定时器名称 /
rt_device_t hw_dev; / 定时器设备句柄 /
rt_hwtimer_mode_t mode; / 定时器模式 /
rt_uint32_t freq = 10000; / 计数频率 /
/ 定时器超时回调函数 /
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
static uint8_t state = 0;
if (state == 0)
{
state = 1;
rt_pin_write(LED_PIN1, PIN_HIGH);
rt_pin_write(LED_PIN2, PIN_LOW);
}
else {
state = 0;
rt_pin_write(LED_PIN2, PIN_HIGH);
rt_pin_write(LED_PIN1, PIN_LOW);
}
rt_kprintf("this is hwtimer timeout callback fucntion!n");
rt_kprintf("tick is :%d !n", rt_tick_get());
return 0;
}
int main(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; / 计数频率 /
/ 初始化LED引脚*/
rt_pin_mode(LED_PIN1, PIN_MODE_OUTPUT);
rt_pin_mode(LED_PIN2, PIN_MODE_OUTPUT);
/* 查找定时器设备 /
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 = 0; / 秒 /
timeout_s.usec = 100000; / 微秒 /
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);
for(;;)
return 0;
}
PWM
在led0上实现呼吸灯效果
默认的pwm0只有通道3,没有通道0,需要通过修改Kconfig文件来实现通道0
修改KCONFIG文件,增加PWM0通道0
PWM0通道0对应LED1
使能pwm0 CH0
在drv_pwm.h中添加通道信息
#define LED_PIN GET_PIN(0, 0)
#define PWM_DEV_NAME "pwm0" /* PWM设备名称 /
#define PWM_DEV_CHANNEL 0 / PWM通道 */
struct rt_device_pwm pwm_dev; / PWM设备句柄 /
int main(void)
{
// rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
rt_uint32_t period, pulse, dir;
period = 500000; / 周期为0.5ms,单位为纳秒ns /
dir = 1; / PWM脉冲宽度值的增减方向 /
pulse = 0; / PWM脉冲宽度值,单位为纳秒ns /
/ 查找设备 */
pwm_dev = (struct rt_device_pwm )rt_device_find(PWM_DEV_NAME);
if (pwm_dev == RT_NULL)
{
rt_kprintf("pwm sample run failed! can't find %s device!n", PWM_DEV_NAME);
return RT_ERROR;
}
/ 设置PWM周期和脉冲宽度默认值 /
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
/ 使能设备 /
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
while (1)
{
rt_thread_mdelay(50);
if (dir)
{
pulse += 12500; / 从0值开始每次增加5000ns /
}
else
{
pulse -= 12500; / 从最大值开始每次减少5000ns /
}
if (pulse >= period)
{
dir = 0;
}
if (0 == pulse)
{
dir = 1;
}
/ 设置PWM周期和脉冲宽度 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
}
}
全部0条评论
快来发表一下你的评论吧 !