ESP8266开硬件中断


一、思路

添加hw_time.c文件
初始化硬件定时器
用户写回调函数
注册回调函数
设置定时参数

二、使用步骤

1.添加C文件

在这里插入图片描述
需要添加hw_time.c文件
在这里插入图片描述

2.初始化硬件定时器

在这里插入图片描述
注意:

  • 如果使用NMI中断源,且为自动填装的定时器,调用hw_timer_arm时参数val必须大于100
  • 如果使用NMI中断源,那么该定时器将为最高优先级,可打断其他ISR.
  • 如果使用FRC1中断源,那么该定时器无法打断其他ISR
  • hw_timer.c的接口不能跟PWM驱动接口函数同时使用,因为二者共用了同一个硬件定时器。
  • 硬件中断定时器的回调函数定义,请添加ICACHE_FLASH_ATTR宏。
  • 使用hw_timer.c的接口,请勿调用wifi-set_sleep_type(LIGHT-SLEEP);将自动睡眠模式设置为Light-sleep。因为Light-sleep在睡眠期间会停CPU,停CPU期间不能响应NMI中断。
    例程:hw_timer_init(0,1);

3.创建及注册回调函数

创建硬件回调函数

在这里插入图片描述
注意:中断函数前不要有“ICACHE_FLASH_ATTR”
例程:void Timer_Interrupt()
{
…;
}

注册中断函数

在这里插入图片描述
例程:
hw_timer_set_func(Timer_Interrupt);

4 .设置使能定时器及参数

在这里插入图片描述

例程:
hw_timer_arm(50000);

三.例程

/*
 * ESPRESSIF MIT License
 *
 * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
 *
 * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
 * it is free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "ets_sys.h"
#include "osapi.h"
#include "driver/uart.h"
#include "user_interface.h"
#include "driver/eagle_soc.h"
#include "driver/gpio16.h"

u8 LED;
/************************************************************
 * 定时回调函数
 **************************************************************/
void  Timer_Interrupt()
{
	LED=!LED;
	GPIO_OUTPUT_SET(GPIO_ID_PIN(4), LED);
	os_printf("\r\n------ Timer_Interrupt1--------\r\n");
	system_soft_wdt_feed(); //喂狗函数
}

/*************************************************************
 * 定时器初始化
 ***********************************************************/
void  OS_Time_1_Init()
{
	hw_timer_init(0,1);
	hw_timer_set_func(Timer_Interrupt);
	hw_timer_arm(50000);
}



/********************************************************************
 * GPIO口初始化
 *
 ****************************************************************/
void GPIO_INIT()
{
    //------------------------------------------------------------------初始化管脚功能
   //管脚功能选择(注意:参数1 【PINNAME】管脚名、参数2 【FUNC】管脚功能)
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U ,0);  //将GPIO4设置为IO口
    //将相应管脚设为输出模式,并输出对应电平(参数1 【gpio_no】、参数2:输出电平)
    // GPIO_DIS_OUTPUT(GPIO_ID_PIN(0));            //设置GOIO0为输入模式
    //PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO0_U);//设置IO0使能,禁止上拉
    // PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO0_U);//管脚上拉使能
   /*******************************************************************************************
    * 注意: 【PIN NAME】【FUNC】【gpio_no】不要混淆
    *【PIN NAME】                管脚名称                "PERIPHS_IO_MUX" + "管脚名
    *【FUNC】                       管脚功能                 功能序号-1
    *【gpio_no】                I0端口序号                GPI0 ID PIN (IO端口序号)
    *
    *********************************************************************************************/
  //GPIO中断设置
   // ETS_GPIO_INTR_DISABLE();//关闭中断
   // ETS_GPIO_INTR_ATTACH((ets_isr_t)(GPIO_INTERRUPT), NULL);//注册GPIO中断处理函数
    //gpio_pin_intr_state_set(GPIO_ID_PIN(0),GPIO_PIN_INTR_NEGEDGE);//中断口,中断出发方式
    /*GPIO_PIN_INTR_DISABLE = 0,      不触发中断
    GPIO_PIN_INTR_POSEDGE = 1,        上升沿触发中断
    GPIO_PIN_INTR_NEGEDGE = 2,        下降沿触发中断
    GPIO_PIN_INTR_ANYEDGE = 3,        双边沿中断
    GPIO_PIN_INTR_LOLEVEL = 4,        低电平中断
    GPIO_PIN_INTR_HILEVEL = 5,             高电平中断
    */
    //ETS_GPIO_INTR_ENABLE();//打开中断
}




/******************************************************************************
 * 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 ICACHE_FLASH_ATTR
user_init(void)
{
	//-------------------------------------------------------------------设置串口波特率
	uart_init(115200,115200);//串口0,串口1
    os_delay_us(1000);
	os_printf("\n\r----------------------");
	os_printf("\n\rHello,Wolrd");                                     //串口打印
	os_printf("\n\rSDK version: %s \n", system_get_sdk_version());
    os_printf("\r------------------------\n\r");
    GPIO_INIT();//IO口初始化,
    OS_Time_1_Init();
  //while(1)
 // {
	  // system_soft_wdt_feed(); //喂狗函数


 // }

}


四.完成效果

LED以50MS的频率闪烁
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值