main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*!
  2. \file main.c
  3. \brief PWM output by using comparator output
  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. uint16_t period = 65535;
  12. void rcu_config(void);
  13. void gpio_config(void);
  14. void timer_config(void);
  15. void cmp_config(void);
  16. /*!
  17. \brief main function
  18. \param[in] none
  19. \param[out] none
  20. \retval none
  21. */
  22. int main(void)
  23. {
  24. /* configure RCU */
  25. rcu_config();
  26. /* configure GPIO */
  27. gpio_config();
  28. /* configure TIMER */
  29. timer_config();
  30. /* configure comparator */
  31. cmp_config();
  32. while(1);
  33. }
  34. /*!
  35. \brief configure RCU
  36. \param[in] none
  37. \param[out] none
  38. \retval none
  39. */
  40. void rcu_config(void)
  41. {
  42. rcu_periph_clock_enable(RCU_GPIOA);
  43. rcu_periph_clock_enable(RCU_GPIOB);
  44. rcu_periph_clock_enable(RCU_TIMER1);
  45. rcu_periph_clock_enable(RCU_CFGCMP);
  46. }
  47. /*!
  48. \brief configure GPIO
  49. \param[in] none
  50. \param[out] none
  51. \retval none
  52. */
  53. void gpio_config(void)
  54. {
  55. /* configure PB3 as PWM output */
  56. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  57. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
  58. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_3);
  59. /* configure PA1 as comparator input */
  60. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
  61. gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_PULLUP, GPIO_PIN_1);
  62. }
  63. /*!
  64. \brief configure TIMER
  65. \param[in] none
  66. \param[out] none
  67. \retval none
  68. */
  69. void timer_config(void)
  70. {
  71. /* initialize TIMER1 */
  72. timer_parameter_struct timer_init_parameter;
  73. timer_init_parameter.prescaler = 71;
  74. timer_init_parameter.counterdirection = TIMER_COUNTER_UP;
  75. timer_init_parameter.period = 65535;
  76. timer_init_parameter.clockdivision = TIMER_CKDIV_DIV1;
  77. timer_init(TIMER1, &timer_init_parameter);
  78. /* PWM1 mode configure channel1 in PWM1 mode */
  79. timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM1);
  80. timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, (period/2) + 1);
  81. timer_channel_output_state_config(TIMER1,TIMER_CH_1, TIMER_CCX_ENABLE);
  82. /* select OCREFCLR as source for clearing OC2REF */
  83. timer_channel_output_clear_config(TIMER1, TIMER_CH_1, TIMER_OC_CLEAR_ENABLE);
  84. timer_ocpre_clear_source_config(TIMER1, TIMER_OCPRE_CLEAR_SOURCE_CLR);
  85. /* enable TIMER1 counter */
  86. timer_enable(TIMER1);
  87. }
  88. /*!
  89. \brief configure comparator
  90. \param[in] none
  91. \param[out] none
  92. \retval none
  93. */
  94. void cmp_config(void)
  95. {
  96. /* configure comparator channel0 */
  97. cmp_mode_init(CMP0, CMP_HIGHSPEED, CMP_VREFINT, CMP_HYSTERESIS_NO);
  98. cmp_output_init(CMP0, CMP_OUTPUT_TIMER1OCPRECLR, CMP_OUTPUT_POLARITY_NOINVERTED);
  99. /* enable comparator channel0 */
  100. cmp_enable(CMP0);
  101. }