ledc_example_main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* LEDC (LED Controller) 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 "driver/ledc.h"
  11. #include "esp_err.h"
  12. /*
  13. * About this example
  14. *
  15. * 1. Start with initializing LEDC module:
  16. * a. Set the timer of LEDC first, this determines the frequency
  17. * and resolution of PWM.
  18. * b. Then set the LEDC channel you want to use,
  19. * and bind with one of the timers.
  20. *
  21. * 2. You need first to install a default fade function,
  22. * then you can use fade APIs.
  23. *
  24. * 3. You can also set a target duty directly without fading.
  25. *
  26. * 4. This example uses GPIO18/19/4/5 as LEDC output,
  27. * and it will change the duty repeatedly.
  28. *
  29. * 5. GPIO18/19 are from high speed channel group.
  30. * GPIO4/5 are from low speed channel group.
  31. *
  32. */
  33. #ifdef CONFIG_IDF_TARGET_ESP32
  34. #define LEDC_HS_TIMER LEDC_TIMER_0
  35. #define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
  36. #define LEDC_HS_CH0_GPIO (18)
  37. #define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
  38. #define LEDC_HS_CH1_GPIO (19)
  39. #define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1
  40. #endif
  41. #define LEDC_LS_TIMER LEDC_TIMER_1
  42. #define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
  43. #ifdef CONFIG_IDF_TARGET_ESP32S2
  44. #define LEDC_LS_CH0_GPIO (18)
  45. #define LEDC_LS_CH0_CHANNEL LEDC_CHANNEL_0
  46. #define LEDC_LS_CH1_GPIO (19)
  47. #define LEDC_LS_CH1_CHANNEL LEDC_CHANNEL_1
  48. #endif
  49. #define LEDC_LS_CH2_GPIO (4)
  50. #define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2
  51. #define LEDC_LS_CH3_GPIO (5)
  52. #define LEDC_LS_CH3_CHANNEL LEDC_CHANNEL_3
  53. #define LEDC_TEST_CH_NUM (4)
  54. #define LEDC_TEST_DUTY (4000)
  55. #define LEDC_TEST_FADE_TIME (3000)
  56. void app_main(void)
  57. {
  58. int ch;
  59. /*
  60. * Prepare and set configuration of timers
  61. * that will be used by LED Controller
  62. */
  63. ledc_timer_config_t ledc_timer = {
  64. .duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
  65. .freq_hz = 5000, // frequency of PWM signal
  66. .speed_mode = LEDC_LS_MODE, // timer mode
  67. .timer_num = LEDC_LS_TIMER, // timer index
  68. .clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
  69. };
  70. // Set configuration of timer0 for high speed channels
  71. ledc_timer_config(&ledc_timer);
  72. #ifdef CONFIG_IDF_TARGET_ESP32
  73. // Prepare and set configuration of timer1 for low speed channels
  74. ledc_timer.speed_mode = LEDC_HS_MODE;
  75. ledc_timer.timer_num = LEDC_HS_TIMER;
  76. ledc_timer_config(&ledc_timer);
  77. #endif
  78. /*
  79. * Prepare individual configuration
  80. * for each channel of LED Controller
  81. * by selecting:
  82. * - controller's channel number
  83. * - output duty cycle, set initially to 0
  84. * - GPIO number where LED is connected to
  85. * - speed mode, either high or low
  86. * - timer servicing selected channel
  87. * Note: if different channels use one timer,
  88. * then frequency and bit_num of these channels
  89. * will be the same
  90. */
  91. ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
  92. #ifdef CONFIG_IDF_TARGET_ESP32
  93. {
  94. .channel = LEDC_HS_CH0_CHANNEL,
  95. .duty = 0,
  96. .gpio_num = LEDC_HS_CH0_GPIO,
  97. .speed_mode = LEDC_HS_MODE,
  98. .hpoint = 0,
  99. .timer_sel = LEDC_HS_TIMER
  100. },
  101. {
  102. .channel = LEDC_HS_CH1_CHANNEL,
  103. .duty = 0,
  104. .gpio_num = LEDC_HS_CH1_GPIO,
  105. .speed_mode = LEDC_HS_MODE,
  106. .hpoint = 0,
  107. .timer_sel = LEDC_HS_TIMER
  108. },
  109. #elif defined CONFIG_IDF_TARGET_ESP32S2
  110. {
  111. .channel = LEDC_LS_CH0_CHANNEL,
  112. .duty = 0,
  113. .gpio_num = LEDC_LS_CH0_GPIO,
  114. .speed_mode = LEDC_LS_MODE,
  115. .hpoint = 0,
  116. .timer_sel = LEDC_LS_TIMER
  117. },
  118. {
  119. .channel = LEDC_LS_CH1_CHANNEL,
  120. .duty = 0,
  121. .gpio_num = LEDC_LS_CH1_GPIO,
  122. .speed_mode = LEDC_LS_MODE,
  123. .hpoint = 0,
  124. .timer_sel = LEDC_LS_TIMER
  125. },
  126. #endif
  127. {
  128. .channel = LEDC_LS_CH2_CHANNEL,
  129. .duty = 0,
  130. .gpio_num = LEDC_LS_CH2_GPIO,
  131. .speed_mode = LEDC_LS_MODE,
  132. .hpoint = 0,
  133. .timer_sel = LEDC_LS_TIMER
  134. },
  135. {
  136. .channel = LEDC_LS_CH3_CHANNEL,
  137. .duty = 0,
  138. .gpio_num = LEDC_LS_CH3_GPIO,
  139. .speed_mode = LEDC_LS_MODE,
  140. .hpoint = 0,
  141. .timer_sel = LEDC_LS_TIMER
  142. },
  143. };
  144. // Set LED Controller with previously prepared configuration
  145. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  146. ledc_channel_config(&ledc_channel[ch]);
  147. }
  148. // Initialize fade service.
  149. ledc_fade_func_install(0);
  150. while (1) {
  151. printf("1. LEDC fade up to duty = %d\n", LEDC_TEST_DUTY);
  152. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  153. ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
  154. ledc_channel[ch].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
  155. ledc_fade_start(ledc_channel[ch].speed_mode,
  156. ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
  157. }
  158. vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
  159. printf("2. LEDC fade down to duty = 0\n");
  160. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  161. ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
  162. ledc_channel[ch].channel, 0, LEDC_TEST_FADE_TIME);
  163. ledc_fade_start(ledc_channel[ch].speed_mode,
  164. ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
  165. }
  166. vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
  167. printf("3. LEDC set duty = %d without fade\n", LEDC_TEST_DUTY);
  168. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  169. ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, LEDC_TEST_DUTY);
  170. ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
  171. }
  172. vTaskDelay(1000 / portTICK_PERIOD_MS);
  173. printf("4. LEDC set duty = 0 without fade\n");
  174. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  175. ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, 0);
  176. ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
  177. }
  178. vTaskDelay(1000 / portTICK_PERIOD_MS);
  179. }
  180. }