main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*!
  2. \file main.c
  3. \brief TIMER1 external trigger 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 PA2(TIMER1 CH2) as alternate function*/
  24. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_2);
  25. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_2);
  26. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_2);
  27. /*configure PA5(TIMER1 CH0) as alternate function*/
  28. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5);
  29. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_5);
  30. gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_5);
  31. }
  32. /*!
  33. \brief configure the TIMER peripheral
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void timer_config(void)
  39. {
  40. /* Timer with an external trigger -------------------------------------
  41. TIMER1 is configured as slave timer for an external trigger connected
  42. to TIMER1 CI0 pin :
  43. - The TIMER1 CI0FE0 is used as trigger input
  44. - rising edge is used to start the TIMER1: event mode
  45. - TIMER1 CH2 is used PWM1 Mode
  46. the starts of the TIMER1 counter are controlled by the
  47. external trigger.
  48. -------------------------------------------------------------------- */
  49. timer_oc_parameter_struct timer_ocinitpara;
  50. timer_parameter_struct timer_initpara;
  51. timer_ic_parameter_struct timer_icinitpara;
  52. rcu_periph_clock_enable(RCU_TIMER1);
  53. timer_deinit(TIMER1);
  54. /* TIMER1 configuration */
  55. timer_initpara.prescaler = 41999;
  56. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  57. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  58. timer_initpara.period = 7999;
  59. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  60. timer_initpara.repetitioncounter = 0;
  61. timer_init(TIMER1,&timer_initpara);
  62. /* CH2 configuration in PWM1 mode */
  63. timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  64. timer_ocinitpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  65. timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
  66. timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
  67. timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  68. timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  69. timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocinitpara);
  70. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,3999);
  71. timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM1);
  72. timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  73. /* TIMER1 CH0 input capture configuration */
  74. timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
  75. timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
  76. timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
  77. timer_icinitpara.icfilter = 0x02;
  78. timer_input_capture_config(TIMER1,TIMER_CH_0,&timer_icinitpara);
  79. /* slave mode selection : TIMER1 */
  80. /* TIMER1 input trigger : external trigger connected to CI0 */
  81. timer_input_trigger_source_select(TIMER1,TIMER_SMCFG_TRGSEL_CI0FE0);
  82. timer_slave_mode_select(TIMER1,TIMER_SLAVE_MODE_EVENT);
  83. /* auto-reload preload enable */
  84. timer_auto_reload_shadow_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. }