mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-19 17:35:54 +08:00
添加智能灯固件代码
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(ledc)
|
||||
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := ledc
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
# _LEDC Example_
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example shows how to control intensity of LEDs using ESP32's on-board hardware LED PWM Controller module.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP32 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
|
||||
* A USB cable for power supply and programming
|
||||
|
||||
Connect four LEDs to the following LEDC channels / individual GPIOs:
|
||||
|
||||
|ledc channel|GPIO|
|
||||
|:---:|:---:|
|
||||
|channel 0|GPIO18|
|
||||
|channel 1|GPIO19|
|
||||
|channel 2|GPIO4|
|
||||
|channel 3|GPIO5|
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
Running this example, you will see each ledc's brightness changes differently
|
||||
|
||||
* LEDC 1: Fade up / increase intensity
|
||||
* LEDC 2: Fade down / decrease intensity
|
||||
* LEDC 3: Keep a stable intensity
|
||||
* LEDC 4: LED is not on
|
||||
|
||||
you can also see the following output log on the serial monitor:
|
||||
|
||||
```
|
||||
1. LEDC fade up to duty = 4000
|
||||
2. LEDC fade down to duty = 0
|
||||
3. LEDC set duty = 4000 without fade
|
||||
4. LEDC set duty = 0 without fade
|
||||
...
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
* Programming fail
|
||||
|
||||
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are any output logs.
|
||||
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
|
||||
|
||||
For any technical queries, please open an [issue] (https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "ledc_example_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
||||
@@ -0,0 +1,192 @@
|
||||
/* LEDC (LED Controller) fade example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/ledc.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
/*
|
||||
* About this example
|
||||
*
|
||||
* 1. Start with initializing LEDC module:
|
||||
* a. Set the timer of LEDC first, this determines the frequency
|
||||
* and resolution of PWM.
|
||||
* b. Then set the LEDC channel you want to use,
|
||||
* and bind with one of the timers.
|
||||
*
|
||||
* 2. You need first to install a default fade function,
|
||||
* then you can use fade APIs.
|
||||
*
|
||||
* 3. You can also set a target duty directly without fading.
|
||||
*
|
||||
* 4. This example uses GPIO18/19/4/5 as LEDC output,
|
||||
* and it will change the duty repeatedly.
|
||||
*
|
||||
* 5. GPIO18/19 are from high speed channel group.
|
||||
* GPIO4/5 are from low speed channel group.
|
||||
*
|
||||
*/
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
#define LEDC_HS_TIMER LEDC_TIMER_0
|
||||
#define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
|
||||
#define LEDC_HS_CH0_GPIO (18)
|
||||
#define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
|
||||
#define LEDC_HS_CH1_GPIO (19)
|
||||
#define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1
|
||||
#endif
|
||||
#define LEDC_LS_TIMER LEDC_TIMER_1
|
||||
#define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32S2
|
||||
#define LEDC_LS_CH0_GPIO (18)
|
||||
#define LEDC_LS_CH0_CHANNEL LEDC_CHANNEL_0
|
||||
#define LEDC_LS_CH1_GPIO (19)
|
||||
#define LEDC_LS_CH1_CHANNEL LEDC_CHANNEL_1
|
||||
#endif
|
||||
#define LEDC_LS_CH2_GPIO (4)
|
||||
#define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2
|
||||
#define LEDC_LS_CH3_GPIO (5)
|
||||
#define LEDC_LS_CH3_CHANNEL LEDC_CHANNEL_3
|
||||
|
||||
#define LEDC_TEST_CH_NUM (4)
|
||||
#define LEDC_TEST_DUTY (4000)
|
||||
#define LEDC_TEST_FADE_TIME (3000)
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
int ch;
|
||||
|
||||
/*
|
||||
* Prepare and set configuration of timers
|
||||
* that will be used by LED Controller
|
||||
*/
|
||||
ledc_timer_config_t ledc_timer = {
|
||||
.duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
|
||||
.freq_hz = 5000, // frequency of PWM signal
|
||||
.speed_mode = LEDC_LS_MODE, // timer mode
|
||||
.timer_num = LEDC_LS_TIMER, // timer index
|
||||
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
|
||||
};
|
||||
// Set configuration of timer0 for high speed channels
|
||||
ledc_timer_config(&ledc_timer);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
// Prepare and set configuration of timer1 for low speed channels
|
||||
ledc_timer.speed_mode = LEDC_HS_MODE;
|
||||
ledc_timer.timer_num = LEDC_HS_TIMER;
|
||||
ledc_timer_config(&ledc_timer);
|
||||
#endif
|
||||
/*
|
||||
* Prepare individual configuration
|
||||
* for each channel of LED Controller
|
||||
* by selecting:
|
||||
* - controller's channel number
|
||||
* - output duty cycle, set initially to 0
|
||||
* - GPIO number where LED is connected to
|
||||
* - speed mode, either high or low
|
||||
* - timer servicing selected channel
|
||||
* Note: if different channels use one timer,
|
||||
* then frequency and bit_num of these channels
|
||||
* will be the same
|
||||
*/
|
||||
ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
{
|
||||
.channel = LEDC_HS_CH0_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LEDC_HS_CH0_GPIO,
|
||||
.speed_mode = LEDC_HS_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_HS_TIMER
|
||||
},
|
||||
{
|
||||
.channel = LEDC_HS_CH1_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LEDC_HS_CH1_GPIO,
|
||||
.speed_mode = LEDC_HS_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_HS_TIMER
|
||||
},
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32S2
|
||||
{
|
||||
.channel = LEDC_LS_CH0_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LEDC_LS_CH0_GPIO,
|
||||
.speed_mode = LEDC_LS_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_LS_TIMER
|
||||
},
|
||||
{
|
||||
.channel = LEDC_LS_CH1_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LEDC_LS_CH1_GPIO,
|
||||
.speed_mode = LEDC_LS_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_LS_TIMER
|
||||
},
|
||||
#endif
|
||||
{
|
||||
.channel = LEDC_LS_CH2_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LEDC_LS_CH2_GPIO,
|
||||
.speed_mode = LEDC_LS_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_LS_TIMER
|
||||
},
|
||||
{
|
||||
.channel = LEDC_LS_CH3_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LEDC_LS_CH3_GPIO,
|
||||
.speed_mode = LEDC_LS_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_LS_TIMER
|
||||
},
|
||||
};
|
||||
|
||||
// Set LED Controller with previously prepared configuration
|
||||
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
|
||||
ledc_channel_config(&ledc_channel[ch]);
|
||||
}
|
||||
|
||||
// Initialize fade service.
|
||||
ledc_fade_func_install(0);
|
||||
|
||||
while (1) {
|
||||
printf("1. LEDC fade up to duty = %d\n", LEDC_TEST_DUTY);
|
||||
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
|
||||
ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
|
||||
ledc_channel[ch].channel, LEDC_TEST_DUTY, LEDC_TEST_FADE_TIME);
|
||||
ledc_fade_start(ledc_channel[ch].speed_mode,
|
||||
ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
|
||||
}
|
||||
vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
|
||||
|
||||
printf("2. LEDC fade down to duty = 0\n");
|
||||
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
|
||||
ledc_set_fade_with_time(ledc_channel[ch].speed_mode,
|
||||
ledc_channel[ch].channel, 0, LEDC_TEST_FADE_TIME);
|
||||
ledc_fade_start(ledc_channel[ch].speed_mode,
|
||||
ledc_channel[ch].channel, LEDC_FADE_NO_WAIT);
|
||||
}
|
||||
vTaskDelay(LEDC_TEST_FADE_TIME / portTICK_PERIOD_MS);
|
||||
|
||||
printf("3. LEDC set duty = %d without fade\n", LEDC_TEST_DUTY);
|
||||
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
|
||||
ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, LEDC_TEST_DUTY);
|
||||
ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
|
||||
}
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
|
||||
printf("4. LEDC set duty = 0 without fade\n");
|
||||
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
|
||||
ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, 0);
|
||||
ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel);
|
||||
}
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user