ledc_example_main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #define LEDC_HS_TIMER LEDC_TIMER_0
  34. #define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
  35. #define LEDC_HS_CH0_GPIO (18)
  36. #define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
  37. #define LEDC_HS_CH1_GPIO (19)
  38. #define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1
  39. #define LEDC_LS_TIMER LEDC_TIMER_1
  40. #define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
  41. #define LEDC_LS_CH2_GPIO (4)
  42. #define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2
  43. #define LEDC_LS_CH3_GPIO (5)
  44. #define LEDC_LS_CH3_CHANNEL LEDC_CHANNEL_3
  45. #define LEDC_TEST_CH_NUM (4)
  46. #define LEDC_TEST_DUTY (4000)
  47. #define LEDC_TEST_FADE_TIME (3000)
  48. void app_main()
  49. {
  50. int ch;
  51. /*
  52. * Prepare and set configuration of timers
  53. * that will be used by LED Controller
  54. */
  55. ledc_timer_config_t ledc_timer = {
  56. .duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
  57. .freq_hz = 5000, // frequency of PWM signal
  58. .speed_mode = LEDC_HS_MODE, // timer mode
  59. .timer_num = LEDC_HS_TIMER, // timer index
  60. .clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
  61. };
  62. // Set configuration of timer0 for high speed channels
  63. ledc_timer_config(&ledc_timer);
  64. // Prepare and set configuration of timer1 for low speed channels
  65. ledc_timer.speed_mode = LEDC_LS_MODE;
  66. ledc_timer.timer_num = LEDC_LS_TIMER;
  67. ledc_timer_config(&ledc_timer);
  68. /*
  69. * Prepare individual configuration
  70. * for each channel of LED Controller
  71. * by selecting:
  72. * - controller's channel number
  73. * - output duty cycle, set initially to 0
  74. * - GPIO number where LED is connected to
  75. * - speed mode, either high or low
  76. * - timer servicing selected channel
  77. * Note: if different channels use one timer,
  78. * then frequency and bit_num of these channels
  79. * will be the same
  80. */
  81. ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
  82. {
  83. .channel = LEDC_HS_CH0_CHANNEL,
  84. .duty = 0,
  85. .gpio_num = LEDC_HS_CH0_GPIO,
  86. .speed_mode = LEDC_HS_MODE,
  87. .hpoint = 0,
  88. .timer_sel = LEDC_HS_TIMER
  89. },
  90. {
  91. .channel = LEDC_HS_CH1_CHANNEL,
  92. .duty = 0,
  93. .gpio_num = LEDC_HS_CH1_GPIO,
  94. .speed_mode = LEDC_HS_MODE,
  95. .hpoint = 0,
  96. .timer_sel = LEDC_HS_TIMER
  97. },
  98. {
  99. .channel = LEDC_LS_CH2_CHANNEL,
  100. .duty = 0,
  101. .gpio_num = LEDC_LS_CH2_GPIO,
  102. .speed_mode = LEDC_LS_MODE,
  103. .hpoint = 0,
  104. .timer_sel = LEDC_LS_TIMER
  105. },
  106. {
  107. .channel = LEDC_LS_CH3_CHANNEL,
  108. .duty = 0,
  109. .gpio_num = LEDC_LS_CH3_GPIO,
  110. .speed_mode = LEDC_LS_MODE,
  111. .hpoint = 0,
  112. .timer_sel = LEDC_LS_TIMER
  113. },
  114. };
  115. // Set LED Controller with previously prepared configuration
  116. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  117. ledc_channel_config(&ledc_channel[ch]);
  118. }
  119. // Initialize fade service.
  120. ledc_fade_func_install(0);
  121. while (1) {
  122. printf("1. LEDC fade up to duty = %d\n", LEDC_TEST_DUTY);
  123. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  124. ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
  125. ledc_channel[ch].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
  126. ledc_fade_start(ledc_channel[ch].speed_mode,
  127. ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
  128. }
  129. vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
  130. printf("2. LEDC fade down to duty = 0\n");
  131. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  132. ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
  133. ledc_channel[ch].channel, 0, LEDC_TEST_FADE_TIME);
  134. ledc_fade_start(ledc_channel[ch].speed_mode,
  135. ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
  136. }
  137. vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
  138. printf("3. LEDC set duty = %d without fade\n", LEDC_TEST_DUTY);
  139. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  140. ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, LEDC_TEST_DUTY);
  141. ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
  142. }
  143. vTaskDelay(1000 / portTICK_PERIOD_MS);
  144. printf("4. LEDC set duty = 0 without fade\n");
  145. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  146. ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, 0);
  147. ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
  148. }
  149. vTaskDelay(1000 / portTICK_PERIOD_MS);
  150. }
  151. }