logic.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "stm32f0xx_hal.h"
  2. #include "logic.h"
  3. #include "pwm_in.h"
  4. #include "gpio.h"
  5. #include "pwm_out.h"
  6. //
  7. void logic_main(void)
  8. {
  9. IWDG->KR = 0xAAAA;
  10. if (get_button())
  11. {
  12. gpio_set_output(true);
  13. HAL_Delay(500);
  14. IWDG->KR = 0xAAAA;
  15. HAL_Delay(500);
  16. IWDG->KR = 0xAAAA;
  17. logic_set_out_pwm();
  18. HAL_Delay(500);
  19. IWDG->KR = 0xAAAA;
  20. gpio_set_output(false);
  21. tim_pwm_pulse_idle();
  22. set_button(false);
  23. }
  24. }
  25. //
  26. void logic_set_out_pwm(void)
  27. {
  28. tim_pwm_out_set_pulse(PWM_OUT_CH_1, 1750);
  29. }
  30. //
  31. void wdt_init(void)
  32. {
  33. RCC_OscInitTypeDef RCC_OscInitStruct;
  34. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  35. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  36. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  37. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  38. // 1. Enable the IWDG by writing 0x0000 CCCC in the IWDG_KR register.
  39. IWDG->KR = 0xCCCC;
  40. // 2. Enable register access by writing 0x0000 5555 in the IWDG_KR register.
  41. IWDG->KR = 0x5555;
  42. // 3. Write the IWDG prescaler by programming IWDG_PR from 0 to 7.
  43. IWDG->PR = 4;
  44. // 4. Write the reload register (IWDG_RLR).
  45. IWDG->RLR = 1000;
  46. // 5. Wait for the registers to be updated (IWDG_SR = 0x0000 0000).
  47. while (IWDG->SR);
  48. // 6. Refresh the counter value with IWDG_RLR (IWDG_KR = 0x0000 AAAA)
  49. IWDG->KR = 0xAAAA;
  50. }