main.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*!
  2. \file main.c
  3. \brief CMP output timer input capture
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include <stdio.h>
  11. #include "gd32f3x0_eval.h"
  12. void rcu_config(void);
  13. void gpio_config(void);
  14. void timer_config(void);
  15. /*!
  16. \brief main function
  17. \param[in] none
  18. \param[out] none
  19. \retval none
  20. */
  21. int main(void)
  22. {
  23. /* configure RCU */
  24. rcu_config();
  25. /* configure GPIO */
  26. gpio_config();
  27. /* configure leds */
  28. gd_eval_led_init(LED3);
  29. /* configure comparator channel0 */
  30. cmp_mode_init(CMP0, CMP_VERYLOWSPEED, CMP_1_4VREFINT, CMP_HYSTERESIS_NO);
  31. cmp_output_init(CMP0, CMP_OUTPUT_TIMER1IC3, CMP_OUTPUT_POLARITY_NOINVERTED);
  32. /* configure TIMER */
  33. timer_config();
  34. /* enable comparator channel0 */
  35. cmp_enable(CMP0);
  36. while(1);
  37. }
  38. /*!
  39. \brief configure RCU
  40. \param[in] none
  41. \param[out] none
  42. \retval none
  43. */
  44. void rcu_config(void)
  45. {
  46. rcu_periph_clock_enable(RCU_GPIOA);
  47. rcu_periph_clock_enable(RCU_GPIOB);
  48. rcu_periph_clock_enable(RCU_GPIOC);
  49. rcu_periph_clock_enable(RCU_TIMER1);
  50. rcu_periph_clock_enable(RCU_CFGCMP);
  51. }
  52. /*!
  53. \brief configure GPIO
  54. \param[in] none
  55. \param[out] none
  56. \retval none
  57. */
  58. void gpio_config(void)
  59. {
  60. /* configure PB11 */
  61. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11) ;
  62. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_11);
  63. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_11);
  64. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1) ;
  65. gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_PULLUP, GPIO_PIN_1);
  66. }
  67. /*!
  68. \brief configure TIMER
  69. \param[in] none
  70. \param[out] none
  71. \retval none
  72. */
  73. void timer_config(void)
  74. {
  75. /* initialize TIMER1 */
  76. timer_parameter_struct timer_init_parameter;
  77. timer_init_parameter.prescaler = 0;
  78. timer_init_parameter.counterdirection = TIMER_COUNTER_UP;
  79. timer_init_parameter.period = 65535;
  80. timer_init_parameter.clockdivision = TIMER_CKDIV_DIV1;
  81. timer_init(TIMER1, &timer_init_parameter);
  82. /* clear flag */
  83. timer_flag_clear(TIMER1, TIMER_FLAG_UP);
  84. nvic_irq_enable(TIMER1_IRQn, 0, 0);
  85. /* reset TIMER1 interrupt flag register */
  86. TIMER_INTF(TIMER1) = 0;
  87. timer_interrupt_enable(TIMER1, TIMER_INT_CH3);
  88. /* enable TIMER1 counter */
  89. timer_enable(TIMER1);
  90. }