logic.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. logic_set_out_pwm();
  14. set_button(false);
  15. }
  16. }
  17. //
  18. void logic_set_out_pwm(void)
  19. {
  20. switch (step_number)
  21. {
  22. case 0:
  23. tim_pwm_out_set_pulse(PWM_OUT_CH_1, 1760);
  24. break;
  25. case 1:
  26. tim_pwm_out_set_pulse(PWM_OUT_CH_2, 1760);
  27. break;
  28. case 2:
  29. tim_pwm_out_set_pulse(PWM_OUT_CH_3, 1760);
  30. break;
  31. case 3:
  32. tim_pwm_out_set_pulse(PWM_OUT_CH_4, 1760);
  33. break;
  34. default : break;
  35. }
  36. step_number = step_number == 3 ? 0 : step_number + 1;
  37. }
  38. //
  39. void wdt_init(void)
  40. {
  41. RCC_OscInitTypeDef RCC_OscInitStruct;
  42. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  43. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  44. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  45. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  46. // 1. Enable the IWDG by writing 0x0000 CCCC in the IWDG_KR register.
  47. IWDG->KR = 0xCCCC;
  48. // 2. Enable register access by writing 0x0000 5555 in the IWDG_KR register.
  49. IWDG->KR = 0x5555;
  50. // 3. Write the IWDG prescaler by programming IWDG_PR from 0 to 7.
  51. IWDG->PR = 4;
  52. // 4. Write the reload register (IWDG_RLR).
  53. IWDG->RLR = 1000;
  54. // 5. Wait for the registers to be updated (IWDG_SR = 0x0000 0000).
  55. while (IWDG->SR);
  56. // 6. Refresh the counter value with IWDG_RLR (IWDG_KR = 0x0000 AAAA)
  57. IWDG->KR = 0xAAAA;
  58. }