ledc_example_main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Ledc fade example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/xtensa_api.h"
  11. #include "freertos/queue.h"
  12. #include "driver/ledc.h"
  13. #include "esp_attr.h"
  14. #include "esp_err.h"
  15. /*
  16. * About this example
  17. * 1. init LEDC module:
  18. * a. You need to set the timer of LEDC first, this decide the frequency and resolution of PWM.
  19. * b. You need to set the LEDC channel you want to use, and bind with one of the timers.
  20. *
  21. * 2. You can install a default fade function, then you can use fade APIs.
  22. *
  23. * 3. You can also set a target duty directly without fading.
  24. *
  25. * 4. This example use GPIO18/19/4/5 as LEDC ouput, and it will change the duty repeatedly.
  26. *
  27. * 5. GPIO18/19 are from high speed channel group. GPIO4/5 are from low speed channel group.
  28. *
  29. */
  30. #define LEDC_HS_TIMER LEDC_TIMER_0
  31. #define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
  32. #define LEDC_HS_CH0_GPIO (18)
  33. #define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
  34. #define LEDC_HS_CH1_GPIO (19)
  35. #define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1
  36. #define LEDC_LS_TIMER LEDC_TIMER_1
  37. #define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
  38. #define LEDC_LS_CH2_GPIO (4)
  39. #define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2
  40. #define LEDC_LS_CH3_GPIO (5)
  41. #define LEDC_LS_CH3_CHANNEL LEDC_CHANNEL_3
  42. #define LEDC_TEST_CH_NUM (4)
  43. typedef struct {
  44. int channel;
  45. int io;
  46. int mode;
  47. int timer_idx;
  48. } ledc_info_t;
  49. void app_main()
  50. {
  51. int ch;
  52. ledc_info_t ledc_ch[LEDC_TEST_CH_NUM] = {
  53. {
  54. .channel = LEDC_HS_CH0_CHANNEL,
  55. .io = LEDC_HS_CH0_GPIO,
  56. .mode = LEDC_HS_MODE,
  57. .timer_idx = LEDC_HS_TIMER
  58. },
  59. {
  60. .channel = LEDC_HS_CH1_CHANNEL,
  61. .io = LEDC_HS_CH1_GPIO,
  62. .mode = LEDC_HS_MODE,
  63. .timer_idx = LEDC_HS_TIMER
  64. },
  65. {
  66. .channel = LEDC_LS_CH2_CHANNEL,
  67. .io = LEDC_LS_CH2_GPIO,
  68. .mode = LEDC_LS_MODE,
  69. .timer_idx = LEDC_LS_TIMER
  70. },
  71. {
  72. .channel = LEDC_LS_CH3_CHANNEL,
  73. .io = LEDC_LS_CH3_GPIO,
  74. .mode = LEDC_LS_MODE,
  75. .timer_idx = LEDC_LS_TIMER
  76. }
  77. };
  78. ledc_timer_config_t ledc_timer = {
  79. .bit_num = LEDC_TIMER_13_BIT, //set timer counter bit number
  80. .freq_hz = 5000, //set frequency of pwm
  81. .speed_mode = LEDC_HS_MODE, //timer mode,
  82. .timer_num = LEDC_HS_TIMER //timer index
  83. };
  84. //configure timer0 for high speed channels
  85. ledc_timer_config(&ledc_timer);
  86. //configure timer1 for low speed channels
  87. ledc_timer.speed_mode = LEDC_LS_MODE;
  88. ledc_timer.timer_num = LEDC_LS_TIMER;
  89. ledc_timer_config(&ledc_timer);
  90. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  91. ledc_channel_config_t ledc_channel = {
  92. //set LEDC channel 0
  93. .channel = ledc_ch[ch].channel,
  94. //set the duty for initialization.(duty range is 0 ~ ((2**bit_num)-1)
  95. .duty = 0,
  96. //GPIO number
  97. .gpio_num = ledc_ch[ch].io,
  98. //GPIO INTR TYPE, as an example, we enable fade_end interrupt here.
  99. .intr_type = LEDC_INTR_FADE_END,
  100. //set LEDC mode, from ledc_mode_t
  101. .speed_mode = ledc_ch[ch].mode,
  102. //set LEDC timer source, if different channel use one timer,
  103. //the frequency and bit_num of these channels should be the same
  104. .timer_sel = ledc_ch[ch].timer_idx,
  105. };
  106. //set the configuration
  107. ledc_channel_config(&ledc_channel);
  108. }
  109. //initialize fade service.
  110. ledc_fade_func_install(0);
  111. while (1) {
  112. printf("LEDC fade up\n");
  113. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  114. ledc_set_fade_with_time(ledc_ch[ch].mode, ledc_ch[ch].channel, 4000, 2000);
  115. ledc_fade_start(ledc_ch[ch].mode, ledc_ch[ch].channel, LEDC_FADE_NO_WAIT);
  116. }
  117. vTaskDelay(3000 / portTICK_PERIOD_MS);
  118. printf("LEDC fade down\n");
  119. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  120. ledc_set_fade_with_time(ledc_ch[ch].mode, ledc_ch[ch].channel, 0, 2000);
  121. ledc_fade_start(ledc_ch[ch].mode, ledc_ch[ch].channel, LEDC_FADE_NO_WAIT);
  122. }
  123. vTaskDelay(3000 / portTICK_PERIOD_MS);
  124. printf("LEDC set duty without fade\n");
  125. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  126. ledc_set_duty(ledc_ch[ch].mode, ledc_ch[ch].channel, 2000);
  127. ledc_update_duty(ledc_ch[ch].mode, ledc_ch[ch].channel);
  128. }
  129. vTaskDelay(1000 / portTICK_PERIOD_MS);
  130. printf("LEDC set duty without fade\n");
  131. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  132. ledc_set_duty(ledc_ch[ch].mode, ledc_ch[ch].channel, 0);
  133. ledc_update_duty(ledc_ch[ch].mode, ledc_ch[ch].channel);
  134. }
  135. vTaskDelay(1000 / portTICK_PERIOD_MS);
  136. }
  137. }