main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*!
  2. \file main.c
  3. \brief DAC DMA convert demo
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #define DAC_R8DH_ADDRESS 0x40007410
  11. const uint8_t convertarr[10] = {0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF, 0xCC, 0x99, 0x66, 0x33};
  12. void rcu_config(void);
  13. void gpio_config(void);
  14. void dma_config(void);
  15. void dac_config(void);
  16. void timer5_config(void);
  17. /*!
  18. \brief main function
  19. \param[in] none
  20. \param[out] none
  21. \retval none
  22. */
  23. int main(void)
  24. {
  25. rcu_config();
  26. gpio_config();
  27. dma_config();
  28. dac_config();
  29. timer5_config();
  30. while (1){
  31. }
  32. }
  33. /*!
  34. \brief configure the RCU of peripherals
  35. \param[in] none
  36. \param[out] none
  37. \retval none
  38. */
  39. void rcu_config(void)
  40. {
  41. /* enable the clock of peripherals */
  42. rcu_periph_clock_enable(RCU_GPIOA);
  43. rcu_periph_clock_enable(RCU_DMA);
  44. rcu_periph_clock_enable(RCU_DAC);
  45. rcu_periph_clock_enable(RCU_TIMER5);
  46. }
  47. /*!
  48. \brief configure the related GPIO
  49. \param[in] none
  50. \param[out] none
  51. \retval none
  52. */
  53. void gpio_config(void)
  54. {
  55. /* once enabled the DAC, the corresponding GPIO pin is connected to the DAC converter automatically */
  56. gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_4);
  57. }
  58. /*!
  59. \brief configure the DMA
  60. \param[in] none
  61. \param[out] none
  62. \retval none
  63. */
  64. void dma_config(void)
  65. {
  66. dma_parameter_struct dma_struct;
  67. /* clear all the interrupt flags */
  68. dma_flag_clear(DMA, DMA_CH2, DMA_FLAG_GIF);
  69. /* configure the DMA0 channel 2 */
  70. dma_struct.periph_addr = DAC_R8DH_ADDRESS;
  71. dma_struct.memory_addr = (uint32_t)convertarr;
  72. dma_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  73. dma_struct.number = 10;
  74. dma_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  75. dma_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  76. dma_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  77. dma_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  78. dma_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  79. dma_circulation_enable(DMA_CH2);
  80. dma_init(DMA_CH2, dma_struct);
  81. dma_channel_enable(DMA_CH2);
  82. }
  83. /*!
  84. \brief configure the DAC
  85. \param[in] none
  86. \param[out] none
  87. \retval none
  88. */
  89. void dac_config(void)
  90. {
  91. dac_deinit();
  92. /* configure the DAC */
  93. dac_trigger_source_config(DAC_TRIGGER_T5_TRGO);
  94. dac_trigger_enable();
  95. dac_wave_mode_config(DAC_WAVE_DISABLE);
  96. dac_output_buffer_disable();
  97. /* enable DAC and DMA for DAC */
  98. dac_enable();
  99. dac_dma_enable();
  100. }
  101. /*!
  102. \brief configure the TIMER5
  103. \param[in] none
  104. \param[out] none
  105. \retval none
  106. */
  107. void timer5_config(void)
  108. {
  109. /* configure the TIMER5 */
  110. timer_prescaler_config(TIMER5, 0xF, TIMER_PSC_RELOAD_UPDATE);
  111. timer_autoreload_value_config(TIMER5, 0xFF);
  112. timer_master_output_trigger_source_select(TIMER5, TIMER_TRI_OUT_SRC_UPDATE);
  113. timer_enable(TIMER5);
  114. }