https://blog.csdn.net/lsalp/article/details/80592533
https://blog.csdn.net/lalala098/article/details/81240806
控制小灯https://blog.csdn.net/XiaoCaiDaYong/article/details/82289276
https://blog.csdn.net/xh870189248/article/details/84580239
1.中断 (这个非常重要)
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
//#include "gpio.h"
uint16 flag;
/******************************************************************************
* FunctionName : user_rf_cal_sector_set
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
* We add this function to force users to set rf cal sector, since
* we don't know which sector is free in user's application.
* sector map for last several sectors : ABCCC
* A : rf cal
* B : rf init data
* C : sdk parameters
* Parameters : none
* Returns : rf cal sector
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR
user_rf_cal_sector_set(void)
{
enum flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
switch (size_map) {
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 - 5;
break;
case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 - 5;
break;
case FLASH_SIZE_16M_MAP_512_512:
rf_cal_sec = 512 - 5;
break;
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 - 5;
break;
case FLASH_SIZE_32M_MAP_512_512:
rf_cal_sec = 1024 - 5;
break;
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec = 1024 - 5;
break;
case FLASH_SIZE_64M_MAP_1024_1024:
rf_cal_sec = 2048 - 5;
break;
case FLASH_SIZE_128M_MAP_1024_1024:
rf_cal_sec = 4096 - 5;
break;
default:
rf_cal_sec = 0;
break;
}
return rf_cal_sec;
}
void ICACHE_FLASH_ATTR
user_rf_pre_init(void)
{
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void GPIO_INTERRUPTED(void)//这个函数前不能有 ICACHE_FLASH_ATTR
{
uint32 gpio_status;
uint16 gpio_4;
gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);//从寄存器中读出状态
//clear interrupt status
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);//清除中断标志位
gpio_4=gpio_status&(0x01<<4);//gpio_n=gpio_status&(0x01<<n) 取出中断位
if(gpio_4)
{
flag=!flag;
GPIO_OUTPUT_SET(GPIO_ID_PIN(5),flag);
}
}
void ICACHE_FLASH_ATTR
user_init(void)
{
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5);
GPIO_OUTPUT_SET(GPIO_ID_PIN(5),1);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U , FUNC_GPIO4);
GPIO_DIS_OUTPUT(GPIO_ID_PIN(4));
//PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO4_U);
ETS_GPIO_INTR_DISABLE();
ETS_GPIO_INTR_ATTACH((ets_isr_t)GPIO_INTERRUPTED,NULL);//注册中断函数
gpio_pin_intr_state_set(
GPIO_ID_PIN(4),GPIO_PIN_INTR_NEGEDGE);//设置中断触发方式(一高一低才是下降沿)
ETS_GPIO_INTR_ENABLE();
}
2.软件定时
void ICACHE_FLASH_ATTR soft_delay_func(void)
{
flag = !flag;
GPIO_OUTPUT_SET(GPIO_ID_PIN(4),flag);
}
void ICACHE_FLASH_ATTR soft_delay(uint16 time,uint8 repeat)
{
os_timer_disarm (&ptimer); //关闭软件定时器
os_timer_setfn(&ptimer,(os_timer_func_t *)soft_delay_func,NULL);//设置软件定时器,回调函数,参数
os_timer_arm (&ptimer,time,repeat); //使能软件定时器,repeat = 1 重复 =0 不重复
}
void ICACHE_FLASH_ATTR
user_init(void)
{
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4) ;
soft_delay(1000,1);
}
3.硬件定时器
void hardwoft_delay_func(void) //硬件定时器回调函数 不能带ICACHE_FLASH_ATTR宏
{
flag = !flag;
GPIO_OUTPUT_SET(GPIO_ID_PIN(4),flag);
}
void ICACHE_FLASH_ATTR
user_init(void)
{
hw_timer_init(0,1); //初始化 设置定时器类型 并设置是否重复
hw_timer_set_func(hardwoft_delay_func); //设置回调函数
hw_timer_arm(500000); //使能硬件定时器,并设置时间 单位us
}