Hardware Reference
In-Depth Information
Example 6.5
▼
Write a C program to use the RTI to time-multiplex four seven-segment displays using
the circuit shown in Figure 4.18 and shift the seven-segment display pattern as described in
Example 6.4. Turn on one display at a time and light each display for about 1 ms, then switch
to the next display. Use display 0 to display 3. Use CodeWarrior and a demo board programmed
with serial monitor to implement the circuit.
Solution:
To implement the digit sequence shifting program in C language, we make the follow-
ing arrangement:
•
Place the segment patterns in one array (
segPat[]
). This array is the overlapping
arrangement of 10 display sequences as shown in Figure 5.2.
•
Place digit select values in one array (
digit[]
).
•
Use a variable (
seq
) as an index to the segment pattern array that identifies the first
digit of the current sequence.
•
Use a variable (
ix
) as an index to the digits within one sequence. The range of this
variable is from 0 to 3.
•
Use a variable (
count
) to specify the repetition count of a sequence.
The main function that performs the initialization and the RTI service routine that performs
time multiplexing is
#include “c:\cwHCS12\include\hcs12.h”
#include “c:\cwHCS12\include\SetClk.h”
int seq; // start index to segPat[] of a sequence of digits (0 to 9)
int ix; // index of digits of a sequence (0 to 3)
int count; // repetition count of a sequence
char segPat[13]
5
{0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x67, 0x3F, 0x06, 0x5B, 0x4F};
char digit[4]
5
{0xFE, 0xFD, 0xFB, 0xF7};
void main (void) {
seq
5
0;
// initialize the start index to segPat[] for the display sequence
ix
5
0;
// initialize the index of a new sequence
count
5
400;
// initialize the RTI count of a sequence
SetClk8();
// set E-clock to 24 MHz from an 8-MHz crystal oscillator
RTICTL
5
0x40;
// RTI interval set to 2**10 OSCCLK cycles
DDRB
5
0xFF;
// configure Port B for output
DDRP
5
0xFF;
// configure Port P for output
CRGINT|
5
RTIE;
// enable RTI
asm(“CLI”);
// enable interrupt globally
while(1);
}
// RTI service routine
interrupt void rtiISR(void) {
CRGFLG
5
0x80;
// clear RTIF bit
PTB
5
segPat[seq
1
ix];
// send out digit segment pattern
PTP
5
digit[ix];
// turn on the display
ix
11
;
// increment the index to digits of a sequence
if (ix
55
4)
// make sure the index to digits of a sequence is from 0 to 3
ix
5
0;
//
“
Search WWH ::
Custom Search