/* Ledc 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 "freertos/xtensa_api.h"
#include "freertos/queue.h"
#include "driver/ledc.h"
#include "esp_attr.h"
#include "esp_err.h"
/*
* About this example
* 1. init LEDC module:
* a. You need to set the timer of LEDC first, this decide the frequency and resolution of PWM.
* b. You need to set the LEDC channel you want to use, and bind with one of the timers.
*
* 2. You can 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 use GPIO18/19/4/5 as LEDC ouput, and it will change the duty repeatedly.
*
*
*/
#define LEDC_IO_0 (2)
esp_err_t app_main()
{
ledc_timer_config_t ledc_timer = {
//set timer counter bit number
.bit_num = LEDC_TIMER_13_BIT,
//set frequency of pwm
.freq_hz = 5000,
//timer mode,
.speed_mode = LEDC_HIGH_SPEED_MODE,
//timer index
.timer_num = LEDC_TIMER_0
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
//set LEDC channel 0
.channel = LEDC_CHANNEL_0,
//set the duty for initialization.(duty range is 0 ~ ((2**bit_num)-1)
.duty = 0,
//GPIO number
.gpio_num = LEDC_IO_0,
//GPIO INTR TYPE, as an example, we enable fade_end interrupt here.
.intr_type = LEDC_INTR_FADE_END,
//set LEDC mode, from ledc_mode_t
.speed_mode = LEDC_HIGH_SPEED_MODE,
//set LEDC timer source, if different channel use one timer,
//the frequency and bit_num of these channels should be the same
.timer_sel = LEDC_TIMER_0
};
//set the configuration
ledc_channel_config(&ledc_channel);
//initialize fade service.
ledc_fade_func_install(0);
while(1) {
printf("LEDC fade up\n");
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 1000, 2000);
ledc_fade_start(LEDC_CHANNEL_0, LEDC_FADE_NO_WAIT);
vTaskDelay(3000 / portTICK_PERIOD_MS);
printf("LEDC fade down\n");
ledc_set_fade_with_time(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0, 2000);
ledc_fade_start(LEDC_CHANNEL_0, LEDC_FADE_NO_WAIT);
vTaskDelay(3000 / portTICK_PERIOD_MS);
/*printf("LEDC set duty without fade\n");
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 1000);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
vTaskDelay(2000 / portTICK_PERIOD_MS);
printf("LEDC set duty without fade\n");
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
vTaskDelay(2000 / portTICK_PERIOD_MS);*/
}
}
noticeable
- 粉丝: 9
- 资源: 45
最新资源
- 题目源码2024年强网杯全国网络安全挑战赛 PWN题目baby-heap源码
- 题目源码2024年强网杯全国网络安全挑战赛 PWN题目expect-number源码
- 省份劳动率最终.dta
- 题目源码2024年强网杯全国网络安全挑战赛 PWN题目prpr源码
- Rust Trait 静态派发与动态派发示例
- 拼多多官方_main_main_baidu_sem_dz1_ARM64.apk
- 小米机型小米助手界面检测当前机型 包括 Android 版本、MIUI 版本和固件详细信息
- share6620081042528496742.jpg
- 21英语210405010143罗杰_周霜红 文献综述.doc
- share6329583338574047795.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈