main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. \file main.c
  3. \brief TIMER2 PWM input capture 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. #include "systick.h"
  36. extern __IO uint16_t dutycycle;
  37. extern __IO uint16_t frequency;
  38. void gpio_configuration(void);
  39. void timer_configuration(void);
  40. void nvic_configuration(void);
  41. int fputc(int ch, FILE *f);
  42. /* retarget the C library printf function to the USART */
  43. int fputc(int ch, FILE *f)
  44. {
  45. usart_data_transmit(EVAL_COM1, (uint8_t)ch);
  46. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  47. return ch;
  48. }
  49. /*!
  50. \brief configure the GPIO ports
  51. \param[in] none
  52. \param[out] none
  53. \retval none
  54. */
  55. void gpio_configuration(void)
  56. {
  57. rcu_periph_clock_enable(RCU_GPIOA);
  58. rcu_periph_clock_enable(RCU_AF);
  59. /*configure PA6 (TIMER2 CH0) as alternate function*/
  60. gpio_init(GPIOA,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  61. }
  62. /*!
  63. \brief configure the nested vectored interrupt controller
  64. \param[in] none
  65. \param[out] none
  66. \retval none
  67. */
  68. void nvic_configuration(void)
  69. {
  70. nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
  71. nvic_irq_enable(TIMER2_IRQn, 1, 1);
  72. }
  73. /*!
  74. \brief configure the TIMER peripheral
  75. \param[in] none
  76. \param[out] none
  77. \retval none
  78. */
  79. void timer_configuration(void)
  80. {
  81. /* TIMER2 configuration: PWM input mode ------------------------
  82. the external signal is connected to TIMER2 CH0 pin
  83. the rising edge is used as active edge
  84. the TIMER2 CH0CV is used to compute the frequency value
  85. the TIMER2 CH1CV is used to compute the duty cycle value
  86. ------------------------------------------------------------ */
  87. timer_ic_parameter_struct timer_icinitpara;
  88. timer_parameter_struct timer_initpara;
  89. rcu_periph_clock_enable(RCU_TIMER2);
  90. timer_deinit(TIMER2);
  91. /* TIMER2 configuration */
  92. timer_initpara.prescaler = 119;
  93. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  94. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  95. timer_initpara.period = 65535;
  96. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  97. timer_initpara.repetitioncounter = 0;
  98. timer_init(TIMER2,&timer_initpara);
  99. /* TIMER2 configuration */
  100. /* TIMER2 CH0 PWM input capture configuration */
  101. timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
  102. timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
  103. timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
  104. timer_icinitpara.icfilter = 0x0;
  105. timer_input_pwm_capture_config(TIMER2,TIMER_CH_0,&timer_icinitpara);
  106. /* slave mode selection: TIMER2 */
  107. timer_input_trigger_source_select(TIMER2,TIMER_SMCFG_TRGSEL_CI0FE0);
  108. timer_slave_mode_select(TIMER2,TIMER_SLAVE_MODE_RESTART);
  109. /* select the master slave mode */
  110. timer_master_slave_mode_config(TIMER2,TIMER_MASTER_SLAVE_MODE_ENABLE);
  111. /* auto-reload preload enable */
  112. timer_auto_reload_shadow_enable(TIMER2);
  113. /* clear channel 0 interrupt bit */
  114. timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH0);
  115. /* channel 0 interrupt enable */
  116. timer_interrupt_enable(TIMER2,TIMER_INT_CH0);
  117. /* TIMER2 counter enable */
  118. timer_enable(TIMER2);
  119. }
  120. /*!
  121. \brief main function
  122. \param[in] none
  123. \param[out] none
  124. \retval none
  125. */
  126. int main(void)
  127. {
  128. systick_config();
  129. gpio_configuration();
  130. gd_eval_com_init(EVAL_COM1);
  131. nvic_configuration();
  132. timer_configuration();
  133. while (1){
  134. delay_1ms(1000);
  135. printf("\r /**** TIMER2 PWM Input Capture Demo ****/\r\n");
  136. printf("the dutycycle is %d\n",dutycycle);
  137. printf("the frequence is %d\n",frequency);
  138. }
  139. }