打印

GD32E230 PWM配置

[复制链接]
499|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
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

使用特权

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

本版积分规则

1510

主题

4543

帖子

6

粉丝