Date post: | 14-Apr-2015 |
Category: |
Documents |
Upload: | brett-wildermoth |
View: | 36 times |
Download: | 2 times |
Introduction to Input and Output(GPIO)
Topic Video 044
1Saturday, 29 August 2009
Input / Output (I/O) • Input refers to any information provided to the
microprocessor from the outside world.
• Output is any information the microprocessor provides to the outside world.
• Without some form of I/O the processor is useless (blind, deaf, mute, and paralysed).
• The microprocessor must be able to receive information from the outside world (input).
• The microprocessor must also return the results of any calculations back to the outside world.
µPINPUT OUTPUT
2Saturday, 29 August 2009
I/O• Microprocessors consist of many
I/O subsystems.• With two possible ways of
accessing the them:– Ported I/O
Access to the device is provided by a small port in the memory (generally through a GPIO port.
– Memory Mapped I/OThe entire device including all
its internal registers are mapped into consecutive memory locations in the memory map.
3Saturday, 29 August 2009
I/OMemory Mapped I/O
PORTAPORTBDDRADDRB
....
$00
...
$7FF
4Saturday, 29 August 2009
Input / Output (I/O) • There are two basic types of I/O:
• Serial– Serial devices generally use only one data pin on MCU. The data
is transmitted via a shift register out of this pin one bit at a time.
– The data can be transmitted from the MSB to the LSB or the LSB to the MSB depending on the transmission characteristics.
– The rate a which the transmit and receive shift register operate depends on some prearranged clock frequency.
– The serial interface can either embedded the clocking information into the data or use a separate pin to pass the clocking signal.
5Saturday, 29 August 2009
I/OSerial
• RS232– RS232 is commonly referred to as asynchronous serial. This is the most
common serial interface that is provided on many microprocessors and computers. Provides bit rates up to 115kbps. Clocking information is embedded into the data (3 pin interface: RX, TX, and GND).
• SPI– SPI is a serial interface commonly found on microprocessors and
microcontrollers. Provides bit rates up to 4Mbps and has a separate clock line (4 pin interface MISO, MOSI, SCLK, and GND.
• I2C– I2C (I squared C) or Inter-Integrated Circuit is an interface found on
some microprocessors. Provides speeds up to 100kbps and has a separate clock line ( 3 pin interface SDA, SCL and GND).
• CAN– CAN or Controller Area Network is a differential serial system used in
control applications. Can operate up to 1Mbps. Is robust against noise and has a 5 pin interface (RXCAN, TXCAN, and GND).
6Saturday, 29 August 2009
Input / Output (I/O) • Parallel
– Parallel interfaces generally write whole bytes or words to the outside world at a time.
– These interfaces do not require complex hardware to transmit or receive the information and they do not require a clocking signal to reconstruct the information at the other end.
– These interfaces are simple and very quick to transmit information.
– However, parallel interfaces come at a large cost. They consume pins on the processor very quickly.
– An 8bit parallel interface would require 8 pins on the processor, whereas a serial interface requires at most only 4 pins.
– An example of a parallel interface is GPIO.
7Saturday, 29 August 2009
I/O
• Communication with an I/O device can be achieved using three different methods:
• Programmed I/OTransfers take place under software control, the processor accepts and
delivers data when the software dictates. Eg.
LDAA PTH ;Software tells processor to read from Port H.
STAA PORTA ;Software tells processor to write to Port A.
8Saturday, 29 August 2009
I/O
• Interrupt Driven I/OExternal events control when data is read or written from the I/O
device, the program flow is halted to deal with the event.It is in fact the external I/O device that causes the processor to
halt what it is doing and force it to service its request.The I/O device contains a control line called an interrupt line
(INT/IRQ), when this line goes from high to low it signals an interrupt. The interrupt line of the device is connected to the microprocessor's interrupt pin. So when the I/O device's line goes low, it cause the processor to stop what it is doing and service the request of the device.
9Saturday, 29 August 2009
I/O
• Interrupt Driven I/O Example
We have a RS232 serial subsystem that reads in serial information, it reconstructs the byte and makes it available in a data receive register. Because the serial data is constantly being piped into the serial system the processor will need to read the value in the data register before the next byte overwrites it.
The RS232 subsystem can be set up so that is triggers an interrupt when there is new data to be read. Therefore the processor is forced to read the byte before it is overwritten by the next byte in the sequence, preventing the data from being lost.
10Saturday, 29 August 2009
I/O
• DMA– DMA allows devices to transfer data without subjecting the CPU to a heavy
overhead.
– DMA consists of multiple channels. Each DMA channel has associated with it a 16-bit address register and a 16-bit count register.
– To initiate a data transfer the device driver sets up the DMA channel's address and count registers together with the direction of the data transfer, read or write.
– It then instructs the DMA hardware to begin the transfer. When the transfer is complete, the device interrupts the CPU.
11Saturday, 29 August 2009
I/ONormal Single Chip Mode
• The MC9S12XDP512 is capable of various modes of operations, the most common is single chip mode.
• In Normal Single Chip Mode the microcontroller is totally self contained, except for the need of an external clock source.
• In most situations single chip is more than adequate.
• All the resources required such as input/output devices, SRAM, EEPROM and Flash memories reside on a single IC and only a limited amount of specialized I/O circuitry is required for certain I/O devices.
12Saturday, 29 August 2009
I/OProgrammers I/O Model
• The MC9S12XDP512 contains numerous I/O possibilities, including parallel, serial and analog interfaces.
• All of the internal I/O subsystems are mapped to the lowest 2 kilobytes of the memory, from $0000 to $07FF.
13Saturday, 29 August 2009
GPIO
• The MC9S12XDP12 contains 12 8bit GPIO ports.
• The pins of each of these GPIO ports are shared with other I/O subsystems.
• If the device that shares the GPIO port is activated then the GPIO port is no longer available for use.
• On power up the GPIO ports are enabled by default (all other I/O subsystems are disabled).
Port Name Physical Pins
PORTA H2:pins 1- 8
PORTB H2:pins 9 -15
PORTJ H2:pins 40 - 47
PORTM H2:pins 40,21,39,38
PORTS H1:pins 4,3,2,1,32,34,5,48
PORTT H1:pins 6 - 13
PORTP H1:pins 14 - 21
PORTH H1:pins 35 - 42
PORTE H1:pins 33,32,34,46
PORTAD1 H1:pins 22-29
PORTAD0 H2:pins 22-29
14Saturday, 29 August 2009
• The subsystem responsible for maintaining the GPIO ports is called the Port Integration Module.
• The port integration module is responsible for ensuring the GPIO and subsystems that share the pins of the processor do not conflict.
• If a subsystem is activated then the GPIO that use the same pins as that subsystem is turned off.
GPIO
15Saturday, 29 August 2009
INPUT
OUTPUT
GPIO• The GPIO allows 8bit data to be driven
on to the physical pins of the microprocessor and read from the pins via a data register.
• If the GPIO port is set up purely as output, then any value written to the GPIO's data register will appear on the 8 pins associated with that port.
• The value will appear on the corresponding pin as either 5 volts for logical '1' or 0 volts for logical '0'.
• So a logical '1' in the MSB of a GPIO data register will appear on the pin PX7 as 5 volts (replace 'X' for the port letter).
• If the GPIO is setup purely as input then any voltage driven to the external pins of the port will appear in the data registers.
• A '1' will appear in the data register where the corresponding pin had a voltage of 5volts applied.
1 1 1 0 0 1 0 1
5V 5V 5V 0V 0V 5V 0V 5V
0V 5V 0V 0V 0V 5V 5V 5V
0 1 0 0 0 1 1 1
16Saturday, 29 August 2009
GPIO• The GPIO ports can act as 8bit
input, 8 bit output or any combination in between.
• Top nibble input, bottom nibble output?
• The direction of each pin in the GPIO port can be defined to be either input or output.
• The Data Direction Register (DDR) allows the direction of each pin to be defined.
• So therefore each GPIO port has two registers associated with it:
– A data register,
– And a data direction register.
HS12
17Saturday, 29 August 2009
GPIO
1
1DDRX7
PTX7
1
0DDRX7
PTX7
5V 5V
18Saturday, 29 August 2009
GPIO
• The registers of the GPIOs are memory mapped.
• There data registers and data direction registers can be set in the same way as writing a value to a memory location or a variable.
• You can access the registers either via their memory location or their name.
Port NameData Direction
Register Location
Data Register Location
PORTA $002 (DDRA) $0000 (PORTA)
PORTB $003 (DDRB) $0001 (PORTB)
PORTK $0033 (DDRK) $0032 (PORTK)
PORTJ $0268 (DDRJ) $026A (PTJ)
PORTM $0252 (DDRM) $0250 (PTM)
PORTS $024A (DDRS) $0248 (PTS)
PORTT $0242 (DDRT) $0240 (PTT)
PORTP $025A (DDRP) $0258 (PTP)
PORTH $0262 (DDRH) $0260 (PTH)
PORTE $0009 (DDRE) $0008 (PORTE)
PORTATD1 See A/D TopicSee A/D Topic
PORTATD0 See A/D TopicSee A/D Topic
19Saturday, 29 August 2009
GPIOExamples
; Setup Port H as an 8bit output port.LDAA #$FFSTAA DDRH ; Put the value $FF in DDRH.
; Write $AB to the pins of Port H.LDAA #$ABSTAA PTH
; Setup Port K as an 8bit input port.CLR DDRK
; Read a value from PortK.LDAA PORTK
20Saturday, 29 August 2009
GPIOExamples
; Setup Port H[7:4] as a 4bit output port and; H[3:0] as a 4 bit input port.
MOVB #$F0,DDRH ; Put the value $F0 in DDRH.
; Write $AB to the pins of Port H.MOVB #$AB,PTH
; Read a value from Port HLDAA PTH
Since only the 4 most significant bits of port H are
output, the lower 4 bits remain unchanged. So $A0 is
only written to PTH.
The entire 8 bits of port H are loaded into Accumulator A. The 4 lsb of the input as
well as the last value written to the 4 msb.
21Saturday, 29 August 2009
GPIOInput and Output Pin Electronics
• Generally it is a good idea to set any unused input pins to either GND (0V) or VCC(5V).
• This will prevent the pins from causing unpredictable errors.• It is also important to ensure that any unused output pins are left
unconnected.• If an unused output pin is connected to GND or VCC and an opposite
logical value is written to the pin (pin tied to VCC and '0' written to the pin) then a phenomenon called latch up occurs.
• This will cause the processor to draw more current, which in turn will cause processor to get hot, which cause it to draw more current. This continues until the processor burns out.
22Saturday, 29 August 2009
GPIOInput and Output Pin Electronics
• To ensure that latch up doesn't occur each GPIO pin has a pull-up resistor associated with it.
• This resistor limits the current on the pin and ensures the processor doesn't burn out.
• The the pull-up resistor ensures the safety of the processor. However, the pull-up resistor slows down the responsiveness of the GPIO pins and increases the power consumption of the processor.
• Therefore it is possible to turn on or off each of the pull-up resistors associated with a GPIO port using the Pull Device Enable Register (PER).
• By default the resistors are “on” and care must be taken if they are turned off.
23Saturday, 29 August 2009
GPIOInput and Output Pin Electronics
• When driving devices connected to the GPIO, some devices require lots of current and yet some require only a little.
• Providing lots of current to a device ensure fast switch times for the GPIO pins, however consumes lots of power.
• By default the GPIO pins are set for high drive (current) operation.• But, if a device doesn't need lots of current or it doesn't need fast
switching then reducing the drive would reduce the power consumption.
• So if the processor was being used in a mobile application then each bit of power is important.
• So it is possible to turn off the high drive of a GPIO pin simply by putting that pin in reduced drive mode.
• Each GPIO port has a reduced drive control register associated with it.
24Saturday, 29 August 2009
GPIO
• The MC9S12XDP512 has 12 8bit GPIO ports.• Each port has– A Data Register (PORTX / PTX),– A Data Direction Register (DDRX),– A Pull Device Enable Register (PERX) ,– And A Reduced Drive Control Register
(RDRX).
25Saturday, 29 August 2009
I/OI/O Software
• In I/O software there are three major components of the software:– Initialisation,– Data input or Output, and– Software Synchronisation.
• The software must be synchronised with the reading and writing timing requirements of the connected device.
• Generally microprocessors are a lot faster than attached I/O devices, so the processor must be synchronised via software and hardware techniques.
• Possible techniques include:– Real-Time Synchronisation,– Polled I/O, and– Hardware Handshaking I/O.
26Saturday, 29 August 2009
AsideTiming Diagrams
• Understanding the timing diagram is very important for successfully communication with an external I/O devices.
• The timing diagram illustrates visually the relationships between the I/O signals of an I/O device.
• The various signals of the I/O device are represented by square wave of varying periods.
• The relationship of each signal with respect to time can easily be seen in the timing diagram.
27Saturday, 29 August 2009
AsideTiming Diagrams: Simple Example
A timing diagram showing the changes in logic levels over time.
28Saturday, 29 August 2009
AsideTiming Diagrams: Not so Simple Example
A timing diagram showing the changes in logic levels over time, as well as timing dependencies
29Saturday, 29 August 2009
I/OReal-Time Synchronisation
• Real-time synchronisation uses software delays to match the timing requirements of software and hardware.
• Using real-time synchronisation has it problems:– The delays are generated by instructions and therefore can not
be exact, due to errors caused by instruction execution overheads.
– The execution of instructions is an integer multiple of the instruction cycle period, so delays smaller than the instruction cycle period can not be implemented.
30Saturday, 29 August 2009
I/OReal-Time Synchronisation
• Common Delays– Small Delays
Delays of only a few instruction cycles (IC) can be done using sequential “NOP” instructions. Each “NOP” delays by one instruction cycle.
31Saturday, 29 August 2009
I/OReal-Time Synchronisation
– Delays of less than 1021 instruction cyclesDelays of less than 1021 instruction cycles can be done using a
looping structure. However the delays are multiples of 4 instruction cycles.
LDAA #X (1 IC delay)LOOP: DECA (1 IC delay)
BNE LOOP (1(F) or 3(T) IC delay)
Total Delay = ( 4 * X ) -1 Instruction Cycles.
32Saturday, 29 August 2009
I/OReal-Time Synchronisation
– Delays of less than 262,141 instruction cyclesDelays of less than 262,141 instruction cycles can be done using a
similar looping structure as above. The delays are still multiples of 4 instruction cycles.
LDX #X (2 IC delay)LOOP: DEX (1 IC delay)
BNE LOOP (1(F) or 3(T) IC delay)
Total Delay = ( 4 * X ) Instruction Cycles.
33Saturday, 29 August 2009
I/OReal-Time Synchronisation
– Delays greater than 262,140 instruction cyclesDelays of greater than 262,140 instruction cycles can be done using
a nested loop structure. The delays are approximately multiples of 4 instruction cycles.
LDY #Y (2 IC delay) OLOOP: LDX #X (2 IC delay)ILOOP: DEX (1 IC delay)
BNE ILOOP (1(F) or 3(T) IC delay) DEY (1 IC delay)
BNE OLOOP (1(F) or 3(T) IC delay)
Total Delay = ( 4 * Y ) + ( Y * ( 4 * X ) ) Instruction Cycles.
34Saturday, 29 August 2009
I/OReal-Time Synchronisation
Example
Assuming an instruction cycle of 125ns, generate the following waveform on the pin PH7.
PH7
750ns 2.5us 120ms
35Saturday, 29 August 2009
I/OReal-Time Synchronisation
Example (continued)In order to produce the waveform on PH7.We will need to write a program that does the following:
– Writes '0' to PH7– Waits 6 Instruction cycles– Writes '1' to PH7– Waits 20 Instruction cycles– Writes '0' to PH7– Waits 960,000 Instruction cycles– Writes '1' to PH7
36Saturday, 29 August 2009
I/OReal-Time Synchronisation
Example (continued)BCLR PTH,%10000000NOPNOPNOPNOPNOPNOPBSET PTH,%10000000LDAA #5 ;( X*4 )-1 = 20 , ∴X=5.25 rounded down.
LOOP: DECABNE LOOPBCLR PTH,%10000000
37Saturday, 29 August 2009
I/OReal-Time Synchronisation
Example (continued)LDY #10
OLOOP: LDX #23,999ILOOP: DEX
BNE ILOOPDEYBNE OLOOPBSET PTH,%10000000
Total Delay = ( 4 * Y ) + ( Y * ( 4 * X ) ) Instruction Cyclespick a value for Y, say 10 then solve for X.
960,000 =4 * 10 + (10 * 4 * X)= 40 + 40X
∴ X = (960,000 – 40 )/ 40 = 23,999
38Saturday, 29 August 2009
I/OPolled I/O
• Because I/O devices operate a lot slower than the microprocessor. There needs to be mechanisms in place to cause the processor to wait, until the I/O device is ready before the microprocessor requests or sends information to the I/O device.
• An I/O device will generally a have a status register mapped into the memory of the microprocessor.
• The status register will contain bit sized flags to tell the microprocessor when the I/O device is ready to receive or send information.
• Therefore in order for the microprocessor to know when the device is ready, it needs to constantly check the status bit before communicating with the device.
• The act of continually checking a status bit is commonly referred to as “polling”.
µP
I/O Device
DataReady Status flag
39Saturday, 29 August 2009
I/OPolled I/O
Assume our microprocessor is connect to an I/O device via two GPIO ports.
The data to the device is provided via Port A and the status of the devices is read via PB7.
mainLoop:
MOVB #$FF,DDRA
MOVB #$7F,DDRB
LDAA Data
Wait: BRCLR PORTB,%10000000,Wait
STAA PORTA
µP
I/O Device
Data
Ready flag ('1' = RDY)
PB7 PA[7:0]
40Saturday, 29 August 2009
I/OHardware Handshaking
• In situations were the processor is talking to an extremely slow device or it is extremely important that information is transferred correctly, then hardware handshaking may be used.
• Hardware handshaking consists of two devices talking to each other.– The first system requests the information from
the second system,
– The second system then notifies the first system when the information requested is ready to be read
– The first system then reads the information and acknowledges that it has read the information.
µP
I/O DeviceData
Read Ready flag
Acknowledge Flag
Request Data Flag
41Saturday, 29 August 2009
Parallel I/OExample
mainLoop: MOVB #$00, DDRA MOVB #$%11111101, DDRB
BSET PORTB, %00000001NOPNOPBCLR PORTB, %00000001
LOOP: BRCLR PORTB,#%00000010,LOOPMOVB PORTA, DataBSET PORTB, %00000100NOPNOPBCLR PORTB, %00000100
Spin: BRA Spin µP
I/O DeviceData
Read Ready flag
('1' = RDY)
PB0 PA[7:0]PB2PB1
Acknowledge Flag
('1' = ACK)
Request Data Flag
('1' = RQD)
Set Request Flag
Wait for Data Read Data Set Acknowledge
Flag
42Saturday, 29 August 2009
Need Further Assistance?
• Ask your Demonstrator,
• Post a question on the Forum,
• Email the Convener, or
• Make an appointment.
43Saturday, 29 August 2009