本帖最后由 leeyangy 于 2023-11-22 14:39 编辑
简单了拍几张
点灯过程 -- 先上图
1. 编译PWM循环呼吸灯
代码过程参考自本论坛:参考教程
2. 环境搭建(参考论坛文档)3. 工程文件创建
# 在 aithinker_Ai-M6X_SDK/examples 新建一个文件夹my_pwm作为自己的工程项目(aithinker_Ai-M6X_SDK/examples/my_pwm)
# 拷贝以下aithinker_Ai-M6X_SDK/examples/peripherals/pwm_v2/pwm_basic全部内容到自己的工程路径下(my_pwm)- cmake_minimum_required(VERSION 3.15)
-
- include(proj.conf)
-
- find_package(bouffalo_sdk REQUIRED HINTS $ENV{BL_SDK_BASE})
-
- sdk_set_main_file(main.c)
-
- project(my_pwm)
复制代码 - [cfg]
- # 0: no erase, 1:programmed section erase, 2: chip erase
- erase = 1
- # skip mode set first para is skip addr, second para is skip len, multi-segment region with ; separated
- skip_mode = 0x0, 0x0
- # 0: not use isp mode, #1: isp mode
- boot2_isp_mode = 0
-
- # [FW]
- # filedir = ./build/build_out/pwm*_$(CHIPNAME).bin
- # address = 0x000000
-
- [boot2]
- filedir = ./build/build_out/boot2_*.bin
- address = 0x000000
-
- [partition]
- filedir = ./build/build_out/partition*.bin
- address = 0xE000
-
- [FW]
- filedir = ./build/build_out/my_pwm_$(CHIPNAME).bin
- address = @partition
复制代码
4. PWM呼吸灯功能实现
- #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;
- #define PWM_PERIOD_MAX 255
- 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 = PWM_PERIOD_MAX,
- };
- /*初始化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 = 0; i < PWM_PERIOD_MAX; i++) {
- bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, 0, i);
- bflb_mtimer_delay_ms(5);
- }
- for (i = PWM_PERIOD_MAX; i > 0; i--) {
- bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, 0, i);
- bflb_mtimer_delay_ms(5);
- }
- bflb_pwm_v2_channel_positive_stop(pwm, PWM_CH0); //失能红灯通道
- //绿灯呼吸亮灭
- bflb_pwm_v2_channel_positive_start(pwm, PWM_CH2); //使能绿灯通道
- for (i = 0; i < PWM_PERIOD_MAX; i++) {
- bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH2, 0, i);
- bflb_mtimer_delay_ms(5);
- }
- for (i = PWM_PERIOD_MAX; i > 0; i--) {
- bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH2, 0, i);
- bflb_mtimer_delay_ms(5);
- }
- bflb_pwm_v2_channel_positive_stop(pwm, PWM_CH2); //失能绿灯通道
- //蓝灯循环亮灭
- bflb_pwm_v2_channel_positive_start(pwm, PWM_CH3); //使能蓝灯通道
- for (i = 0; i < PWM_PERIOD_MAX; i++) {
- bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH3, 0, i);
- bflb_mtimer_delay_ms(5);
- }
- for (i = PWM_PERIOD_MAX; i > 0; i--) {
- bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH3, 0, i);
- bflb_mtimer_delay_ms(5);
- }
- bflb_pwm_v2_channel_positive_stop(pwm, PWM_CH3); //失能蓝灯通道
- }
- }
复制代码
5. 编译和烧录
- # 编译,打开终端,cd 到当前项目下,执行 make
- make
- # Linux 烧录命令
- # make flash COMX=/dev/ttyUSB0
- make flash COMX=COM5
复制代码
6. 实现代码
my_pwm.zip
(2.29 KB, 下载次数: 1)
|