OS Timer
内容
效果
每隔0.5s反转LED的状态,并发送字符串
步骤
- 定义软件定时器(os_timer_t型结构体)
- 定义回调函数
- 关闭软件定时器
- 设置定时器
- 设置定时器参数并使能定时器
代码
头文件
#include "user_config.h" // 用户配置
#include "driver/uart.h" // 串口
//#include "at_custom.h"
#include "c_types.h" // 变量类型
#include "eagle_soc.h" // GPIO函数、宏定义
//#include "espconn.h"
//#include "espnow.h"
#include "ets_sys.h" // 回调函数
//#include "gpio.h"
//#include "ip_addr.h"
//#include "mem.h"
#include "os_type.h" // os_XXX
#include "osapi.h" // os_XXX、软件定时器
//#include "ping.h"
//#include "pwm.h"
//#include "queue.h"
//#include "smartconfig.h"
//#include "sntp.h"
//#include "spi_flash.h"
//#include "upgrade.h"
#include "user_interface.h" // 系统接口、system_param_xxx接口、WIFI、RateContro
宏定义
#define ProjectName "OS_Timer" // 工程名宏定义
全局变量
u8 F_LED = 0; // LED状态标志位
os_timer_t OS_Timer_1; //定时器
OS_Timer_1_cb
void ICACHE_FLASH_ATTR OS_Timer_1_cb(void)
{
F_LED = !F_LED; //翻转LED的状态
GPIO_OUTPUT_SET(GPIO_ID_PIN(4),F_LED); //设置电平
os_printf("\r\n-----OS_Timer_1_cb-----\r\n"); //打印进入回调函数语句
}
OS_Timer_1_init
void ICACHE_FLASH_ATTR OS_Timer_1_init(u32 time_ms, u8 time_repetitive)
{
//关闭定时器
//参数一:要关闭的定时器
os_timer_disarm(&OS_Timer_1); //关闭软件定时器
//设置定时器
//参数一:要设置的定时器;参数二:回调函数(需类型转换);参数三:回到函数的参数
//注:os_timer_setfn必须在软件定时器未使能的情况下调用
os_timer_setfn(&OS_Timer_1,(os_timer_func_t*)OS_Timer_1_cb, NULL); //设置回调函数
//使能(启动)ms定时器
//参数一:要失能的定时器;参数二:定时时间(单位:ms);参数三:1=重复/0=只一次
os_timer_arm(&OS_Timer_1,time_ms,time_repetitive);
}
LED_init
void ICACHE_FLASH_ATTR LED_init(void)
{
//选择GPIO4的功能
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4);
//设置GPIO4的电平
GPIO_OUTPUT_SET(GPIO_ID_PIN(4),1);
}
user_init
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(115200,115200); // 初始化串口波特率
os_delay_us(10000); // 等待串口稳定
os_printf("\r\n=================================================\r\n");
os_printf("\t Project:\t%s\r\n", ProjectName);
os_printf("\t SDK version:\t%s", system_get_sdk_version());
os_printf("\r\n=================================================\r\n");
// 初始化LED
LED_init(); // LED初始化
// 初始化软件定时器
OS_Timer_1_init(500,1); // 500ms(重复)
os_printf("\r\n-------------------- user_init OVER --------------------\r\n");
}
其他
/******************************************************************************
* 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)
{
}
整体代码
#include "user_config.h" // 用户配置
#include "driver/uart.h" // 串口
//#include "at_custom.h"
#include "c_types.h" // 变量类型
#include "eagle_soc.h" // GPIO函数、宏定义
//#include "espconn.h"
//#include "espnow.h"
#include "ets_sys.h" // 回调函数
//#include "gpio.h"
//#include "ip_addr.h"
//#include "mem.h"
#include "os_type.h" // os_XXX
#include "osapi.h" // os_XXX、软件定时器
//#include "ping.h"
//#include "pwm.h"
//#include "queue.h"
//#include "smartconfig.h"
//#include "sntp.h"
//#include "spi_flash.h"
//#include "upgrade.h"
#include "user_interface.h" // 系统接口、system_param_xxx接口、WIFI、RateContro
#define ProjectName "OS_Timer" // 工程名宏定义
// 全局变量
//==================================================================================
u8 F_LED = 0; // LED状态标志位
os_timer_t OS_Timer_1;
void ICACHE_FLASH_ATTR OS_Timer_1_cb(void)
{
F_LED = !F_LED; //翻转LED的状态
GPIO_OUTPUT_SET(GPIO_ID_PIN(4),F_LED); //设置电平
os_printf("\r\n-----OS_Timer_1_cb-----\r\n"); //打印进入回调函数语句
}
void ICACHE_FLASH_ATTR OS_Timer_1_init(u32 time_ms, u8 time_repetitive)
{
//关闭定时器
//参数一:要关闭的定时器
os_timer_disarm(&OS_Timer_1); //关闭软件定时器
//设置定时器
//参数一:要设置的定时器;参数二:回调函数(需类型转换);参数三:回到函数的参数
//注:os_timer_setfn必须在软件定时器未使能的情况下调用
os_timer_setfn(&OS_Timer_1,(os_timer_func_t*)OS_Timer_1_cb, NULL); //设置回调函数
//使能(启动)ms定时器
//参数一:要失能的定时器;参数二:定时时间(单位:ms);参数三:1=重复/0=只一次
os_timer_arm(&OS_Timer_1,time_ms,time_repetitive);
}
void ICACHE_FLASH_ATTR LED_init(void)
{
//选择GPIO4的功能
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4);
//设置GPIO4的电平
GPIO_OUTPUT_SET(GPIO_ID_PIN(4),1);
}
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(115200,115200); // 初始化串口波特率
os_delay_us(10000); // 等待串口稳定
os_printf("\r\n=================================================\r\n");
os_printf("\t Project:\t%s\r\n", ProjectName);
os_printf("\t SDK version:\t%s", system_get_sdk_version());
os_printf("\r\n=================================================\r\n");
// 初始化LED
LED_init(); // LED初始化
// 初始化软件定时器
OS_Timer_1_init(500,1); // 500ms(重复)
os_printf("\r\n-------------------- user_init OVER --------------------\r\n");
}
/******************************************************************************
* 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)
{
}
总结框架
os_timer_t OS_Timer_1; //使用os_timer_t型结构体,定义一个os_timer_t型变量
void ICACHE_FLASH_ATTR OS_Timer_1_cb(void)
{
....... //到达指定时间,完成的功能
}
//参数1:【定时时间】;参数2:【重复模式】
void ICACHE_FLASH_ATTR OS_Timer_1_init(u32 time_ms, u8 time_repetitive)
{
//关闭定时器
//参数一:要关闭的定时器
os_timer_disarm(&OS_Timer_1); //关闭软件定时器
//设置定时器
//参数一:要设置的定时器;参数二:回调函数(需类型转换);参数三:回到函数的参数
//注:os_timer_setfn必须在软件定时器未使能的情况下调用
os_timer_setfn(&OS_Timer_1,(os_timer_func_t*)OS_Timer_1_cb, NULL); //设置回调函数
//使能(启动)ms定时器
//参数一:要失能的定时器;参数二:定时时间(单位:ms);参数三:1=重复/0=只一次
os_timer_arm(&OS_Timer_1,time_ms,time_repetitive);
}