2040|0

282

帖子

2

TA的资源

一粒金砂(高级)

楼主
 

【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由明到暗再到明周期显示

 

此帖出自GD32 MCU论坛
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
【干货上新】电源解决方案和技术第二趴 | DigiKey 应用探索站
当月好物、电源技术资源、特色活动、DigiKey在线实用工具,干货多多~

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 7

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表