最新16定时器汇总.docx
《最新16定时器汇总.docx》由会员分享,可在线阅读,更多相关《最新16定时器汇总.docx(28页珍藏版)》请在冰豆网上搜索。
最新16定时器汇总
16定时器
Chapter16
TimerModule(TIM16B8CV2)
基本定时器包含一个16位可编程的计数器,可被一可编程增强型分频器。
定时器可用于多种用途,包括当产生一个输出波形,同时进行输入波形测量。
脉宽可从几微秒变到几秒。
这定时器包含8完成输入捕捉/输出比较通道和一个脉冲累加器。
这输入捕捉功能被用来探测选定的过渡期边缘和记录时间。
输出比较功能是用于生成输出信号或定时器的软件延迟。
16位脉冲计数器是用来作为一个简单的事件计数器或一个门控的时间累加器。
在运行模式时,脉冲累加器共享定时器通道7。
全面进入计数器寄存器,或输入捕捉/输出比较寄存器应在一个时钟周期运行。
单独地访问所有寄存器的高低字节和同一时间访问,结果是不一样的。
Thebasictimerconsistsofa16-bit,software-programmablecounterdrivenbyaenhancedprogrammableprescaler.
Thistimercanbeusedformanypurposes,includinginputwaveformmeasurementswhilesimultaneouslygeneratinganoutputwaveform.Pulsewidthscanvaryfrommicrosecondstomanyseconds.
Thistimercontains8completeinputcapture/outputcomparechannelsandonepulseaccumulator.Theinputcapturefunctionisusedtodetectaselectedtransitionedgeandrecordthetime.Theoutputcomparefunctionisusedforgeneratingoutputsignalsorfortimersoftwaredelays.The16-bitpulseaccumulatorisusedtooperateasasimpleeventcounteroragatedtimeaccumulator.Thepulseaccumulatorshares
timerchannel7whenineventmode.
Afullaccessforthecounterregistersortheinputcapture/outputcompareregistersshouldtakeplaceinoneclockcycle.Accessinghighbyteandlowbyteseparatelyforalloftheseregistersmaynotyieldthesameresultasaccessingtheminoneword.
16.1.1Features
8个输入捕捉/输出比较通道
时钟预分频
16位计数器
16位脉冲累加器
TheTIM16B8CV2includesthesedistinctivefeatures:
•Eightinputcapture/outputcomparechannels.
•Clockprescaling.
•16-bitcounter.
•16-bitpulseaccumulator.
16.1.2ModesofOperation
Stop:
Timerisoffbecauseclocksarestopped.
停止:
因为时钟停止,定时器停止。
冻结:
定时器计数器继续运行,除非TSFRZinTSCR(0x0006)置1.
等待:
计数器继续运行,除非TSWAIinTSCR(0x0006)置1.
正常:
定时器计数器继续运行,除非TENinTSCR(0x0006)被清0.
Stop:
Timerisoffbecauseclocksarestopped.
Freeze:
Timercounterkeeponrunning,unlessTSFRZinTSCR(0x0006)issetto1.
Wait:
Counterskeeponrunning,unlessTSWAIinTSCR(0x0006)issetto1.
Normal:
Timercounterkeeponrunning,unlessTENinTSCR(0x0006)isclearedto0.
16.2.1IOC7—InputCaptureandOutputCompareChannel7Pin
Thispinservesasinputcaptureoroutputcompareforchannel7.这也可以被配置为脉冲累加器输入。
16.2ExternalSignalDescription
TheTIM16B8CV2modulehasatotalofeightexternalpins.
16.2.1IOC7—InputCaptureandOutputCompareChannel7Pin
Thispinservesasinputcaptureoroutputcompareforchannel7.Thiscanalsobeconfiguredaspulseaccumulatorinput.
16.2.2IOC6—InputCaptureandOutputCompareChannel6Pin
Thispinservesasinputcaptureoroutputcompareforchannel6.
16.2.3IOC5—InputCaptureandOutputCompareChannel5Pin
Thispinservesasinputcaptureoroutputcompareforchannel5.
16.2.4IOC4—InputCaptureandOutputCompareChannel4Pin
Thispinservesasinputcaptureoroutputcompareforchannel4.Pin
16.2.5IOC3—InputCaptureandOutputCompareChannel3Pin
Thispinservesasinputcaptureoroutputcompareforchannel3.
16.2.6IOC2—InputCaptureandOutputCompareChannel2Pin
16.2.7IOC1—InputCaptureandOutputCompareChannel1Pin
Thispinservesasinputcaptureoroutputcompareforchannel1.
16.2.8IOC0—InputCaptureandOutputCompareChannel0Pin
Thispinservesasinputcaptureoroutputcompareforchannel0.
NOTE
ForthedescriptionofinterruptsseeSection16.6,“Interrupts”.Thispinservesasinputcaptureoroutputcompareforchannel2.
16.3.2RegisterDescriptions
16.3.2.1TimerInputCapture/OutputCompareSelect(TIOS)
0=输入捕捉
1=输出比较
TIOS
TIOS_IOS0
16.3.2.2TimerCompareForceRegister(CFORC)
Read:
Anytimebutwillalwaysreturn0x0000(1stateistransient)
1=强制输出比较
其实当比较寄存器的值和计数器的值相等时,也会输出比较,只不过它的作用是立即输出比较
CFORC
CFORC_FOC0
16.3.2.3OutputCompare7MaskRegister(OC7M)
1=设为输出比较(当TIOS中的IOS7和IOSn被设为输出比较时)
该寄存器的作用是控制0到6的任意一路或全部输出比较
OCPDx(x是0to6)置0:
使能定时器端.
OC7M
OC7M_OC7M0
16.3.2.4OutputCompare7DataRegister(OC7D)
OC7DATE当OC7比较事件发生时,引脚的逻辑电平
OC7D
注意:
不能进行位运算
16.3.2.5TimerCountRegister(TCNT)
The16-bitmaintimerisanupcounter.
Afullaccessforthecounterregistershouldtakeplaceinoneclockcycle.Aseparateread/writeforhighbyteandlowbytewillgiveadifferentresultthanaccessingthemasaword.
Read:
Anytime
Write:
Hasnomeaningoreffectinthenormalmode;onlywritableinspecialmodes(test_mode=1).TheperiodofthefirstcountafterawritetotheTCNTregistersmaybeadifferentsizebecausethewriteisnotsynchronizedwiththeprescalerclock.
这个是向上记数的,记数器寄存器,它是自由记数器,不用写
TCNTHi高八位
TCNTLo低八位,不能进行位运算
TCNT16位
16.3.2.6TimerSystemControlRegister1(TSCR1)
多了第三位:
PRNT
PRNT:
精密定时器
0使用传统的定时器。
TSCR2寄存器的PR0,PR1和PR2位用于定时计数器分频器的选择。
1使精密定时器。
所有PTPSR寄存器的位都用于精密定时器分频器的选择
此位只有在重置时写有效
TEN:
1=定时器使能(开启定时器)
0=不使能
TSWAI–在WAIT模式时,定时器停止
0=不停止
1=停止
TSFRZ:
TSBCK–在DEBUG模式时,定时器停止
0=不停止
1=停止
TFFCA–定时器快速标志位清除使能
0=定时器标志位正常清零
1=输入捕捉时读取寄存器,输出比较时写寄存器,可以对相应通道标志位清零
TSCR1
TSCR1_PRNT
TSCR1_TFFCA
TSCR1_TSFRZ
TSCR1_TSWAI
TSCR1_TEN
16.3.2.7TimerToggleOnOverflowRegister1(TTOV)
TOVx–定时器溢出时,电平翻转
只有当通道被配置成输出比较时,才有效
1=电平翻转
0=不翻转
TTOV
TTOV_TOV0
TTOV_TOV7
16.3.2.8TimerControlRegister1/TimerControlRegister2(TCTL1/TCTL2)
OMn=输出模式
OLn=输出电平
TCTL1
TCTL1_OL4--------》TCTL1_OL7
TCTL1_OM4-------》TCTL1_OM7
TCTL2
TCTL2_OL0------》TCTL2_OL3
TCTL2_OM0--------》TCTL2_OM3
独立运行输入捕捉或输出比较的16位脉冲累加器,用户必须设置相应的位IOSx=1,OMx=0andOLx=0.。
OC7M7在OC7M寄存器还必须清除。
Tooperatethe16-bitpulseaccumulatorindependentlyofinputcaptureoroutputcompare7and0respectivelytheusermustsetthecorrespondingbitsIOSx=1,OMx=0andOLx=0.OC7M7intheOC7Mregistermustalsobecleared.
16.3.2.9TimerControlRegister3/TimerControlRegister4(TCTL3andTCTL4)
输入捕捉边沿控制
TCTL4也是控制8位脉冲记数器PAC0---3的寄存器,控制捕捉方式。
EDGE0B&EDGE0这两位控制16位脉冲记数器PACB的捕捉方式。
TCTL3
TCTL3_EDG4A----------》TCTL3_EDG7A
TCTL3_EDG4B-----------》TCTL3_EDG7B
TCTL3_EDG4x
TCTL3_EDG5x
TCTL3_EDG6x
TCTL3_EDG7x
TCTL4
TCTL4_EDG0A----------》TCTL4_EDG3A
TCTL4_EDG0B-----------》TCTL4_EDG3B
TCTL4_EDG1x
TCTL4_EDG2x
TCTL4_EDG3x
EDGnBEDGnA:
InputCaptureEdgeControl—Theseeightpairsofcontrolbitsconfiguretheinputcaptureedgedetectorcircuits.
16.3.2.10TimerInterruptEnableRegister(TIE)
定时器输入捕捉/输出比较中断使能
TIE
TIE_C0I---------》TIE_C7I
C7I:
C0I:
InputCapture/OutputCompare“x”InterruptEnable—ThebitsinTIEcorrespondbit-for-bitwiththebitsintheTFLG1statusregister.Ifcleared,thecorrespondingflagisdisabledfromcausingahardwareinterrupt.Ifset,thecorrespondingflagisenabledtocauseainterrupt.
16.3.2.11TimerSystemControlRegister2(TSCR2)
TOI:
1=定时器溢出中断使能
0=中断不使能
TCRE:
1=定时器由OC7比较输出时复位
0=定时器自由运行
PR2,PR1,PR0;分频系数选择
NOTE
Thenewlyselectedprescalefactorwillnottakeeffectuntilthenextsynchronizededgewhereallprescalecounterstagesequalzero.
TSCR2
TSCR2_PR0
TSCR2_PR1
TSCR2_PR2
TSCR2_PR
TSCR2_TCRE
TSCR2_TOI
TSCR2_PR
新预定因素直到下一个同步边缘(所有计数等于零)就会有影响,
7TOI:
TimerOverflowInterruptEnable
0Interruptinhibited.
1HardwareinterruptrequestedwhenTOFflagset.
3TCRE
TimerCounterResetEnable—Thisbitallowsthetimercountertoberesetbyasuccessfuloutputcompare7event.Thismodeofoperationissimilartoanup-countingmoduluscounter.
0Counterresetinhibitedandcounterfreeruns.
1Counterresetbyasuccessfuloutputcompare7.
IfTC7=0x0000andTCRE=1,TCNTwillstayat0x0000continuously.IfTC7=0xFFFFandTCRE=1,TOFwillneverbesetwhenTCNTisresetfrom0xFFFFto0x0000.
2PR[2:
0]
TimerPrescalerSelect—ThesethreebitsselectthefrequencyofthetimerprescalerclockderivedfromtheBusClockasshowninTable16-14.
16.3.2.12MainTimerInterruptFlag1(TFLG1)
Write:
用于清除标志时(设置相应的位使相应的位清除).写0将不会影响到当前的位状态。
比较/捕捉中断标志位,写“1”清除中断标志位
每次捕捉到两个有效的值,就会产生中断
C0F是16位脉冲记数器B的中断标志位
C3F-C0F是8位脉冲记数器PAC3-PAC0的中断标志位
当TFFCA=1时,这些标志位会被自动清除。
TFLG1
TFLG1_C0F--------》TFLG1_C7F
C[7:
0]F:
InputCapture/OutputCompareChannel“x”Flag—Theseflagsaresetwhenaninputcaptureoroutputcompareeventoccurs.Clearachannelflagbywritingonetoit.
WhenTFFCAbitinTSCRregisterisset,areadfromaninputcaptureorawriteintoanoutputcomparechannel(0x0010–0x001F)willcausethecorrespondingchannelflagCxFtobecleared.
16.3.2.13MainTimerInterruptFlag2(TFLG2)
TFLG2indicateswheninterruptconditionshaveoccurred.Toclearabitintheflagregister,writethebittoone.
Read:
Anytime
Write:
Usedinclearingmechanism(setbitscausecorrespondingbitstobecleared).AnyaccesstoTCNTwillclearTFLG2registeriftheTFFCAbitinTSCRregisterisset.
定时器溢出标志位,写“1”清零
TFLG2
TFLG2_TOF
如果在TSCR寄存器中的TFFCA被置,任何访问TCNT将会清除TFLG2寄存器位
7TOF:
TimerOverflowFlag—Setwhen16-bitfree-runningtimeroverflowsfrom0xFFFFto0x0000.ThisbitisclearedautomaticallybyawritetotheTFLG2registerwithbit7set.(SeealsoTCREcontrolbitexplanation.)
16.3.2.14TimerInputCapture/OutputCompareRegistersHighandLow0–7(TCxHandTCxL)
原:
未分高低
根据相应的通道的TIOS位,当定义的过渡段被相应的输入捕捉边缘探测器感知或者促发一个外部比较,这些寄存器就被用于锁存自由运行计数器的值。
Read:
Anytime
Write:
随时输出比较功能。
在输入捕捉的时候,写入这些寄存器没有效果。
所有定时器输入捕捉/输出比较寄存器都重置为0x0000。
DependingontheTIOSbitforthecorrespondingchannel,theseregistersareusedtolatchthevalueofthefree-runningcounterwhenadefinedtransitionissensedbythecorrespondinginputcaptureedgedetectorortotriggeranoutputactionforoutputcompare.
Read:
Anytime
Write:
Anytimeforoutputcomparefunction.Writestotheseregistershavenomeaningoreffectduringinputcapture.Alltimerinputcapture/outputcompareregistersareresetto0x0000.
NOTE
Read/Writeaccessinbytemodeforhighbyteshouldtakesplace