Hardware Reference
In-Depth Information
To output a value to an output port, simply assign the value to its associated port data reg-
ister. For example, the following statement outputs the value 0x35 to Port B:
PTB 5 0x35; // the user can also use PORTB instead of PTB
To read a value from an input port, simply assign the associated port data register to the
destination variable. The following statement reads the value from Port A and assigns the value
to the variable xyz :
xyz
5 PTA;
// the user can also use PORTA instead of PTA
Example 5.8
Write a program to drive the LED circuit in Figure 4.16 and display one LED at a time from
the one driven by pin 7 toward the one driven by pin 0 and then reverse. Repeat this operation
forever. Each LED is lighted for 200 ms assuming that the HCS12 uses an 8-MHz crystal oscil-
lator to generate a system clock.
Solution: The values to drive Port B to turn on one LED at a time should be placed in a lookup
table. The program reads one value at a time from the table and outputs it to Port B and then
waits for 200 ms.
The creation of time delays requires the user to set the E-clock frequency properly. The
clock setting issue will be discussed in Chapter 6. However, we will invoke the SetClk8 func-
tion in Chapter 6 to set the E-clock to 24 MHz. The 200-ms time delay can be created by calling
the delayby100ms() function and passing 2 as the parameter. The C program that performs the
desired operation is as follows:
#include “c:\cwHCS12\include\hcs12.h”
void SetClk8(void);
void delayby100ms(int k);
void main (void)
{
unsigned char led_tab[16] 5 {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
char i;
DDRB
5 0xFF; // configure Port B for output
DDRJ
| 5 0x02; // configure PJ1 pin for output
PTJ
& 5 0xFD; // enable LEDs to light
SetClk8();
// enable PLL and set E-clock to 24-MHz using a 4-MHz crystal oscillator
while (1) {
for (i 5 0; i , 16; i 11 ) {
PTB 5 led_tab[i]; // output a new LED pattern
delayby100ms(2);
// wait for 200 ms
}
}
}
// Include the SetClk8() function here (to be discussed in Chapter 6)
// Include the delayby100ms() function here (to be discussed in Chapter 8)
 
Search WWH ::




Custom Search