打印

GD32E230 PWM配置

[复制链接]
1969|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jcky001|  楼主 | 2024-11-13 10:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
GD32E230 PWM配置
//GPIO配置
void PWM_gpio_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);

  gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
    gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_1);
  gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_9);
  gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_10);
}
void PWM_timer_config(void)
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;
    rcu_periph_clock_enable(RCU_TIMER0);
    timer_deinit(TIMER0);
    rcu_periph_clock_enable(RCU_TIMER2);
    timer_deinit(TIMER2);
    /* TIMER0 configuration */
    timer_initpara.prescaler         = 3;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 999;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER0,&timer_initpara);
  timer_init(TIMER2,&timer_initpara);
  /* CH1,CH2 and CH3 configuration in PWM mode1 */
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_HIGH;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER2,TIMER_CH_3,&timer_ocintpara);
    timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
    timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
    /* CH1 configuration in PWM mode1,duty cycle 25% */
    timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3,500);
    timer_channel_output_mode_config(TIMER2,TIMER_CH_3,TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,500);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,500);
    timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
    timer_primary_output_config(TIMER0,ENABLE);
    /* auto-reload preload enable */
    timer_enable(TIMER0);
    timer_primary_output_config(TIMER2,ENABLE);
    /* auto-reload preload enable */
    timer_enable(TIMER2);
}
int main(void)
{
//PWM Init  Motor
PWM_gpio_config();
PWM_timer_config();
while(1)
{
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3,(uint16)rollPhase[2]);
  timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,(uint16)rollPhase[0]);
  timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,(uint16)rollPhase[1]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
以上例程经测试OK,希望能帮到你!!
————————————————

                        
                        
原文链接:https://blog.csdn.net/u014694105/article/details/105976954

使用特权

评论回复
沙发
tpgf| | 2024-12-2 15:40 | 只看该作者
#include "gd32e230.h"

void PWM_Init(void) {
    // 使能GPIOB和TIMER0的时钟
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_TIMER0);

    // 配置GPIOB的第0引脚为复用功能,映射到定时器0的通道0
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
    gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_0);

    // 配置定时器0的基本参数
    timer_parameter_struct timer_initpara;
    timer_oc_parameter_struct timer_ocintpara;
    timer_deinit(TIMER0);
    rcu_periph_clock_enable(RCU_TIMER0);

    timer_initpara.prescaler = 71; // 预分频系数
    timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = 999; // 自动重装载值
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_init(TIMER0, &timer_initpara);

    // 配置定时器0的通道0为PWM模式
    timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, 500); // 设置占空比为50%
    timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);

    // 启动定时器0
    timer_enable(TIMER0);
}

使用特权

评论回复
板凳
heimaojingzhang| | 2024-12-3 09:40 | 只看该作者
要配置GD32E230的PWM输出,首先需要初始化相关的GPIO引脚为复用功能,并将其映射到定时器的通道上

使用特权

评论回复
地板
keaibukelian| | 2024-12-3 13:43 | 只看该作者
配置定时器的基本参数,包括时钟源、工作模式等,并选择PWM输出模式

使用特权

评论回复
5
paotangsan| | 2024-12-3 18:20 | 只看该作者
根据需要设置PWM信号的占空比和周期

使用特权

评论回复
6
renzheshengui| | 2024-12-3 20:03 | 只看该作者
需要根据具体的需求调整定时器的预分频系数、自动重装载值以及PWM信号的占空比等参数

使用特权

评论回复
7
wowu| | 2024-12-3 21:59 | 只看该作者
从代码层次上来讲 二楼的代码应该更好一点

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1522

主题

4615

帖子

6

粉丝