main.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. \file main.c
  3. \brief DAC in triangle noise mode demo
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. void rcu_config(void);
  11. void gpio_config(void);
  12. void dac_config(void);
  13. void timer14_config(void);
  14. /*!
  15. \brief main function
  16. \param[in] none
  17. \param[out] none
  18. \retval none
  19. */
  20. int main(void)
  21. {
  22. rcu_config();
  23. gpio_config();
  24. dac_config();
  25. timer14_config();
  26. while (1){
  27. }
  28. }
  29. /*!
  30. \brief configure the RCU of peripherals
  31. \param[in] none
  32. \param[out] none
  33. \retval none
  34. */
  35. void rcu_config(void)
  36. {
  37. /* enable the clock of peripherals */
  38. rcu_periph_clock_enable(RCU_GPIOA);
  39. rcu_periph_clock_enable(RCU_DAC);
  40. rcu_periph_clock_enable(RCU_TIMER14);
  41. }
  42. /*!
  43. \brief configure the related GPIO
  44. \param[in] none
  45. \param[out] none
  46. \retval none
  47. */
  48. void gpio_config(void)
  49. {
  50. /* once enabled the DAC, the corresponding GPIO pin is connected to the DAC converter automatically */
  51. gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_4);
  52. }
  53. /*!
  54. \brief configure the DAC
  55. \param[in] none
  56. \param[out] none
  57. \retval none
  58. */
  59. void dac_config(void)
  60. {
  61. dac_deinit();
  62. /* configure the DAC */
  63. dac_trigger_source_config(DAC_TRIGGER_T14_TRGO);
  64. dac_trigger_enable();
  65. dac_wave_mode_config(DAC_WAVE_MODE_TRIANGLE);
  66. dac_triangle_noise_config(DAC_TRIANGLE_AMPLITUDE_2047);
  67. dac_output_buffer_disable();
  68. /* enable DAC and set data */
  69. dac_enable();
  70. dac_data_set(DAC_ALIGN_12B_R, 0x7F0);
  71. }
  72. /*!
  73. \brief configure the TIMER14
  74. \param[in] none
  75. \param[out] none
  76. \retval none
  77. */
  78. void timer14_config(void)
  79. {
  80. /* configure the TIMER14 */
  81. timer_prescaler_config(TIMER14, 0x10, TIMER_PSC_RELOAD_UPDATE);
  82. timer_autoreload_value_config(TIMER14, 0x1FF);
  83. timer_master_output_trigger_source_select(TIMER14, TIMER_TRI_OUT_SRC_UPDATE);
  84. timer_enable(TIMER14);
  85. }