【GD32L233C-START评测】7. PWM驱动LED
[复制链接]
之前的帖子可以参考:
【GD32L233C-START评测】1.开箱
【GD32L233C-START评测】2.手把手创建新工程
【GD32L233C-START评测】3.移植FreeRTOS到GD32L233
【GD32L233C-START评测】4. 移植RT-Thread到GD32L233
【GD32L233C-START评测】5. IIC驱动OLED
【GD32L233C-START评测】6. 获取RTC时间并通过OLED显示
PWM在工程中应用比较广泛,比如LED调光明暗,电机调速,舵机控制等。
本文讲解如何使用PWM来驱动LED3和LED4。
一、LED接线图
LED原理图如下所示:
LED3使用的引脚为PC6,LED4使用的引脚为PC7。
二、引脚复用关系
查看数据手册中PC6和PC7的复用关系,
PC6复用TIMER2的CH0,PC7复用TIMER2的CH1,复用为AF1。
如下图所示:
了解到上面信息之后,可以开始进行代码编写了。
三、代码编辑
1. 引脚初始化
void gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOC);
/*Configure PC7(TIMER2_CH1) as alternate function*/
gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7|GPIO_PIN_6);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_6);
gpio_af_set(GPIOC, GPIO_AF_1, GPIO_PIN_7|GPIO_PIN_6);
}
2. Timer初始化
void timer_config(void)
{
/* TIMER2 configuration: generate PWM signals with different duty cycles:
TIMER2CLK = SystemCoreClock / 64 = 1MHz */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
/* TIMER2 configuration */
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 63;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = PWM_PERIOD;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER2, &timer_initpara);
/* CH0 configuration in PWM mode - PC6 */
timer_channel_output_struct_para_init(&timer_ocintpara);
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_channel_output_config(TIMER2, TIMER_CH_0, &timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, (uint32_t)(PWM_PERIOD>>1));
timer_channel_output_mode_config(TIMER2, TIMER_CH_0, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER2, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
/* CH1 configuration in PWM mode - PC7 */
timer_channel_output_struct_para_init(&timer_ocintpara);
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_channel_output_config(TIMER2, TIMER_CH_1, &timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, (uint32_t)(PWM_PERIOD>>1));
timer_channel_output_mode_config(TIMER2, TIMER_CH_1, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER2, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER2);
/* TIMER enable */
timer_enable(TIMER2);
}
3.LED3和LED4 PWM驱动
void led3_pwm_ctrl(void)
{
static int16_t i = 0;
static uint8_t breath_flg = 0;
i = (breath_flg == 0)?(i+20):(i-20);
if(PWM_PERIOD < i)
{
breath_flg = 1;
}
else if(0 >= i)
{
breath_flg = 0;
}
/* configure TIMER channel output pulse value */
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, i);
}
void led4_pwm_ctrl(void)
{
static int16_t i = 1000;
static uint8_t breath_flg = 1;
i = (breath_flg == 0)?(i+20):(i-20);
if(PWM_PERIOD < i)
{
breath_flg = 1;
}
else if(0 >= i)
{
breath_flg = 0;
}
/* configure TIMER channel output pulse value */
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, i);
}
4.主函数
int main(void)
{
/* configure systick */
systick_config();
/* configure the GPIO ports */
gpio_config();
/* configure the TIMER peripheral */
timer_config();
while(1)
{
/* delay a time in milliseconds */
delay_1ms(40);
led3_pwm_ctrl();
led4_pwm_ctrl();
}
}
四、效果展示
LED3由暗到明再到暗周期显示
LED4由明到暗再到明周期显示
|