main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*!
  2. \file main.c
  3. \brief debug timer1 when the mcu is in debug mode
  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 "systick.h"
  34. #include <stdio.h>
  35. #include "gd32f307c_eval.h"
  36. void gpio_configuration(void);
  37. void timer_configuration(void);
  38. /*!
  39. \brief main function
  40. \param[in] none
  41. \param[out] none
  42. \retval none
  43. */
  44. int main(void)
  45. {
  46. dbg_periph_disable(DBG_TIMER1_HOLD);
  47. gpio_configuration();
  48. gd_eval_led_init(LED2);
  49. systick_config();
  50. timer_configuration();
  51. /* keep timer1 counter when the mcu is in debug mode */
  52. dbg_periph_enable(DBG_TIMER1_HOLD);
  53. while (1){
  54. /* toggle LED2 output status every second */
  55. gd_eval_led_toggle(LED2);
  56. /* you can set breakpoint here, then look over the register in timer1 */
  57. delay_1ms(1000);
  58. }
  59. }
  60. /*!
  61. \brief configure the GPIO ports
  62. \param[in] none
  63. \param[out] none
  64. \retval none
  65. */
  66. void gpio_configuration(void)
  67. {
  68. rcu_periph_clock_enable(RCU_AF);
  69. rcu_periph_clock_enable(RCU_GPIOB);
  70. gpio_pin_remap_config(GPIO_TIMER1_FULL_REMAP, ENABLE);
  71. /* configure PB3 PB10 PB11(TIMER1 CH1 CH2 CH3) as alternate function */
  72. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_3 | GPIO_PIN_10 | GPIO_PIN_11);
  73. }
  74. /*!
  75. \brief configure the TIMER peripheral
  76. \param[in] none
  77. \param[out] none
  78. \retval none
  79. */
  80. void timer_configuration(void)
  81. {
  82. /* TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
  83. TIMER1CLK = SystemCoreClock / 168 = 1MHz
  84. TIMER1 channel 1 duty cycle = 4000/ 16000 = 25%
  85. TIMER1 channel 2 duty cycle = 8000/ 16000 = 50%
  86. TIMER1 channel 3 duty cycle = 12000/ 16000 = 75% */
  87. timer_parameter_struct timer_initpara;
  88. timer_oc_parameter_struct timer_ocintpara;
  89. rcu_periph_clock_enable(RCU_TIMER1);
  90. /* TIMER1 configuration */
  91. timer_deinit(TIMER1);
  92. /* TIMER1 configuration */
  93. timer_initpara.prescaler = 167;
  94. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  95. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  96. timer_initpara.period = 15999;
  97. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  98. timer_initpara.repetitioncounter = 0;
  99. timer_init(TIMER1,&timer_initpara);
  100. /* CH1,CH2 and CH3 configuration in PWM mode */
  101. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  102. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  103. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
  104. timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
  105. timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);
  106. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,3999);
  107. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  108. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  109. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,7999);
  110. timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  111. timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  112. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,11999);
  113. timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
  114. timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
  115. /* auto-reload preload enable */
  116. timer_auto_reload_shadow_enable(TIMER1);
  117. timer_enable(TIMER1);
  118. }