main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*!
  2. \file main.c
  3. \brief ADC discontinuous mode for regular channel
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include "systick.h"
  11. #include <stdio.h>
  12. #include "main.h"
  13. #include "gd32f3x0_eval.h"
  14. uint16_t adc_value[16];
  15. void rcu_config(void);
  16. void gpio_config(void);
  17. void dma_config(void);
  18. void adc_config(void);
  19. /*!
  20. \brief main function
  21. \param[in] none
  22. \param[out] none
  23. \retval none
  24. */
  25. int main(void)
  26. {
  27. /* system clocks configuration */
  28. rcu_config();
  29. /* systick configuration */
  30. systick_config();
  31. /* GPIO configuration */
  32. gpio_config();
  33. /* DMA configuration */
  34. dma_config();
  35. /* ADC configuration */
  36. adc_config();
  37. while(1){
  38. /* delay 1s */
  39. delay_1ms(1000);
  40. /* ADC software trigger enable */
  41. adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
  42. }
  43. }
  44. /*!
  45. \brief configure the different system clocks
  46. \param[in] none
  47. \param[out] none
  48. \retval none
  49. */
  50. void rcu_config(void)
  51. {
  52. /* enable GPIOC clock */
  53. rcu_periph_clock_enable(RCU_GPIOA);
  54. /* enable ADC clock */
  55. rcu_periph_clock_enable(RCU_ADC);
  56. /* enable DMA clock */
  57. rcu_periph_clock_enable(RCU_DMA);
  58. /* config ADC clock */
  59. rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);
  60. }
  61. /*!
  62. \brief configure the GPIO peripheral
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void gpio_config(void)
  68. {
  69. /* config the GPIO as analog mode */
  70. gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
  71. gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
  72. }
  73. /*!
  74. \brief configure the DMA peripheral
  75. \param[in] none
  76. \param[out] none
  77. \retval none
  78. */
  79. void dma_config(void)
  80. {
  81. /* ADC_DMA_channel configuration */
  82. dma_parameter_struct dma_data_parameter;
  83. /* ADC DMA_channel configuration */
  84. dma_deinit(DMA_CH0);
  85. /* initialize DMA single data mode */
  86. dma_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA);
  87. dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  88. dma_data_parameter.memory_addr = (uint32_t)(&adc_value);
  89. dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  90. dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  91. dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
  92. dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
  93. dma_data_parameter.number = 16;
  94. dma_data_parameter.priority = DMA_PRIORITY_HIGH;
  95. dma_init(DMA_CH0, dma_data_parameter);
  96. dma_circulation_enable(DMA_CH0);
  97. /* enable DMA channel */
  98. dma_channel_enable(DMA_CH0);
  99. }
  100. /*!
  101. \brief configure the ADC peripheral
  102. \param[in] none
  103. \param[out] none
  104. \retval none
  105. */
  106. void adc_config(void)
  107. {
  108. /* ADC trigger config */
  109. adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE);
  110. /* ADC data alignment config */
  111. adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
  112. /* ADC channel length config */
  113. adc_channel_length_config(ADC_REGULAR_CHANNEL, 8);
  114. /* ADC regular channel config */
  115. adc_regular_channel_config(0, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
  116. adc_regular_channel_config(1, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5);
  117. adc_regular_channel_config(2, ADC_CHANNEL_2, ADC_SAMPLETIME_55POINT5);
  118. adc_regular_channel_config(3, ADC_CHANNEL_3, ADC_SAMPLETIME_55POINT5);
  119. adc_regular_channel_config(4, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5);
  120. adc_regular_channel_config(5, ADC_CHANNEL_5, ADC_SAMPLETIME_55POINT5);
  121. adc_regular_channel_config(6, ADC_CHANNEL_6, ADC_SAMPLETIME_55POINT5);
  122. adc_regular_channel_config(7, ADC_CHANNEL_7, ADC_SAMPLETIME_55POINT5);
  123. adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE);
  124. /* ADC discontinuous mode */
  125. adc_discontinuous_mode_config(ADC_REGULAR_CHANNEL, 3);
  126. /* enable ADC interface */
  127. adc_enable();
  128. delay_1ms(1);
  129. /* ADC calibration and reset calibration */
  130. adc_calibration_enable();
  131. /* ADC DMA function enable */
  132. adc_dma_mode_enable();
  133. }