main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*!
  2. \file main.c
  3. \brief TIMER0 complementary signals 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_GPIOB);
  47. rcu_periph_clock_enable(RCU_AF);
  48. /*configure PA8 PA9 PA10(TIMER0 CH0 CH1 CH2) as alternate function*/
  49. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  50. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  51. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  52. /*configure PB13 PB14 PB15(TIMER0 CH0N CH1N CH2N) as alternate function*/
  53. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_13);
  54. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_14);
  55. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_15);
  56. }
  57. /*!
  58. \brief configure the TIMER peripheral
  59. \param[in] none
  60. \param[out] none
  61. \retval none
  62. */
  63. void timer_config(void)
  64. {
  65. /* -----------------------------------------------------------------------
  66. TIMER0 configuration to:
  67. generate 3 complementary PWM signals with 3 different duty cycles:
  68. TIMER0CLK is fixed to systemcoreclock, the TIMER0 prescaler is equal to 6000 so the
  69. TIMER0 counter clock used is 20KHz.
  70. the three duty cycles are computed as the following description:
  71. the channel 0 duty cycle is set to 25% so channel 1N is set to 75%.
  72. the channel 1 duty cycle is set to 50% so channel 2N is set to 50%.
  73. the channel 2 duty cycle is set to 75% so channel 3N is set to 25%.
  74. ----------------------------------------------------------------------- */
  75. timer_oc_parameter_struct timer_ocintpara;
  76. timer_parameter_struct timer_initpara;
  77. rcu_periph_clock_enable(RCU_TIMER0);
  78. timer_deinit(TIMER0);
  79. /* TIMER0 configuration */
  80. timer_initpara.prescaler = 5999;
  81. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  82. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  83. timer_initpara.period = 15999;
  84. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  85. timer_initpara.repetitioncounter = 0;
  86. timer_init(TIMER0,&timer_initpara);
  87. /* CH1,CH2 and CH3 configuration in PWM mode */
  88. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  89. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  90. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  91. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  92. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  93. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  94. timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
  95. timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
  96. timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
  97. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,3999);
  98. timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  99. timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
  100. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,7999);
  101. timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  102. timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  103. timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,11999);
  104. timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  105. timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  106. timer_primary_output_config(TIMER0,ENABLE);
  107. /* auto-reload preload enable */
  108. timer_auto_reload_shadow_enable(TIMER0);
  109. timer_enable(TIMER0);
  110. }
  111. /*!
  112. \brief main function
  113. \param[in] none
  114. \param[out] none
  115. \retval none
  116. */
  117. int main(void)
  118. {
  119. gpio_config();
  120. timer_config();
  121. while (1);
  122. }