main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*!
  2. \file main.c
  3. \brief TIMER0 complementary signals demo
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include <stdio.h>
  11. #include "gd32f3x0_eval.h"
  12. void gpio_config(void);
  13. void timer_config(void);
  14. /*!
  15. \brief configure the GPIO ports
  16. \param[in] none
  17. \param[out] none
  18. \retval none
  19. */
  20. void gpio_config(void)
  21. {
  22. rcu_periph_clock_enable(RCU_GPIOA);
  23. rcu_periph_clock_enable(RCU_GPIOB);
  24. /*Configure PA8 PA9 PA10(TIMER0 CH0 CH1 CH2) as alternate function*/
  25. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
  26. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  27. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
  28. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  29. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
  30. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  31. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_8);
  32. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_9);
  33. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_10);
  34. /*Configure PB13 PB14 PB15(TIMER0 CH0N CH1N CH2N) as alternate function*/
  35. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
  36. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);
  37. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_14);
  38. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_14);
  39. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_15);
  40. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_15);
  41. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_13);
  42. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_14);
  43. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_15);
  44. }
  45. /*!
  46. \brief configure the TIMER peripheral
  47. \param[in] none
  48. \param[out] none
  49. \retval none
  50. */
  51. void timer_config(void)
  52. {
  53. /* -----------------------------------------------------------------------
  54. TIMER0 configuration to:
  55. generate 3 complementary PWM signals with 3 different duty cycles:
  56. TIMER0CLK is fixed to systemcoreclock, the TIMER0 prescaler is equal
  57. to 4200(GD32F330) or 5399(GD32F350) so the TIMER0 counter clock used
  58. is 20KHz.
  59. the three duty cycles are computed as the following description:
  60. the channel 0 duty cycle is set to 25% so channel 1N is set to 75%.
  61. the channel 1 duty cycle is set to 50% so channel 2N is set to 50%.
  62. the channel 2 duty cycle is set to 75% so channel 3N is set to 25%.
  63. ----------------------------------------------------------------------- */
  64. timer_oc_parameter_struct timer_ocintpara;
  65. timer_parameter_struct timer_initpara;
  66. rcu_periph_clock_enable(RCU_TIMER0);
  67. timer_deinit(TIMER0);
  68. /* TIMER0 configuration */
  69. #ifdef GD32F330
  70. timer_initpara.prescaler = 4199;
  71. #endif /* GD32F330 */
  72. #ifdef GD32F330
  73. timer_initpara.prescaler = 5399;
  74. #endif /* GD32F330 */
  75. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  76. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  77. timer_initpara.period = 15999;
  78. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  79. timer_initpara.repetitioncounter = 0;
  80. timer_init(TIMER0,&timer_initpara);
  81. /* CH1,CH2 and CH3 configuration in PWM mode */
  82. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  83. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  84. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  85. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  86. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  87. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  88. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  89. timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
  90. timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
  91. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,3999);
  92. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  93. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  94. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,7999);
  95. timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  96. timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  97. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,11999);
  98. timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  99. timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  100. timer_primary_output_config(TIMER0,ENABLE);
  101. /* auto-reload preload enable */
  102. timer_auto_reload_shadow_enable(TIMER0);
  103. timer_enable(TIMER0);
  104. }
  105. /*!
  106. \brief main function
  107. \param[in] none
  108. \param[out] none
  109. \retval none
  110. */
  111. int main(void)
  112. {
  113. gpio_config();
  114. timer_config();
  115. while (1);
  116. }