main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*!
  2. \file main.c
  3. \brief DAC ADC convert demo
  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) 2023, 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. /* configure RCU peripheral */
  35. void rcu_config(void);
  36. /* configure GPIO peripheral */
  37. void gpio_config(void);
  38. /* configure NVIC peripheral */
  39. void nvic_config(void);
  40. /* configure ADC peripheral */
  41. void adc_config(void);
  42. /* configure DAC peripheral */
  43. void dac_config(void);
  44. /*!
  45. \brief main function
  46. \param[in] none
  47. \param[out] none
  48. \retval none
  49. */
  50. int main(void)
  51. {
  52. /* configure systick */
  53. systick_config();
  54. /* configure RCU peripheral */
  55. rcu_config();
  56. /* configure GPIO peripheral */
  57. gpio_config();
  58. /* configure NVIC peripheral */
  59. nvic_config();
  60. /* configure ADC peripheral */
  61. adc_config();
  62. /* configure DAC peripheral */
  63. dac_config();
  64. while(1) {
  65. }
  66. }
  67. /*!
  68. \brief configure RCU peripheral
  69. \param[in] none
  70. \param[out] none
  71. \retval none
  72. */
  73. void rcu_config(void)
  74. {
  75. /* enable GPIOA clock */
  76. rcu_periph_clock_enable(RCU_GPIOA);
  77. /* enable GPIOC clock */
  78. rcu_periph_clock_enable(RCU_GPIOC);
  79. /* enable ADC clock */
  80. rcu_periph_clock_enable(RCU_ADC0);
  81. /* enable DAC clock */
  82. rcu_periph_clock_enable(RCU_DAC);
  83. }
  84. /*!
  85. \brief configure GPIO periphera
  86. \param[in] none
  87. \param[out] none
  88. \retval none
  89. */
  90. void gpio_config(void)
  91. {
  92. /* configure PC3 as ADC input */
  93. gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  94. /* configure PA4 as DAC output */
  95. gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
  96. }
  97. /*!
  98. \brief configure NVIC peripheral
  99. \param[in] none
  100. \param[out] none
  101. \retval none
  102. */
  103. void nvic_config(void)
  104. {
  105. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  106. nvic_irq_enable(ADC0_1_IRQn, 0, 0);
  107. }
  108. /*!
  109. \brief configure ADC peripheral
  110. \param[in] none
  111. \param[out] none
  112. \retval none
  113. */
  114. void adc_config(void)
  115. {
  116. /* ADC continuous function enable */
  117. adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
  118. /* ADC data alignment config */
  119. adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
  120. /* ADC channel length config */
  121. adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
  122. /* ADC regular channel config */
  123. adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_13, ADC_SAMPLETIME_55POINT5);
  124. /* ADC external trigger enable */
  125. adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
  126. /* ADC trigger config */
  127. adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
  128. /* enable ADC interface */
  129. adc_enable(ADC0);
  130. delay_1ms(1);
  131. /* ADC calibration and reset calibration */
  132. adc_calibration_enable(ADC0);
  133. /* ADC interrupt enable */
  134. adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);
  135. adc_interrupt_enable(ADC0, ADC_INT_EOC);
  136. /* enable ADC software trigger */
  137. adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
  138. }
  139. /*!
  140. \brief configure DAC peripheral
  141. \param[in] none
  142. \param[out] none
  143. \retval none
  144. */
  145. void dac_config(void)
  146. {
  147. /* initialize DAC */
  148. dac_deinit(DAC0);
  149. /* DAC trigger config */
  150. dac_trigger_source_config(DAC0, DAC_OUT0, DAC_TRIGGER_SOFTWARE);
  151. /* DAC trigger enable */
  152. dac_trigger_enable(DAC0, DAC_OUT0);
  153. /* DAC wave mode config */
  154. dac_wave_mode_config(DAC0, DAC_OUT0, DAC_WAVE_DISABLE);
  155. /* DAC output buffer config */
  156. dac_output_buffer_enable(DAC0, DAC_OUT0);
  157. /* DAC enable */
  158. dac_enable(DAC0, DAC_OUT0);
  159. }