本帖最后由 st780206 于 2024-5-25 11:35 编辑
环境还是参考 Ubuntu22下Ai-M61-32S开发环境搭建 搭建开发环境
开发参考了 【Ai-M61-32S-Kit】【教程】PWM点呼吸灯
1.根据参考,找到关于PWM的样例程序
目录是aithinker_Ai-M6X_SDK/examples/peripherals/pwm_v2/pwm_basic
直接编译烧录后,两个独立的led灯会亮。RGB灯没有反应,不符合我们的预期。
打开main.c 准备修改
Ai-M61-32S板子 记录https://docs.ai-thinker.com/ai_m61中的Ai-M61-32S开发板原理图里设置的RGB端口号
分别是 15 12 14
查看main.c中有 board_pwm_gpio_init(),这个函数应该是pwm初始化的
找到此函数所在的文件 使用命令 grep board_pwm_gpio_init -r /home/aipi/AiPi-Open-Kits/ --include=*.c --include=*.h
找到好几个这个函数实现,
打开Makefile文件
BOARD ?= bl616dk,所以打开bl616dk/board.c文件
找到board_pwm_gpio_init()函数定义
函数中没有RGB灯用到的端口。注释掉函数中的所有端口,加入RGB用到的12 端口。
bflb_gpio_init(gpio, GPIO_PIN_12, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLDOWN | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_init(gpio, GPIO_PIN_14, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLDOWN | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_init(gpio, GPIO_PIN_15, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
编译烧录后,开发板上的RGB灯会长亮。
编辑main.c
可以根据需要修改struct bflb_pwm_v2_config_s cfg
修改main.c
bflb_pwm_v2_init(pwm, &cfg);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, 1 , 2); /* duty = (500-100)/1000 = 40% */
bflb_pwm_v2_channel_positive_start(pwm, PWM_CH0);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH2, 1 , 2); /* duty = (500-100)/1000 = 40% */
bflb_pwm_v2_channel_positive_start(pwm, PWM_CH2);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH3, 1 , 2); /* duty = (500-100)/1000 = 40% */
bflb_pwm_v2_channel_positive_start(pwm, PWM_CH3);
while (1) {
if (flag ) {
starti++;
} else {
starti--;
}
if(starti>=999 || starti <= 2){
flag = !flag;
}
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, 1 , starti); /* duty = (500-100)/1000 = 40% */
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH2, 1 , starti); /* duty = (500-100)/1000 = 40% */
bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH3, 1 , starti); /* duty = (500-100)/1000 = 40% */
//printf("pwm basic running\r\n");
//bflb_mtimer_delay_ms(2000);
bflb_mtimer_delay_ms(5);
}
编译烧写,RGB灯会呼吸闪烁,但绿色灯会比另两个颜色亮的多,有时间调一调。
|