main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. \file main.c
  3. \brief TIMER1 oc active 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. /*Configure PA0 PA1 PA2(TIMER1 CH0 CH1 CH2) as alternate function*/
  24. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0);
  25. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_0);
  26. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
  27. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
  28. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_2);
  29. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_2);
  30. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_0);
  31. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_1);
  32. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_2);
  33. }
  34. /*!
  35. \brief configure the TIMER peripheral
  36. \param[in] none
  37. \param[out] none
  38. \retval none
  39. */
  40. void timer_config(void)
  41. {
  42. /* ---------------------------------------------------------------
  43. TIMER1 Configuration:
  44. TIMER1CLK is 2K,
  45. And generate 3 signals with 3 different delays:
  46. TIMER1_CH0 delay = 4000/2000 = 2s
  47. TIMER1_CH1 delay = 8000/2000 = 4s
  48. TIMER1_CH2 delay = 12000/2000 = 6s
  49. --------------------------------------------------------------- */
  50. timer_oc_parameter_struct timer_ocintpara;
  51. timer_parameter_struct timer_initpara;
  52. rcu_periph_clock_enable(RCU_TIMER1);
  53. timer_deinit(TIMER1);
  54. /* TIMER1 configuration */
  55. #ifdef GD32F330
  56. timer_initpara.prescaler = 41999;
  57. #endif /* GD32F330 */
  58. #ifdef GD32F350
  59. timer_initpara.prescaler = 53999;
  60. #endif /* GD32F350 */
  61. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  62. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  63. timer_initpara.period = 19999;
  64. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  65. timer_initpara.repetitioncounter = 0;
  66. timer_init(TIMER1,&timer_initpara);
  67. /* CH0,CH1 and CH2 configuration in OC active mode */
  68. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  69. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  70. timer_channel_output_config(TIMER1,TIMER_CH_0,&timer_ocintpara);
  71. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
  72. timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
  73. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_0,3999);
  74. timer_channel_output_mode_config(TIMER1,TIMER_CH_0,TIMER_OC_MODE_ACTIVE);
  75. timer_channel_output_shadow_config(TIMER1,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  76. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,7999);
  77. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_ACTIVE);
  78. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  79. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,11999);
  80. timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_ACTIVE);
  81. timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  82. /* auto-reload preload enable */
  83. timer_auto_reload_shadow_enable(TIMER1);
  84. timer_enable(TIMER1);
  85. }
  86. /*!
  87. \brief main function
  88. \param[in] none
  89. \param[out] none
  90. \retval none
  91. */
  92. int main(void)
  93. {
  94. gpio_config();
  95. timer_config();
  96. while (1);
  97. }