[ESP32 RTOS]点亮一个LED灯

介绍

使用ESP32 EROS SDK点亮一个LED 灯 让你呈现心跳灯 还完成了电平翻转函数

代码准备

/* Blink 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/gpio.h"
#include "esp_log.h"
#include "sdkconfig.h"

#define LED_NUM 2

const char *TAG="LED BLINK";

void delay_ms(uint32_t i)
{
     vTaskDelay(i/portTICK_PERIOD_MS);
}
//翻转GPIO电平
void gpio_toggle(gpio_num_t gpio_num)
{
    bool level = gpio_get_level(gpio_num);
    gpio_set_level(gpio_num,!level);
    //ESP_LOGI(TAG,"LED LEVEL:%d",level);

}
//心跳灯 高电平点亮 低电平熄灭
void HeartbeatLED(gpio_num_t gpio_num)
{
    gpio_set_level(gpio_num,1);
    delay_ms(40);
    gpio_set_level(gpio_num,0);
    delay_ms(70);

    gpio_set_level(gpio_num,1);
    delay_ms(40);
    gpio_set_level(gpio_num,0);


}

void app_main(void)
{
    gpio_config_t gpio_cfg={

        .pin_bit_mask=1ULL << LED_NUM,
        .mode = GPIO_MODE_INPUT_OUTPUT,
        .pull_down_en = false,
        .pull_up_en = false,
        .intr_type = GPIO_INTR_DISABLE
    };

    ESP_ERROR_CHECK(gpio_config(&gpio_cfg));
    


    while (1) {

        HeartbeatLED(LED_NUM);
        vTaskDelay(1000/portTICK_PERIOD_MS);

    }
}

电平翻转函数
image.png
读到的电平值都是0
image.png
查看文档
在他设置为单独的输出模式时是0,所以我们设置为输入输出模式
image.png
修改输出模式
image.png

效果展示

IMG_5275.gif