上一篇文章配置了基本环境,下面开始记录一些基本的外设驱动
1、GPIO使用
GPIO基本使用
#include "led_driver.h"
void led_init(gpio_num_t gpio_num)
{
gpio_config_t cfg = {
.pin_bit_mask = (1ull << gpio_num),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = 0,
.pull_down_en = 1,
.intr_type = 0,
};
gpio_config(&cfg);
}
.h文件使用如下所示:
#ifndef _LED_DRIVER__H_
#define _LED_DRIVER__H_
#include "driver/gpio.h"
void led_init(gpio_num_t gpio_num);
#endif // !_BASIC__H_
详细的使用可见下面这个图片
设置io状态变化
2、freertos框架
加入下面的头文件
代码示例如下
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "basic.h"
#include "led_driver.h"
#define LED_IO_NUM GPIO_NUM_2
TaskHandle_t LED_Handle; /*创建任务句柄*/
TimerHandle_t Timer0_Handle;
void led_task(void *param)
{
gpio_set_direction(LED_IO_NUM, GPIO_MODE_DEF_OUTPUT);
while (1)
{
// gpio_set_level(LED_IO_NUM, 1);
// vTaskDelay(500 / portTICK_PERIOD_MS);
// gpio_set_level(LED_IO_NUM, 0);
// vTaskDelay(500 / portTICK_PERIOD_MS);
printf("running \r\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void Timer0_callback(TimerHandle_t time)
{
static bool flag = 0;
if (flag == 0)
{
flag = 1;
gpio_set_level(LED_IO_NUM, 1);
}
else
{
flag = 0;
gpio_set_level(LED_IO_NUM, 0);
}
xTimerReset(Timer0_Handle, portMAX_DELAY);
}
void app_main(void)
{
xTaskCreate(led_task,
"led_task",
1024 * 2,
NULL,
1,
&LED_Handle);
Timer0_Handle = xTimerCreate("timer0", (1000 / portTICK_PERIOD_MS), pdFAIL, NULL, Timer0_callback);
xTimerStart(Timer0_Handle, 0);
// while (1)
// {
// gpio_set_level(LED_IO_NUM, 1);
// vTaskDelay(500/portTICK_PERIOD_MS);
// gpio_set_level(LED_IO_NUM, 0);
// vTaskDelay(500/portTICK_PERIOD_MS);
// }
}
标签:LED,记录,esp32,NUM,IO,idf,gpio,include,void
From: https://www.cnblogs.com/lx2035/p/17962310