timers and atd
TRANSCRIPT
-
8/9/2019 Timers and ATD
1/17
Timers and ATDFeatures Covered
Timers
Input CaptureATD
Features Not Covered
Pulse AccumulatorOC7 Data Output
-
8/9/2019 Timers and ATD
2/17
-
8/9/2019 Timers and ATD
3/17
Timer Modes
For TimersFor External Interrupt Generation
-
8/9/2019 Timers and ATD
4/17
Turning on the timer
Set to 1 to enable
Clock counts in TCNT
-
8/9/2019 Timers and ATD
5/17
Creating a regular time interrupt 1
Clock prescale @ 8MHzResolution = 125ns X Prescale Factor
Max Duration = 125ns X Prescale Factor X (2^16)
-
8/9/2019 Timers and ATD
6/17
Interrupt Triggering
Set to one to enable interrupt for that port
Write a One to clear the interrupt flag
-
8/9/2019 Timers and ATD
7/17
Scheduling an Interrupt
Must be in output compare mode.
Store the time to interrupt in thecorresponding TC register.
-
8/9/2019 Timers and ATD
8/17
In the interrupt handler Clear the interrupt by writing a
1 to the correct bit in TFLG1.
Schedule the next interrupt
relative to the TC, not theTCNT.
Note, the example code
schedules future interrupts off
of the TCNT, which will cause
the period to drift due tointerrupt handling time.
Movb #$80,TFLG1 ;clear C7F
FLG1
;Schedule Next T7
Interrupt
ldd TC7H ;Load Timer Counter
into D
addd T7RATE ;Add T7RATE to D
std TC7H ;Store D to TC7H
bsr work ;do actual work
rti
-
8/9/2019 Timers and ATD
9/17
Automatic Actions
-
8/9/2019 Timers and ATD
10/17
Example 5ms clockT7RATE equ #1250 ;1250 / 250KHz = 5ms
sei ;set interrupt mask - disable interrupts
movw #Timer7IRQ,Timer7vec ;Load Address of Timer7IRQ into vector
movb #$05,TMSK2 ;Prescalar = /32 = 250KHz clock
movb #$80,TIOS ;Set Timer 7 and Timer 6 to output compare
movb #$80,TCTL1 ;Set Timer 7 and Timer 6 output to toggle
movb #$80,TMSK1 ;Enable interrupt for Timer 7
; Schedule first interupt Relative to TCNT
ldd TCNTH ;Load Timer Counter into D
addd T7RATE ;Add T7RATE to D
std TC7H ;Store D to TC7H
;Schedule Timer 6 Interrupts
movb #$80,TSCR ;Enables timers
;do nothing else before enabling interupts
cli ;clear interrupt mask - enable interru
Timer7IRQ
Movb #$80,TFLG1 ;clear C7F FLG1
;Schedule Next T7 Interrupt
ldd TC7H ;Load Timer Counter into D
addd T7RATE ;Add T7RATE to D
std TC7H ;Store D to TC7H
rti
-
8/9/2019 Timers and ATD
11/17
Input Capture
Bit in TIOS should be clear
Select which edge to trigger on in
When the edge occurs, an input is generated(if enabled) and the current TCNT is copiedinto the TC for that port.
-
8/9/2019 Timers and ATD
12/17
ATD
Benefits over external ATD
Easier to set up, less wiring.
More accurate ( up to 10 bits ).
You get 8 channels ( 0 7 ).
Still have to level shift to between 0 and 5V
-
8/9/2019 Timers and ATD
13/17
Proper Configuration
SignalSource
Conditioning
+
-
68HC12
0~5V
R
For Low Frequency Signals
Add a resistor to protect HC12 in case
input goes above 5V or less than ground.
-
8/9/2019 Timers and ATD
14/17
Powering On the ATD
Write a 1 here
Allow 100us to stabilizeOther Bits ControlATD Interrupts
-
8/9/2019 Timers and ATD
15/17
Select Time per sample
Sets 10 Bit mode when 1
-
8/9/2019 Timers and ATD
16/17
Choose What to sample
4 Or 8 Conversions
Run Continuously?
Divide Conversions between registers?
Which Ports to run conversion on
Sample Values
$70 -Continuously do one conversion on each ATD port
$67 -Continuously do 8 conversion sequences on port 7
-
8/9/2019 Timers and ATD
17/17
Getting the Results
Read ADRnH to get 8 ( or 10 ) bits of ATD
data Most significant 8 bits in ADRnH,
lower two bits in ADRnL.
If the flag in ATDSTAT is 1, the conversion
is complete.