main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*!
  2. \file main.c
  3. \brief debug TIMER1 when the MCU is in debug mode
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include "gd32f3x0_eval.h"
  11. #include "systick.h"
  12. void gpio_configuration(void);
  13. void timer_configuration(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_periph_clock_enable(RCU_GPIOB);
  23. gpio_configuration();
  24. gd_eval_led_init(LED1);
  25. timer_configuration();
  26. /* keep timer1 counter when the mcu is in debug mode */
  27. dbg_periph_enable(DBG_TIMER1_HOLD);
  28. while (1)
  29. {
  30. /* toggle LED1 output status every second */
  31. gd_eval_led_toggle(LED1);
  32. /* you can set breakpoint here, then look over the register in timer1 */
  33. delay_1ms(1000);
  34. }
  35. }
  36. /*!
  37. \brief configure the GPIO ports
  38. \param[in] none
  39. \param[out] none
  40. \retval none
  41. */
  42. void gpio_configuration(void)
  43. {
  44. /*Configure PB3 PB10 PB11(TIMER1 CH1 CH2 CH3) as alternate function*/
  45. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
  46. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);
  47. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
  48. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  49. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
  50. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
  51. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_3);
  52. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_10);
  53. gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_11);
  54. }
  55. /*!
  56. \brief configure the TIMER peripheral
  57. \param[in] none
  58. \param[out] none
  59. \retval none
  60. */
  61. void timer_configuration(void)
  62. {
  63. /* TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
  64. TIMER1CLK = SystemCoreClock / 72 = 1MHz
  65. TIMER1 Channel1 duty cycle = 4000/ 16000 = 25%
  66. TIMER1 Channel2 duty cycle = 8000/ 16000 = 50%
  67. TIMER1 Channel3 duty cycle = 12000/ 16000 = 75% */
  68. timer_parameter_struct timer_initpara;
  69. timer_oc_parameter_struct timer_ocintpara;
  70. rcu_periph_clock_enable(RCU_TIMER1);
  71. /* TIMER1 configuration */
  72. timer_deinit(TIMER1);
  73. /* TIMER1 configuration */
  74. timer_initpara.prescaler = 71;
  75. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  76. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  77. timer_initpara.period = 15999;
  78. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  79. timer_initpara.repetitioncounter = 0;
  80. timer_init(TIMER1,&timer_initpara);
  81. /* CH1,CH2 and CH3 configuration in PWM mode */
  82. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  83. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  84. timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
  85. timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
  86. timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);
  87. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,3999);
  88. timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM1);
  89. timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  90. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,7999);
  91. timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM1);
  92. timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
  93. timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,11999);
  94. timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM1);
  95. timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
  96. /* auto-reload preload enable */
  97. timer_auto_reload_shadow_enable(TIMER1);
  98. timer_enable(TIMER1);
  99. }