RGB紫色呼吸灯
PWM又称脉冲宽度调制,是学习掌握单片机外设的基本功能之一,对于温度调节,电机控制,等场景中使用较多。星空派手册资料详细介绍了PWM的原理和使用方法,根据官方提供的例程做了简单修改通过两路PWM波控制调节出紫色呼吸灯效果和6种颜色的灯光进行循环显示。通过此实验进一步加深了对gd32单片机PWM功能的学习。
首先利用单片机的PA8和PB0两个IO口进行复用成定时器PWM模式,通过两组PWM波同时工作可以兑出RGB灯的任意颜色,在通过官方提供的库进行基础配置,最后设置定时器的频率和占空比,在主函数里进行对占空比这个变量的调节,就可以实现呼吸灯的效果,此实验较为简单根据官方提供代码简单修改就可以实现本次效果。
- #define __tiMER_PWM_C__
- #include
- /**
- 设置引脚为PWM模式
- */
- void gpio_pwm_config(void)
- {
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_GPIOB);
- gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
- gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
- }
- /**
- brief configure the TIMER peripheral
- param[in] none
- param[out] none
- retval none
- */
- //120MHz
- void timer_pwm_config(void)
- {
- /* TIMER0 configuration: generate PWM signals with different duty cycles:
- TIMER0CLK = SystemCoreClock / 120 = 1MHz */
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
- gpio_pwm_config();
- rcu_periph_clock_enable(RCU_TIMER2);
- rcu_periph_clock_enable(RCU_TIMER0);
- rcu_periph_clock_enable(RCU_AF);
- timer_deinit(TIMER0);
- timer_deinit(TIMER2);
- /* TIMER0 configuration */
- //预分频 119
- timer_initpara.prescaler = 119;
- //边沿触发
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- //装载值 500 CAR
- timer_initpara.period = 500;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER0,&timer_initpara);
- timer_init(TIMER2,&timer_initpara);
-
- /* CH0 configuration in PWM mode */
- //使能通道输出
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- //不需要通道互补
- timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
- //通道输出极性 为高电平
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- //通道互补输出极性 高电平
- timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
- //通道输出空闲状态 低电平
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
- //通道互补输出空闲状态 低电平
- timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
-
- timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);
- timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
- //配置TIMER通道输出脉冲值 也就是 CHX_CV值
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,250);
- timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,250);
- //设置定时器通道0输出为PWM模式
- timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
-
- timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
- timer_primary_output_config(TIMER0,ENABLE);
- timer_primary_output_config(TIMER2,ENABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER2);
- timer_auto_reload_shadow_enable(TIMER0);
- timer_enable(TIMER2);
- timer_enable(TIMER0);
- }
#include "gd32f30x.h"
- #include "gd32f303e_eval.h"
- #include "systick.h"
- #include "timer_pwm.h"
- int main(void)
- {
- int16_t i = 0;
- FlagStatus breathe_flag = SET;
-
- /* configure the TIMER peripheral */
- timer_pwm_config();
-
- /* configure systick */
- systick_config();
- while (1){
- /* delay a time in milliseconds */
- delay_1ms(40);
- if (SET == breathe_flag){
- i = i + 10;
- }else{
- i = i - 10;
- }
- if(500 < i){
- breathe_flag = RESET;
-
- }
- if(0 >= i){
- breathe_flag = SET;
- }
- //配置TIMER通道输出脉冲值 也就是 CHX_CV值
- timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,i);
- timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,i);
- }
- }
复制代码
RGB七彩灯
|