main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*!
  2. \file main.c
  3. \brief TIMER0 deadtime break 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(TIMER0 CH0) 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_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_8);
  28. /*configure PB13(TIMER0 CH0N) as alternate function*/
  29. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
  30. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);
  31. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_13);
  32. /*configure PB12(TIMER0 BKIN) as alternate function*/
  33. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12);
  34. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);
  35. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_12);
  36. }
  37. /*!
  38. \brief configure the TIMER peripheral
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. void timer_config(void)
  44. {
  45. /* -----------------------------------------------------------------------
  46. TIMER0 configuration:
  47. generate 1 complementary PWM signal.
  48. TIMER0CLK is fixed to systemcoreclock, the TIMER0 prescaler is equal to 84(GD32F330)
  49. or 107(GD32F350) so the TIMER0 counter clock used is 1MHz.
  50. the duty cycle is computed as the following description:
  51. the channel 0 duty cycle is set to 25% so channel 0N is set to 75%.
  52. insert a dead time equal to 48us(GD32F330) or 37.3us(GD32F350)
  53. configure the break feature, active at high level, and using the automatic
  54. output enable feature.
  55. use the locking parameters level1.
  56. ----------------------------------------------------------------------- */
  57. timer_oc_parameter_struct timer_ocintpara;
  58. timer_parameter_struct timer_initpara;
  59. timer_break_parameter_struct timer_breakpara;
  60. rcu_periph_clock_enable(RCU_TIMER0);
  61. timer_deinit(TIMER0);
  62. /* TIMER0 configuration */
  63. #ifdef GD32F330
  64. timer_initpara.prescaler = 83;
  65. #endif /* GD32F330 */
  66. #ifdef GD32F350
  67. timer_initpara.prescaler = 107;
  68. #endif /* GD32F350 */
  69. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  70. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  71. timer_initpara.period = 1599;
  72. timer_initpara.clockdivision = TIMER_CKDIV_DIV4;
  73. timer_initpara.repetitioncounter = 0;
  74. timer_init(TIMER0,&timer_initpara);
  75. /* CH0/CH0N configuration in PWM mode0 */
  76. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  77. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  78. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  79. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  80. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
  81. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  82. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  83. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,399);
  84. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  85. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  86. /* automatic output enable, break, dead time and lock configuration*/
  87. timer_breakpara.runoffstate = TIMER_ROS_STATE_DISABLE;
  88. timer_breakpara.ideloffstate = TIMER_IOS_STATE_DISABLE ;
  89. timer_breakpara.deadtime = 255;
  90. timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
  91. timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE;
  92. timer_breakpara.protectmode = TIMER_CCHP_PROT_0;
  93. timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
  94. timer_break_config(TIMER0,&timer_breakpara);
  95. /* TIMER0 primary output function enable */
  96. timer_primary_output_config(TIMER0,ENABLE);
  97. /* auto-reload preload enable */
  98. timer_auto_reload_shadow_enable(TIMER0);
  99. /* TIMER0 counter enable */
  100. timer_enable(TIMER0);
  101. }
  102. /*!
  103. \brief main function
  104. \param[in] none
  105. \param[out] none
  106. \retval none
  107. */
  108. int main(void)
  109. {
  110. gpio_config();
  111. timer_config();
  112. while (1);
  113. }