1、准备工作
a. 开发工具环境搭建
安装安信可WindowsIDE工具,部署ESP8266 freeRTOS开发环境_usdnfo的专栏-CSDN博客
b. 硬件准备
电源:做一个3.3V 和5.0V的电源_usdnfo的专栏-CSDN博客
连线:USB TTL 连接ESP8266 12F 外接电源方法_usdnfo的专栏-CSDN博客
小改动:
准备一个LED灯,连接GPIO14 和 GND
将GPIO13,引出一条杜邦线
2、写代码
a.定义引脚
//定义端口
#define GPIO_OUTPUT_IO_14 14
#define GPIO_OUTPUT_PIN_SEL 1ULL<<GPIO_OUTPUT_IO_14
#define GPIO_INPUT_IO_13 13
#define GPIO_INPUT_PIN_SEL 1ULL<<GPIO_INPUT_IO_13
b.初始化引脚
gpio_config_t io_conf;
//禁用中思
io_conf.intr_type = GPIO_INTR_DISABLE;
//设置工作模式为:输出模式
io_conf.mode = GPIO_MODE_OUTPUT;
//选择端口Pin,GPIO14
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
//允许下啦
io_conf.pull_down_en = 1;
//允许上拉
io_conf.pull_up_en = 1;
//设置配置
gpio_config(&io_conf);
//设置下拉
gpio_set_level(GPIO_OUTPUT_IO_14,0);
//禁用中思
io_conf.intr_type = GPIO_INTR_DISABLE ;
//设置工作模式为:输入模式
io_conf.mode = GPIO_MODE_INPUT;
//选择端口Pin,GPIO13
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
//不允许下啦
io_conf.pull_down_en = 0;
//不允许上拉
io_conf.pull_up_en = 0;
//设置配置
gpio_config(&io_conf);
c.上拉
//设置下拉
gpio_set_level(GPIO_OUTPUT_IO_14,0);
3、烧录
a. Build project
b. GPIO0 接地
c. 右键项目=》Make Targets =》Build =》 下载命令(请参照开发环境的文章)
4、运行
GPIO0 上拉,接上电源。
将GPIO13引出来的线接到GND,1秒后发现灯亮了,断开,1秒后,灯灭了。
5、完整代码
/* Hello World 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 <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "driver/uart.h"
#include "driver/gpio.h"
//定义端口
#define GPIO_OUTPUT_IO_14 14
#define GPIO_OUTPUT_PIN_SEL 1ULL<<GPIO_OUTPUT_IO_14
#define GPIO_INPUT_IO_13 13
#define GPIO_INPUT_PIN_SEL 1ULL<<GPIO_INPUT_IO_13
void app_main()
{
printf("Hello world!\n");
gpio_config_t io_conf;
//禁用中思
io_conf.intr_type = GPIO_INTR_DISABLE;
//设置工作模式为:输出模式
io_conf.mode = GPIO_MODE_OUTPUT;
//选择端口Pin,GPIO14
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
//允许下啦
io_conf.pull_down_en = 1;
//允许上拉
io_conf.pull_up_en = 1;
//设置配置
gpio_config(&io_conf);
//设置下拉
gpio_set_level(GPIO_OUTPUT_IO_14,0);
//禁用中思
io_conf.intr_type = GPIO_INTR_DISABLE ;
//设置工作模式为:输入模式
io_conf.mode = GPIO_MODE_INPUT;
//选择端口Pin,GPIO13
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
//不允许下啦
io_conf.pull_down_en = 0;
//不允许上拉
io_conf.pull_up_en = 0;
//设置配置
gpio_config(&io_conf);
//install gpio isr service
//gpio_install_isr_service(0);
//hook isr handler for specific gpio pin
//gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *) GPIO_INPUT_IO_0);
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is ESP8266 chip with %d CPU cores, WiFi, ",
chip_info.cores);
printf("silicon revision %d, ", chip_info.revision);
printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
int lig = 0;
while (1) {
//延时1秒
vTaskDelay(1000 / portTICK_RATE_MS);
//获取GPIO13状态
if(gpio_get_level(GPIO_NUM_13)==1){
lig=0;
}else{
lig=1;
}
gpio_set_level(GPIO_OUTPUT_IO_14, lig);
}
}