/** 定时器结构体 */
static os_timer_t os_timer;
/** LED操作命令 */
void Led_Cmd(bool status ){
if (status == true ) {
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 0);
} else {
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 1);
}
}
void Led_Task_Run(void){
static bool status = false;
if ( status == true ) {
status = false;
} else {
status = true;
}
Led_Cmd( status );
}
void user_init(void)//初始化
{
// 设置串口0和串口1的波特率
uart_init(57600, 57600);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
Led_Cmd(false);
/** 关闭该定时器 */
os_timer_disarm( &os_timer );
/** 配置该定时器回调函数 */
os_timer_setfn( &os_timer, (ETSTimerFunc *) ( Led_Task_Run ), NULL );
/** 启动该定时器 */
os_timer_arm( &os_timer, 500, true );
}
void user_rf_pre_init()
{
}
①、初始化。
void ICACHE_FLASH_ATTR user_init(void) {
uart_init(57600, 57600);
os_printf("SDK version:%sn", system_get_sdk_version());
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
Led_Cmd(false);
//第一个参数0使用的是FRC1_SOURCE,如果是1则为NMI_SOURCE类型,第二个参数是否自动填充,也就是是否重复。
hw_timer_init(0, 1);
//设置定时器的回调函数
hw_timer_set_func(hw_test_timer_cb);
//设置定时时间
hw_timer_arm(500);
}
②、这是控制LED的方法,同上面的软件定时器。
void Led_Cmd(bool status) {
if (status == true) {
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 0);
} else {
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 1);
}
}
void hw_test_timer_cb(void) {
static bool status = false;
if (status == true) {
status = false;
os_printf("Led_Cmd false");
} else {
status = true;
os_printf("Led_Cmd true");
}
Led_Cmd(status);
}
软件定时器工程源码下载 : https://github.com/xuhongv/StudyInEsp8266/tree/master/3_TimerLED
硬件中断定时器工程源码下载 :https://github.com/xuhongv/StudyInEsp8266/tree/master/15_ESP8266_Timer2
五、附加。
群里有小伙伴问到怎么实现GPIO中断函数回复:
首先你得初始化GPIO:
GPIO_ConfigTypeDef *pGPIOConfig;
pGPIOConfig = (GPIO_ConfigTypeDef *)zalloc(sizeof(GPIO_ConfigTypeDef));
pGPIOConfig->GPIO_IntrType = GPIO_PIN_INTR_NEGEDGE; //下降沿
pGPIOConfig->GPIO_Pullup = GPIO_PullUp_EN;
pGPIOConfig->GPIO_Mode = GPIO_Mode_Input;
pGPIOConfig->GPIO_Pin = GPIO_Pin_5; //把GPIO5作为下降沿触发
gpio_config(pGPIOConfig);
gpio_intr_handler_register(key_intr_handler, NULL); //绑定回调事件
//enable gpio iterrupt
_xt_isr_unmask(1 << ETS_GPIO_INUM);
下面是回调函数:
LOCAL void key_intr_handler() {
uint8 i;
uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
if (gpio_status & BIT(PKT_GPIO)) {
//禁止中断
gpio_pin_intr_state_set(GPIO_ID_PIN(PKT_GPIO), GPIO_PIN_INTR_DISABLE);
//清楚中断标志
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(PKT_GPIO));
//do your things
//再次使能中断
gpio_pin_intr_state_set(GPIO_ID_PIN(PKT_GPIO), GPIO_PIN_INTR_NEGEDGE);
}
}