大量的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).
相关推荐: