ENG2008 Lab 3
1 Overview
ENG2008 Lab exercise 3 General purpose I/O
In this lab exercise you will compose C programs to control I/O ports of the KL25Z microcontroller. There are five tasks in this lab.
Assessment: Complete the missing code for Tasks 1‐3 in Lab 3 answer sheet. 2 Task 1: Traffic lights
In this task, you will configure output pins to turn on and off the LEDs on the board. There are three LEDs on the board, i.e. one red, one green and one blue LED. Connection of these LEDs to the microcontroller is shown in Fig. 1. A ‘0’ on the output pin will turn on corresponding LED and a ‘1’ will turn it off.
Figure 1. Connection of LEDs.
Modify Blinky.c that we used in our first lab exercise to implement a traffic light. The traffic
light should have the following pattern in an infinite loop: Green (3 seconds)
↓
Flashing Yellow (5 times with an interval of half second) ↓
Red (3 seconds)
↓
Yellow (3 seconds)
Hint:
1. Turn on Green and Red LEDs at the same time will result in yellow.
2. Use 0xC00000 as input to Delay subroutine for a 3‐second delay.
3. Use 0x200000 as input to Delay subroutine for a half‐second delay.
1/5
ENG2008 Lab 3
3 Task 2: Input
In this task, you will control the on‐board LEDs using input signals. Connect three toggle switches to three inputs of the microcontroller (PTD0, PTD2 and PTD5). You must enable pull‐up resistors of these input pins. Each toggle switch controls one of the three on‐board LEDs. When the input is logic 1, the relevant LED will be on. When the input is logic 0, the relevant LED will be off.
4 7‐Segment display
In this exercise, you will use the KL25Z microcontroller to control a 7‐segment display, HDSP‐ B09G.
4.1 Basics
HDSP‐B09G can display four digits each has 7 LEDs. These LEDs are common cathode and their connection is shown in Fig 2. There are two test of pins, i.e. control pins (A‐G and DP) and selection pins (DIGIT1‐4).
Figure 2. Internal connection of the 7‐segment display, HDSP‐B09G.
Fig. 3 shows layout of the LEDs with respect to its control pins. Depending on the decimal digit to be displayed, the particular set of LEDs is forward biased. For instance, to display the numerical digit 0, we will need to light up six of the LED segments corresponding to a, b, c, d, e and f. Thus the various digits from 0 through 9 can be displayed using a 7‐segment display as shown in Fig. 4.
Figure 3. LEDs layout of the 7‐segment display, HDSP‐B09G. 2/5
ENG2008 Lab 3
Figure 4. 7‐Segment Display Segments for all numbers.
4.2 Connection between the FRDM‐KL25Z board and HDSP‐B09G
Fig. 5 shows the pin layout of HDSP‐B09G. Plug the HDSP‐B09G to the Protostation Advanced Breadboard Fig. 6. Recommended connection between the microcontroller and HDSP‐B09G is shown in Fig. 7. A resistor is supposed to be connected between each selection pin and the relevant microcontroller pin to limit the current provided by the output pin of the microcontroller. Since KL25Z has limited the output current of its output pin to 6 mA, this resistor is not required in this exercise.
(a) (b) Figure 5. (a) HDSP‐B09G and (b) its pin layout.
Figure 6. Protostation advanced breadboard.
3/5
ENG2008 Lab 3
1 (E) 2 (D) 3 (DP)
4 (C) 5 (G)
6 (Digit4) 7 (B)
(Digit1) 12 (A) 11 (F)10 (Digit2) 9 (Digit3) 8
PTC17
PTC16 PTC7 PTC13 PTC0 PTC12 PTC3 PTC11 PTC4 PTC10 PTC5
PTC6
Figure 7. Recommended connection between the FRDM‐KL25Z board and HDSP‐B09G.
4.3 Task 3: Display 1 digit
Since HDSP‐B09G is common cathode, the control pins need to be high and the relevant selection pin(s) needs to be low to display the digits. For example, if you want to display 3 in DIGIT 4 as in Fig. 8, segments A, B, C, D and G, i.e. PTC0, PTC6, PTC12, PTC16 and PTC11, should be high while Digit 4, i.e. PTC10 should be low.
Figure 8. Example 1: display ‘3’ in DIGIT 4.
Now, compose a C program to display ‘0’, ‘1’ … ‘9’ in DIGIT 4 with an interval of 0.5 second.
4/5
HDSP‐B09G
ENG2008 Lab 3
The following two tasks are not part of the assessment.
4.4 Task 4: Display multiple digits
As can be seen in Fig. 2, the four digits in HDSP‐B09G share the same control pins, i.e. A‐G and DP. In order to display different numbers in different digits, the four selection pins DIGIT1‐4 needs to be cleared in turn at the right time. For example, if you want to display ‘1234’ as in Fig. 9, follow the following steps.
Figure 9. Example 2: display ‘1234’.
Step 1: display ‘1’ in Digit 1. Set segments B and C (PTC6 and PTC12) while clearing Digit 1
(PTC7) and setting Digit 2‐4 (PTC4, PTC8 and PTC10).
Step 2: display ‘2’ in Digit 2. Set segments A, B, D, E and G (PTC0, PTC6, PTC16, PTC17 and PTC11) while clearing Digit 2 (PTC4) and setting Digit 1, 3, 4 (PTC7, PTC8 and PTC10).
Step 3: display ‘3’ in Digit 3. Set segments A, B, C, D and G ( PTC0, PTC6, PTC12, PTC16 and PTC11) while clearing Digit 3 (PTC5) and setting Digit 1, 2, 4 (PTC7, PTC4 and PTC10).
Step 4: display ‘4’ in Digit 4. Set segments B, C, F and G (PTC6, PTC12, PTC3 and PTC11) while clearing Digit 4 (PTC10) and setting Digit 1‐3 (PTC7, PTC4 and PTC8).
A short delay (~1ms) should be implemented between steps in order for us to see the four digits ‘simultaneously’ due to persistence of vision. Try longer (50 ms) and see what happens.
Now, display 0000 to 9999 with an interval of 1 ms.
5 Task 5: Digital Clock
In this task, you need to display the current time on the 4‐digit 7‐segment display. Display hours in the first two digits and minutes in the other two digits. DP2 and DP3 need to flash once every second. To light up DP2 and DP3 simultaneously, DP pin (PTC13) should be high while clearing Digit2 (PTC9) and Digit3 (PTC8) at the same time.
5/5