logic.c 1.9 KB

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