使用定時器翻轉(zhuǎn)LED電平
使能TIM1
使用硬件定時器,使能定時器后,需要主動添加構(gòu)建
cyhal_pwm.c/cyhal_tcpwm_common.c/cy_tcpwm_counter.c 三個文件否則會出現(xiàn)函數(shù)未定義錯誤
#define LED_PIN1 GET_PIN(0, 0)
#define LED_PIN2 GET_PIN(0, 1)
#define HWTIMER_DEV_NAME "time1" /* 定時器名稱 /
rt_device_t hw_dev; / 定時器設(shè)備句柄 /
rt_hwtimer_mode_t mode; / 定時器模式 /
rt_uint32_t freq = 10000; / 計數(shù)頻率 /
/ 定時器超時回調(diào)函數(shù) /
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; / 定時器設(shè)備句柄 /
rt_hwtimer_mode_t mode; / 定時器模式 /
rt_uint32_t freq = 10000; / 計數(shù)頻率 /
/ 初始化LED引腳*/
rt_pin_mode(LED_PIN1, PIN_MODE_OUTPUT);
rt_pin_mode(LED_PIN2, PIN_MODE_OUTPUT);
/* 查找定時器設(shè)備 /
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;
}
/ 以讀寫方式打開設(shè)備 /
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;
}
/ 設(shè)置超時回調(diào)函數(shù) /
rt_device_set_rx_indicate(hw_dev, timeout_cb);
/ 設(shè)置計數(shù)頻率(若未設(shè)置該項,默認為1Mhz 或 支持的最小計數(shù)頻率) /
rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
/ 設(shè)置模式為周期性定時器(若未設(shè)置,默認是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;
}
/ 設(shè)置定時器超時值為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);
/ 讀取定時器當(dāng)前值 */
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上實現(xiàn)呼吸燈效果
默認的pwm0只有通道3,沒有通道0,需要通過修改Kconfig文件來實現(xiàn)通道0
修改KCONFIG文件,增加PWM0通道0
PWM0通道0對應(yīng)LED1
使能pwm0 CH0
在drv_pwm.h中添加通道信息
#define LED_PIN GET_PIN(0, 0)
#define PWM_DEV_NAME "pwm0" /* PWM設(shè)備名稱 /
#define PWM_DEV_CHANNEL 0 / PWM通道 */
struct rt_device_pwm pwm_dev; / PWM設(shè)備句柄 /
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 /
/ 查找設(shè)備 */
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;
}
/ 設(shè)置PWM周期和脈沖寬度默認值 /
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
/ 使能設(shè)備 /
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;
}
/ 設(shè)置PWM周期和脈沖寬度 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
}
}
-
LED電平
+關(guān)注
關(guān)注
0文章
5瀏覽量
6200 -
PWM波
+關(guān)注
關(guān)注
0文章
99瀏覽量
16903 -
定時器中斷
+關(guān)注
關(guān)注
0文章
49瀏覽量
11232 -
呼吸燈
+關(guān)注
關(guān)注
10文章
110瀏覽量
42769 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1294瀏覽量
40230
發(fā)布評論請先 登錄
相關(guān)推薦
評論