123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- #include "stm32f0xx_hal.h"
- #include "logic.h"
- #include "pwm_in.h"
- #include "gpio.h"
- #include "pwm_out.h"
- #include "button.h"
- #include "led.h"
- #include "tim.h"
- #include <stdbool.h>
- static uint8_t step_number = 0;
- #define CH_NUM 10
- uint8_t channel_index = 0; // текущий индекс канала для срабатывания
- bool string_finished = true;
- channel_cnt_t channel_cnt[CH_NUM];
- channel_t channels[CH_NUM] = {{GPIOA, GPIO_PIN_9},
- {GPIOA, GPIO_PIN_10},
- {GPIOA, GPIO_PIN_11},
- {GPIOA, GPIO_PIN_12},
- {GPIOA, GPIO_PIN_15},
- {GPIOB, GPIO_PIN_3},
- {GPIOB, GPIO_PIN_4},
- {GPIOB, GPIO_PIN_5},
- {GPIOB, GPIO_PIN_6},
- {GPIOB, GPIO_PIN_7}};
- //
- void channel_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
-
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
-
- for (int i = 0; i < CH_NUM; i++)
- {
- GPIO_InitStruct.Pin = channels[i].pin;
- HAL_GPIO_Init(channels[i].port, &GPIO_InitStruct);
-
- HAL_GPIO_WritePin(channels[i].port, channels[i].pin, GPIO_PIN_RESET);
- }
-
- for (int i = 0; i < CH_NUM; i++)
- {
- channel_cnt[i].active = false;
- channel_cnt[i].cnt = 0;
- }
- }
- // Активировать текущий канал
- // Запустить счетчик
- void logic_single(void)
- {
- HAL_GPIO_WritePin(channels[channel_index].port, channels[channel_index].pin, GPIO_PIN_SET);
- channel_cnt[channel_index].active = true;
-
- channel_index++;
-
- if (channel_index == 10) {
- channel_index = 0;
- start_pressed = false;
- }
- }
- void logic_string(void)
- {
- // Проверяем в работе ли строка
- if (string_finished == false) {
- return;
- }
-
- tim_set_string_timer(true);
- }
- //
- void logic_string_next(void)
- {
- HAL_GPIO_WritePin(channels[channel_index].port, channels[channel_index].pin, GPIO_PIN_SET);
- channel_cnt[channel_index].active = true;
-
- channel_index++;
-
- if (channel_index == 10) {
- tim_set_string_timer(false);
- channel_index = 0;
- string_finished = true;
- }
- }
- // Управление LED "одиночный/строка".
- // Сброс каналов через 0.5 секунды.
- void logic_cnt_task(void)
- {
- //if (!but_is_string())
-
- for (int i = 0; i < CH_NUM; i++)
- {
- if (channel_cnt[i].active == true)
- {
- channel_cnt[i].cnt++;
-
- if (channel_cnt[i].cnt >= 50)
- {
- channel_cnt[i].active = false;
- channel_cnt[i].cnt = 0;
- HAL_GPIO_WritePin(channels[i].port, channels[i].pin, GPIO_PIN_RESET);
-
- // Если режим "Одиночный" нужно включить LED "Freq"
- if (!but_is_string()) {
- LED_FREQ_ON
- }
-
- }
- }
- }
- }
- //
- void logic_led_freq(void)
- {
- if (!but_is_string() && start_pressed == false)
- {
- LED_FREQ_ON
- }
- }
- //
- void logic_main(void)
- {
- IWDG->KR = 0xAAAA;
-
- if (get_button())
- {
- gpio_set_output(true);
- HAL_Delay(500);
- IWDG->KR = 0xAAAA;
- HAL_Delay(500);
- IWDG->KR = 0xAAAA;
- logic_set_out_pwm();
- HAL_Delay(500);
- IWDG->KR = 0xAAAA;
- gpio_set_output(false);
- tim_pwm_pulse_idle();
-
- set_button(false);
- }
-
- }
- //
- void logic_set_out_pwm(void)
- {
- switch (step_number)
- {
- case 0:
- tim_pwm_out_set_pulse(PWM_OUT_CH_1, 1300);
- break;
-
- case 1:
- tim_pwm_out_set_pulse(PWM_OUT_CH_2, 1300);
- break;
-
- case 2:
- tim_pwm_out_set_pulse(PWM_OUT_CH_1, 1850);
- break;
-
- case 3:
- tim_pwm_out_set_pulse(PWM_OUT_CH_2, 1850);
- break;
-
- default : break;
- }
-
- step_number = step_number == 3 ? 0 : step_number + 1;
- }
- //
- void wdt_init(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
-
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- RCC_OscInitStruct.LSIState = RCC_LSI_ON;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
-
- // 1. Enable the IWDG by writing 0x0000 CCCC in the IWDG_KR register.
- IWDG->KR = 0xCCCC;
- // 2. Enable register access by writing 0x0000 5555 in the IWDG_KR register.
- IWDG->KR = 0x5555;
- // 3. Write the IWDG prescaler by programming IWDG_PR from 0 to 7.
- IWDG->PR = 4;
- // 4. Write the reload register (IWDG_RLR).
- IWDG->RLR = 1000;
- // 5. Wait for the registers to be updated (IWDG_SR = 0x0000 0000).
- while (IWDG->SR);
- // 6. Refresh the counter value with IWDG_RLR (IWDG_KR = 0x0000 AAAA)
- IWDG->KR = 0xAAAA;
- }
|