Docsity

Using a Free-Running Counter for Input Capture in EE251: Timer Module - Prof. William D. E, Study notes of Microprocessors

Partial preview of the text

Download Using a Free-Running Counter for Input Capture in EE251: Timer Module - Prof. William D. E and more Study notes Microprocessors in PDF only on Docsity!

EE251: Thursday October 23 • Timer Module – Chapter 8• Homework: #5 due on Oct. 30.• Lab Practicals this week in lab.• Lab #8 is next week, due in 2 weeks.

Activities Needed to Use Free-Running Counter •^ Note! •^ Set the prescaler inTSCR2 to the intervalyou want to use incounting.•^ Set TEN (Timer Enable)bit of TSCR1 to 1 toenable counting.•^ How could you use thiscounter?•^ How can you easily turn^ DELTAT^ into time?

  • Example^ of^ using^ Free-Running

CounterNOLIST #include^ 9S12Creg.incLISTTSCR1_IN^ EQU^ $^

;Sets^ TEN^ bit^ of^ TSCR TM2_IN^ EQU^ %^

;Sets^ PR^ bits^ to *^

divide^ by^ 2^3^ =^8 ORG $ TSTART^ DC.W 0^

;Counter^ start^ value TSTOP^ DC.W 0^

;Counter^ stop^ value DELTAT^ DC.W 0^

;Elapsed^ counts

  • Initialize^ CountingORG^ $1000LDAB^ TSCR^

;Get^ current^ valueANDB #%11111000 ;Clear^ PR^ bitsORAB #TM2_IN ;Set^ PR^ bitsSTAB TSCR2 ;Put^ back^ in^ TSCR2LDAB #TSCR1_INSTAB TSCR1 ;Initialize^ TSCR1LDD TCNT ;Get^ initial^ counter^ valueSTD TSTART ;Save^ it ^ Other^ computer activity *^ End^ of^ other^ computer^ activityLDD^ TCNT^

;Get^ end^ counter^ valueSTD TSTOP ;Save^ itSUBD TSTART ;Compute^ DifferenceSTD DELTAT ;Save^ itSWIEND

Input Capture Signals • The Input Capture system captures snapshots of the16-bit free-running counter at input signal edges, so wemust define the parameters (rising, falling, or any edge)of this signal which we want to measure.• For this purpose, two bits of the Timer ControlRegisters (TCTL3 and TCTL4-- $004A and $004B) areused.

Input Capture Edge Selection^ EDGnB EDGnA

Configuration 0 0 Capture disabled 0 1 Rising Edge 1 0 Falling Edge 1 1 Any Edge

Input Capture Algorithm 1. Choose an edge to capture. Set TCTL3, 4 appropriately.2. Monitor the associated Channel Flag in TFLG13. When this flag sets, store contents of TCn into memory.(Save the snapshot count).4. Reset CnF by writing a 1 to it (in TFLG1).5. If appropriate, choose a different edge for nextmeasurement, e.g. pulse width measurement.6. Again monitor associated Channel Flag; when it sets saveTCn into another memory address. (Save 2nd snapshotcount).8. Take arithmetic difference of these counts, multiply bycounter period. You now have a pulse period or width.

Input Capture Example: Pulse Width You want to determine the pulse width of a low active (negative logic) signal on Channel 3. 1. First we need a falling edge. (Set EDG3B to 1 and EDG3A to 0)2. Monitor Channel Flag C3F in TFLG1.3. When this flag sets, store contents of TC3 into memory. MOVW^ TC3^ Start 4. Reset C3F by writing a 1 to it in TFLG1.5. Specify a rising edge for next measurement (Set EDG3B to 0 andEDG3A to 1).6. Again monitor Channel Flag C3F in TFLG1.7. When flag sets, store contents of TC3 into another memory address. MOVW^ TC3^ Stop 8. (Stop - Start)*Period = Pulse Width.

Input Capture Example: Period You want to determine the period of a signal on Channel 2,to 0.5^ μsec. resolution, with Bus Clock = 8 MHz. 1. Store %010 into ____ of ______, setting counter period to 0.

μsec.

  1. We choose a falling edge. (Set ______ to 1 and ______ to 0)3. Monitor Channel Flag ____ in ________.4. When this flag sets, store contents of TC2 into memory.^ MOVW^ TC^ Start 5. Reset C2F by writing a __ to it in TFLG1.6. Again monitor Channel Flag C2F in TFLG1.7. When flag sets, store contents of TC2 into another memoryaddress.^ MOVW^

TC2^ Stop

8.^ (Stop-Start)*_________ .^ = Period of input signal in

μsec. Question: Assuming perfect clock accuracy and a rock steady signal,what is the largest error you would expect to see in this periodmeasurement? How much can you reduce this error?

Initialize registers and masks- TSCR2- TCTL2- TIOS- TSCR1^ Start^11 Lecture #^ Declare Variables:- Start- Period Clear IC2 flagSave count in Start Clear IC2 flag1st Edge? Calculate PeriodStore Period End^ Yes NoNo 2nd Edge? Yes

Flowchart of Period Example

Program Example: Period (cont’d)^ ORG^ $800^ ;Data

at^ $ edge_1^ DS.W 1^

;reserve^ word^ for^ variable period^ DS.W 1^

;reserve^ word^ for^ variable *^ MAINORG^ $^

;Program^ at^ $1000BSR TIMERINIT ;timer^ initialization

subroutine BSR^ MEAS_PER^

;measure^ period SWI-----------------------------------------------Subroutine^ TIMERINIT:^ Initialize

timer^ for^ IC *-----------------------------------------------TIMERINIT^ CLR^ TIE^

;disable^ timer^ interrupts LDAA^ #TSCR2_IN^

;load^ TSCR2^ using^ index

addr STAA^ TSCR^

;disable^ overflow,^ prescale= LDAA^ #TCTL4_IN^

;conf^ IC2^ for^ rising^ edge STAA^ TCTL4BCLR^ TIOS,%^

;Assure^ IC2^ is^ input^ device MOVB^ #TSCR1_IN,TSCR^

;conf^ IC2^ for^ rising^ edge-enable

timer RTS

Program Example: Period (cont’d) ------------------------------------------------------------------* Subroutine^ to^ measure^ period

of^ signal^ and^

*^ store^ result^ at^ 2-byte

location^ PERIOD^

------------------------------------------------------------------MEAS_PER^ LDAA^ #CLR_CH

;Clear^ IC

flag STAA^ TFLG1WTFLG BRCLR^ TFLG1,#$04,WTFLG

;wait^ for^ first^ edge MOVW^ TC2^ EDGE_^

;save^ value^ from^ TC2^ reg LDAA^ #CLR_CH^

;Clear^ IC2^ flag STAA^ TFLG1WTFLG1 BRCLR^ TFLG1,#$04,WTFLG

;wait^ for^ second^ edge LDD^ TC^

;load^ value^ from^ TC SUBD^ EDGE_^

;calculate^ period:^ reg_D

-^ Edge_ STD^ PERIOD^

;store^ period RTSEND