Advertisement
Guest User

Untitled

a guest
Dec 29th, 2019
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <stm32f4xx.h>
  2.  
  3. const uint16_t TIM_PRESCALER = 84000;
  4. const uint32_t TIM_PERIOD = 1000;
  5.  
  6. static void init_timers(void) {
  7. TIM_TimeBaseInitTypeDef timer_struct;
  8.  
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  10.  
  11. timer_struct.TIM_Prescaler = TIM_PRESCALER - 1; /* Scale value to microseconds */
  12. timer_struct.TIM_CounterMode = TIM_CounterMode_Up;
  13. timer_struct.TIM_Period = TIM_PERIOD - 1; /* Gives us a second interval */
  14. timer_struct.TIM_ClockDivision = TIM_CKD_DIV1; /* Tell timer to divide clocks */
  15. timer_struct.TIM_RepetitionCounter = 0;
  16.  
  17. TIM_TimeBaseInit(TIM2, &timer_struct);
  18.  
  19. // возьмем самое первое в списке TIM_IT_Update
  20. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  21.  
  22. // misc.h
  23. NVIC_InitTypeDef nvic_struct;
  24.  
  25. nvic_struct.NVIC_IRQChannel = TIM2_IRQn; // какое прерывание включить
  26. nvic_struct.NVIC_IRQChannelPreemptionPriority = 0; // приоритеты
  27. nvic_struct.NVIC_IRQChannelSubPriority = 1;
  28. nvic_struct.NVIC_IRQChannelCmd = ENABLE;
  29.  
  30. NVIC_Init(&nvic_struct);
  31.  
  32. /* После всех инициализаций включаем таймер */
  33. TIM_Cmd(TIM2, ENABLE);
  34. }
  35.  
  36. void TIM2_IRQHandler(void) {
  37. // проверяем какое событие вызвало прерывание
  38.  
  39. if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
  40. // обязательно сбросить прерывание !!!
  41. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  42. static int j = 0;
  43. j++;
  44. if (j >= 3)
  45. j = 0;
  46.  
  47. switch (j)
  48. {
  49. case 0:
  50. {
  51. GPIO_ResetBits(GPIOD, GPIO_Pin_14 );
  52. GPIO_SetBits(GPIOD, GPIO_Pin_12 );
  53. break;
  54. }
  55. case 1:
  56. {
  57. GPIO_ResetBits(GPIOD, GPIO_Pin_12 );
  58. GPIO_SetBits(GPIOD, GPIO_Pin_13 );
  59. break;
  60. }
  61. case 2:
  62. {
  63. GPIO_ResetBits(GPIOD, GPIO_Pin_13 );
  64. GPIO_SetBits(GPIOD, GPIO_Pin_14 );
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. int main(void) {
  71. setup_clock(HSE);
  72. init_leds();
  73. init_button();
  74. init_timers();
  75.  
  76. while (1) {
  77.  
  78. }
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement