感谢园长,感谢安信可,第一次中奖,诚惶诚恐
以下是我点灯代码
#include "bflb_mtimer.h"
#include "bflb_pwm_v2.h"
#include "bflb_clock.h"
#include "bflb_gpio.h"
#include "board.h"
struct bflb_device_s *pwm;
struct bflb_device_s *gpio;
int main(void)
{
int i;
board_init();
pwm = bflb_device_get_by_name("pwm_v2_0");
gpio = bflb_device_get_by_name("gpio");
/*初始化RGB灯,IO15、12、14,对应蓝灯、红灯、绿灯*/
bflb_gpio_init(gpio, GPIO_PIN_15, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_init(gpio, GPIO_PIN_12, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_init(gpio, GPIO_PIN_14, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
/* period = .XCLK / .clk_div / .period = 40MHz / 40 / 1000 = 1KHz */
struct bflb_pwm_v2_config_s cfg = {
.clk_source = BFLB_SYSTEM_XCLK,
.clk_div = 40,
.period = 1000,
};
/*初始化PWM输出*/
bflb_pwm_v2_init(pwm, &cfg);
bflb_pwm_v2_start(pwm);
/*功能实现,红、绿、蓝灯依次呼吸循环亮灭*/
while (1) {
//红灯呼吸亮灭
bflb_pwm_v2_channel_positive_start(pwm, PWM_CH0);
for(i=100;i>0;i--)
{
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, i, 100);
bflb_mtimer_delay_ms(10);
}
for(i=1;i<100;i++)
{
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, i, 100);
bflb_mtimer_delay_ms(10);
}
bflb_pwm_v2_channel_positive_stop(pwm, PWM_CH0);
//绿灯呼吸亮灭
bflb_pwm_v2_channel_positive_start(pwm, PWM_CH2);
for(i=100;i>0;i--)
{
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH2, i, 100);
bflb_mtimer_delay_ms(10);
}
for(i=1;i<100;i++)
{
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH2, i, 100);
bflb_mtimer_delay_ms(10);
}
bflb_pwm_v2_channel_positive_stop(pwm, PWM_CH2);
//蓝灯循环亮灭
bflb_pwm_v2_channel_positive_start(pwm, PWM_CH3);
for(i=100;i>0;i--)
{
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH3, i, 100);
bflb_mtimer_delay_ms(10);
}
for(i=1;i<100;i++)
{
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH3, i, 100);
bflb_mtimer_delay_ms(10);
}
bflb_pwm_v2_channel_positive_stop(pwm, PWM_CH3);
}
}
环境我很久就按教程搭建好了
就不单独再次搭建了
本次代码使用AiPi-Eyes-DU代码中的 PWM 调光代码
以下是我效果图
额外说一些我踩坑的地方,
第一,注释代码
AiPi-Open-Kits/aithinker_Ai-M6X_SDK/bsp/board/bl616dk/board.c
该文件中,第11行
#include "lwip/dns.h"
![image.png image.png](data/attachment/forum/202405/05/155422q9zfo0v2um9ouxj3.png)
第212行
ip_addr_t dns_addr;
![image.png image.png](data/attachment/forum/202405/05/155457dei42q9rw0fq292l.png)
第二步,如果串口烧录失败,需要手动给权限,代码
sudo chmod 777 /dev/ttyUSB0
接下来就欧克了
显示效果
![b2aad64626c19230a60cdffdf2256b3.jpg b2aad64626c19230a60cdffdf2256b3.jpg](data/attachment/forum/202405/05/155906f6hzmy62xfk7yjts.jpg)
![59de3b9266fafe4a1536aeeea5b02a4.jpg 59de3b9266fafe4a1536aeeea5b02a4.jpg](data/attachment/forum/202405/05/155921bul9og6b5ww6bren.jpg)
![63eab1ccbe9bf0fb104499c473164be.jpg 63eab1ccbe9bf0fb104499c473164be.jpg](data/attachment/forum/202405/05/155933vqzrrjap5ygaafrn.jpg)