main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*!
  2. \file main.c
  3. \brief TIMER trigger injected channel of ADC 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. void rcu_config(void);
  15. void gpio_config(void);
  16. void timer_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. /* system clocks configuration */
  27. rcu_config();
  28. /* systick configuration */
  29. systick_config();
  30. /* GPIO configuration */
  31. gpio_config();
  32. /* TIMER configuration */
  33. timer_config();
  34. /* ADC configuration */
  35. adc_config();
  36. /* enable TIMER1 */
  37. timer_enable(TIMER1);
  38. while(1){
  39. }
  40. }
  41. /*!
  42. \brief configure the different system clocks
  43. \param[in] none
  44. \param[out] none
  45. \retval none
  46. */
  47. void rcu_config(void)
  48. {
  49. /* enable GPIOC clock */
  50. rcu_periph_clock_enable(RCU_GPIOA);
  51. /* enable ADC clock */
  52. rcu_periph_clock_enable(RCU_ADC);
  53. /* enable timer1 clock */
  54. rcu_periph_clock_enable(RCU_TIMER1);
  55. /* config ADC clock */
  56. rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);
  57. }
  58. /*!
  59. \brief configure the GPIO peripheral
  60. \param[in] none
  61. \param[out] none
  62. \retval none
  63. */
  64. void gpio_config(void)
  65. {
  66. /* config the GPIO as analog mode */
  67. gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
  68. }
  69. /*!
  70. \brief configure the TIMER peripheral
  71. \param[in] none
  72. \param[out] none
  73. \retval none
  74. */
  75. void timer_config(void)
  76. {
  77. timer_oc_parameter_struct timer_ocintpara;
  78. timer_parameter_struct timer_initpara;
  79. /* TIMER1 configuration */
  80. timer_initpara.prescaler = 8399;
  81. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  82. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  83. timer_initpara.period = 9999;
  84. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  85. timer_initpara.repetitioncounter = 0;
  86. timer_init(TIMER1,&timer_initpara);
  87. /* CH0 configuration in PWM mode1 */
  88. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  89. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  90. timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocintpara);
  91. timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0, 3999);
  92. timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM1);
  93. timer_channel_output_shadow_config(TIMER1, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
  94. }
  95. /*!
  96. \brief configure the ADC peripheral
  97. \param[in] none
  98. \param[out] none
  99. \retval none
  100. */
  101. void adc_config(void)
  102. {
  103. /* ADC continous function enable */
  104. adc_special_function_config(ADC_SCAN_MODE, ENABLE);
  105. /* ADC trigger config */
  106. adc_external_trigger_source_config(ADC_INSERTED_CHANNEL, ADC_EXTTRIG_INSERTED_T1_CH0);
  107. /* ADC data alignment config */
  108. adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
  109. /* ADC channel length config */
  110. adc_channel_length_config(ADC_INSERTED_CHANNEL, 4);
  111. /* ADC inserted channel config */
  112. adc_inserted_channel_config(0, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
  113. adc_inserted_channel_config(1, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5);
  114. adc_inserted_channel_config(2, ADC_CHANNEL_2, ADC_SAMPLETIME_55POINT5);
  115. adc_inserted_channel_config(3, ADC_CHANNEL_3, ADC_SAMPLETIME_55POINT5);
  116. /* ADC external trigger enable */
  117. adc_external_trigger_config(ADC_INSERTED_CHANNEL, ENABLE);
  118. /* enable ADC interface */
  119. adc_enable();
  120. delay_1ms(1);
  121. /* ADC calibration and reset calibration */
  122. adc_calibration_enable();
  123. }