Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

ESE 350

Embedded Systems/Microcontroller
Laboratory

Lecture 7
Tackling Lab 2

Professor Rahul Mangharam


Electrical and Systems Engineering
University of Pennsylvania
Morse Code
Code: Variable length series of dots and dashes Assumptions for Lab
A .- M -- 1 .---- . .-.-.-
N -. 2 ..--- , --..--
B -...
O --- 3 ...-- : ---... 200 ms>t>30 ms

C -.-. P .--. 4 ....- ? ..--.. 1


Q --.- 5 ..... ' .----. Dot
D -..
R .-. 6 -.... - -....- 0
E. S ... 7 --... / -..-.
T- 8 ---.. ( -.--.
F ..-. U ..- 9 ----. ) -.--.-
t>200 ms
G --. V ...- 0 ----- " .-..-.
1
W .-- = -...- Dash
H .... X -..- + .-.-.
0
I .. Y -.-- * -..-
Z --.. Acknowledgement ...-.
J .--- Transmission request -.- Space
K -.- Wait .-... t=400 ms
End of transmission ...-.-
L .-.. 0
Start of transmission -.-.- 2
Error .........
Selecting the Edge: TCTL4
The program selects which edge to detect using Timer Control 4 register.

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

We are interested input capture channel 0. For this channel, capturing


rising edge: TCTL4=0x01;
falling edge: TCTL4=0x02;
rising and falling edge: TCTL4=0x03;

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

TCNT(H) TCNT(L) 16-bit latch

16-bit free-running TC1H and TC1L


counter
Input Capture
Register

2. An input capture flag C0F is set in register TFLG1.


7 6 5 4 3 2 1 0
TFLG1
C7 FC6F C5F C4F C3F C2F C1F C0F

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 */

Note that a status flag is cleared by writing a 1 to it. 4


Responding to Edge Detection: TIE
If we want the microcontroller to respond to edge detection, that is, after
C0F flag becoming set, we need to enable a hardware interrupt.
1. Enable the local interrupt using Timer Interrupt Enable, or TIE, register.

To set local interrupt for IC1:


TIE = 0x01; /* writes a 1 to bit 0 to enable C0I interrupt */
TIE=TIE|0x01; /*writes 1 to COI but leave other bits unaffected */
TIE|=0x01; /*same as above, combine assignment operator */
2. Enable global interrupt with

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:

TIE has been set to 0x03;

If we attempt to enable C0I by using


TIE=0x04;
we have inadvertently cleared C1I and C0I by writing 0 to these bits.
If we use instead
TIE|=0x04;
Then 0x03|0x04 is a bitwise OR operator:
0000 0011 OR 0000 0100 gives 0000 0111 =0x07
6
Interrupt Programming for C0I
Enabling the interrupt is one of the steps of interrupt programming.
Another step is:

1. Write an interrupt service routine (ISR) to tell the microcontroller what to


do when the particular interrupt occurs. An example is as follows.

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

To set TOI: TMSK2=0x80; /* writes 1 to bit 7 for enable TOI */


TMSK2|=0x80; /*writes 1 to TOI but leave all other bits unaffected */ 8
Outputting Detected Character
1. Output using serial port through the putchar( ) command.

putchar(‘.’); /* outputs a dot */


putchar(‘-’); /* outputs a dash*/

2. Output to 7-segment display using PORTB.

PORTB=0x10; /*outputs 5 V (HIGH) maybe for dot */


PORTB=0x20; /*outputs 5V (HIGH) maybe for dash */

Connect appropriate PORTB pins to LED anode pins. Don’t


forget to use a current limiting resistor.
Diagram for 7-segment given under Course
Documents/Datasheets 9
Deciphering a Polling Statement
Polling looks at the logic level at the input port of interest.
while (!(PORTA & 0x04)); /* wait for key press; not using IOC0*/

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.

You might also like