You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. /**
  2. * @file heating_plate.c
  3. * @author Finny (tianjialong0106@163.com)
  4. * @brief
  5. * @version 0.1
  6. * @date 2022-09-26
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #include "heating_plate.h"
  12. #include "driver/ledc.h"
  13. #include "esp_err.h"
  14. #define LEDC_TIMER LEDC_TIMER_0 //
  15. #define LEDC_MODE LEDC_LOW_SPEED_MODE //
  16. #define LEDC_OUTPUT_IO (20) // Define the output GPIO
  17. #define LEDC_CHANNEL LEDC_CHANNEL_0 //
  18. #define LEDC_DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
  19. #define LEDC_DUTY (4095) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095
  20. #define LEDC_FREQUENCY (5000) // Frequency in Hertz. Set frequency at 5 kHz
  21. #define heating_plate_target_temp 48.0
  22. #define heating_plate_variable_temperature_range 0.5
  23. static heating_plate_structer_t *heating_plate_structer_s;
  24. static get_temp_callback_t get_temp_cb_s;
  25. void T_heating_plate_init(heating_plate_structer_t *heating_plate_structer)
  26. {
  27. heating_plate_structer_s = heating_plate_structer;
  28. // Prepare and then apply the LEDC PWM timer configuration
  29. ledc_timer_config_t ledc_timer = {
  30. .speed_mode = LEDC_MODE,
  31. .timer_num = LEDC_TIMER,
  32. .duty_resolution = LEDC_DUTY_RES,
  33. .freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
  34. .clk_cfg = LEDC_AUTO_CLK};
  35. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  36. // Prepare and then apply the LEDC PWM channel configuration
  37. ledc_channel_config_t ledc_channel = {
  38. .speed_mode = LEDC_MODE,
  39. .channel = LEDC_CHANNEL,
  40. .timer_sel = LEDC_TIMER,
  41. .intr_type = LEDC_INTR_DISABLE,
  42. .gpio_num = LEDC_OUTPUT_IO,
  43. .duty = 0, // Set duty to 0%
  44. .hpoint = 0};
  45. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  46. }
  47. void T_heating_plate_registered_cb(get_temp_callback_t cb) { get_temp_cb_s = cb; }
  48. void T_heating_plate_start(void)
  49. {
  50. T_heating_plate_set_and_update_duty(LEDC_DUTY);
  51. }
  52. void T_heating_plate_stop(void)
  53. {
  54. T_heating_plate_set_and_update_duty(0);
  55. }
  56. void T_heating_plate_schedule(void)
  57. {
  58. if (heating_plate_structer_s->heating_plate_preheat_start_flag)
  59. {
  60. if (get_temp_cb_s() >= heating_plate_target_temp + heating_plate_variable_temperature_range)
  61. {
  62. T_heating_plate_stop();
  63. }
  64. else if (get_temp_cb_s() <= heating_plate_target_temp - heating_plate_variable_temperature_range)
  65. {
  66. T_heating_plate_start();
  67. }
  68. }
  69. }
  70. void T_heating_plate_set_and_update_duty(uint32_t duty)
  71. {
  72. // Set duty
  73. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, duty));
  74. // Update duty to apply the new value
  75. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  76. }