基于PWM外设轮询模式编写呼吸灯程序
免费
成为会员,免费下载资料
文件大小:225.49 KB
上传者:虽万人吾往矣
时间:2022-12-02 08:52:17
下载量:0
软件实现
软件代码见 examples/pwm/pwm_breath_led
#define BSP_PWM_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK#define BSP_PWM_CLOCK_DIV 1
配置 PWM 设备时钟源,见 bsp/board/bl706_iot/clock_config . h
#define CONFIG_GPIO22_FUNC GPIO_FUN_PWM
配置 PWM 设备复用引脚,见 bsp/board/bl706_iot/pinmux_config . h
#define BSP_USING_PWM_CH2
#if defined ( BSP_USING_PWM_CH2)#ifndef PWM_CH2_CONFIG#define PWM_CH2_CONFIG \{ \ . ch = 2,
\ . frequency = 1000000, \ . dutycycle = 0, \ . it_pulse_count = 0, \}#endif#endif
使能 BSP_USING_PWM_CH2 并配置 PWM 设备配置,见 bsp/board/bl706_iot/peripheral_config . h
pwm_register( PWM_CH2_INDEX, " led_breath " ) ;
struct device *led_breath = device_find ( " led_breath " ) ;
if ( led_breath ) {
PWM_DEV( led_breath ) - >period = 32; //frequence = 32M/1/32 = 1Mhz
PWM_DEV( led_breath ) - >threshold_low = 16;
PWM_DEV( led_breath ) - >threshold_high = 32;
device_open ( led_breath, DEVICE_OF LAG_STREAM_TX) ;
pwm_channel_start ( led_breath ) ; }
首先调用 pwm_register 函数注册 PWM 设备的一个通道,当前注册 PWM_CH2
然后通过 find 函数找到设备对应的句柄,保存于 led_breath 句柄中
设置 PWM_CH2 的频率为 1Mhz,占空比为 50%
使用 device_open 以轮询模式来打开 led_breath 设备
for ( pwm_cfg . threshold_high = 0; pwm_cfg . threshold_high <= 32; pwm_cfg . threshold_high++) {
device_control ( led_breath, DEVICE_CTRL_PWM_DUTYCYCLE_CONFIG, &pwm_cfg) ;
bflb_platform_delay_ms ( 50) ;
}
for ( pwm_cfg . threshold_high = 32; 0 <= pwm_cfg . threshold_high && pwm_cfg . threshold_high <= 32;
pwm_cfg . threshold_high - - ) {
device_control ( led_breath, DEVICE_CTRL_PWM_DUTYCYCLE_CONFIG, &pwm_cfg) ;
bflb_platform_delay_ms ( 50) ;
}
使用 device_contorl 函数,配合 DEVICE_CTRL_PWM_DUTYCYCLE_CONFIG 指令,可以修改当前 PWM 通道的占空
比。
详情请下载~~~
展开》
折叠》