main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*!
  2. \file main.c
  3. \brief TIMER1 single pulse 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(TIMER1 CH0 CH1) 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_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_0);
  29. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_1);
  30. }
  31. /*!
  32. \brief configure the TIMER peripheral
  33. \param[in] none
  34. \param[out] none
  35. \retval none
  36. */
  37. void timer_config(void)
  38. {
  39. /* -----------------------------------------------------------------------
  40. TIMER1 configuration: single pulse mode
  41. the external signal is connected to TIMER1 CH0 pin (PA0) and the falling
  42. edge is used as active edge.
  43. the single pulse signal is output on TIMER1 CH1 pin (PA1).
  44. the TIMER1CLK frequency is set to systemcoreclock (84MHz for GD32F330,108MHz
  45. for GD32F350),the prescaler is 7(GD32F330) or 9(GD32F350),so the TIMER1 counter
  46. clock is 12MHz.
  47. single pulse value = (TIMER1_Period - TIMER1_Pulse) / TIMER1 counter clock
  48. = (41999 - 20999) / 12MHz = 1.75 ms.
  49. ----------------------------------------------------------------------- */
  50. timer_oc_parameter_struct timer_ocinitpara;
  51. timer_parameter_struct timer_initpara;
  52. timer_ic_parameter_struct timer_icinitpara;
  53. rcu_periph_clock_enable(RCU_TIMER1);
  54. timer_deinit(TIMER1);
  55. /* TIMER1 configuration */
  56. #ifdef GD32F330
  57. timer_initpara.prescaler = 6;
  58. #endif /* GD32F330 */
  59. #ifdef GD32F350
  60. timer_initpara.prescaler = 8;
  61. #endif /* GD32F330 */
  62. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  63. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  64. timer_initpara.period = 41999;
  65. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  66. timer_initpara.repetitioncounter = 0;
  67. timer_init(TIMER1,&timer_initpara);
  68. /* auto-reload preload disable */
  69. timer_auto_reload_shadow_disable(TIMER1);
  70. /* CH1 configuration in OC PWM1 mode */
  71. timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  72. timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
  73. timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  74. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocinitpara);
  75. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,20999);
  76. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM1);
  77. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  78. /* TIMER1 CH0 input capture configuration */
  79. timer_icinitpara.icpolarity = TIMER_IC_POLARITY_FALLING;
  80. timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
  81. timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
  82. timer_icinitpara.icfilter = 0x00;
  83. timer_input_capture_config(TIMER1,TIMER_CH_0,&timer_icinitpara);
  84. /* single pulse mode selection */
  85. timer_single_pulse_mode_config(TIMER1,TIMER_SP_MODE_SINGLE);
  86. /* slave mode selection : TIMER1 */
  87. /* TIMER1 input trigger : external trigger connected to CI0 */
  88. timer_input_trigger_source_select(TIMER1,TIMER_SMCFG_TRGSEL_CI0FE0);
  89. timer_slave_mode_select(TIMER1,TIMER_SLAVE_MODE_EVENT);
  90. }
  91. /*!
  92. \brief main function
  93. \param[in] none
  94. \param[out] none
  95. \retval none
  96. */
  97. int main(void)
  98. {
  99. gpio_config();
  100. timer_config();
  101. while (1);
  102. }