main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*!
  2. \file main.c
  3. \brief ADC analog watchdog demo
  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. #define BOARD_ADC_CHANNEL ADC_CHANNEL_11
  15. #define ADC_GPIO_PORT GPIOC
  16. #define ADC_GPIO_PIN GPIO_PIN_1
  17. uint16_t adc_value;
  18. uint8_t data;
  19. void rcu_config(void);
  20. void gpio_config(void);
  21. void nvic_config(void);
  22. void adc_config(void);
  23. /*!
  24. \brief main function
  25. \param[in] none
  26. \param[out] none
  27. \retval none
  28. */
  29. int main(void)
  30. {
  31. /* system clocks configuration */
  32. rcu_config();
  33. /* systick configuration */
  34. systick_config();
  35. /* GPIO configuration */
  36. gpio_config();
  37. /* NVIC configuration */
  38. nvic_config();
  39. /* ADC configuration */
  40. adc_config();
  41. while(1){
  42. if(1 == data){
  43. data = 0;
  44. delay_1ms(3000);
  45. gd_eval_led_off(LED2);
  46. }
  47. }
  48. }
  49. /*!
  50. \brief configure the different system clocks
  51. \param[in] none
  52. \param[out] none
  53. \retval none
  54. */
  55. void rcu_config(void)
  56. {
  57. /* enable GPIOC clock */
  58. rcu_periph_clock_enable(RCU_GPIOC);
  59. /* enable ADC clock */
  60. rcu_periph_clock_enable(RCU_ADC);
  61. /* config ADC clock */
  62. rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);
  63. }
  64. /*!
  65. \brief configure the GPIO peripheral
  66. \param[in] none
  67. \param[out] none
  68. \retval none
  69. */
  70. void gpio_config(void)
  71. {
  72. /* configure led GPIO */
  73. gd_eval_led_init(LED2);
  74. /* config the GPIO as analog mode */
  75. gpio_mode_set(ADC_GPIO_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, ADC_GPIO_PIN);
  76. }
  77. /*!
  78. \brief configure interrupt priority
  79. \param[in] none
  80. \param[out] none
  81. \retval none
  82. */
  83. void nvic_config(void)
  84. {
  85. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  86. nvic_irq_enable(ADC_CMP_IRQn, 0, 0);
  87. }
  88. /*!
  89. \brief configure the ADC peripheral
  90. \param[in] none
  91. \param[out] none
  92. \retval none
  93. */
  94. void adc_config(void)
  95. {
  96. /* ADC contineous function enable */
  97. adc_special_function_config(ADC_CONTINUOUS_MODE, ENABLE);
  98. /* ADC trigger config */
  99. adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE);
  100. /* ADC data alignment config */
  101. adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
  102. /* ADC channel length config */
  103. adc_channel_length_config(ADC_REGULAR_CHANNEL, 1);
  104. /* ADC regular channel config */
  105. adc_regular_channel_config(0, BOARD_ADC_CHANNEL, ADC_SAMPLETIME_55POINT5);
  106. adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE);
  107. /* enable ADC interface */
  108. adc_enable();
  109. delay_1ms(1);
  110. /* ADC calibration and reset calibration */
  111. adc_calibration_enable();
  112. /* ADC analog watchdog threshold config */
  113. adc_watchdog_threshold_config(0x0400, 0x0A00);
  114. /* ADC analog watchdog single channel config */
  115. adc_watchdog_single_channel_enable(BOARD_ADC_CHANNEL);
  116. /* ADC interrupt config */
  117. adc_interrupt_enable(ADC_INT_WDE);
  118. /* ADC software trigger enable */
  119. adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
  120. }