Professional Documents
Culture Documents
Lecture 7
Lecture 7
Embedded Systems/Microcontroller
Laboratory
Lecture 7
Tackling Lab 2
Remember…
EDGxB EDGxA
0 0 capture disabled
0 1 capture on rising edge
1 0 capture on falling edge
1 1 capture on both edges
x = 1,...,3
3
Detecting an Edge: TFLG1
When a selected edge is detected at the input capture pin PT0
1. Contents of TCNT are latched into input capture register TC1H and TC1L
Once we make note of the flag, we should clear in order to detect future
edges on IOC0. To clear the flag C0F, use:
TFLG1=0x01; /* writes a 1 to bit 0 */
EnableInterrupts;
5
Proper Enable of Interrupt
TIE
Assume that we are using 3 channels as input capture: C0I, C1I, and C2I but C0I and
C1I have already enabled elsewhere, that is:
void IC1ISR ( )
{ TFLG1 = 0x01; /* clear C0I flag */
if (edge_cnt == 2)
edge1 = TCxH; /* save the first edge */
-- edge_cnt;
}
7
Timer Overflow:TFLG2 and TSCR2
To measure duration greater than the overflow period, it is necessary to
keep track of how many times the timer overflows.
1. A timer overflow flag (TOF) in Timer Flag 2 register is set every time the 16-bit
counter transition from $FFFF to $0000.
7 6 5 4 3 2 1 0
TFLG2
TOF
Similar to the C0F flag in TFLG1, the TOF flag should be cleared afterwards as follows.
TFLG2=0x80; /* writes a 1 to bit 7 to clear TOF flag */
2. Enable interrupt so that an ISR could be written to count the number of overflows
that occurs. Local interrupt uses Timer System Control Register 2.
7 6 5 4 3 2 1 0
TMSK2
TOI at $1024
A. If PA2 is connected a high asserting switch, then, when the key is pressed,
bit 2 is set to 1 (PORTA=0x04) .
B. When PA2 is 1, PORTA & 0x04 gives TRUE, saying the key was pressed.
C. Since we’re waiting for the key to be pressed, the wait state is the opposite
of key press which is the opposite of PORTA &0x04 being TRUE. So we use
the “!” or NOT logical operator.
D. The while loop keeps executing as long as the condition is TRUE which in
this case occurs while key is not pressed.
while (PORTA & 0x04); /* wait for key release, not using IOC0 */
The opposite of the previous situation. While the key is pressed, PORTA=0x04
and therefore the statement inside ( ) evaluates TRUE and thus the while loop
10
keeps executing.