logic.c 5.1 KB

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