nrf51822学习笔记之TIMER

2026/4/24 13:13:28

大量的TIMER task/event及中断,可以使各种系统外设(包括GPIO)通过PPI 系统执行定时或记数任务(timing/count tasks).

PPI系统也可以使能定时器(TIMER task/event features)来产生周期性输出(periodic output)或PWM信号到任意的GPIO口。

The PPI system also enables the TIMER task/event features to generate periodic output and PWM signals to any GPIO.

定时计数器的时钟源是HFCLK,并可以通过一个4 bit (1/2X) 分配器对HFCLK进行分频.

TIMER可以处于两种模式: 1定时模式(Timer mode) 2计数模式(Counter mode)

两种模式都可以通过触发启动或停止任务(START task or STOP task)来启动或停止

TIMER。

TIMER是count-up timer:

Timer mode:每个定时器周期(FTIMER),内部计数寄存器(Counter register ) +1. Counter mode:每次COUNT task被触发的时候,Counter register +1. 此模式下,FTIMER和分频系数就不起作用了。

TIMER可以通过位宽寄存器(BITMODE register)设置计数最大值(maximum value). 分频和位宽寄存器(PRESCALER register and the BITMODE register) 只能在TIMER停止运行的时候对其进行修改,否则结果难以预料。当计数超过了最大值时,计数寄存器会溢出,并自动从0重新开始计数。可通过执行CLEAR task 使TIMER清0。

比较和捕获(Compare/Capture):

比较:每个捕获比较寄存器(CC[0..3])都可以产生一个COMPARE event.当某个计

数值计到与capture compare register CC[n]中的值相等时,就会产生一个COMPARE[n] event,这两个n 是对应着的。 捕获:每个capture/compare register都可以执行一个 capture task。每次capture[n] task被触发,计数值都会被复制到CCP[n]寄存器

every time the CAPTURE[n] task is triggered the Counter value is copied to the CC[n] register.

任务的优先权(Task priority):

当START task and the STOP task同时来到的时候,STOP task优先

任务延时(Task delays):

CLEAR task, COUNT task and the STOP task 都会在一个HFCLK时钟周期内完成,子电源模式下,START task 需要更长的时间。

The CLEAR task, COUNT task and the STOP task will guarantee to take effect within one clock cycle of the HFCLK. Depending on sub-power mode, the START task may require longer time to take effect。

示例代码:

static void timer2_init(void) {

NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; //16MHZ NRF_CLOCK->TASKS_HFCLKSTART = 1;

while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {} //wait for clk start NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer; //Mode :Timer NRF_TIMER2->PRESCALER = 4; // 16M/2^4=1us NRF_TIMER2->CC[0] = LED_INTENSITY_LOW; //32

NRF_TIMER2->CC[1] = (LED_INTENSITY_HALF*2); //128*2=256 us =0.256ms (1~224) NRF_TIMER2->CC[2] = LED_INTENSITY_HIGH; //224

NRF_TIMER2->INTENSET=

TIMER_INTENSET_COMPARE2_Enabled<< TIMER_INTENSET_COMPARE2_Pos;

//1<<16 enable Interrupt 使能中断 NRF_TIMER2->SHORTS =

(TIMER_SHORTS_COMPARE1_CLEAR_Enabled << TIMER_SHORTS_COMPARE1_CLEAR_Pos); //clear compare1 event

}

? #define NRF_TIMER2 ((NRF_TIMER_Type*) NRF_TIMER2_BASE) 其中:

? #define NRF_TIMER2_BASE 0x4000A000UL

typedef struct { //TIMER Structure

__O uint32_t TASKS_START; // Start Timer. __O uint32_t TASKS_STOP; //Stop Timer. __O uint32_t TASKS_COUNT; //Increment Timer (In counter mode).


nrf51822学习笔记之TIMER.doc 将本文的Word文档下载到电脑
搜索更多关于: nrf51822学习笔记之TIMER 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219