Documente Academic
Documente Profesional
Documente Cultură
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.
Section 11.4
Section 11.4
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
Port Select
A2 A1 A0 0 0 0 0 0 1 0 1 0 0 1 1
4
Section 11.4
Section 11.4
Port Select
A2 A1 A0 0 0 0 0 1 0 1 0 0 1 1 0
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
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
. xgfe dcba PortA ( ~ 00111111b) and select digit 1 with PortB ( ~ 00000001b ) writes 0 on digit-0
Section 12.1
Section 12.1
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
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
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.
A3 A3 A4 A5 A6 A7 A8 A9
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
16
Initialize an 8-bit integer, STEP=0; Initialize a look-up table with four values,
0x06,0x03, 0x09, 0x0C.
Initialize an 8-bit integer, STEP=0; Initialize a simple look-up table with four values,
0x06,0x03, 0x09, 0x0C.
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.
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 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.
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); }
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
Detector, D
SM1
Stamp
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
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
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
26
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
t (seconds)
28
DAC is at PA of 8255 which is enabled at addresses 300h 303h. Generate a sawtooth between 0-3V with a period 500 ms.
.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
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.
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
32
Example-3,
Example-4,
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
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
0.512V LM35D Vo
Clk R Clk in
LCD D module E RS
35
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 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
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.
Port addresses
308h ADC0848 (channel select, and read) 30Ah Output port for aver.
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
Q[0..7]
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