You are on page 1of 11

Objectives

CMPE328 Microprocessors
(Spring 2007-08) 8255 I/O Applications D/A and A/D Converters
You will be able to: Describe the purpose of 8255 PPI Writing code for I/O through the 8255. Design and write code for 7-segment display circuits. Design circuits and write code for interfacing switches and multiplexed displays Writing code for using LCD displays. Code I/O programming for C/C++ Use ADC and DAC circuits.

By Dr. Mehmet Bodur

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

Section 11.4

Section 11.4

Pin Layout and Address Decoding of 8255 PPI


D[0..7] A0, A1 ~CS ~RD ~WR PA[0..7] PB[0..7] PC[0..3] PC[4..7] RESET
CMPE328 Spring 2007-08

PPI Register Addressing


CS 0 0 0 0 1 A1 0 0 1 1 x A0 Selects 0 Port A 1 Port B 0 Port C 1 Control Reg. x Chip Disabled Example: For the following circuit find addresses of PA, PB, PC and ComR.

Data Bus Lines Register Select Chip Select Read from Register Write to Register Port-A i/o Pins Port-B i/o Pins Port-C lower four Pins Port-C higher four Pins. PPI reset line.
Dr.Mehmet Bodur, EMU-CMPE 3

286 system bus and 16-bit ISA bus has ~AEN line. It is active only if processor Address Map asserts a valid PORTA is at 310h, System Bus address to the PORTB is at 311h PORTA address bus. PORTB

Chip Select Circuit


A9 A8 A7 1 1 0 1 1 0 1 1 0 1 1 0 A6 0 0 0 0 A5 0 0 0 0 A4 1 1 1 1 A3 0 0 0 0

Port Select
A2 A1 A0 0 0 0 0 0 1 0 1 0 0 1 1
4

PORTC is at 312h ComR is at 313h

AEN 0 0 PORTC 0 CommandReg 0

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

Section 11.4

Section 11.4

PPI Register Addressing


Example: 80286 uses D[0..7] only for even addresses. Design a PPI circuit (including addres decoding) that has PortA at 310h, PortB at 312h, PortC at 314h, and ComR at 316h.
Address Map
Chip Select Circuit
A6 0 0 0 0 A5 0 0 0 0 A4 1 1 1 1 A3 0 0 0 0

Mode-0 of 8255A Simple IO Settings


Command Register Control Format
D7: 1 I/O mode / 0 BSR mode D6D5: 00 Mode0 / 01 Mode1 / 1x Mode2 D4: PortA, 0 Output / 1 Input. D3: PortCH, 0 Output / 1 Input. D2: Mode Selection, 0 Mode0 / 1 Mode1. D1: PortB, 0 Output / 1 Input. D0: PortCL, 0 Output / 1 Input

Port Select
A2 A1 A0 0 0 0 0 1 0 1 0 0 1 1 0

System Bus AEN A9 A8 A7 PORTA 0 1 1 0 PORTB 0 1 1 0 PORTC 0 1 1 0 CommandReg 0 1 1 0

8255A D[0..7] ~IOW ~IOR A1 A2 PA[0..7] PB[0..7] PC[0..3] PC[4..7]

Example:

Output

A0 A3 A4 A5 A6 A7 A8 A9 ~AEN
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE

A system has relays connected to PA, a keypad with 6 pushbutton switches at PB, two LED at PCL. PCH is empty, Output and shall be output. Input Output What is the command register settings?

ComR = 10000010b
A Ch
5 CMPE328 Spring 2007-08

B CL
6

Dr.Mehmet Bodur, EMU-CMPE

Section 11.4

7-Segment Interfacing
Two kinds of 7-seg LED display devices
Common Cathode
a b dp fg b e common cathode Common Cathode devices work active-high. High applied on a lights the a-segment-LED. Example, (a,b,dp) connected to PortA PA[0..7], i.e., a connected to PA7. PA= 0110 0000b
CMPE328 Spring 2007-08

Display Code
A 7-segment needs display codes to write a figure on device.
Display code depends on how port connections d0, d1, , d7 to segments a, b .., dp is assigned.
e a fg b d c dp

Common Anode
common anode a

Example-1. d0 a, d1 b, d6 g, d7 dp.
...

...

c dp

~a

~b

~dp

abcd efgx .

Common Anode devices work active-low, Low applied on ~a lights the a-segment. Example, (a,b,dp) connected to PortA PA[0..7], i.e., ~a connected to PA7. abcd efgx . PA=10011111b
7

Example-2. d0

writing 0 needs a,b,c,d,e,f active code for 0 = 00111111b. xgfedcba writing 1 needs b,c active; code for 1= 00000110b = 03h writing 2 needs a,b,d,e,g; code for 2= 01011011b = 5Bh writing 3 needs a,b,c,d,g; code for 3= 01001111b = 4Fh . abcdefgx abcdefgx code for 0 = 11111100b = FCh; code for 1= 01100000b = C0h code for 2 = 11011010b = DAh; code for 3= 11110010b = F2h . etc.
Dr.Mehmet Bodur, EMU-CMPE

dp, d1 g, d6 b, d7 a

Dr.Mehmet Bodur, EMU-CMPE

CMPE328 Spring 2007-08

Brey 6th Ed 11.3

From 74S05 and ULN2004 data sheets

Multiplexed CA 7-Segment Displays


Up to 8 7-segments can be driven one at a time, fast enough to give a non-flicking view of all digits.
8255 PPI PORT-A PA6 PA7 PA0 PA1 ~a ~b ~g ~dp buffers with inverting outputs e a fg b d c dp ComAn2 e a fg b d c dp ComAn1 e a fg b d c dp ComAn0 send code of 0 to

Open Collector Output Buffers


The total power for two 7-segment-LEDs is hardly fulfilled by a 400mW 8255A chip.
Power to light one LED is PLED= 10mA x 3V=30mW. Mostly, an inverted open-collector driver with output current over 50mA per pin is used to drive up to 5 multiplexed 7-segments. The current through each segment is restricted to 10mA not to damage the display segments.

74S05 is a hex inverter with open collector output.


It is used mostly to drive high current of multiplexed 7segment common lines.

PORT-B PB0 PB1 PB2

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

. xgfe dcba PortA ( ~ 00111111b) and select digit 1 with PortB ( ~ 00000001b ) writes 0 on digit-0

ULN2004A is 7x inverting Darlington pairs with open collector output.


Each output can pull down 100 mA.
9 CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 10

Section 12.1

Section 12.1

LCD Display Module


Hitachi developed a Multi-line dot-matrix character generator and LCD-driver (OPTREX). It has 8-data lines, D[0..7] Three control lines.
E = enable, (high-to-low edge active) RS= register-select, (0 for Text, 1 for Command) R /~W = read/write, (1 read from LCD, 0 write)

LCD Module Commands


An LCD module needs 200ms reset-time, and can be configured by the following commands.

It has a slower 4-bit mode, which uses only D[4..7] to consume less i/o pin. LCD Module consumes about 20 mA to drive 32x5x7 Liquid Crystal Pixels at a rate over 50 cycles per second.
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 11 CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 12

LCD Module Writing a Text


Power up the Module, wait 200ms. Send Commands with RS line high
38h to initialize LCD for 2-lines & 5x7 matrix
wait 4.8ms for initialization

Stepper Motor Circuit


Output Port shall deliver 0110, 0011 1001 1100 to turn one step right (cw). and it needs 0110 1100 1001 0011 to turn one step left (ccw).
CMPE328 Spring 2007-08

8255A PA0 PA1 PA2 PA3

ULN2003 1 2 3 4 16 15 14 13 GND GND Stepper Motor

0Eh for LCD on, cursor on 01h to clear all characters.


wait 1.2ms to complete clearing

06h command for shifting cursor right 80h to locate cursor


80h = beginning of the first line. C0h = beginning of the second line.

Pin-8 GND Pin-9 +5V

Send Text with RS line low.


Cursor will move to next column while writing.
H,e,l,l,o will display Hello at the first line of the LCD.
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 13

In the program code, the steps of rotation can stay in an integer variable, and the last two bit of the steps may point the motor output code PA[0..3].
Dr.Mehmet Bodur, EMU-CMPE 14

Stepper Motor Coding Example


Problem: An 8255A is interfaced to 8088 system bus to have port addresses PA at 0x0308, PB at 0x030A, PC at 0x030C, and ComR at 0x030E. PA is connected to a stepper motor, PB reads 2 switches, Sup, and Sdn. PC outputs the position of the stepper motor in binary format. The code shall Initialize an 8-bit integer, STEP=0; Initialize a simple look-up table with four values,
0x06,0x03, 0x09, 0x0C.

Initialize 8255A ; Start to an endless mainloop. In the loop


Read switches. If Sup is low then increment STEP. If Sdn is low then decrement STEP. Mask the lower byte of STEP with 0x03 to get only the least significant two bits of STEP, and read the corresponding motor code. Apply the code to port PA. Use D4-bit of port 0x0061 to wait exactly 100ms (calling jump to mainloop to loop forever.

Problem: An 8255A is interfaced to 8088 system bus to have port addresses PA at 0x0308, PB at 0x030A, PC at 0x030C, and ComR at 0x030E. PA is connected to a stepper motor, PB reads 2 switches, Sup, and Sdn. PC outputs the position of the stepper motor in binary format.

Solution H/W Design

A3 A3 A4 A5 A6 A7 A8 A9

End the assembler code


A10 A11 A12 A13 A14 A15

a) Design the IO address decoder for 8255, using only NAND gates and inverters. b) Write the assembler source for the described algorithm.
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 15

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

16

The code shall

Initialize an 8-bit integer, STEP=0; Initialize a look-up table with four values,
0x06,0x03, 0x09, 0x0C.

Solution ASM Coding 1


.model small mainloop: .data ; read switches step db ? mov dx,30Ah mcode db 06h,03h,09h,0Ch in al,dx ; switches .code ; take a copy for Sdn mov ax,@data mov ah,al mov ds,ax and al,01h ; Sup ;initialize 8255 jnz othersw mov dx,30Eh inc byte ptr step mov al, 10000010b othersw: out dx,al and ah,02h ; Sdn ;initialize step jnz dispstep mov byte ptr step,0 dec byte ptr step
Dr.Mehmet Bodur, EMU-CMPE 17

The code shall

Initialize an 8-bit integer, STEP=0; Initialize a simple look-up table with four values,
0x06,0x03, 0x09, 0x0C.

Solution ASM Coding 2

Initialize 8255A ; Start to an endless mainloop. In the loop


Read switches.
If Sup is low then incr STEP by 1 If Sdn is low then decr STEP by 1

Mask the lower byte of STEP with 0x03 to get only the least significant two bits of STEP, and read the corresponding motor code. Apply the code to port PA. Use D4-bit of port 0x0061 to wait exactly 100ms (calling jump to mainloop to loop forever.

End ASM code


CMPE328 Spring 2007-08

dispstep: push dx mov dx,30Ch mov dx,61h mov al, step Read switches. waitlp1: out dx,al If Sup is low then incr in al,dx STEP by 1 and al,03h ; mask and al,10h If Sdn is low then decr mov bx,offset mcode STEP by 1 jnz waitlp1 Mask the lower byte of mov al, [ bx + al ] waitlp2: STEP with 0x03 to get mov dx, 308h only the least significant in al,dx two bits of STEP, and out dx,al read the corresponding and al,10h mov cx, 3314 ; decimal jz waitlp2 motor code. Apply the code to port PA. call W30xCX loop waitlp1 Use D4-bit of port 0x0061 jmp mainloop to wait exactly 100ms pop dx (calling W30xCX proc pop ax jump to mainloop to loop pushf forever. popf push ax End ASM code ret end
Initialize 8255A ; Start to an endless mainloop. In the loop
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 18

The code shall

Initialize STEP=0; Initialize a simple look-up table with four values,


0x06,0x03, 0x09, 0x0C.

Solution- the Same Code in Borland C


mcode[ ]= (0x06, 0x03, 0x09, 0x0C); outp(0x30E, 0x82); // Configure 8255A char switch, step=0; do{ // mainloop switch=inp(0x30A); if ( !(switch&0x01) ){ step++; } if ( !(switch&0x02) ){ step - -; } outp(0x30C, step); outp(0x308, mcode[ step&0x03 ] ); delay(100); // 100ms delay } while(1); }
19

The following circuit must if switch S1 is off display on Disp-1 an E; otherwise F; if switch S2 is off display on Disp-2 an E, otherwise F. give high from PC7 if only S1 is off and S2 is on, otherwise make PC7 low Write the necessary Assembly Code
a b c d e f g

Example

#include <conio.h> Initialize 8255A ; Start to an endless mainloop. #include <stdio.h> main() { In the loop const char Read switches.
If Sup is low then incr STEP by 1 If Sdn is low then decr STEP by 1

Mask STEP with 0x03 to get only the least significant two bits, and read the corresponding motor code. Apply the code to port PA. Use D4-bit of port 0x0061 to wait exactly 100ms (calling jump to mainloop to loop forever.

End ASM code


CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

20

The following circuit must if switch S1 is off display on Disp-1 an E; otherwise F; if switch S2 is off display on Disp-2 an E, otherwise F. give high from PC7 if only S1 is off and S2 is on, otherwise make PC7 low Write the necessary Assembly Code

in ASM:

.model small mov al,ah .code and al,08h ; test S2 mov dx, 4657h ; ComR addr jz S2on mov al, 81h ; =10000001b mov al,86h out dx,al ; 8255 settings jmp S1S2 mainloop: S2on: mov dx, 4653h ; PC addr mov al,71h in al, dx ; read switches S1S2: mov ah, al ; save for S2 out dx,al mov dx, 4651 ; PA mov dx, 4653h and al,04h ; test S1 and ah,0Ch jz S1on cmp ah,08h mov al,86h ; ~79h for E jz C7low jmp S2 mov al,80h S1on: C7low: mov al,71h; ~8Eh for F out dx,al IO Addr: of PA, PB, PC, ComR are 4651h, 4655h, 4653h, 4657h. S2: jmp mainloop Display code of E is ~01111001b = ~79h out dx,al end Display code of F is ~01110001b = ~8Eh mov dx,4655h
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 21

The following circuit must if switch S1 is off display on Disp-1 an E; otherwise F; if switch S2 is off display on Disp-2 an E, otherwise F. give high from PC7 if only S1 is off and S2 is on, otherwise make PC7 low Write the necessary C Code IO Addresses of PA, PB, PC, ComR are 0x4651, 0x4655, 0x4653, 0x4657. Display code of E is ~01111001b = ~79h Display code of F is ~01110001b = ~71h

Solution

#include <conio.h> #include <stdio.h> main() { char PC; outp(04657 do{ PC=inp(0x4653); if (PC&0x04) outp(0x4651, 0FFh-79h); else outp(0x4651,0FFh71h); if (PC&0x08) outp(0x4655, 0FFh-79h); else outp(0x4651,0FFh71h); if ((PC&0x0C)==0x08) outp(0x4653,0x80); else outp(0x4653,0x00) } while(1); }

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

22

Consider a microprocessor based system interfaced to Stepper Motors SM1 and SM2, and a detector D.
SM2 moves the conveyor belt. A part is dropped on moving conveyor belt, When part is exactly under D, D sends +5V. SM2 shall move the belt 20 steps clockwise (forward) to bring the part under the stamp. SM1 shall move the stamp 30 steps down (cw), and then 30 steps up (ccw). Then, SM2 shall start rotation to drop the part into bin, and wait a part dropped onto the conveyor band.
D[0..7] ~IOR ~IOW RESET A0 A1 Decoder 300h303h

Example Stamp
PPI PA PB PC PA[0..3] PB[0..3] PC0

Part Dropper Bin

Detector, D
SM1

Stamp

Write a complete assembly program with a 1ms between-steps delay.


CMPE328 Spring 2007-08

SM2

Consider a microprocessor based system interfaced to Stepper Motors SM1 and SM2, and a detector D. SM2 moves the conveyor belt. A part is dropped on moving conveyor belt, When part is exactly under D, D sends +5V. SM2 shall move the belt 20 steps clockwise (forward) to bring the part under the stamp. SM1 shall move the stamp 30 steps down (cw), and then 30 steps up (ccw). Then, SM2 shall start rotation to drop the part into bin, and wait a part dropped onto the conveyor band. Write a complete assembly program with a 1ms between-steps delay.
CMPE328 Spring 2007-08

Part Collector Bin.


23

SM macro port,step mov dx,port mov bx,word ptr step and bx,0003h mov al,CS:[bx + offset cwlut] out dx,al endm .model small .code delay macro microsec jmp start local waitL, waitH cwlut: pushf db 0Ch,06h push cx db 03h,09h push dx SM1 dw 0 push ax SM2 dw 0 mov cx, microsec/30 mov dx, 61h start: waitL: mov dx, 303h in al,dx mov al, 81h and al,10h out dx, al jnz waitL waitH: mainloop: in al,dx inc word ptr CS:SM2 and al,10h SM 301h, SM2 jz waitH delay 1000 loop waitL mov dx,302h ; read D pop ax in al, dx pop dx test al, 01h pop cx jz mainloop popf endm
Dr.Mehmet Bodur, EMU-CMPE

Stamp Solution
mov cx,20 m2more: inc word ptr CS:SM2 SM 301h, SM2 delay 1000 loop m2more mov cx,30 m1down: inc word ptr CS:SM1 SM 300, SM1 delay 1000 loop m1down mov cx,30 m1up: dec word ptr CS:SM1 SM 300, SM1 delay 1000 loop m1up jmp mainloop end
24

Dr.Mehmet Bodur, EMU-CMPE

Section 12.3
Consider a microprocessor based system interfaced to Stepper Motors SM1 and SM2, and a detector D. SM2 moves the conveyor belt. A part is dropped on moving conveyor belt, When part is exactly under D, D sends +5V. SM2 shall move the belt 20 steps clockwise (forward) to bring the part under the stamp. SM1 shall move the stamp 30 steps down (cw), and then 30 steps up (ccw). Then, SM2 shall start rotation to drop the part into bin, and wait a part dropped onto the conveyor band. Write a complete assembly program with a 1ms between-steps delay.
CMPE328 Spring 2007-08

SM macro port,step,microsec mov dx,port mov bx,word ptr step and bx,0003h mov al,CS:[bx + offset cwlut] out dx,al endm mov cx,20 m2more: .model small inc word ptr CS:SM2 .code SM 301h, SM2 jmp start push cx cwlut: mov cx, 33; 33x30=1000us db 0Ch,06h call delay db 03h,09h pop cx SM1 dw 0 loop m2more SM2 dw 0 mov cx,30 start: m1down: mov dx, 303h inc word ptr CS:SM1 mov al, 81h SM 300h, SM1 out dx, al push cx mov cx, 33; 33x30=1000us mainloop: call delay inc word ptr CS:SM2 pop cx SM 301h, SM2 loop m1down delay 1000 mov dx,302h ; read D mov cx,30 in al, dx m1up: test al, 01h dec word ptr CS:SM1 jz mainloop SM 300h, SM1
Dr.Mehmet Bodur, EMU-CMPE

Stamp Solution-2
push cx mov cx, 33 call delay pop cx loop m1up jmp mainloop delay: local waitL, waitH push dx push ax mov dx, 61h waitL: in al,dx and al,10h jnz waitL waitH: in al,dx and al,10h jz waitH loop waitL pop ax pop dx ret 25 end

INTERFACING DAC TO THE PC


8255 connected to 1408 (DAC808)

Two methods of making a DAC:


binary weighted and R/2R ladder PWM (pulse width modulation) methods

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

26

Reference Voltage of DAC808


Iref=Iref ref = 2.5V/2.5k +2.5V/2.5k =2mA because the supply is +5V. Vref on a 5kOhm resistor is Vref = 5kOhm Iref = 10V
For an 8-bit converter voltage steps for an increment of input is
+_I

Waveform Generation
A sawtooth wave is obtained by increment and delay
For a 0-3V sawtooth wave with period T=500ms you need Ni = 0 (to start from 0V) Nf = 3000mV / 39.0625 mV = 76.8 77. For a period of 500ms, you need t= 500ms / 77 = 6.5ms between each step. 3v v t 0v
Dr.Mehmet Bodur, EMU-CMPE

V = Vref /256. = 10V/ 256 = 39.0625 mV.


Dr.Mehmet Bodur, EMU-CMPE 27

t (seconds)
28

CMPE328 Spring 2007-08

CMPE328 Spring 2007-08

DAC is at PA of 8255 which is enabled at addresses 300h 303h. Generate a sawtooth between 0-3V with a period 500 ms.

Code for Sawtooth


delay proc push dx push ax mov dx, 61h waitL: in al,dx and al,10h jnz waitL waitH: in al,dx and al,10h jz waitH loop waitL pop ax pop dx ret delay endp end
29

.model small ; delay for 6500us .code push cx ; initialize 8255 for all output mov cx,216 ;=6500/30 mov al,80h call delay mov dx,303h pop cx out dx,al ; increment al mov dx, 300h inc al ; mainloop for period ; loop for 77 times mainloop: loop steploop ; initialize al =0 ;(Ni) ; jump to mainloop mov al,0 jmp mainloop mov cx,77 ; do 77 (Nf) times steploop out dx, al

A DAC with Vref=5.12 V is interfaced to port 308h. What will be the waveform generated by Vo (the output of DAC) while the following code-loop is running. (assume that W30xCX provides exactly 30CX micro second delay period). macro delay n delay 5000 push cx mov cx,n mov cx, 64 call W30xCX steploop2: pop cx out dx, al endm dec al delay 100 mainloop: loop steploop2 mov dx, 308h mov al,40h jmp mainloop mov cx, 64 5.12V steploop1: out dx, al inc al 2.56V delay 100 1.28V loop steploop1 0V
CMPE328 Spring 2007-08

DAC example
Step voltage is V=5.12/256 =20mV Waveform starts from voltage Vo = 20 x 64 mV = 1.28 Volts It raises in 64 steps to 0x80 = 128 to Vo = 20 x 128 mV = 2.56 Volts in total 64 x 100 x30 s = 192ms. Then a delay keeps it constant for total 5000x30s = 150ms. Next, it makes steps downward to Vo = 20 x 64mV = 1.28V in total 64 steps of each 3ms =192ms. Then it repeats complete cycle

Period T=434ms

192

342

534
30

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

Dr.Mehmet Bodur, EMU-CMPE

ADC Interfacing
Digital computers use binary (discrete) values, but most of the physical signals are analog (continuous).
Temperature, pressure, humidity, and velocity are physical quantities. A transducer converts a physical quantity to voltage.
Transducers are also referred to as sensors.

ADC0804 is a SAR ADC 8-bit resolution single channel 8080 bus interfacing.

ADC0804 SAR IC

We need an analog-to-digital converter to translate the analog signals to digital numbers so that the PC can read and process them.

There are several kinds of ADC devices


Period or pulse counting:
the cheapest, the slowest and the least accurate method. Dual Slope Integration and Delta-Sigma methods are the most developed form of period counting. (very accurate but slow, 16-bit, 500s to .5s ).

Successive Approximation Registers (SAR):


the most widely known and used method. It finds each bit of digital result in one comparison. (8 to 12 bit accuracy, 10 to 100 s conversion speed.)

Flash converters:
the fastest, but expensive method. Used for video digitization. It uses 2n comparators and an encoder. (4 or 6 bit, 40ns to 1s conv.time)
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 31

from National Semiconductor Corp. Pin Functions CS: (Chip select) an active low input used to activate the 804 chip. RD (read): active low input signal to transfer the converted data to the data lines. WR (write or "start conversion"). ADCO804 enables the channel latch and starts the conversion of analog input at that channel. Vref/2 (reference voltage input) sets the reference voltage of conversion. For an 8-bit converter the step size V = Vref/256. DB[0..7] (data outputs) The conversion result depends on the reference and input voltages

Dout = Vin / V = Vin 256 / Vref

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

32

ADC Vref and Step Size


Example-1,
If Vref/2 is +1.024 volts, then V = Vref/256 =8mV If ADC reads D=32h then Vin = 50x 8mV = 400mV. If Vin=1.2V, ADC reads D=1200/8= 150 =96h if the analog input range needs to be 0 to 4 volts, Vref/2 is connected to 2 volts. That gives V = 4V/256 = 15.625 mV for step size.

ADC 0804 Conversion Cycle


~CS is set to low for both RD and ~WR pulses ~CS ~WR D[0..7] ~INTR ~RD Conversion starts Conversion ends Dats transfer over Read Delay Data Out

Example-3,

Example-4,

if we need the step size V = 10 mV then Vref = V 28 = 10mV 256 = 2.56 V.

Write to ADC port to start the conversion. Wait until ~INTR line is low, which indicates that the conversion is over, or wait long enough so that conversion is over. Read ADC port to get conversion result.
33 CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE 34

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

LM35D Temperature Sensor


LM35 works from -55oC to 150oC
Vo = 10mV x (oC + 55)
Vo is in mV
1 +5V 2 Vo

LM35D works from LM34 works from


Vo = 10mV x (oF

0oC

to to

100oC 200oF

3 GND

Vo = 10mV x oC ; ( Vo is in mV).

-50oF
+50)

ADC is interfaced to port address 0308h. ~INTR is connected to PC0. Read temperature and display in oC. We need some calculations before we start coding. V = 1024/256 = 4mV LM35D produces Vo = 10mV oC Then, the reading D becomes D= Vo / V = 10mV oC /4 mV = 2.5 oC Displayed value N must be N = D 2/5. (=oC). We have to determine the addresses -Writing 308h will start conversion -Wait until port 302h bit-0 is low -Read D from 308h -Port 301h bit-0 is E of LCD -Port 301h bit-1 is RS of LCD -Port 300h is Data of LCD.
CMPE328 Spring 2007-08

ADC0804 Example
ADC0804 D[0..7] ~IOR ~IOW IO Addr Decoder PC0 0308h 300h-303h D[0..7] ~IOR ~IOW A0 A1 ~CS RESET PPI 8255 ~RD ~WR A0 A1 ~CS RESET PA PB0 PB PB1 PC PC0
36

D[0..7] ~RD ~WR ~INTR ~CS

Vref/2 Vin+ Vin-

0.512V LM35D Vo

Clk R Clk in

LCD D module E RS

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

35

Dr.Mehmet Bodur, EMU-CMPE

ADC is interfaced to port -Writing 308h will start conversion address 0308h. ~INTR -Wait until port 302h bit-0 is low is connected to PC0. -Read D from 308h Read temperature and -Port 301h bit-0 is E of LCD display in oC. -Port 301h bit-1 is RS of LCD (0:disp, 1:com)
-Port 300h is Data of LCD. ; initialize LCD

Coding

.model small .data Disp db ?,?,? .code mov ax,@data mov ds,ax ; initialize PPI mov dx, 303h mov al, 81h ; PCL input, out dx,al

stc mov al,38h call chrLCD mov cx,0FFFh W0: loop W0 mov al,0Eh call chrLCD mov al,01h call chrLCD mov al,06h LCD Initialization Commands call chrLCD 38h to initialize LCD 2-lines 5x7
wait 4.8ms 0Eh for LCD on, cursor on 01h to clear all characters. wait 1.2ms for clearing 06h for shifting cursor right
CMPE328 Spring 2007-08

; start mainloop mainloop:

; start ADC conversion mov dx, 308h out dx,al ; wait conversion is over mov dx,302h wco: in al,dx and al,01h ; bit-0 mask jnz wco ; read the ADC result mov dx,308h in al,dx ; scale it to N mov cl, 2 mul cl ; ax= D . 5 mov cl, 5 div cl ; ax=D . 2/5
37

ADC is interfaced to port address 0308h. ~INTR is connected to PC0. Read temperature and display it on LCD in oC. -Port 301h bit-0 is E of LCD ; CF set for command -Port 301h bit-1 is RS of LCD ; CF clear for display ; convert to ascii -Port 300h is Data of LCD. chrLCD: ; Assume first two digits blank. mov dx,300h mov word ptr Disp,2020h ; write digits to LCD out dx,ah ; start from line-1 col-1 cmp al,10 mov dx,301h stc ; set carry flag jb digit1 mov al,1 mov ah,80h cmp al,100 mov cx, 1000 call chrLCD jb digit2 jnc chrdisp clc ; clear carry flag ; Start to BCD conversion or al,2 mov ah,byte ptr Disp mov cl,100 mov cx, 10000 call chrLCD div cl mov ah,byte ptr Disp+1 chrdisp: add ah,30h ; make ascii out dx, al call chrLCD mov byte ptr Disp,ah mov ah,byte ptr Disp+2 w1: loop w1 digit2: and al,FEh ; clear E call chrLCD mov cl,10 out dx,al ; repeat mainloop div cl mov cx, 1000 jmp mainloop add ah,30h or al, 01h ; set E mov byte ptr Disp+1,ah LCD control lines w2: loop w2 digit1: E = enable, ret (high-to-low edge active) add al,30h end RS= register-select, mov byte ptr Disp+2,al
(0 for Text, 1 for Command)
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE

Coding-2

Dr.Mehmet Bodur, EMU-CMPE

38

ADC0844 and ADC0848 Both are 8-bit converters. 4 and 8 channel ADC
ADC0844 has 4 channels. ADC0848 has 8-channels, MA[0..4] (multiplexed address).
ADC0848 uses address/data pins to select the channel. A portion of the D[0..7] pins are multiplexed for MA[0..4] . The D[0 ..7] pins are inputs when the channel's address is sent in. However, when the converted data is being read, DO - D7 are outputs.

ADC is interfaced to port address 0308h. ~INTR is not connected. Read 8 temperatures and send the average oC to port 030Ah.

Interfacing ADC0848 Problem


D[0..7] ADC0848 Vref/2 D[0..7] Vin[0..7] ~RD ~WR ~INTR 0308h 30Ah ~IOW D[0..7] ~CS Clk R Clk in 100k 22p 1.28V 8 8 LM Vo[0..7] 35D ~IOR ~IOW IO Addr Decoder

Some Calculations: V= 2560/250 = 10mV


no scaling required

Port addresses
308h ADC0848 (channel select, and read) 30Ah Output port for aver.

WR (write or "start conversion"). At the LH edge of WR


ADCO804 enables the channel latch and starts the conversion of analog input at that channel.
CMPE328 Spring 2007-08 Dr.Mehmet Bodur, EMU-CMPE

ADC0848 8Channel 8-bit ADC


39

After each ADC start, wait minimum 100 microsecond to have conversion over. Assume that execution of 10 loop instruction takes 1 s.
CMPE328 Spring 2007-08

LS373 D[0..7] Q[0..7] G ~OE


40

Q[0..7]

Dr.Mehmet Bodur, EMU-CMPE

ADC is interfaced to port address 0308h. ~INTR is not connected. Read 8 temperatures and send the average to port 030Ah. Assume that execution of 10 loop instruction takes 1 microsecond. .model small ; start ADC conversion .data ADch: ADC db 8 dup(0) mov dx, 308h .code mov ax,cx ; channel number mov ax,@data add al,08h ; for single ended mov ds,ax out dx,al ; channel select and start ; initialize PPI ; wait conversion is over mov dx, 303h push cx mov al, 81h ; PCL input, mov cx,1000 out dx,al WL: loop WL ;10 loop takes 1 s. pop cx ; start mainloop ; read the ADC result mainloop: mov dx,308h ; get 8 ADC readings in al,dx mov cx,7 mov bx, cx mov [bx+offset ADC],al loop ADch

Coding
; take average of 8 readings mov bx,0 mov cx,7 mov ax,0 addloop: add ax,[bx+offset ADC] inc bx loop addloop ; divide by 8 mov cl,8 div cl ; send to port mov dx,30Ah out dx,al ; repeat it forever jmp mainloop end
41

CMPE328 Spring 2007-08

Dr.Mehmet Bodur, EMU-CMPE

You might also like