ledc_example_main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. };
  61. // Set configuration of timer0 for high speed channels
  62. ledc_timer_config(&ledc_timer);
  63. // Prepare and set configuration of timer1 for low speed channels
  64. ledc_timer.speed_mode = LEDC_LS_MODE;
  65. ledc_timer.timer_num = LEDC_LS_TIMER;
  66. ledc_timer_config(&ledc_timer);
  67. /*
  68. * Prepare individual configuration
  69. * for each channel of LED Controller
  70. * by selecting:
  71. * - controller's channel number
  72. * - output duty cycle, set initially to 0
  73. * - GPIO number where LED is connected to
  74. * - speed mode, either high or low
  75. * - timer servicing selected channel
  76. * Note: if different channels use one timer,
  77. * then frequency and bit_num of these channels
  78. * will be the same
  79. */
  80. ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
  81. {
  82. .channel = LEDC_HS_CH0_CHANNEL,
  83. .duty = 0,
  84. .gpio_num = LEDC_HS_CH0_GPIO,
  85. .speed_mode = LEDC_HS_MODE,
  86. .timer_sel = LEDC_HS_TIMER
  87. },
  88. {
  89. .channel = LEDC_HS_CH1_CHANNEL,
  90. .duty = 0,
  91. .gpio_num = LEDC_HS_CH1_GPIO,
  92. .speed_mode = LEDC_HS_MODE,
  93. .timer_sel = LEDC_HS_TIMER
  94. },
  95. {
  96. .channel = LEDC_LS_CH2_CHANNEL,
  97. .duty = 0,
  98. .gpio_num = LEDC_LS_CH2_GPIO,
  99. .speed_mode = LEDC_LS_MODE,
  100. .timer_sel = LEDC_LS_TIMER
  101. },
  102. {
  103. .channel = LEDC_LS_CH3_CHANNEL,
  104. .duty = 0,
  105. .gpio_num = LEDC_LS_CH3_GPIO,
  106. .speed_mode = LEDC_LS_MODE,
  107. .timer_sel = LEDC_LS_TIMER
  108. },
  109. };
  110. // Set LED Controller with previously prepared configuration
  111. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  112. ledc_channel_config(&ledc_channel[ch]);
  113. }
  114. // Initialize fade service.
  115. ledc_fade_func_install(0);
  116. while (1) {
  117. printf("1. LEDC fade up to duty = %d\n", LEDC_TEST_DUTY);
  118. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  119. ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
  120. ledc_channel[ch].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
  121. ledc_fade_start(ledc_channel[ch].speed_mode,
  122. ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
  123. }
  124. vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
  125. printf("2. LEDC fade down to duty = 0\n");
  126. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  127. ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
  128. ledc_channel[ch].channel, 0, LEDC_TEST_FADE_TIME);
  129. ledc_fade_start(ledc_channel[ch].speed_mode,
  130. ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
  131. }
  132. vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
  133. printf("3. LEDC set duty = %d without fade\n", LEDC_TEST_DUTY);
  134. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  135. ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, LEDC_TEST_DUTY);
  136. ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
  137. }
  138. vTaskDelay(1000 / portTICK_PERIOD_MS);
  139. printf("4. LEDC set duty = 0 without fade\n");
  140. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  141. ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, 0);
  142. ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
  143. }
  144. vTaskDelay(1000 / portTICK_PERIOD_MS);
  145. }
  146. }