3.1. PWM - Breathing LED
This demo is based on PWM polling mode.
3.1.1. Hardware Connection
This demo is based on BL706_IOT, and the connection method is as follows
GPIO function GPIO pin
----------------------------------
PWM_CH2 <--> GPIO22
3.1.2. Software Implementation
-See examples/pwm/pwm_breath_led
for the software code
1#define BSP_PWM_CLOCK_SOURCE ROOT_CLOCK_SOURCE_XCLK
2#define BSP_PWM_CLOCK_DIV 1
Configure the
PWM
device clock source, seebsp/board/bl706_iot/clock_config.h
1#define CONFIG_GPIO22_FUNC GPIO_FUN_PWM
Configure
PWM
device multiplexing pins, seebsp/board/bl706_iot/pinmux_config.h
1#define BSP_USING_PWM_CH2
2
3#if defined(BSP_USING_PWM_CH2)
4#ifndef PWM_CH2_CONFIG
5#define PWM_CH2_CONFIG \
6{ \
7 .ch = 2, \
8 .frequency = 1000000, \
9 .dutycycle = 0, \
10 .it_pulse_count = 0,\
11}
12#endif
13#endif
Enable
BSP_USING_PWM_CH2
and configurePWM
device configuration, seebsp/board/bl706_iot/peripheral_config.h
1pwm_register(PWM_CH2_INDEX, "led_breath", DEVICE_OFLAG_RDWR);
2
3struct device *led_breath = device_find("led_breath");
4
5if (led_breath) {
6 PWM_DEV(led_breath)->period = 32; //frequence = 32M/1/32 = 1Mhz
7 PWM_DEV(led_breath)->threshold_low = 16;
8 PWM_DEV(led_breath)->threshold_high = 32;
9 device_open(led_breath, DEVICE_OFLAG_STREAM_TX);
10 pwm_channel_start(led_breath);
11}
First call the
pwm_register
function to register a channel of thePWM
device, currently registerPWM_CH2
Then use the
find
function to find the handle corresponding to the device and save it in theled_breath
handleSet the frequency of
PWM_CH2
to 1Mhz, and the duty cycle to 50%Use
device_open
to open theled_breath
device in polling mode
1 for (pwm_cfg.threshold_high = 0; pwm_cfg.threshold_high <= 32; pwm_cfg.threshold_high++) {
2 device_control(led_breath, DEIVCE_CTRL_PWM_DUTYCYCLE_CONFIG, &pwm_cfg);
3 bflb_platform_delay_ms(50);
4 }
5
6 for (pwm_cfg.threshold_high = 32; 0 <= pwm_cfg.threshold_high && pwm_cfg.threshold_high <= 32; pwm_cfg.threshold_high--) {
7 device_control(led_breath, DEIVCE_CTRL_PWM_DUTYCYCLE_CONFIG, &pwm_cfg);
8 bflb_platform_delay_ms(50);
9 }
Use the
device_contorl
function with theDEIVCE_CTRL_PWM_DUTYCYCLE_CONFIG
instruction to modify the duty cycle of the current PWM channel.
3.1.3. Compile and Program
CDK compilation
Open project:pwm_breath_led.cdkproj
Refer to the steps of Guide to using CDK (like MDK Keil) under Windows to compile and download
Command compilation
1 $ cd <sdk_path>/bl_mcu_sdk
2 $ make BOARD=bl706_iot APP=pwm_breath_led
Program
3.1.4. Experimental Phenomena
pwm breath led!
Video display: