logic.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "stm32f0xx_hal.h"
  2. #include "logic.h"
  3. #include "pwm_in.h"
  4. #include "gpio.h"
  5. #include "pwm_out.h"
  6. #include "button.h"
  7. #include "led.h"
  8. static uint8_t step_number = 0;
  9. #define CH_NUM 10
  10. uint8_t channel_index = 0; // текущий индекс канала для срабатывания
  11. bool string_finished = true;
  12. channel_cnt_t channel_cnt[CH_NUM];
  13. channel_t channels[CH_NUM] = {{GPIOA, GPIO_PIN_9},
  14. {GPIOA, GPIO_PIN_10},
  15. {GPIOA, GPIO_PIN_11},
  16. {GPIOA, GPIO_PIN_12},
  17. {GPIOA, GPIO_PIN_15},
  18. {GPIOB, GPIO_PIN_7},
  19. {GPIOB, GPIO_PIN_6},
  20. {GPIOB, GPIO_PIN_5},
  21. {GPIOB, GPIO_PIN_4},
  22. {GPIOB, GPIO_PIN_3}};
  23. //
  24. void channel_init(void)
  25. {
  26. GPIO_InitTypeDef GPIO_InitStruct = {0};
  27. __HAL_RCC_GPIOA_CLK_ENABLE();
  28. __HAL_RCC_GPIOB_CLK_ENABLE();
  29. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  30. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  31. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  32. for (int i = 0; i < CH_NUM; i++)
  33. {
  34. GPIO_InitStruct.Pin = channels[i].pin;
  35. HAL_GPIO_Init(channels[i].port, &GPIO_InitStruct);
  36. HAL_GPIO_WritePin(channels[i].port, channels[i].pin, GPIO_PIN_RESET);
  37. }
  38. for (int i = 0; i < CH_NUM; i++)
  39. {
  40. channel_cnt[i].active = false;
  41. channel_cnt[i].cnt = 0;
  42. }
  43. }
  44. // Активировать текущий канал
  45. // Запустить счетчик
  46. void logic_single(void)
  47. {
  48. HAL_GPIO_WritePin(channels[channel_index].port, channels[channel_index].pin, GPIO_PIN_SET);
  49. channel_cnt[channel_index].active = true;
  50. channel_index++;
  51. if (channel_index == 10)
  52. channel_index = 0;
  53. }
  54. void logic_string(void)
  55. {
  56. // Проверяем в работе ли строка
  57. if (string_finished == false) {
  58. return;
  59. }
  60. HAL_GPIO_WritePin(channels[0].port, channels[0].pin, GPIO_PIN_SET);
  61. channel_cnt[0].active = true;
  62. channel_index++;
  63. }
  64. // Управление LED "одиночный/строка".
  65. // Сброс каналов через 0.5 секунды.
  66. void logic_cnt_task(void)
  67. {
  68. //if (!but_is_string())
  69. for (int i = 0; i < CH_NUM; i++)
  70. {
  71. if (channel_cnt[i].active == true)
  72. {
  73. channel_cnt[i].cnt++;
  74. if (channel_cnt[i].cnt >= 50)
  75. {
  76. channel_cnt[i].active = false;
  77. channel_cnt[i].cnt = 0;
  78. HAL_GPIO_WritePin(channels[i].port, channels[i].pin, GPIO_PIN_RESET);
  79. // Если режим "Одиночный" нужно включить LED "Freq"
  80. if (!but_is_string()) {
  81. LED_FREQ_ON
  82. }
  83. }
  84. }
  85. }
  86. }
  87. //
  88. void logic_main(void)
  89. {
  90. IWDG->KR = 0xAAAA;
  91. if (get_button())
  92. {
  93. gpio_set_output(true);
  94. HAL_Delay(500);
  95. IWDG->KR = 0xAAAA;
  96. HAL_Delay(500);
  97. IWDG->KR = 0xAAAA;
  98. logic_set_out_pwm();
  99. HAL_Delay(500);
  100. IWDG->KR = 0xAAAA;
  101. gpio_set_output(false);
  102. tim_pwm_pulse_idle();
  103. set_button(false);
  104. }
  105. }
  106. //
  107. void logic_set_out_pwm(void)
  108. {
  109. switch (step_number)
  110. {
  111. case 0:
  112. tim_pwm_out_set_pulse(PWM_OUT_CH_1, 1300);
  113. break;
  114. case 1:
  115. tim_pwm_out_set_pulse(PWM_OUT_CH_2, 1300);
  116. break;
  117. case 2:
  118. tim_pwm_out_set_pulse(PWM_OUT_CH_1, 1850);
  119. break;
  120. case 3:
  121. tim_pwm_out_set_pulse(PWM_OUT_CH_2, 1850);
  122. break;
  123. default : break;
  124. }
  125. step_number = step_number == 3 ? 0 : step_number + 1;
  126. }
  127. //
  128. void wdt_init(void)
  129. {
  130. RCC_OscInitTypeDef RCC_OscInitStruct;
  131. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  132. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  133. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  134. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  135. // 1. Enable the IWDG by writing 0x0000 CCCC in the IWDG_KR register.
  136. IWDG->KR = 0xCCCC;
  137. // 2. Enable register access by writing 0x0000 5555 in the IWDG_KR register.
  138. IWDG->KR = 0x5555;
  139. // 3. Write the IWDG prescaler by programming IWDG_PR from 0 to 7.
  140. IWDG->PR = 4;
  141. // 4. Write the reload register (IWDG_RLR).
  142. IWDG->RLR = 1000;
  143. // 5. Wait for the registers to be updated (IWDG_SR = 0x0000 0000).
  144. while (IWDG->SR);
  145. // 6. Refresh the counter value with IWDG_RLR (IWDG_KR = 0x0000 AAAA)
  146. IWDG->KR = 0xAAAA;
  147. }