Escolar Documentos
Profissional Documentos
Cultura Documentos
Topics
I/O Addressing Techniques I/O Port Structure CPU12 I/O Ports Programming I/O Ports:
Driving LEDs/7-Segment Displays Interfacing to Switches Switch Debouncing Keypad Interfacing Techniques/Issues Liquid Crystal Displays
Resources
4.10 Intro to Parallel I/O Ports 4.11 Simple I/O Devices 7.2 I/O Related Issues 7.3 I/O Addressing Issues 7.5 The HCS12 Parallel Ports 7.6 Electrical Characteristics 7.7 Liquid Crystal Displays Interfacing Parallel Ports to a keypad
ENG3640 Fall 2012 3
Several embedded applications require interfacing an MCU with Light Emitting Diodes, Switches, Liquid Crystal Display, Seven Segment Displays. I/O ports therefore have to be programmed to handle input/output signals.
If the same address bus is used for both memory and I/O, how should the hardware be designed to differentiate between memory and I/O reads and writes?
CPU
Data Address Control
Memory
I/O Interface
2.
3.
1.
Memory Mapped I/O (MOTOROLA): Any instruction that reads or writes memory can read/write I/O Port Address specifies which module (input, output, RAM, ROM), will communicate with the processor Ex: LDAA #56 STAA $0024 (copy value to port H) Isolated I/O (INTEL):
The control bus signals that activate the I/O are separate from those that activate the memory device. These systems have a separate address space. Separate instructions are used to access I/O and Memory. Ex: IN AL, $10 (copy values of port $10 into register AL)
2. 3. 4.
Advantages/Disadvantages?
ENG3640 Fall 2012 6
Data Register: for data in transit Control Register: Hold commands from processor to port Status Register: Used to monitor I/O activity Polling
Principle functionality is serve as way station for data in transit between the computer and external world.
Interrupt Driven
1-KB SRAM
Memory Map
Port Details
Port Direction Function
Port A Port B
I/O I/O
Single-chip modes: general-purpose I/O Expanded modes: external address bus ADDR15ADDR8 Single-chip modes: general-purpose I/O Expanded modes: external address bus ADDR7ADDR0 Single-chip modes: general-purpose I/O Expanded wide modes: external data bus DATA15DATA8 Expanded narrow modes: external data bus DATA15DATA8/DATA7 DATA0 Single-chip and expanded narrow modes: general-purpose I/O External data bus DATA7DATA0 in expanded wide mode(1) External interrupt request inputs, mode select inputs, bus control signals General-purpose I/O
Chip select General-purpose I/O Memory expansion General-purpose I/O
Port C
I/O
Port D Port E
Port F
Port G
Port H
Port J
Port S Port T
I/O
I/O
I/O I/O
Port AD
Most GPIO pins on the 68HC12 MCU can be programmed for use in either direction. Two registers: the data register PORT and data direction register DDR.
The DDR determines the direction of the port pin.
If the DDR = 1 then the port is an output and if the DDR = 0 the data register output is disabled and the port pin is placed in high impedance state.
11
Port H Data Register Port H Data Direction Register Port J Data Register Port J Data Direction Register
12
Parallel ports are often used for simple I/O such as turning on LEDs, 7-segment displays or reading switches (unconditional transfer) Steps for using Ports: 1. Identify the address of an I/O port and its Data Direction Register (DDR) 2. Program the DDR by writing a value to it. 3. The value written to the DDR reflects the appropriate setting for the port (i.e a `0 will make the corresponding pin an input and a `1 will force the pin in the port to be an output). 4. Load a register with a value and store this value to the address of the I/O PORT.
ENG3640 Fall 2012 14
When the MCU is powered up or reset, all GPIO ports are configured as inputs.
The port will remain an input until the software changes the data direction register.
Can this cause a problem when a port is used as an output in normal operation?
The external device connected to the port may do strange things! For example, if an output is connected to a motor or solenoid driver, the motor or solenoid may run intermittently during this time and have serious consequences!
16
2.
A pull-up or pull-down resistor can be added to the port so the node will always be pulled to the inactive level instead of floating. The port can be preset to the inactive level before changing its direction to an output (when the port direction is changed a temporary glitch on the output will occur!)
To avoid the glitch, we can write to the port data register before the direction is changed.
bset PORTH, $00 bset DDRH, $01 ; preset PORTH bit-0 low ; PORTH bit-0 an output
17
Like any digital device, before we can connect something to an input or output, we need to know the specification for the interface parameter. The first parameter to consider are the input and output voltage levels and corresponding noise margins.
VDD and VSS are supply voltages. The output voltage parameters are VOL and VOH. The input voltage parameters are VIL and VIH. For a digital system to work correctly, the output high voltage always must be between VIH,min and VDD.
Noise Margin?
ENG3640 Fall 2012 18
Interfacing: Applications
1.
2.
Like most digital logic devices, the output of MCUs can sink more current than they can source. Consequently, devices that require significant load current like an LED should be connected active-low.
19
2.
An LED emits light when current flows through it in the positive direction i.e. when the voltage on the anode side is made higher than the voltage on the cathode side. The forward voltage across the LED is typically about 1.5 to 2 Volts.
Determine whether we can drive an active-low LED from an M68HC12 output? Assume that a high-efficiency LED with IF,min = 10mA and VF,max = 2.0 V is used
The LED in the figure is connected active low so that when the output goes low, the current IL flows through the LED and turns it on.
ENG3640 Fall 2012 21
Using a 5-volt supply and assuming that the LED has a 2.0 V drop across it, what resistor value will limit the current to 10mA? Answer: 5V = 2.0V + IRx x Rx Setting IRx to 10mA the resistor Rx is solved to be 300 Ohm.
22
An I/O port pin of a microcontroller generally does not have enough drive to supply the current. So an inverter is often used as a switch to turn the LED on and off.
VCC
Use the 68HC12 Port H to drive green, yellow, red, and blue LEDs. Light each of them for half of a second in turn and repeat. The 68HC12 uses a 16-MHz crystal oscillator to generate internal clock signals.
Solution: The upper four pins of the Port H can be used for this purpose.
68HC12
5V
5V
5V
5V
green
red 300W
blue 300W
74HC04 74HC04
PORTH equ DDRH equ org ldaa staa Forever ldaa staa jsr ldaa staa jsr ldaa staa jsr ldaa staa jsr jmp swi
$24 $25 $1000 #$FF DDRH #$80 PORTH delay_hs #$40 PORTH delay_hs #$20 PORTH delay_hs #$10 PORTH delay_hs forever
; configure PORTH for output ; ; turn on green LED and turn off other LEDs ; ; wait for half of a second ; turn on yellow LED and turn off other LEDs ; ; wait for half of a second ; turn on red LED and turn off other LEDs ; ; wait for half of a second ; turn on blue LED and turn off other LEDs ; ; wait for half of a second ; repeat
25
Consists of seven LED segments (a, b, c, d, e, f, g) Alphanumeric characters can be displayed by controlling the segments. Two types of Seven Segment Displays:
1.
2.
26
1.
Common Anode:
All anodes are tied in common. Segment will be lit whenever a low voltage is applied.
2.
1.
Common Cathode:
all cathodes are tied in common. Segment will be lit whenever a high voltage is applied. Current limiting resistors must be included or else you might damage ENG3640 Fall 2012 display.
2.
27
28
Some ICs are specially designed to drive 7segment displays. They contain buffers to supply required drive currents. When using MC144495 decoder, no current limiting resistors have to be used since they are built in the MC14495
29
If your interface circuit uses only buffers and resistors to connect an output port to a seven segment display, you will have to use software to generate the character codes. LAB #2
The program can do this by using a look-up table. The contents of the table depends on the application and type of display. For example, to display hex digits for a common anode display, entry 4 would have the byte $19. What addressing mode to use?
30
To convert the mechanical signal into an electrical signal, a resistor pull-up is used. The amount of current this output can source is determined by the resistor value. When the switch is open, Output? When the switch is closed, Output? +5V 1K I/O
switch Open Closed Output +5V 0V
Port
31
When the switch in pull-down circuit is open the output is pulled to the ground The amount of current this output can sink (IOL) is determined by the resistor value.
+5V
I/O
switch Open Closed Output 0V +5V
1K
Port
32
Port J on the MC68HC812A4 supports both internal pull-ups and pull-downs. Either of the two previous circuits could be implemented on the 6812 without the resistor
+5V PJ1 with pull-down PJ0 with pull-up
33
Each bit in the PUPSJ Register corresponds to a PORT J pin. Each bit selects a pull-up or pull-down device for the associated PORT J pin. The pull-up or pull-down is active only if enabled by the PULEJ Register
34
PULEJ bit X 0 1 1
Port J mode Regular output Regular input Input with passive pulldown Input with passive pullup
35
RECALL: PORTJ can act as a general purpose I/O PORTJ (Data Register) and DDRJ (Data Direction Register) are used to setup the port for reading/writing information PORT J is also used in the key wakeup feature of the MC6812!
36
The key wakeup feature of the MC6812 issues an interrupt that wakes up the CPU when it is in stop or wait mode. Wakeups are triggered with falling/rising signal edge An interrupt is generated when a bit in KWIFJ register and its corresponding KWIEJ bit are both set.
37
Switch Bouncing
When mechanical switches are opened or closed there are brief oscillations due to mechanical bouncing (switch bounce).
+5V +5V A 0V
Switch Debouncing
When the mechanical switch is touched (pressed) or released it bounces microscopically for a period of milliseconds. The MCU will see many occurrences of ``make and ``break instead of one occurrence. The MCU should see only one ``break and one ``make
39
1. 2. 3.
1.
Software Techniques:
Delay loops
40
NAND
I1 I2 0 1 0 1 F 1 1 1 0
0 0 1 1
S 0 1 1 1 0
ENG3640 Fall 2012
R 1 1 0 1 0
Q 1 1 0 0 X
41
A Schmitt trigger is a special circuit that uses feedback internally to shift the switching threshold depending on whether the input is changing from low to high or high to low. The difference between V T+ and V T- is called hysteresis. A 74LS14 Schmitt Trigger inverter can be used to debounce a switch. VOUT V TV T+
5.0
Example: 74LS14
2.1
2.9
5.0
VIN
42
43
Integrating Debouncer:
5V
The RC constant of the integrator determines the rate at which the capacitor charges up towards the supply voltage. The capacitor value is chosen large enough so that Vout does not exceed the zero threshold value while the switch is bouncing! Threshold Level
Vout
45
Given R = 1K, bounce time = 5ms, what is the value of C such that the output voltage seen by the MCU = 0.7V?
1.16 > = e 5ms/(1K.C) ln(1.16) >= 5ms/(1K.C) C >= 5ms/(1K.ln(1.16)) = 33micro Farad.
ENG3640 Fall 2012 46
Integrating Debouncer:
5V
The capacitor value is chosen such that the voltage does not exceed the 0.7-V threshold of the NOT gate while it is bouncing.
R Vout C
74LS14
MCU Input Port
Problem?
ENG3640 Fall 2012 47
Integrating Debouncer:
5V 74LS14 1K Vout MCU Input Port C Current can be large (causes spark), these sparks will produce carbon deposits on the switch that will build up until switch no longer works. To limit the current a small resistor is placed in series with the switch.
ENG3640 Fall 2012 48
22 ohm
Software Debouncing
To debounce a switch we can use the following simple approach: 1. A software time delay is used that provides a time delay (usually 10-20 ms) longer than the duration of the switch bouncing action. 2. So if switch goes low, wait for longer than 10ms or 20ms and then test for the switch still being low.
49
Keyboard/Keypad Interfacing
Keyboards are used to enter input into a computer. A common type of keyboard is the matrix type. It saves an amount of I/O wiring because the keys share wires. Each has its own combination of row and column.
50
Keypad Interfacing
Each key has an identifying number (key code or scan code) as well as a character or function associated with it. For example key 11 has the character (0). How does the MCU identify that key 11 was pressed? By Sending a signal to terminal G and Checking terminal E, it will find a short circuit thus it will identify the key to be 11.
51
Keypad Decoding
1. To detect a short circuit, the MCU drives one of the output lines low 2. Checks the corresponding input line, if it is low, key was pressed. 3. If the key was not pressed, the open circuit allows the resistor to pull up the input line to logic high. 4. The combination of both low logic column and row will identify the pressed key. 68HC12
PP7 PP6 PP5 PP4 PP3 PP2 PP1 PP0 10KW
0
3 2 1 0 7 6 5 4 B A 9 8 F E D C
Table 7.6 Sixteen-key keypad row selections PP7 1 1 1 0 PP6 1 1 0 1 PP5 1 0 1 1 PP4 0 1 1 1 0, 4, 8, C, Selected keys 1, 5, 9, D, 2, 6, A, E, and 3 and 7 and B and F
Keypad Decoding
Write an assembly subroutine that reads a character from the keypad. The subroutine should perform keypad scanning, debouncing, and ASCII code generation.
Steps:
1. Configure Port H such that 4 MSB are output and 4 LSB are input. 2. Select the row containing keys 0,1,2,3
6. else if key 1 pressed then 7. debounce key 1 (i.e. jump to subroutine wait 10ms)
Liquid Crystal Displays (LCDs) are widely used in microcomputer systems (Embedded Systems).
1.
They are used in watches, calculators, instrument panels, consumer electronic displays (VCRs)
1.
2.
1.
Disadvantage?
Low response.
54
4. Unlike LEDs that convert electric power to optical power, an LCD uses AC voltage to change the light.
5. An LCD could be either of reflective type or an absorption type.
ENG3640 Fall 2012 55
An LCD display requires an alternating excitation wave applied to selected electrodes to change selected areas. A constant (DC) excitation signal will polarize and destroy the crystal.
Control
XOR
FP
Front Plane
VLCD
60 Hz Oscillator
BP
Back Plane
57
The oscillator output BP is a square wave with frequency of 60Hz. When the Control signal is low, FP is in phase with BP. Therefore, VLCD will be zero (display is blank). When control signal is high, VLCD will be an AC square wave and the display reflects light (display is visible).
58
The HD44780 is an industry standard LCD controller. An I/O port can easily be used to interface with the HD44780. By controlling the value of RS and R/W the MCU can easily either sends instructions or data to the controller. Most operations require 40 micro seconds to complete. To be used in LAB #4.
59
Clear Display
00000001
Clears Display
1.64 ms
Cursor Home
0000001x
1.64 ms
Function Set
001 DL N F * *
40 micro sec
0000 1 D C B
Set on/off of all display (D), cursor on/off (C), and blink (B)
40 micro sec
0=off, 1=on 0=5x7 dots, 1=5x10 dots 0=off, 1=on 0=1 line, 1=2 lines
ENG3640 Fall 2012 60
HD44780: Initialization
LCD Initialization Routine initlcd ldaa jsr ldaa jsr ldaa jsr ldaa jsr rts #$3C lcdcmd #$0f lcdcmd #$14 lcdcmd #$01 lcdcmd ; configure display format to 2x40 ; ; turn on display and cursor ; ; shift cursor right ; ; clear display and return cursor to home ;
; Send a command in A to the LCD command register lcdcmd staa cmd_reg ; write command jsr delay40 ; wait rts
ENG3640 Fall 2012 61
62
Interfacing: Loads
The second case is to loads which will require more current than 10mA
1.
2.
We might want to connect an LED or small relay to an output of the MCU. To do this we need to add a resistor to limit the current or a driver IC to protect the MCU from dangerous current levels or transients.
The Maximum Ratings in the electrical specifications for the 68HC12 is +/- 25mA. This Maximum ratings give the value that if exceeded may destroy the part.
ENG3640 Fall 2012 63
Interfacing: No-Loads
The data sheet for a digital device lists two values for the voltage output levels. 1. The first is for small loads, typically 10mA (use for the same family, i.e HCMOS devices) 2. This represents about 10 HCMOS logic gates.
64
The interface current parameters are the output currents, IOH and IOL, and the input leakage current IIN.
1. 2. 3.
IOH is the current flowing out of a high output IOL is the current flowing out of a low output IIN is the leakage current that flows into or out of an input pin.
These currents are used to determine the static fanout of a device, that is, the number of inputs that can be connected to one output while preserving the required voltage margins.
1. 2. 3.
Static fanout for a low output is: nL = | IOL,max |/| IIn | Static fanout for a high output is: nH = | IOH,max |/| IIn | n = min [ nH, nL ]
ENG3640 Fall 2012 65
To determine the output characteristics, we need to model the port output as shown. Rp is the equivalent resistance of the internal PMOS, and Rn is the equivalent resistor of the internal NMOS. When the output is high, the PMOS is on and the NMOS is off.
Assume that the resistance for a transistor that is off is infinite. Rp(on)max = VDD-VOH,min / | IOH | Rn(on)max = VOL,max / | IOL |
66
1. 2.
The MCU can operate in eight different modes. Each mode has a different default memory map and external bus configuration. After reset, most system resources can be mapped to other addresses by writing to the appropriate control register. The states of BKGD, MODB and MODA pins during reset determine the operating mode after reset. Two basic types of operating modes Normal Mode Special Mode
67
Operating Modes
Normal Modes: Some registers and bits are protected against accidental changes. Special Modes: Protected control registers and bits are allowed greater access for special purposes such as testing and emulation.
BKGD
MODB
MODA
Mode
Port A Port B
Port C
Port D
0 0 0 0 1 1
1
0 0 1 1 0 0
1
0 1 0 1 0 1
0
G.P. I/O G.P. I/O DATA DATA G.P. I/O G.P. I/O
ADDR
DATA
DATA
68
Operating Modes
Normal Single Chip: No external buses. The MCU operates as a stand-alone device and all program and data resources are on chip. Special Single-Chip Mode: This mode can be used to force the MCU to active Background Debug Mode (BDM). Normal Expanded Wide Mode: The 16-bit external address bus uses port A for the high byte and port B for the low byte. The 16-bit external data bus uses port C for the high byte and port D for the low byte (Factory Configured!) Special Peripheral Mode: The CPU is not active in this mode. An external master can control on-chip peripherals for testing purposes.
69
Parameter
VOH,min (no load, IOH < 10m A) VOH,min (IOH = -0.8mA) VOL,max (no-load, IOL < 10m A) VOL,max (IOL = 1.6mA) R p(on), max R n(on), max
68HC912B32 Value
VDD-0.2V VDD-0.8V 0.2V 0.4V 1000W 250W
70
1.
2. 3. 4.
Step 2. Determine if 147 is large enough to limit the current to less than 25mA under all conditions and assume VDD,max = +5V (+-10%) = 5.5V
1. 2.
Conservative approach: set R n(on), min =0 and V F,min = 0 Practical approach: use 50% of the maximum values for R n(on), min and V F,min
ENG3640 Fall 2012 71
72
Practical Estimation (Cost of adding an extra IC can be prohibitive for some cost-sensitive designs!)
IF,max = (VDD,max VF,min)) / (RL,min + R n(on).min) = (5.5V-1V)/(145.5 ohm 125 ohm) = 16.64mA
This is well within the 25mA limit of the output and the maximum current limit of the LED.
ENG3640 Fall 2012 73
PORTJ: Initialization
The software initialization sets bits in PUPEJ register to enable pull-up or pull-down. For each Port J pin that is enabled for pull-up or pull-down, the corresponding bit in the PUPSJ register determines if it is pull-up (1) or pull-down (0). // MC68HC812A4 // Port J bit 1 is connected to a switch to +5, using internal pull-down // port J bit 0 is connected to a switch to 0, using internal pull-up Void Initialization(void){ DDRJ &= 0xFC; // PJ1 PJ0 inputs KPOLJ |= 0x03; // flags set on the rise of PJ1 and PJ0 KWIEJ &= 0xFC; // disarm PJ1, PJ0 KWIFJ = 0x03; // clear flags PUPSJ = (PUPSJ&0xFC)|0x01; // pull-down on PJ1, pull-up on PJ0 PULEJ |= 0x03;} // enable pull-up and pull-down
+5V
+5V
ENG3640 Fall 2012 75
Switch
Switch
not pressed
pressed
pressed
Wait 10 ms
not pressed
Wait 10 ms
RTS
RTS
77
Software Debouncing
1.
2.
1.
Approach #2:
Initialize a counter with a value of 10 and after the first logic low level is detected, poll the switch every millisecond. If the switch output is low, decrement the counter. If the switch output is high increment the counter. When the counter reaches zero, we know the switch output has been low for at least 10 ms. But if the counter reaches 20, we know that the switch has been open for at least 10 ms.
2.
3.
78
Keyboard Decoding
Some keyboards have an extra common terminal. So if a key is pressed, a short circuit occurs between the common and the keys row and column line. When PC0 is driven low and one key is pressed, one of the row inputs and one of the column inputs will be low. The keyboard software driver routine checks which inputs are low and determine the key code.
79
Keypad Decoding
1. To detect a short circuit, the MCU drives one of the output lines low 2. Checks the corresponding input line. 3. If it is low, the key was pressed. 4. If the key was not pressed, the open circuit allows the resistor to pull up the input line to logic high. 5. The combination of both low logic column and row will identify the pressed key. 6. For example, to check key code 5 the MCU drives PC1 (terminal J) low and checks the input at PC5 (terminal E) .
ENG3640 Fall 2012 80
Keypad Interfacing
Understand Keypad Interface *(Note: 68HC12812A4 has
internal pull-up on all pins; not all are shown here)
To identify the key code, the MCU scans each contact in sequence.
81
82