发帖
10 1 0

啃小安派代码-8-小安派 AiPi-Eyes-DU

ckdsx.cn
论坛元老

74

主题

172

回帖

5289

积分

论坛元老

积分
5289
小安派&M61基本系统教程 1594 10 2023-11-24 09:06:31
工程目录:AiPi-Open-Kits/ AiPi-Eyes-DU
文件目录:AiPi-Open-Kits/ AiPi-Eyes-DU / main.c



AiPi-Eyes_DU PWM呼吸灯一、使用指南
实现RGB三色灯循环呼吸亮灭
main 工程代码:
--------------------------------------------------
#include "bflb_mtimer.h"  //引入定时器库
#include "bflb_pwm_v2.h"  //引入PWM库
#include "bflb_clock.h"  //引入时钟库
#include "bflb_gpio.h"  //引入GPIO库
#include "board.h"  //引入开发板库

struct bflb_device_s *pwm;
struct bflb_device_s *gpio;
int main(void)  //主程序
{
    int i;  // 定义一个整型变量 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--)  //for循环100次 i--  等于  i = i-1
    {
        bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, i, 100);
        bflb_mtimer_delay_ms(10);  //延时10ms
    }
    for(i=1;i<100;i++)  //for循环100次  i++  等于  i = i+1
    {
        bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, i, 100);
        bflb_mtimer_delay_ms(10);//延时10ms
    }
    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);  //延时10ms
    }   
     for(i=1;i<100;i++)
    {
        bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH2, i, 100);
        bflb_mtimer_delay_ms(10);  //延时10ms
    }      
    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);  //延时10ms
    }  
     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);
    }

}


──── 1人觉得很赞 ────

使用道具 举报

2023-11-24 09:21:08
老师辛苦啦~
2023-11-24 09:30:52
2023-11-24 09:41:35
学习
2023-11-24 09:52:56
2023-11-24 10:26:16
学习了
2023-11-24 10:52:22
2023-11-24 21:03:24
学习
2023-11-26 19:30:10
打卡
2023-11-26 23:12:51
学习
您需要登录后才可以回帖 立即登录
高级模式
12下一页
统计信息
  • 会员数: 28296 个
  • 话题数: 40290 篇