main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. \file main.c
  3. \brief TIMER1 PWM output 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. /*Configure PB3 PB10 PB11(TIMER1 CH1 CH2 CH3) as alternate function*/
  23. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
  24. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);
  25. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
  26. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  27. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
  28. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
  29. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_3);
  30. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_10);
  31. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_11);
  32. }
  33. /*!
  34. \brief configure the TIMER peripheral
  35. \param[in] none
  36. \param[out] none
  37. \retval none
  38. */
  39. void timer_config(void)
  40. {
  41. /* -----------------------------------------------------------------------
  42. TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
  43. TIMER1CLK is 1MHz
  44. TIMER1 channel1 duty cycle = (4000/ 16000)* 100 = 25%
  45. TIMER1 channel2 duty cycle = (8000/ 16000)* 100 = 50%
  46. TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
  47. ----------------------------------------------------------------------- */
  48. timer_oc_parameter_struct timer_ocintpara;
  49. timer_parameter_struct timer_initpara;
  50. rcu_periph_clock_enable(RCU_TIMER1);
  51. timer_deinit(TIMER1);
  52. /* TIMER1 configuration */
  53. #ifdef GD32F330
  54. timer_initpara.prescaler = 83;
  55. #endif /* GD32F330 */
  56. #ifdef GD32F350
  57. timer_initpara.prescaler = 107;
  58. #endif /* GD32F350 */
  59. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  60. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  61. timer_initpara.period = 15999;
  62. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  63. timer_initpara.repetitioncounter = 0;
  64. timer_init(TIMER1,&timer_initpara);
  65. /* CH1,CH2 and CH3 configuration in PWM mode0 */
  66. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  67. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  68. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
  69. timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
  70. timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);
  71. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,3999);
  72. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  73. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  74. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,7999);
  75. timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  76. timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  77. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,11999);
  78. timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
  79. timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
  80. /* auto-reload preload enable */
  81. timer_auto_reload_shadow_enable(TIMER1);
  82. /* auto-reload preload enable */
  83. timer_enable(TIMER1);
  84. }
  85. /*!
  86. \brief main function
  87. \param[in] none
  88. \param[out] none
  89. \retval none
  90. */
  91. int main(void)
  92. {
  93. rcu_periph_clock_enable(RCU_GPIOB);
  94. gpio_config();
  95. timer_config();
  96. while (1);
  97. }