main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*!
  2. \file main.c
  3. \brief TIMER1 single pulse demo for gd32f30x
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f30x.h"
  33. #include <stdio.h>
  34. #include "gd32f307c_eval.h"
  35. void gpio_config(void);
  36. void timer_config(void);
  37. /*!
  38. \brief configure the GPIO ports
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. void gpio_config(void)
  44. {
  45. rcu_periph_clock_enable(RCU_GPIOA);
  46. rcu_periph_clock_enable(RCU_AF);
  47. /*configure PA0 PA1(TIMER1 CH0 CH1) as alternate function*/
  48. gpio_init(GPIOA,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
  49. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
  50. }
  51. /*!
  52. \brief configure the TIMER peripheral
  53. \param[in] none
  54. \param[out] none
  55. \retval none
  56. */
  57. void timer_config(void)
  58. {
  59. /* -----------------------------------------------------------------------
  60. TIMER1 configuration: single pulse mode
  61. the external signal is connected to TIMER1 CH0 pin (PA0) and the falling
  62. edge is used as active edge.
  63. the single pulse signal is output on TIMER1 CH1 pin (PA1).
  64. the TIMER1CLK frequency is set to systemcoreclock,the prescaler is
  65. 60,so the TIMER1 counter clock is 2MHz.
  66. single pulse value = (TIMER1_Period - TIMER1_Pulse) / TIMER1 counter clock
  67. = (65535 - 11535) / 2MHz = 27 ms.
  68. ----------------------------------------------------------------------- */
  69. timer_oc_parameter_struct timer_ocinitpara;
  70. timer_parameter_struct timer_initpara;
  71. timer_ic_parameter_struct timer_icinitpara;
  72. rcu_periph_clock_enable(RCU_TIMER1);
  73. timer_deinit(TIMER1);
  74. /* TIMER1 configuration */
  75. timer_initpara.prescaler = 59;
  76. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  77. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  78. timer_initpara.period = 65535;
  79. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  80. timer_initpara.repetitioncounter = 0;
  81. timer_init(TIMER1,&timer_initpara);
  82. /* auto-reload preload disable */
  83. timer_auto_reload_shadow_disable(TIMER1);
  84. /* CH1 configuration in OC PWM mode */
  85. timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
  86. timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
  87. timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  88. timer_ocinitpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  89. timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  90. timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  91. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocinitpara);
  92. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,11535);
  93. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM1);
  94. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  95. /* TIMER1 CH0 input capture configuration */
  96. timer_icinitpara.icpolarity = TIMER_IC_POLARITY_FALLING;
  97. timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
  98. timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
  99. timer_icinitpara.icfilter = 0x00;
  100. timer_input_capture_config(TIMER1,TIMER_CH_0,&timer_icinitpara);
  101. /* single pulse mode selection */
  102. timer_single_pulse_mode_config(TIMER1,TIMER_SP_MODE_SINGLE);
  103. /* slave mode selection : TIMER1 */
  104. /* TIMER1 input trigger : external trigger connected to CI0 */
  105. timer_input_trigger_source_select(TIMER1,TIMER_SMCFG_TRGSEL_CI0FE0);
  106. timer_slave_mode_select(TIMER1,TIMER_SLAVE_MODE_EVENT);
  107. }
  108. /*!
  109. \brief main function
  110. \param[in] none
  111. \param[out] none
  112. \retval none
  113. */
  114. int main(void)
  115. {
  116. gpio_config();
  117. timer_config();
  118. while (1);
  119. }