timer
1.定时器的功能:定时(需要配置比较/捕获寄存器的比较值)、计数
2.nrf52832具有5个32位的timer定时器
3.时钟源:
1.当f < 1MHz,系统自动选择PCLK1M的时钟源(低功耗)
2.当f > 1MHz,系统自动选择PCLK16M的时钟源
4.timer的时钟频率可以选择(0-9分频)
无论使用哪个时钟源都是以16M的为基准
timer常见函数
1.定义定时器实例:
nrfx_timer_t timer_instance = NRF_DRV_TIMER_INSTANCE(timer_id)
2.定义定时器结构体:
nrfx_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG
(定时器默认配置,可以字sdkconfig.h进行修改)
1.frequency :时钟频率
2.mode :模式
3.bit_witdth:位宽
4.interrupt_priority:优先级
5.中断处理函数
3.定时器初始化函数:
nrfx_timer_init(&timer_instance,&timer_cfg,timer_cb_handle)
4.定时器使能函数:
nrfx_timer_enable(&timer_instance)
5.定时器不使能函数
nrfx_timer_disable(&timer_instance)
6.定时器的计数(count)方式:
读取计数值函数;
nrfx_timer_capture(&timer_instance,cc_channal)
cc_channal:比较通道
触发方式;nrfx_timer_increment(&timer_instance)
手动触发:
PPI(可编程外设互联)触发:
timer定时示例程序
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_drv_timer.h"
static void log_init(void)
{
ret_code_t err = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
const nrfx_timer_t timer_instance = NRFX_TIMER_INSTANCE(0); //定义一个定时器实例,选择timer0
void timer_event_handle(nrf_timer_event_t event_type,void* p_context) //定时器事件回调函数
{
switch (event_type)
{
case NRF_TIMER_EVENT_COMPARE0: //定时器0事件,翻转电平
nrf_gpio_pin_toggle(17);
break;
default:
break;
}
}
void timer_init(void)
{
uint32_t err = NRF_SUCCESS;
uint32_t timer_ms = 500;
uint32_t timer_ticks;
//可以在sdkconfig.h文件中修改默认配置(16M,16bit, priority:6,无参数,mode:timer)
nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG; //定义一个定时器结构体,默认配置
err = nrfx_timer_init(&timer_instance,&timer_cfg,timer_event_handle); //初始化定时器,注册回调函数
APP_ERROR_CHECK(err);
timer_ticks = nrfx_timer_ms_to_ticks(&timer_instance,timer_ms); //计算500ms占用几个tick
nrfx_timer_extended_compare(&timer_instance,NRF_TIMER_CC_CHANNEL0,timer_ticks,NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,true);
//定时器0比较通道溢出清0
}
int main(void)
{
log_init();
nrf_gpio_cfg_output(17);
nrf_gpio_pin_set(17);
timer_init();
nrfx_timer_enable(&timer_instance); //使能定时器
NRF_LOG_INFO("GPIO example started");
NRF_LOG_FLUSH();
while(1)
{
}
}
timer的计数示例程序
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
/* 包含log头文件 */
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
/* 包含定时器头文件 */
#include "nrf_drv_timer.h"
/* 包含uart头文件 */
#include "app_uart.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif
/* 定义uart发送接收缓冲区 */
#define UART_TX_BUF_SIZE 256
#define UART_RX_BUF_SIZE 256
const nrfx_timer_t timer_instance = NRFX_TIMER_INSTANCE(0); //定义一个定时器实例,选择timer0
/**
* log初始化函数
*/
static void log_init(void)
{
ret_code_t err = NRF_LOG_INIT(NULL); //log初始化
APP_ERROR_CHECK(err); //检测log配置信息
NRF_LOG_DEFAULT_BACKENDS_INIT(); //开启log
}
/**
* uart事件回调函数
*/
void uart_event_handle(app_uart_evt_t* event)
{
//uart通讯出错事件
if(event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(event->data.error_communication);
}
//fifo错误事件
else if(event->evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(event->data.error_code);
}
}
/**
* uart初始化参数配置函数
*/
void uart_config(void)
{
uint32_t err;
/* 串口结构体参数 */
const app_uart_comm_params_t comm_params = {
RX_PIN_NUMBER, //接收引脚p0.08
TX_PIN_NUMBER, //发送引脚p0.06
RTS_PIN_NUMBER, //输出信号引脚p0.05
CTS_PIN_NUMBER, //输入信号引脚p0.07
APP_UART_FLOW_CONTROL_DISABLED, //使能软件控制流
false, //不校验
NRF_UART_BAUDRATE_115200 //串口波特率115200
};
/* 串口初始化 */
APP_UART_FIFO_INIT(&comm_params, //串口结构体
UART_RX_BUF_SIZE, //串口接收缓冲区大小
UART_TX_BUF_SIZE, //串口发送缓冲区大小
uart_event_handle, //串口事件回调函数
APP_IRQ_PRIORITY_LOWEST, //串口中断优先级最低
err); //配置信息
APP_ERROR_CHECK(err); //检测配置是否成功
}
void timer_event_handler(nrf_timer_event_t event_type, void* p_context){}
void timer_init(void)
{
uint32_t err = NRF_SUCCESS;
//可以在sdkconfig.h文件中修改默认配置(16M,16bit, priority:6,无参数,mode:timer)
nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG; //定义一个定时器结构体,默认配置
timer_cfg.mode = NRF_TIMER_MODE_COUNTER;
err = nrfx_timer_init(&timer_instance,&timer_cfg,timer_event_handler); //初始化定时器,不需要注册回调函数
APP_ERROR_CHECK(err);
}
/**
* uart初始化函数
*/
int main(void)
{
uint32_t value = 0;
//log初始化
log_init();
nrf_gpio_cfg_output(17);
nrf_gpio_pin_clear(17);
//串口初始化
uart_config();
timer_init();
nrfx_timer_enable(&timer_instance);
NRF_LOG_INFO("uart example start");
NRF_LOG_FLUSH();
while(1)
{
nrfx_timer_increment(&timer_instance);
value = nrfx_timer_capture(&timer_instance,NRF_TIMER_CC_CHANNEL2);
printf("value = %d\r\n",value);
nrf_delay_ms(500);
nrf_gpio_pin_toggle(17);
}
}
记得包含串口的源文件,否则可能出现编译没问题,运行没效果的现象