370 likes | 384 Views
ELE22MIC Lecture 16. Introduction to Parallel Input Output (I/O) Ported vs Memory Mapped I/O IO Methods: Polled Interrupt Driven Direct Memory Access (DMA) Binary Codes Contact Bounce Gray Codes 68HC11 Gray Code software example. I/O.
E N D
ELE22MIC Lecture 16 • Introduction to Parallel Input Output (I/O) • Ported vs Memory Mapped I/O • IO Methods: • Polled • Interrupt Driven • Direct Memory Access (DMA) • Binary Codes • Contact Bounce • Gray Codes • 68HC11 Gray Code software example
I/O • I/O devices are peripheral devices used by the processor to sommunicate to the outside world, for example: • terminals, • keyboards, • displays, • mice, • network interface • sound • disk drives • printers • Analog to Digital Converters
I/O Types • I/O can be separated to two basic types: • Parallel • Parallel Printer, Absolute position encoder, ... • Serial • SPI, RS232, RS485, Ethernet, USB, ...
Ported I/O vs Memory Mapped I/O • Two basic ways of accessing I/O devices • Ported - As used in IBM PCs • Separate memory & I/O address spaces • I/O occurs on separate I/O address space • Separate In & Out instructions to access I/O • Different I/O & Memory timing constraints • Memory Mapped - As used in most Motorola CPUs including the 68HC11 • I/O occurs using memory address space • Same instructions & timing for I/O as for memory • For slow I/O peripherals timing can be slowed down • Data Transfer ACKnowledge (DTACK# ) held high to increase memory cycle time.
I/O Ports • An I/O port is an 8-bit, or 16 bit, register where data en-route from the input, or to the output, resides. • The 68HC11 microprocessor treats these ports as memory locations that may be read-from, or written-to, using memory load & store instructions (eg LDAA STAA) • The 80x86 family of microprocessors treats these ports as I/O addresses and uses special I/O instructions (eg IN OUT)
I/O Devices • Input devices must hold data for as long as is required by the processor to read the data. • Output devices must hold data for as long as is required by the peripheral to output the data. • The I/O devices must transfer data between the processor and the port under processor control. • For memory mapped I/O they must operate as a functional equivalent of memory devices.
Simple Parallel I/O (1) • Simplest form of Output is latching data from the data bus • Simplest form of Input is reading the state of an external device onto the data bus. • Example Devices: • Refer HCCOM Schematic • eg U11 = 74LS245 buffer to input header J12 jumper settings • 74HC574 Latch for output
Simple Parallel I/O (3) • There is no way to asynchronously inform the processor when new data is arrived • There is no way of telling when the external devices has read the current output data and is ready for the next data • A output port is always an output port • A input port is always an input port • Neither versatile nor re-configurable • Not programmer-friendly if data needs frequent reading/writing
Methods of I/O device access • Three main methods of performing I/O: • Polled IO • Interrupt-Driven I/O • Direct Memory Access (DMA)
Polled I/O • Transfers take place under main program control • Processor accepts/delivers data when it choses
Interrupt-Driven I/O • External interrupts trigger the beginning of I/O operation • The Interrupt Service Routine (ISR) handles the I/O transfer • Main program is suspended during ISR • Sequence: Interrupt is signalled. • CPU stops current program & saves state • CPU executes ISR & returns • CPU restores previous state and continues
Direct Memory Access (DMA) • Processor address & data bus is taken over by a DMA controller which performs memory <-> I/O transfer without CPU control. • Separate DMA hardware performs Memory <-> I/O transfer • Main program may/may not be halted during transfer depending on type of transfer and instruction in execution
Parallel Inputs • Using parallel inputs on the 68HC11 we can read physical quantities that are readily converted to digital logic signal levels. • Position, angle, air pressure, water pressure, water flow, door open/closed, can be electro-mechanically converted to digital values. • We can use banks of switches and a suitable mechanical arrangement to switch the contacts.
Contact Bounce & Noise • Contact Bounce • Switch contacts in switches and relays Bounce • Bounce is the time between valid break and make where electrical noise is present on the signal. • simple spring & contact momentum causes Bounce • Manufacturer normally specifies maximum contact bounce time, eg: 5ms at 15rpm (rotary encoder) • Other sources of contact related noise • Contact wiping action and corrosion/degraded contacts • Also Ringing of the signal is possible in inductive switching arrangements increasing the uncertainty of when a contact closed.
Contact De-bounce • De-bouncing contacts: • Electronics can be added to remove the electrical bounce noise - eg RC filter - this adds a time constant delay, however, limits speed of switch position recognition which could become a limiting factor • Software can de-bounce by requiring several successive reads of the same value before accepting a change of the switch state • simplifies hardware design and decreases cost
Binary Code: • Binary Codes • Useful for encoding of data eg: position, angle etc • However, as mechanical switching can cause contact bounce and incorrect readings with highly undesirable consequences. • Consider the transition from 3 -> 4. • 011 -> 100 • What if the interim value 111 = 7 is read? • If used in a feedback loop => this represents a huge error value • Radio Telescope…
Binary Coded Position Wheel This Binary Code repeats after 360 degrees - Angular Position is reported in 45 degree bands Consider the error if bit 2’s contact is out of alignment by a few degrees. The transition from 011 to 100 could go through te state 111 or 000 - a large error!
Binary Coded Position Wheel The more bit transitions occuring consecutively, the worse the possible error. There are 2 occurances of 2 or 3 bits changing at once.
Gray Code: • Gray Codes • Useful where contact bounce is potentially a source of error - Only 1 bit changes per step Decimal Number Binary Gray Code 0 000 000 1 001 001 2 010 011 3 011 010 4 100 110 5 101 111 6 110 101 7 111 100
3 Bit Gray Code Angular Encoder This Gray Code repeats after 360 degrees - Angular Position is reported in 45 degree bands
2 Bit Gray Code Angular Encoder Code repeats after 180 degrees - Good if absolute angle is not required
2 Bit Gray Code Angular Encoder Code repeats after 60 degrees - Good if finer angular resolution is required
2 Bit Gray Code Shaft Encoder Code repeats after 60 degrees Some mechanical encoders are relatively inexpensive. Useful where analog control of digital signals are needed: Eg: Modern AV systems volume controls - Single digital volume control with multiple control sources InfraRed remote control & Front Panel knob. Source: www.digikey.com
Panel Mount Optical Shaft Encoder Optical encoders are relatively expensive, but have excellent wear properties, and no contact bounce. Available from 96-512 code Cycles per revolution. Useful where fine angular measurement with direct digital interface is needed: Eg: Computer Numerically Controlled (CNC) machines Manufacturer: Agilent (HP) http://literature.agilent.com/litweb/pdf/5988-5856EN.pdf
Gray Code using Input Port A Get Input from port A bits 0 & 1 If Value has changed from previous value, re-read it to ensure it is not noise. If several valid reads then process the value: Map Gray to Binary Value Work out difference to previous value +1 or -1. Process the +/- 1 difference - add it to the value to be changed. make the current value the previous value
Gray Code Conversion Software (1) RAM EQU $2000 org RAM jmp start ; 68HC11 Equates REGBAS EQU $1000 Starting address for register block PORTA EQU $00 ; Port A INPUT:PA0..2, OUTPUT:PA3..6, & I/O PA7 ; Buffalo Equates: OUTLHF EQU $FFB2 ; Print left half OUTRHF EQU $FFB5 ; Print right half CRLF EQU $FFC4 ; Print CRLF
Gray Code Conversion Software (2) PrevGrayCode RMB 1 ; The Previous Gray Code CurrBinCode RMB 1 ; Gray Code converted to binary PrevBinCode RMB 1 ; Previous Binary Code COUNTER RMB 1 ; Number to increment/decrement start: LDAA #0 STAA COUNTER LDX #PORTA+REGBAS LDAB 0, X ANDB #%00000011 ; Mask all but bottom 2 bits STAB PrevGrayCode ; Previous gray value - initialisation
Gray Code Conversion Software (3) Initialisations: BSR Gray2Bin ; Convert Gray -> Binary code STAB CurrBinCode ; Binary equivalent of gray value STAB PrevBinCode ; Previous Value of binary value LoopStart: LDX #PORTA+REGBAS LDAB 0, X ANDB #%00000011 ; Mask all but bottom 2 bits CMPB PrevGrayCode ; Has the reading changed? BEQ LoopStart ; No, so read again STAB PrevGrayCode ; else Gray Code has changed
Gray Code Conversion Software (4) ; Convert Gray code to Binary BSR Gray2Bin * Convert Gray 2 Binary - uses Acc. B STAB CurrBinCode SUBB PrevBinCode * subtract previous * Valid differences are -1 or +3 = down 1, or -3 = up 1 CMPB #1 BEQ Up1 CMPB #-1 BEQ Dn1 CMPB #-3 BEQ Up1 CMPB #3 BEQ Dn1 * JSR BigStepError * If we get here we have stepped by +/-2 BRA Continue
Gray Code Conversion Software (5) Up1: ; Subroutine Up1 Knob has been turned clockwise ; Increment - but limit up to 255 - No wrapping back to 0 LDAA COUNTER CMPA #$FF ; are we already at the maximum? BEQ ALREADY_MAX ; Branch if already maximum. INCA ; else increment STAA COUNTER ; and save result ALREADY_MAX: BRA Continue
Gray Code Conversion Software (6) ; Subroutine Dn1 - Knob has been turned anti-clockwise Dn1: ; Decrement - limit downto 0 - no wrapping to -1 LDAA COUNTER CMPA #0 ; Are we already at the minimum? BEQ ALREADY_ZERO ; Branch if zero DECA ; Else Decrement STAA COUNTER ; and save result ALREADY_ZERO:
Gray Code Conversion Software (7) Continue: * Save new binary value in PrevCode LDAB CurrBinCode ; Get new binary code STAB PrevBinCode ; make it previous code also * Output the hex value to the terminal LDAA COUNTER JSR OutHexByte JSR CRLF ; output carriage return - line feed BRA LoopStart
Gray Code Conversion Software (8) ************************************************* * Convert Gray code in Accumulator B to Binary ************************************************* Gray2Bin: CMPB #2 ; Is Bit 1 set? BHS XorBit1 ; Branch if 2 or 3 to XorBit1 RTS ; Do nothing XorBit1: EORB #1 ; This inverts Bit 0 RTS ; We return with Acc.B = Binary code
Gray Code Conversion Software (9) OutHexByte: * Outputs to console - Hexadecimal value of byte passed in Acc A PSHB ; Save Acc. B PSHA ; Save Acc. A twice. PSHA ; JSR OUTLHF ; OUTput Left HalF - high nybble PULA ; Recover Acc.A - byte to display JSR OUTRHF ; OUTput Right HalF - low nybble PULA ; Restore Acc.A PULB ; Restore Acc.B RTS ; ReTurn from Subroutine
Acknowledgments • I drew the Angular Shaft Encoder drawings in AutoSketch V6. • Motorola M68HC11 Reference Manual • Agilent (HP) Web site • DigiKey online web site - Bournes encoder • Images of the configuration registers, and some source code examples, are derived from the Motorola M68HC11 Reference Manual. Paul Main 2004.