main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*!
  2. \file main.c
  3. \brief ADC channel of temperature and Vref 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. float temperature;
  15. float vref_value;
  16. void rcu_config(void);
  17. void adc_config(void);
  18. /*!
  19. \brief main function
  20. \param[in] none
  21. \param[out] none
  22. \retval none
  23. */
  24. int main(void)
  25. {
  26. /* configure systick */
  27. systick_config();
  28. /* system clocks configuration */
  29. rcu_config();
  30. /* ADC configuration */
  31. adc_config();
  32. /* USART configuration */
  33. gd_eval_com_init(EVAL_COM1);
  34. while(1){
  35. /* ADC software trigger enable */
  36. adc_software_trigger_enable(ADC_INSERTED_CHANNEL);
  37. /* delay a time in milliseconds */
  38. delay_1ms(2000);
  39. /* value convert */
  40. temperature = (1.43 - ADC_IDATA0 * 3.3/4096) * 1000 / 4.3 + 25;
  41. vref_value = (ADC_IDATA1 * 3.3 / 4096);
  42. /* value print */
  43. printf(" the temperature data is %2.0f degrees Celsius\r\n", temperature);
  44. printf(" the reference voltage data is %5.3fV \r\n", vref_value);
  45. printf(" \r\n");
  46. }
  47. }
  48. /*!
  49. \brief configure the different system clocks
  50. \param[in] none
  51. \param[out] none
  52. \retval none
  53. */
  54. void rcu_config(void)
  55. {
  56. /* enable ADC clock */
  57. rcu_periph_clock_enable(RCU_ADC);
  58. /* config ADC clock */
  59. rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);
  60. }
  61. /*!
  62. \brief configure the ADC peripheral
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void adc_config(void)
  68. {
  69. /* ADC channel length config */
  70. adc_channel_length_config(ADC_INSERTED_CHANNEL, 2);
  71. /* ADC temperature sensor channel config */
  72. adc_inserted_channel_config(0, ADC_CHANNEL_16, ADC_SAMPLETIME_239POINT5);
  73. /* ADC internal reference voltage channel config */
  74. adc_inserted_channel_config(1, ADC_CHANNEL_17, ADC_SAMPLETIME_239POINT5);
  75. /* ADC trigger config */
  76. adc_external_trigger_source_config(ADC_INSERTED_CHANNEL, ADC_EXTTRIG_INSERTED_NONE);
  77. /* ADC data alignment config */
  78. adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
  79. /* ADC SCAN function enable */
  80. adc_special_function_config(ADC_SCAN_MODE, ENABLE);
  81. /* ADC temperature and Vrefint enable */
  82. adc_tempsensor_vrefint_enable();
  83. adc_external_trigger_config(ADC_INSERTED_CHANNEL, ENABLE);
  84. /* enable ADC interface */
  85. adc_enable();
  86. delay_1ms(1);
  87. /* ADC calibration and reset calibration */
  88. adc_calibration_enable();
  89. }
  90. /* retarget the C library printf function to the USART */
  91. int fputc(int ch, FILE *f)
  92. {
  93. usart_data_transmit(EVAL_COM1, (uint8_t)ch);
  94. while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  95. return ch;
  96. }