main.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. \file main.c
  3. \brief TIMER1 oc toggle 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_GPIOB);
  23. /*configure PB3(TIMER1 CH1) as alternate function*/
  24. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
  25. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);
  26. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_3);
  27. }
  28. /*!
  29. \brief configure the timer peripheral
  30. \param[in] none
  31. \param[out] none
  32. \retval none
  33. */
  34. void timer_config(void)
  35. {
  36. /* ---------------------------------------------------------------------------
  37. TIMER1 configuration: output compare toggle mode:
  38. TIMER1CLK is 2K,
  39. CH1 update rate = TIMER1 counter clock / CH2CC = 2000/4000 = 0.5 Hz
  40. ----------------------------------------------------------------------------*/
  41. timer_oc_parameter_struct timer_ocintpara;
  42. timer_parameter_struct timer_initpara;
  43. rcu_periph_clock_enable(RCU_TIMER1);
  44. timer_deinit(TIMER1);
  45. /* TIMER1 configuration */
  46. #ifdef GD32F330
  47. timer_initpara.prescaler = 41999;
  48. #endif /* GD32F330 */
  49. #ifdef GD32F350
  50. timer_initpara.prescaler = 53999;
  51. #endif /* GD32F350 */
  52. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  53. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  54. timer_initpara.period = 3999;
  55. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  56. timer_initpara.repetitioncounter = 0;
  57. timer_init(TIMER1,&timer_initpara);
  58. /* CH1 configuration in OC TOGGLE mode */
  59. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  60. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  61. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
  62. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,3999);
  63. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_TOGGLE);
  64. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  65. /* auto-reload preload enable */
  66. timer_auto_reload_shadow_enable(TIMER1);
  67. timer_enable(TIMER1);
  68. }
  69. /*!
  70. \brief main function
  71. \param[in] none
  72. \param[out] none
  73. \retval none
  74. */
  75. int main(void)
  76. {
  77. gpio_config();
  78. timer_config();
  79. while (1);
  80. }