在51822的产品手册中,可以看到TIMER1和TIMER2最高只有16位,也就是2^16 = 65536,所以在配置定时器1和定时器2是,分频之后的时钟不能超过65536,
如下是配置1S时钟定时的方法
void bsp_timer_init(void)
{
NRF_TIMER1->TASKS_STOP = 1;// Stop timer
NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer; //timer模式
NRF_TIMER1->BITMODE = (TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos); // 设置32bit
NRF_TIMER1->PRESCALER = 8; //2^8 256分频得到62500hz的时钟,这里是因为TIMER1和TIMER2的计数器最高只能是16位,所以计数器最高值是65536,所以分频之后的时钟不能超过6553
NRF_TIMER1->TASKS_CLEAR = 1; // Clear timer
NRF_TIMER1->CC[0] = 62500; //1s时钟
NRF_TIMER1->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos); // taken from Nordic dev zone
NRF_TIMER1->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
//启动timer模块
//NRF_TIMER1->TASKS_START = 1;
//开启MCU的timer1中断
NVIC_SetPriority(TIMER1_IRQn, 3);
NVIC_ClearPendingIRQ(TIMER1_IRQn);
NVIC_EnableIRQ(TIMER1_IRQn);
}
void TIMER1_IRQHandler()
{
if(NRF_TIMER1->EVENTS_COMPARE[0] == 1){
NRF_TIMER1->EVENTS_COMPARE[0] = 0; //清除事件
}
}