您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > nrf51822学习笔记之TIMER
大量的TIMERtask/event及中断,可以使各种系统外设(包括GPIO)通过PPI系统执行定时或记数任务(timing/counttasks).PPI系统也可以使能定时器(TIMERtask/eventfeatures)来产生周期性输出(periodicoutput)或PWM信号到任意的GPIO口。ThePPIsystemalsoenablestheTIMERtask/eventfeaturestogenerateperiodicoutputandPWMsignalstoanyGPIO.定时计数器的时钟源是HFCLK,并可以通过一个4bit(1/2X)分配器对HFCLK进行分频.TIMER可以处于两种模式:1定时模式(Timermode)2计数模式(Countermode)两种模式都可以通过触发启动或停止任务(STARTtaskorSTOPtask)来启动或停止TIMER。TIMER是count-uptimer:Timermode:每个定时器周期(FTIMER),内部计数寄存器(Counterregister)+1.Countermode:每次COUNTtask被触发的时候,Counterregister+1.此模式下,FTIMER和分频系数就不起作用了。TIMER可以通过位宽寄存器(BITMODEregister)设置计数最大值(maximumvalue).分频和位宽寄存器(PRESCALERregisterandtheBITMODEregister)只能在TIMER停止运行的时候对其进行修改,否则结果难以预料。当计数超过了最大值时,计数寄存器会溢出,并自动从0重新开始计数。可通过执行CLEARtask使TIMER清0。比较和捕获(Compare/Capture):比较:每个捕获比较寄存器(CC[0..3])都可以产生一个COMPAREevent.当某个计数值计到与capturecompareregisterCC[n]中的值相等时,就会产生一个COMPARE[n]event,这两个n是对应着的。捕获:每个capture/compareregister都可以执行一个capturetask。每次capture[n]task被触发,计数值都会被复制到CCP[n]寄存器everytimetheCAPTURE[n]taskistriggeredtheCountervalueiscopiedtotheCC[n]register.任务的优先权(Taskpriority):当STARTtaskandtheSTOPtask同时来到的时候,STOPtask优先任务延时(Taskdelays):CLEARtask,COUNTtaskandtheSTOPtask都会在一个HFCLK时钟周期内完成,子电源模式下,STARTtask需要更长的时间。TheCLEARtask,COUNTtaskandtheSTOPtaskwillguaranteetotakeeffectwithinoneclockcycleoftheHFCLK.Dependingonsub-powermode,theSTARTtaskmayrequirelongertimetotakeeffect。示例代码:staticvoidtimer2_init(void){NRF_CLOCK-EVENTS_HFCLKSTARTED=0;//16MHZNRF_CLOCK-TASKS_HFCLKSTART=1;while(NRF_CLOCK-EVENTS_HFCLKSTARTED==0){}//waitforclkstartNRF_TIMER2-MODE=TIMER_MODE_MODE_Timer;//Mode:TimerNRF_TIMER2-PRESCALER=4;//16M/2^4=1usNRF_TIMER2-CC[0]=LED_INTENSITY_LOW;//32NRF_TIMER2-CC[1]=(LED_INTENSITY_HALF*2);//128*2=256us=0.256ms(1~224)NRF_TIMER2-CC[2]=LED_INTENSITY_HIGH;//224NRF_TIMER2-INTENSET=TIMER_INTENSET_COMPARE2_EnabledTIMER_INTENSET_COMPARE2_Pos;//116enableInterrupt使能中断NRF_TIMER2-SHORTS=(TIMER_SHORTS_COMPARE1_CLEAR_EnabledTIMER_SHORTS_COMPARE1_CLEAR_Pos);//clearcompare1event}#defineNRF_TIMER2((NRF_TIMER_Type*)NRF_TIMER2_BASE)其中:#defineNRF_TIMER2_BASE0x4000A000ULtypedefstruct{//TIMERStructure__Ouint32_tTASKS_START;//StartTimer.__Ouint32_tTASKS_STOP;//StopTimer.__Ouint32_tTASKS_COUNT;//IncrementTimer(Incountermode).__Ouint32_tTASKS_CLEAR;//Cleartimer.__Ouint32_tTASKS_SHUTDOWN;//Shutdowntimer.__Iuint32_tRESERVED0[11];__Ouint32_tTASKS_CAPTURE[4];//CaptureTimervaluetoCC[n]registers.__Iuint32_tRESERVED1[60];__IOuint32_tEVENTS_COMPARE[4];//CompareeventonCC[n]match.__Iuint32_tRESERVED2[44];__IOuint32_tSHORTS;//ShortcutsforTimer.__Iuint32_tRESERVED3[64];__IOuint32_tINTENSET;//Interruptenablesetregister.__IOuint32_tINTENCLR;//Interruptenableclearregister.__Iuint32_tRESERVED4[126];__IOuint32_tMODE;//TimerModeselection.__IOuint32_tBITMODE;//Setstimerbehaviour.__Iuint32_tRESERVED5;__IOuint32_tPRESCALER;//4-bitprescalertosourceclockfrequency(maxvalue9).Source.clockfrequencyisdividedby2^SCALE.__Iuint32_tRESERVED6[11];__IOuint32_tCC[4];//Capture/compareregisters.__Iuint32_tRESERVED7[683];__IOuint32_tPOWER;//Peripheralpowercontrol.}NRF_TIMER_Type;NRF_TIMER2-MODE=TIMER_MODE_MODE_Timer;//Mode:Timer…0NRF_TIMER2-PRESCALER=4;//16M/2^4=1usNRF_TIMER2-CC[0]=32NRF_TIMER2-INTENSET=116//TIMER_INTENSET_COMPARE2_EnabledTIMER_INTENSET_COMPARE2_Pos;使能中断NRF_TIMER2-SHORTS=11//(TIMER_SHORTS_COMPARE1_CLEAR_EnabledTIMER_SHORTS_COMPARE1_CLEAR_Pos);//clearcompare1eventvoidTIMER2_IRQHandler(void){if((NRF_TIMER2-EVENTS_COMPARE[2]==1)&&(NRF_TIMER2-INTENSET&TIMER_INTENSET_COMPARE2_Msk))//Clearinterrupt.NRF_TIMER2-EVENTS_COMPARE[2]=0;…..}主函数:NVIC_EnableIRQ(TIMER2_IRQn);//EnableinterruptonTimer__enable_irq();NRF_TIMER2-TASKS_START=1;//Startthetimer.
本文标题:nrf51822学习笔记之TIMER
链接地址:https://www.777doc.com/doc-2889883 .html