Professional Documents
Culture Documents
CH 5 Interfacing
CH 5 Interfacing
Peripherals Interfacing
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
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:
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
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 */
5.2 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
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
Real switch
5 to 20 ms 5 to 20 ms
Real switch
3 - 39
2- Software solutions
Software solutions: using a delay to skip the bouncing signal
5 to 20 ms 5 to 20 ms
EE 308 Spri
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. • 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:
PORTB = 0 x3F ;
PTP = 0 x0D ;
MSDelay (1);
PORTB = 0 x06 ;
PTP = 0 x0B ;
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
PA4, Row_0
PA5, Row_1
PA6, Row_2
PA7, Row_3
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 ( 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
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 */
while (1) {
PORTB =0 b00000011 ;
MSDelay (50);
PORTB =0 b00000110 ;
MSDelay (50);
PORTB =0 b00001100 ;
MSDelay (50);
PORTB =0 b00001001 ;
MSDelay (50);
}
}