You are on page 1of 8

Chapter 5

Peripherals Interfacing

5.1 HCS12 Input and Output Ports

A Port on the MC9S12 is a device the MC9S12 uses to control some hardware. Many of the MC9S12 ports are used to
communicate with hardware outside of the MC9S12.

Data from outside looks like a memory location. The MC9S12 ports (Port A, Port B, Port K and Port E) are accessed
by the MC9S12 by reading and writing memory locations $0000 to $03FF. Most I/O ports on MC9S12 can be configured
as either input or output.

Any or all bits of the I/O ports can be made as outputs by writing a 1 to the Device
MC9S12DP512 corresponding bits of their Data Direction
Guide V01.26
Registers DDRx and vice versa. On power up or reset the MC9S12, PORTA, PORTB, PTK and PTE are initialised as
1.5.1
input Detailed
ports. Register Map

$0000 - $000F MEBI map 1 of 3 (HCS12 Multiplexed External Bus Interface)

Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Read:
$0000 PORTA Bit 7 6 5 4 3 2 1 Bit 0
Write:
Read:
$0001 PORTB Bit 7 6 5 4 3 2 1 Bit 0
Write:
Read:
$0002 DDRA Bit 7 6 5 4 3 2 1 Bit 0
Write:
Read:
$0003 DDRB Bit 7 6 5 4 3 2 1 Bit 0
Write:
$0004 - Read: 0 0 0 0 0 0 0 0
Reserved
$0007 Write:
Read: Bit 1 Bit 0
$0008 PORTE Bit 7 6 5 4 3 2
Write:
Read: 0 0
$0009 DDRE Bit 7 6 5 4 3 Bit 2
Write:
Read: 0 0 0
$000A PEAR NOACCE PIPOE NECLK LSTRE RDWE
Write:
Read: 0 0
$000B MODE MODC MODB MODA IVIS EMK EME
Write:
Read: 0 0 0 0
$000C PUCR PUPKE PUPEE PUPBE PUPAE
Write:
Read: 0 0 0 0
$000D RDRIV RDPK RDPE RDPB RDPA
Write:
Read: 0 0 0 0 0 0 0
$000E EBICTL ESTR
Write:
Read: 0 0 0 0 0 0 0 0
$000F Reserved
Write:

$0010 - $0014 MMC map 1 of 4 (HCS12 Module Mapping Control)


Figure bellow shows simplified 2-bit Input/Output port:
Address Name Bit 7 Bit 6 Bit 5 Bit 45
4 Bit 3 Bit 2 Bit 1 Bit 0
Read: 0 0
$0010 INITRM RAM15 RAM14 RAM13 RAM12 RAM11 RAMHAL
Write:
46
Simple 2-bit port Chapter 5 Peripherals Interfacing

0
Tri state Buffer

1 0
=

1
=
Microcontroller Outside world Output = i

0
If Bit 7 of DDRA is 0, the port is an input port. Data written to flipflop does not get to pin through tristate buffer

If Bit 7 of DDRA is 1, the port is an output port. Data written to flipflop does get to pin through tristate buffer

I/O Data Register:


- When inputting data from input port, the user reads from the port data register.
- When outputting data to an output port, the user writes to the port data register.
- Each I/O data register is assigned to an address. For example, Port A data register is assigned to address 0, port B data
register is assigned to address 1.
- The addresses of the data registers are defined in the header file: mc9s12dp512.h

Example:
# include < hidef .h > /* common defines and macros */
# include " derivative . h " /* d e v i c e s p e c i f i c definitions */

void main ( void ) {

DDRB = 0 xff ; /* Make PORTB output */


PORTB = 0; /* Start with all off */
DDRA = 0 x00 ; /* Make PORTA input */
while (1) {
PORTB = PORTA ; /* Read data from PORTA into PORTB */
}
}

5.2 Switches

5.2.1 DIP Switches

DIP switches are connected to Port H (PTH) of the T-Board. Since there is no pull-up resistors connected for these switches,
we need to tell the microcontroller to pull-up this device as follow.
PERH = 0xff; This register configures whether a pull-up or pull-down device is activated, if the port is an Input
PPSH = 0x00; This register selects a pull-up device is connected to the port (DIP switches in the case of Lab 3)
Chapter 5 Peripherals Interfacing 47

5.2.2 Push Buttons

When the button is pressed, the port bit becomes 0 and when the button is released, the port bit becomes 1. Hence, when
the push button is pressed, there will be a zero pulse for some time (unlike the DIP switches).

Switch bouncing
5.2.3 Switch Debounce
chAbouncing
common problem in mechanical switches
When
mon a switch
problem is asserted,
in mechanical switches we expect a signal like this:
A common problem in mechanical switches is when a switch is asserted, we expect a signal like this:
a switch is asserted, we expect a signal like this:

Pressed Release
Pressed Release

Ideal switch Ideal switch

er, the actual signal is:


However, the actual signal is:
However, the actual signal is:

Real switch

5 to 20 ms 5 to 20 ms
Real switch

short period of time, thems


5 to 20 switch signal is bouncing.
5 to 20 ms 3 - 37

For a- short period


For a short ofoftime,
period theswitch
time, the switch signal
signal is bouncing.
is bouncing. 3 - 37
- Because switch contacts do not come to rest immediately
- Problem: Although it is one press, the microcontroller may consider each low voltage as one press. Humans cannot press
a switch several time in 20 ms, but because the microcontroller is so fast, it can read several low voltages in 20 ms.

Solution: De-bouncing using:

H/W solutions: A simple solution using a capacitor


- When the switch is closed, Vout =0
- Because of bouncing the capacitor charges for a short time making a small voltage on the capacitor.
- This small voltage will be interrupted as logic 0
making a small voltage on the capacitor.
This small voltage will be interrupted as logic 0
48 Chapter 5 Peripherals Interfacing

3 - 39
2- Software solutions
Software solutions: using a delay to skip the bouncing signal

5 to 20 ms 5 to 20 ms

When the port pin is When the port pin is


0, wait for around 1, wait for around
Wait until the switch
20ms to bypass the 20ms to bypass the
is released. bouncing.
bouncing.

ldy #20 HH: ldy #20


jsr Delay_yms brclr PTH,#%00000001,HH jsr Delay_yms

5.3 To solve the bouncing problem in the program of slide 3-35,


LEDs
add these instruction before start label.
3 - 40
LEDs are connected to PORTB of the T-Board. They are active low LEDs (0 to turn on and 1 to turn off).
Write a program to make one 7-segment display count from 0 to 9 and
repeats. The display
5.4 7-Segment Displays should change every one second.

EE 308 Spri

The circuit of the


dragon board
Making a pattern on a seven-segment LED

• Want to generate a particular pattern on a seven-segment LED:

f b
g
Display #3 is
enabledeif PP3 isc0
d

Steps:-
• Determine a number (hex or binary) which will generate each element of the

The microcontroller must send an appropriate value to Port B in order to display a certain value
– For example, following
to display a 0, turn on these steps:-
segments a, b, c, d, e and f, o
1, 2, 3, 4 and 5 of PTB. The binary pattern is 00111111, or $3f.
1- Make
1- Make a tableahaving
table a tohaving
g segments’avalues
to g for segments’
the digits 0, 1, 2, values
..9 for the digits
– To display 0 2 4 6 8, the0, 1, are
hex numbers 2,$3f,..9 $5b, $66, $7d, $7f.

2- Enable only one display.


• Put the numbers in a table

2- Enable only one display. • Go through the table one by one to display the pattern
3- X points at the beginning of the table • When you get to the last element, repeat the loop
3- X points
4- Output atpointed
the value the by beginning
X to port B of the table
5- Wait few milliseconds
4- Output the value pointed by X to port B
6- If X points to the last element, go to 3 else go to 4
5- Wait one second 3 - 22
6- If X points to the last element, go to 3 else go to 4
Chapter 5 Peripherals Interfacing 49
- A time-multiplexing technique is used to display multiple digits simultaneously.
- Repeatedly, enable one display for a short period of time and then disable this display and enable the next one. Port B =
the number you want display on the enabled display.
- Within few milliseconds, each display is lighted in turn many times. Due to the persistence of vision, all digits appear to
be lighted at the same time.
- For Port P, to enable one display at a time, only one bit should be zero and the other bits should be ones.
- To enable a display, its cathode should be 0.

Example:

/* A program to update four 7 - segment displays one by one periodically */


# include < hidef .h > /* common defines and macros */
# include " derivative . h " /* derivative - specific definitions */

void main ( void ) {


DDRB = 0 xFF ; // PORTB output for data
DDRP = 0 xFF ; // PORTP output for enabling / disabling the 7 - segments
while (1){
PORTB = 0 x5B ; // number 2
PTP = 0 x0E ; // on the most left digit of 7 - Seg
MSDelay (1); // Increase the delay to 100 ms on all 4 to see what heppens

PORTB = 0 x3F ;
PTP = 0 x0D ;
MSDelay (1);

PORTB = 0 x06 ;
PTP = 0 x0B ;
MSDelay (1);

PORTB = 0 x7F ; // number 8


PTP = 0 x07 ; // on the most right digit of 7 - Seg
MSDelay (1);
}
}

5.5 Buzzer

The sound transducer (buzzer) SP1 is controlled by the MCU’s port pin PT2. PT2 is internally connected to one of the
eight timer channels of the MCU.
- A sound can be generated by creating a digital waveform in the audible range and using it to drive the buzzer.
- By alternating the frequency of the generated waveform between two different values, a two tone siren can be generated
- Frequency generation is realised using the Output-Compare function of the timer system.
Port A is an 8-bit bi-directional port. Its primary usage is for a 4X4 keypad. If the port is not
used for the keypad, it can be used as a general-purpose I/O.
50 Chapter 5 Peripherals Interfacing
The schematic for the keypad connections is shown below:
5.6 Keypad

PA0 PA1 PA2 PA3


Col_0 Col_1 Col_2 Col_3

PA4, Row_0

PA5, Row_1

PA6, Row_2

PA7, Row_3

- A keypad is an array of switches.


- When a switch is pressed, it connects two conductors one row and one column
Keypad connections:
- First, allPA0
columns are set
connects COL0high
of (1111), then read the rows. If the rows are all low (000), this means no key is pressed. If
the keypad
one of the PA1
row connects
signals isCOL1high, of
a key is pressed. - To detect which key is pressed, we set each column input signal to 1 and
the keypad
PA2 connects COL2 of
set the others to (0), and read the rows the keypad
PA3 connects COL3 of the keypad
- If the rows
PA4areconnects
still lowROW0
(0000),ofthen we move to the next column and repeat the same process
the keypad
- If one of PA5
the rows is high, then the
connects ROW1 of the keypad key is found (row, column)
PA6 connects
# include < hidef .h >
ROW2 of the keypad
/* common defines and macros */
# include " PA7 connects
derivative . h " ROW3 /*ofderivative
the keypad- specific definitions */

Keypad
void mSDelay scan routine
( unsigned int ); sets PA3 low and PA0, PA1,PA2 high, then tests PA4-PA7.
If nocolumn
unsigned char key is, down,
row ; PA4-PA7 remain high.
If PA7 = low, the key 15 is down.
const unsigned char keypad [4][4] = { ’1 ’ , ’2 ’ , ’3 ’ , ’A ’ ,
’4 ’ , ’5 ’ , ’6 ’ , ’B ’ ,
If PA6 = low, the key 14 is down.
’7 ’ , ’8 ’ , ’9 ’ , ’C ’ ,
If PA5 = low, the key 13 is down.
’* ’ , ’0 ’ , ’# ’ , ’D ’ };
If PA4 = low, the key 12 is down.
void main ( void ){ // OPEN MAIN
DDRB Keypad
= 0 xFF ; scan routine sets PA2 low and PA0,
// MAKE PA1,
PORTB PA3 FOR
OUTPUT high,LEDS
then tests PA4-PA7.
DDRA = If no; key is down, PA4-PA7 remain
0 x0F high.ROWS INPUT AND COLUMNS OUTPUT FOR KEYPAD
// MAKE

If PA7 = low, the key 11 is down.


while (1){ // OPEN WHILE (1)
do {
If PA6 = low, the key10 is down.// OPEN do1
If PA5= =PORTA
PORTA low, |the key
9 is down. // COLUMNS SET HIGH
0 x0F ;
If PA4
row = low,
= PORTA & 0the key
8 is down. // READ ROWS
xF0 ;
} while ( row == 0 x00 ); // WAIT UNTIL KEY PRESSED // CLOSE do1
Keypad scan routine sets PA1 low and PA0, PA2, PA3 high, then tests PA4-PA7.
do {
If no key is down, PA4-PA7 remain high.
// OPEN do2
do { // OPEN do3
If PA7 = low,
mSDelay (1);
the key 7 is down. // WAIT
If PA6
row == PORTA
low, the
& 0 key
xF0 ; 6 is down. // READ ROWS
} If PA5( row
while = low,
== 0the
x00 key
); 5 is down. // CHECK FOR KEY PRESS // CLOSE do3
If PA4 = low, the key 4 is down.
mSDelay (15); // WAIT FOR DEBOUNCE
row = PORTA & 0 xF0 ;
} while ( row == 0 x00 ); // FALSE KEY PRESS // CLOSE do2
26
while (1){ // OPEN while (1)
PORTA &= 0 xF0 ; // CLEAR COLUMN
PORTA |= 0 x01 ; // COLUMN 0 SET HIGH
row = PORTA & 0 xF0 ; // READ ROWS
if ( row != 0 x00 ){ // KEY IS IN COLUMN 0
column = 0;
break ; // BREAK OUT OF while (1)
}
PORTA &= 0 xF0 ; // CLEAR COLUMN
PORTA |= 0 x02 ; // COLUMN 1 SET HIGH
row = PORTA & 0 xF0 ; // READ ROWS
Chapter 5 Peripherals Interfacing 51
if ( row != 0 x00 ){ // KEY IS IN COLUMN 1
column = 1;
break ; // BREAK OUT OF while (1)
}
PORTA &= 0 xF0 ; // CLEAR COLUMN
PORTA |= 0 x04 ; // COLUMN 2 SET HIGH
row = PORTA & 0 xF0 ; // READ ROWS
if ( row != 0 x00 ){ // KEY IS IN COLUMN 2
column = 2;
break ; // BREAK OUT OF while (1)
}
PORTA &= 0 xF0 ; // CLEAR COLUMN
PORTA |= 0 x08 ; // COLUMN 3 SET HIGH
row = PORTA & 0 xF0 ; // READ ROWS
if ( row != 0 x00 ){ // KEY IS IN COLUMN 3
column = 3;
break ; // BREAK OUT OF while (1)
}
row = 0; // KEY NOT FOUND
break ; // step out of while (1) loop to not get stuck
} // end while (1)

if ( row == 0 x10 ){
PORTB = keypad [0][ column ]; // OUTPUT TO PORTB LEDS
}
else if ( row == 0 x20 ){
PORTB = keypad [1][ column ];
}
else if ( row == 0 x40 ){
PORTB = keypad [2][ column ];
}
else if ( row == 0 x80 ){
PORTB = keypad [3][ column ];
}

do {
mSDelay (15);
PORTA = PORTA | 0 x0F ; // COLUMNS SET HIGH
row = PORTA & 0 xF0 ; // READ ROWS
} while ( row != 0 x00 ); // MAKE SURE BUTTON IS NOT STILL HELD
} // CLOSE WHILE (1)
} // CLOSE MAIN

5.7 Stepper Motor

Stepper motors have input pins or contacts that allow current from a supply source into the coil windings of the motor.
Pulsed waveforms in the correct pattern can be used to create the electromagnetic fields needed to drive the motor.

The microcontroller controls the stepper motor by sending pulsed waveforms to the motor input. Since the microcontroller
cannot provide enough current for the motor to move, it need a driver such as ULN2003 or ULN2803 IC. The control bits
from the microcontroller are connected to the motor through the driver IC. This provides the motor with enough current to
move. Example waveforms are shown in the following figure:
52 Chapter 5 Peripherals Interfacing

Example:
# include < hidef .h > /* common defines and macros */
# include " derivative . h " /* derivative - specific definitions */

void MSDelay ( unsigned int );

void main ( void ) {

DDRB = 0 x0F ; // PORTB0 - PORTB3 as output


PORTB =0 b00000000 ; // start with all off

while (1) {
PORTB =0 b00000011 ;
MSDelay (50);
PORTB =0 b00000110 ;
MSDelay (50);
PORTB =0 b00001100 ;
MSDelay (50);
PORTB =0 b00001001 ;
MSDelay (50);
}
}

You might also like