Download presentation
Presentation is loading. Please wait.
Published byMarsha Page Modified over 9 years ago
1
The 8255 allows the microprocessor to communicate with the outside world through three programmable 8-bit wide I/O ports The PC uses a few 82C55 (in the chip set) to control the keyboard, speaker, and parallel port The 8255 PPI is available in DIP or surface mount forms Also implemented as functions within modern interface chip sets The 82C55 PPI PPI = Programmable Peripheral Interface
2
A B C 3 programmable 8-bit I/O ports: A, B, C Port A + Upper half of C = Group A (12 bits) Port B + lower half of C = Group B (12 bits) 82C55 DIP Version Data Bus 2-bit Address I/P (select port or Command register for Read or Write) CS Input (low) Read/Write Control On the PC: Two 82C55s -One 82C55 occupies 4 I/O ports 60H-63H: Handling Keyboard, timer, speaker, etc. -One 82C55 occupies 4 I/O ports 378H-37BH Parallel printer port RESET initializes the PPI to operate in mode 0 & all 3 ports as inputs at power up. With all ports as input ports, this avoids damage to the device at Power up 2 Groups 12-bit groups A, B Similar to a 4-byte RAM
3
B
4
I/O Port Assignments Group A is Port A and upper ½ of Port C (PC7-PC4). Group B is Port B and lower ½ of Port C (PC3- PC0). A1A0Function 00Port A 01Port B 10Port C 11Command Register Writing into this register programs the various ports to operate in various modes and be used as either inputs or outputs
5
100100 100100 001 80386SX Processor A7 A6 A5 A4 A3 A2 A1 A0 11 0 0 0 0 0 0 = C0H Port A 11 0 0 0 0 1 0 = C2H Port B 11 0 0 0 1 0 0 = C4H Port C 11 0 0 0 1 1 0 = C6H Comnd Register A1 A0 Inputs on 82C55 Address from 80386SX microprocessor Address from microprocessor Select PPI At decoded P address
6
Programming the 82C55 Using one internal register If bit 7 = 1 select format for Command Byte A If bit 7 = 0 select format for Command Byte B Bit 7 = 1: Command Byte A: Programs Groups A and B (as defined in terms of ports A, B, C - previous slide): - as either inputs or outputs - in either modes 0, 1, or 2 Bit 7 = 0: Command Byte B: Sets (to 1) or Clears (to 0) the specified one of 8 bits of port C (in modes 1 and 2) Only for mode 0 Program
7
8255 Modes Mode 0 (for groups A & B)- the most commonly used mode: All 12 bits of the group are simple inputs or simple latched outputs Mode 1 (for groups A & B)- is used occasionally to provide handshaking to an I/O device and operate asynchronously with the device. Most Port C bits are dedicated for handshake functions for the operation. A few are controlled separately using the Command byte B format for handshaking I/O. Mode 2 (for group A only- Group B not used)- is a bidirectional mode for Port A only (Port B is not used). Port C provides handshaking signals. Data Control, e.g. Busy, Strobe, etc.
8
8255 Modes Not Used Control: Handshaking For the data port When O/P, Set or Reset Using Command Byte B
9
Programming the 82C55 To program the command register of the 82C55 and select operation use command byte A For example, to program all the ports as outputs and in mode 0 (the most common mode) use: MOV AL,80H MOV DX,COMMAND_PORT OUT DX,AL = C6H in slide 50
10
Mode 0 Application: 7-Segement Display Common Segment Driver
11
7-Segement Display Select Segments: Switched Resistors to GND Anode Cathode Select Segments: Switched Resistors to Vcc CA CC Vcc GND
12
7-Segement Display Anode Cathode Segment Data (1 byte) for each character
13
Multiplexed 7-Segement Display Motivation for MUXing: - Reduce the number of segment drivers by a factor of n Common To segment on all Digits Sequentially Turn ON one digit at a time Recommended rate: 100 – 1500 times per sec n = 8 Digits
14
B: O/P Port: Select # Displayed A: O/P Port: Segment data For selected digit B: O/P Port: Select digit To be displayed Digit transistor switch Controlled by Port B bit, e.g. Tr 1 Vcc 7 Segment data transistor switches Controlled by Port A bits, e.g. Tr 2 GND PLD for 14-bit I/O address (A15-A2) + IO/#M decoding Address decoding is similar to that of a 4-byte Read/Write memory Multiplex the eight digit displays (only one is ON at a time) 1 turns segment ON 0 turns digit digit ON Most Significant (MS) Digit... 1 digit Tr 1 Tr 2
15
library ieee; use ieee.std_logic_1164.all; entity DECODER_11_21 is port ( IOM, A15, A14, A13, A12, A11, A10, A9, A8, A7, A6, A5, A4, A3, A2: in STD_LOGIC; D0: out STD_LOGIC ); end; architecture V1 of DECODER_11_17 is begin D0 <= not IOM or A15 or A14 or A13 or A12 or A11 or not A10 or not A9 or not A8 or A7 or A6 or A5 or A4 or A3 or A2; end V1; A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 X X 0700 Port A 0701 Port B 0702 Port C 0703 Control Register 82C55 4 I/O ports: On chip Selection 14-bit address decoding Using PLD PLD Program for Address Decoding Total: 16-bit I/O address
16
; Program the 82C55 for Port A and Port B are output ports in mode 0 MOV AL, 80H; 80H Data into AL MOV DX, 703H ; Address of Command Port into DX OUT DX, AL; Write 80H into Command Port ;; to program PPI ; An assembly language procedure that multiplexes the 8-digit display. ; This procedure must be called often enough for the display to appear stable DISPPROCNEAR USES AX BX DX SI PUSHF MOV BX,8;load counter BX with # of display digits MOV AH,7FH;load initial digit selection pattern to enable MS digit (01111111) MOV SI,OFFSET MEM - 1;Load SI with offset (MEM) - 1 MOV DX,701H;address Port B (for Port A: decrement DX) ;Sequentially display all 8 digits starting with MS digit.REPEAT MOV AL,AH OUT DX,AL ;send digit selection pattern to Port B DEC DX;Address Port A (to send Digit Data) MOV AL,[BX+SI];Load digit data from memory into AL OUT DX,AL ;send digit data to Port A CALL DELAY;wait 1.0 ms leaving displayed digit ON ROR AH,1;adjust selection pattern to point to next digit INC DX;Address port B DEC BX;decrement counter for data of next digit..UNTIL BX == 0 POPF RET DISPENDP...... MEM BX MEM+7 MS Digit 8 bytes of Digit Data In memory LS Digit Procedure for 1 ms delay, e.g. a loop of instructions i.e. digit remains ON for 1 ms before moving to next
17
; Delay Loop DELAY PROC NEAR USES CX MOV CX, XXXX; XXXX determines delay, = Delay required / loop exec time D1: LOOP D1 RET DELAY ENDP Loop execution time is calculated from instruction data and the clock frequency. An 80486 executes “LOOP D1” in 7 clock cycles With a 20 MHz clock, loop exec time = 7 x 50 = 350 ns XXXX = 1ms/350ns Display Flashing Rate: - Assume the DISP Procedure is called continuously - Ignore loop execution times relative to delay time (e.g. 350 ns << 1 ms)... Digit Displayed 8 7 2 1... 7 2 1 8 1 ms 8 ms... Flashing rate = 1/8 ms = 125 Hz DISP Proc
18
Stepper Motor Interface Wk 10 Stepper motor is digital in nature It rotates in a sequence of discrete steps controlled by sequentially energizing a set of coils (windings) Step angles vary from 1 to 15 depending on precision required (and cost) N Pole lies between the two energized coils Rotation Direction: Anti-clock wise Step angle: 90 45 135 225 315 2 coils driven at a time 0 0 1 1 ROL: Motor rotates anticlockwise Anti-clockwise Rotation ROR reverses the direction of Motor rotation
19
Darlington pairs for driving high load currents Anti-surge diodes 33H or 66H or CCH or 99H POS Current angular Position stored at location POS (in memory) 33H = 00110011 66H = 01100110 CCH = 11001100 99H = 10011001 ROL ROR Anti-Clockwise Clockwise ROL 1 activates coil Rotate instruction operates on whole byte We want to rotate half the byte duplicate pattern!
20
PORTEQU40H ;An assembly language procedure that controls the stepper motor STEPPROCNEAR USES CX AX MOV AL,POS;get position OR CX,CX;set flag bits IF !ZERO?.IF !SIGN?;if not sign Rotate left.REPEAT (anti clockwise) ROL AL,1;rotate step left OUT PORT,AL CALL DELAY; wait 1 ms for motor to move.UNTILCXZ.ELSE AND CX,7FFFH;make CX positive.REPEAT ROR AL,1;rotate step right OUT PORT,AL CALL DELAY ;wait 1 ms for motor to move.UNTILCXZ.ENDIF MOV POS,AL; Save POSN for next step RET STEPENDP If CX = 0 Quit No rotation required CX has: - Sign of rotation 0: ACW 1:CW - # of steps e.g. 0000H (0) 0005H (5,ACW) 8007H (7,CW) CX Positive: Rotate Anti-clockwise CX Negative: Rotate Clockwise Not At exit of proc, CX is changed
21
Interfacing a 4x4 Key Matrix Row Inputs Column Outputs With no keys pressed, all row inputs are 1’s due to the pull up resistors connected to Vcc Column outputs are sequentially scanned as 0’s If key (X,Y) is pressed, it connects the scanning 0 from column X output to row Y input. If no other key is pressed on the same column, this allows the pressed key to be identified. X Y 0 2. Scan columns with one 0 to locate a pressed key O/P I/P 0 00 0 1. All 0’s To check For any Press/Release
22
Scan for the next key pressed and locate its column Wait for any remaining pressed keys to be Released Wait for next key to be Pressed to determine it Scan again after things have settled, and determine coordinates (row, column) of pressed key Software Debounce Of Switch Release Software Debounce Of Switch Press Use key position determined To get its corresponding Code stored in a lookup table In memory Still Here, column is known row Determine row of key pressed (look for 0 bit in read data) into CF Read Input Pattern
23
;assembly language version; ;KEY scans the keyboard and returns the key code in AL. COLSEQU4 ROWSEQU4 PORTAEQU50H PORTBEQU51H KEYPROCNEAR USES CX BX MOV BL,FFH;compute row mask SHL BL,ROWS MOV AL,0 OUT PORTB,AL ;place all zeros on Port B.REPEAT;wait for release.REPEAT CALL SCAN.UNTIL ZERO? CALL DELAY10; Release debounce CALL SCAN.UNTIL ZERO?.REPEAT;wait for key press (to be determined).REPEAT CALL SCAN.UNTIL !ZERO?; (not zero, i.e. not = FF) CALL DELAY10; Press debounce CALL SCAN, scan again after things have settled.UNTIL !ZERO? MOV CX,00FEH.WHILE 1;find column MOV AL,CL OUT PORTB,AL CALL SHORTDELAY;Wait till data outputted to PortB have settled! CALL SCAN.BREAK !ZERO?;Key found at this column- Quit WHILE1! ADD CH,COLS;Key not found at this row- move on to next row – add COLS to CH ROL CL,1 ; AL = 11111101 on 2 nd trial.ENDW.WHILE 1;find row from pattern Read into PortA in SCAN SHR AL,1.BREAK.IF !CARRY? ; LSB of AL is shifted into the carry flag by SHR! So we stop on 1 st zero bit INC CH ; for each shift until row is found.ENDW MOV AL,CH ;get key code into AL: AL = CH = (COLS) X + Y = 4 X + Y; X = 0, 1,..,3, Y = 0, 1,.., 3 RET KEYENDP SCANPROCNEAR IN AL,PORTA;read rows OR AL,BL CMP AL,0FFH;test for no keys RET SCANENDP Program the 8255 for: Port A: Input Port B: Output Size in ROWS, COLS can be set, up to 8 x 8, here 4 X 4 BL = F0, for rows = 4 Mask for Oring in SCAN BL: 11110000 = F0H AL: XXXX1111 (No Keys Pressed) Oring: 11111111 = FF Keep calling SCAN Until FF (no key pressed) i.e. wait for key release Keep calling SCAN Until (Not FF) (a key pressed) i.e. wait for key stroke Determine X,Y of Pressed Key as Number In CH AL AL = 11111110 1 st Column (for a key pressed on row 3) After SCAN: AL = XXXX1011 Column # Row # AL from SCAN ASCII? ZERO: No Pressed !ZERO: Pressed SHR into Carry X (Column) Y (row)
24
Ports A and/or B used as latching input ports to store data temporarily from external devices before processor is ready to take the data in. Port C provides the control (handshake) bits: - PC5(1): IBF (Input buffer full) Output indicating (to both external device and P) if port latch has data - PC4(2): #STB (strobe) Input latches data into the port latch at the + ive going edge - PC3(0): INTR (Interrupt Request) If enabled, used to interrupt the processor. High at + ive going edge of #STB and low after data is read by processor - PC4(2) INTE (Interrupt Enable) Internal bit programmed through PC4(2) for enabling/disabling interrupt generation - PC6,7 General Purpose I/O bits. Set/Reset by CBB Mode 1: Port A/B for Strobed Input Data from External Device Two ways to interface to the processor: 1.Polled: Processor polls IBF waiting for data to be available in port latch (IBF=1) 2.Interrupt-driven: INTR is used to interrupt the processor when data becomes available in port - saves processor time as Input Automatically the associated C half assumes these functions- no longer separately Programmed (except PC6,7) To P (Normal I/O bits) Program with Command Byte B (CBB) format Port Latch E x t e r n a l B u s Data Byte Handshake P Data Bus For Input Operations…Action started by who? External Device Side Microprocessor Side Data strobed in from external device into port latch microprocessor reads data from port latch through the data bus Hardware Polling By Device Software Polling by P P P to Dev & P or Release IBF For next transfer IBF = 1 1 2 automatic (If Enabled)
25
;A procedure that reads the keyboard and ;returns the ASCII key code in AL BIT5EQU20H;00100000 Mask defining PC5 (IBF for Port A) PORTCEQU22H PORTAEQU20H READPROCNEAR.REPEAT;poll IBF bit IN AL,PORTC TEST AL,BIT5.UNTIL !ZERO?; Quit polling when bit 5 read is not ZERO (IBF=1) IN AL, PORTA; get ASCII value of key pressed from keyboard RET READENDP Interfacing a Keyboard to P using Port A in Mode 1 (Strobed Input) 1 s Port A Port C To Processor Data H/S Control (PC5 IBF bit) 8255 should be programmed for operation in: - Group A in Mode 1 - Port A is input Handshake (Polling) Method
26
Ports A and/or B used as strobed output ports to write data from processor into external devices. Associated half C bits provide handshake signals for the interface. Port C provides control bits: - PC7(1): #OBF (Output Buffer Full) Activated when port has data written into it by the processor. Deactivated to 1 when #ACK is received from external device indicating that data was read. - PC6(2): #ACK (Acknowledge input) from external device requesting data in port to be put on external bus for reading by external device - PC3(0): INTR (Interrupt Request) Used to interrupt the processor when external device receives data (end of #ACK). - PC6(2) INTE (Interrupt Enable) Internal bit programmed through PC4(2) for enabling/disabling interrupt generation - PC4,5 General Purpose I/O bits Mode 1: Port A/B for Strobed Output Automatically the associated C half assumes these functions- no longer separately programmed (except PC4,5) From P Data from P Is strobed into port latch at trailing edge of #WR from P by P Again P can do its part in two ways: 1.Polling for #OBF high 2.Getting an interrupt with INTR External Bus Port Latch Data Byte Handshake P Data Bus For output operations…Action started by who? External Device Side Microprocessor Side Software Polling By Processor Hardware Polling by Device For device to take data: - #ACK puts latch data on the external bus - P Provides a strobe pulse 1 2 or For P to O/P Next char Device Realizes there is Data in port latch Automatic (If Enabled)
27
;A procedure that transfers an ASCII character from AH to the printer ;connected to port B BIT1EQU2 ; Bit PC1 = #OBF for port B PORTCEQU62H PORTBEQU61H CMDEQU63H; The 8255 command byte address PRINTPROCNEAR.REPEAT ;Wait for printer ready to receive a new char- Poll #OBF till high IN AL,PORTC TEST AL,BIT1.UNTIL !ZERO?; #OBF =1 No data in output buffer, so can write into it! MOV AL,AH; Write ASCII char data into port latch OUT PORTB,AL MOV AL,8 ;Generate data strobe pulse ; on PC4 OUT CMD,AL MOV AL,9 OUT CMD,AL RET PRINTENDP Interfacing a Printer to P using Port B in Mode 1 (Strobed Output ) Port B Port C Data Control (PC1 #OBF bit) Strobe data Into Printer Ordinary C I/O bit Set and reset with command byte B To generate the #DS pulse (Strobe output) signal to Ex Device #OBF not used As a strobe to printer 1 0 0 0 1 0 0 1 In port C Reset PC4 Set PC4 Data from P to port latch Data from port latch to Printer P Polling for #OBF = 1
28
Mode 2: Port A only for Bidirectional I/O Ports A (only) is used for bidirectional I/O. 5 Port C bits provide bidirectional handshake signals. 3 Port C bits are programmable I/O with Command byte B format. Application: Computer-to-computer comm., GPIB bus Handshake signals combine input handshake and output handshake of mode 1: - For Input: #STB, IBF - For Output: #OBF, #ACK - Common: INTR (qualified by 2 internal enable bits INTE1 and INTE2) Program operates the port’s bidirectional bus using the OUT and IN instructions I/P Control O/P Control To/From P
29
Mode 2 Example: Processor Sends Data to External Device on the Bidirectional Bus To send Data from processor to external device: 1. Processor checks if #OBF = 1 (No data pending in port) 2. Processor OUTs data to port (Writes it into Port A latch- not on external bus yet) 3. Port automatically lowers #OBF O/P to alert device to take data 4. Device detects 3 and lowers #ACK input to port 5. This raises #OBF high 6. #ACK enables Port external bus to carry latch data so it can be taken by device 7. After device takes data it raises #ACK high I/P Control O/P Control From P I understand you have data for me in your latch. Please put it on the bus so I can take it! By external device (May need a strobe pulse from P) By Microprocessor Data in Port A latch, but not on its I/O bus yet Port I/O bus is normally HiZ To allow use in The other direction Enable I/O Bus to carry latch data 1 2 3 4 6 5 7 ;A procedure transmits AH through the bidirectional bus BIT7 EQU 80H PORTC EQU 62H PORTA EQU 60H TRANSPROC NEAR.REPEAT IN AL,PORTC TEST AL,BIT7.UNTIL !ZERO? MOV AL,AH OUT PORTA,AL RET TRANSENDP Wait for #OBF =1 Result = 1 (Not zero)
30
Mode 2 Example: Processor Receives Data from External Device on the Bidirectional Bus To Receive Data: 1. External device sending data checks if #IBF = 0 (No pending data in port latch not read by processor) (Hardware Polling) 2. Then it puts its data on external bus and strobes it into port latch using #STB 3. IBF automatically goes high until data is read by processor 4. Processor polls IBF for IBF = 1 to make sure data is in port latch (software polling) 5. Processor reads data from port 6. This automatically lowers IBF to enable further writes I/P Control O/P Control By device on the other side HiZ external bus! (It is OK… processor reads its data bus) To P By Microprocessor latch Port I/O bus is normally HiZ Hardware Polling for IBF = 0 by device to generate #STB To avoid overwriting existing data in Port not read by processor yet ;A procedure that reads data from the bidirectional bus into AL BIT5 EQU 20H PORTC EQU 62H PORTA EQU 60H READ PROC NEAR.REPEAT ;Wait for IBF = 1 IN AL,PORTC TEST AL,BIT5.UNTIL !ZERO? IN AL,PORTA RET READ PROC NEAR 1 2 3 4 5 Step 4 6 2 Step 5
31
Summary of port functions in various modes
32
Programmable Counter/Interval Timer (PIT): 8254 3 Identical and separate 16-bit presettable down counters Clock frequency up to 10 MHz Binary or BCD counting Each can be programmed to operate in any of 6 modes Each can be preset and read under program control Programmed by writing into a command register (A1A0=11) Appears as 4 I/O bytes in the processor I/O space (similar to the PPI 82C55) Note similarity with the 82C55: 3 programmable units + 1 Command register Data bus Chip Select (Obtain by Decoding Remaining I/O Address lines) 2-bit address RD/WR Control PIT For each counter: - Clock Input: - ive edge - Gate Input: Function depends on mode, e.g. 1: Count, 0: No count - Output: Changes state on terminal count WK 11
33
The PIT in the PC The timer appears in the PC at I/O ports 20H and 23H. It generates speaker beeps and generates a periodic signal at the correct repetition frequency to refresh the DRAM A1A0Function 00Counter 0 01Counter 1 10Counter 2 11Control word (command byte)
34
General Applications Generation of accurate time delays under software control Real Time Clock Event Counter: Interrupts processor when a preset number of events occur Digital one-shot (A pulse of a programmable width generated in response to an event) Programmable frequency clock (fout = fin/n, n determined by software) Square wave generator (programmable frequency) Complex waveform generation (counter output fed to a digital to analog converter- DAC) Complex motor control
35
Close-up of one Counter 2-byte Count Register (Input Latches for writing Initial Counts from P Into counter) 16-bit Counting Element 2-byte Output Latches for reading counter Parallel output (at the counter’s I/O address with the same format specified when the counter was programmed) Normally follow counter count and can be read at any time. A “Counter Latch Command” latches the present counter count into them. Will be frozen till read by processor Program the counters By writing into this Control byte (A1A0 = 11) LS MS CR and OL are written/read one byte at a time across the 8-bit data bus Counter Status: Use the Read-back Command to latch into Status Latch for reading by Processor LS MS Inputs & Outputs
36
Programming the Counters: Write Operations Each counter must be programmed before it can be used A counter is programmed by writing a control word (specifying the counter) into the control word register (A1A0=11), followed by an initial count into the I/O address of that counter Counting Method: BCD or Binary Counter Mode Reading/Writing Format Specify Counter to be programmed Initial counts are written into the I/O address of the counter specified, e.g. A1A0 = 01 for counter 1 Initial count bytes or words specified for a counter in the control word should be sent after the control word- but not necessarily immediately following it, and programming of the 3 counters can be interleaved Counter Latch Command Latches present count of specified Counter to its OL. Remains frozen until latch is read by processor Use different formats for the Control word- see Read Operations
37
Programming the Counters: Write Operations All sequences below for the Control Word followed by Initial Count bytes are valid The arrow shows an invalid Sequence. Why?
38
READ Operations: Reading a counter while it is counting Three Ways 1.Simple READ: Does not need a special command….Procedure: - Inhibit clocking by G = 0 (Disrupts future count!) to ensure reading stable levels - Simply Read the counter at the proper I/O address (e.g. A1A0=10 for counter 2) 2.Issue a “Counter Latch Command” by writing the appropriate byte into the Control Word Register (A1A0 = 11), with RW1 RW0 = 00 and specifying the address of the required counter SC1 SC0. When this command is executed, Present count at the counter parallel output is latched into the counter’s output latch (OL) and remains fixed until the latch is read by the processor or the counter is re-programmed. Only then it returns to follow the counter count as before. OL bytes must be read in the same format specified when the counter was last programmed The command byte during a “Counter latch command”
39
READ Operations: Reading a counter while it is counting Three Ways, Contd. 3.Read Back Command Written into the control word (A1A0=11) To latch Status and/or Count of any of the three counters (Up to all 3 counters can be specified simultaneously) Format for the Counter Status Register (latched into the Status latch if so specified in the Read-Back Command) Shows how the counter was programmed i.e. Count = 00000H Up to 3 Counters The command byte written during a “Read back command”
40
The 6 Counter Modes Free Running One off
41
Mode 0: Event Counter G input = 1 always to enable counting n: Initial Count Number loaded into counter after programming it by the Control Word OUT goes high at terminal count, (n+1) negative clock edges after n is written into Counter Register by software Initial Count n = 5 Written into Counter’s CR Initial Count n = 5 Loaded into Counter... Counter decrements till Terminal Count = 00000H To Processor INTR OUT rises high at terminal (null) count Applications: Interrupt Processor on: Arrival of a predetermined number of events (1 0 clk transitions) = (n+1) Or elapse of a time interval t = (n+1) Tclk From the writing of n by software into the counter Count down from n till terminal (null) count
42
Gate G in this mode is used as the monostable hardware trigger It allows n to be loaded into counter, which clears OUT to 0. OUT remains 0 until terminal count Duration of the 0 pulse on OUT = n Tclk If Gate goes high again during the OUT pulse, monostable is retriggered to extend the pulse by another duration Initial Count n = 5 Written into Counter’s CR Initial Count n = 5 Loaded into Counter... Counter decrements till Terminal Count = 00000H Mode 1: Hardware Triggered One-Shot (Monostable Multivibrator- Retriggerable) OUT rises high at terminal count (Trigger) Note: In this mode G needs not be kept high for counting to be enabled OUT goes low At loading n
43
Gate G = 1 always, to enable counting OUT frequency = Clock frequency/n OUT Duty cycle (ON:Total) = (n-1):n A square wave only for n = 2 Initial Count n = 5 Written into Counter’s CR Initial Count n = 5 Loaded into Counter... Counter decrements till Terminal Count = 00000H Mode 2: Divide-by-n Counter (Programmable Frequency Clock) (Trigger) Note: In this mode G needs not be kept high for counting to be enabled OUT goes low At n loading written Into Counter n = 3 Regular Period (3 pulses = n) 2 1
44
Gate G = 1 always, to enable counting OUT frequency = Clock frequency/n OUT Duty cycle (ON:Total) = (1:2) for n even 1:2 for n odd and large Initial Count n = 5 Written into Counter’s CR Initial Count n = 5 Loaded into Counter... Counter decrements till Terminal Count = 00000H Mode 3: Divide-by-n Counter but with a Square Wave Output: (Trigger) Note: In this mode G needs not be kept high for counting to be enabled OUT goes low At n loading written Into Counter written Into Count Register of Counter (Fixed) Regular Period 4 2 2 7 4 3
45
Gate G = 1 always, to enable counting OUT gives an active low strobe pulse - Strobe pulse duration: Fixed at 1 clock interval - Delay from writing initial count into counter (software trigger) = (n+1) Tclk Useful for strobing data generated by processor into external devices, e.g. a printer Note similarity between modes 0 and 4 Mode 4: Software Triggered Strobe Output Pulse: written Into Count Register of counter 1
46
Gate G is used as a hardware trigger. Not needed high for counting OUT gives an active low strobe pulse - Pulse duration: 1 clock interval - Delay from writing initial count into counter (software trigger) = (n) Tclk Useful for strobing data generated by processor into external devices, e.g. a printer Note similarity between modes 1 and 5 Mode 5: Hardware Triggered Strobe Output Pulse: written Into Counter
47
Summary of G effect In the 6 counter modes Event Counter Hardware triggered One shot Divide by N Counter (Square Wave Output) Software triggered Strobe Hardware triggered Strobe 3 pairs of similar modes
48
Summary of the PIT 5 counter modes M2M1M0Mode #Function Specifications (n = pre-loaded initial count, Tclk = clock interval) 0000 Event Counter Delay to rising edge = (n+1) Tclk 0011 Hardware- Triggered One-Shot Width of negative going pulse = (n) Tclk X102 Divide-by-N Counter fout = fclk / (n) Duty cycle: Mark:Total = (n-1):n X113 Square- Wave Generator fout = fclk / (n) Duty cycle 50% (for all n) 1004 Software- Triggered Strobe Strobe width = Tclk Strobe Delay = (n+1) Tclk 1015 Hardware- Triggered Strobe Strobe width = Tclk Strobe Delay = (n) Tclk
49
8254 PIT Programming Example 8 MHz Clock Frequency 100 KHz Square Wave: Mode 3 (requires G = 1) f Out = f in /n n = 8000/100 = 80d 200 KHz non-square wave: Mode 2 (requires G = 1) f Out = f in /n n = 8000/200 = 40d G = 1 Permanently: Enable Counters 0 and 1 Addresses: Counter0: 700H Counter1: 702H Counter2: 704H Control Word: 706H (Even addresses) A0 is decoded as 0
50
;A procedure that programs the 8254 timer to function ;as illustrated in Figure 11-34 TIMEPROCNEAR USES AX DX MOV DX,706H ; address Control register MOV AL,00110110B ; program counter 0 for mode 3 ; Counter Load/Read format is ; 2 bytes (LS first) OUT DX,AL MOV AL,01110100B ;program counter 1 for mode 2 OUT DX,AL MOV DX,700H ; Address counter 0 MOV AL,80 ; Load initial count 80d into counter 0 ; LS byte of initial count OUT DX,AL MOV AL,0; ;Then MS byte of initial count OUT DX,AL MOV DX,702H ; Address counter 1 MOV AL,40 ; Load initial count 40d into counter 1 OUT DX,AL MOV AL,0;Then MS byte of initial count OUT DX,AL RET TIMEENDP Control Word Waveform from Counter 0 starts being generated here Waveform from Counter 1 starts being generated here Program
51
1 0 1 1 0 0 ON OFF ON DC Motor Speed and Direction Control Q 0 #Q 1 Rotation in Opposite Direction Q 1 #Q 0 Rotation in One Direction; e.g. clockwise The Q Output (#Q is the complement): No net motion with duty cycle = 1:2 Net motion in either direction With a duty cycle 1:2 Pulse Width Modulation for Speed Control: We use counter 0 and Counter 1: -Both driven by an 8 MHz clock -Both are divide-by-30,720 counters (Mode 2) (output repetition rate, f = 8 MHz / 30,720 = 260 Hz (motor spec requires: 60 Hz < f < 1000 Hz) - Bistable is cleared (Q=0) by the #CLR signal from counter 0 and preset (Q=1) by the #PS signal from counter 1 - Duty cycle is varied by controlling the timing of #PS relative to #CLR (Preset: Q 1) (Set bistable) (Clear: Q 0) Set-Reset Bistable (Reset bistable
52
Duty Cycle Motor Speed Control The Q Output (#Q is the complement) duty cycle Controls motion: No net motion with duty cycle = 1:2 Net motion in either direction With a duty cycle 1:2 Pulse Width Modulation (Speed Control): Duty Cycle: >1:2 Count < (30,720)/2 OUT of Counter 1 (mode 2) OUT of Counter 0 (mode 2) Wait for Counter 1 to reach this count and start Counter 0 This is done only once and the system is left to free-run until a different speed is required Duty Cycle = 1:2 Count = (30,720)/2 Duty Cycle: < 1:2 Count > (30,720)/2 Assume 256 different speed control settings: Represented by byte variable in AH = 128 for no motion (1:2), = 0 -127: Speed in one direction (<1:2). 0: Maximum speed in that direction = 129 – 255: Speed in the other direction direction (>1:2), 255: Maximum speed in that direction -Increment Count for each speed control setting = 30720/256 = 120 e.g. for AH = 128: Delay count = 128 x 120 = 15360 (as seen above for no net motion (2:1)) Width = ? AH = 0 AH = 255 AH = 128 Waiting Count = 30720 – AH*120 0 128255 AH Speed - max + max
53
= AH x 120 = 128 x 120 230 x 120 25 x 120 From Counter 1 From Counter 0 Waiting Time = 30720 – AH * 120 Approx same speed but in opposite directions
54
;A procedure that controls the speed and direction of the motor ;in Figure 11-40. ; ;AH determines the speed and direction of the motor where ;AH is between 00H and FFH. CNTREQU706H; PIT Control Word CNT0EQU700H CNT1EQU702H COUNTEQU30720 SPEEDPROCNEAR USES BX DX AX MOV BL,AH;calculate count corresponding to AH: AH has speed control byte (0 128 255) MOV AX,120 MUL BL; Multiply AH (speed input) by 120 MOV BX,AX; result in AX, save in BX (BX has AH x 120) MOV AX,COUNT SUB AX,BX MOV BX,AX ; Subtract from 30720, Now BX has 30720 – AH x 120 = waiting count MOV DX,CNTR MOV AL,00110100B; program control word OUT DX,AL; for counter 0: Binary, Mode 2, 2 bytes R/W MOV AL,01110100B; same for counter 1 OUT DX,AL; but do not start it yet by loading COUNT- do this after waiting time MOV DX,CNT1 ; program counter 1 to generate the clear (#CLR) signal for Q (free-running) MOV AX,COUNT; OUT DX,AL; LS byte of 30720 first MOV AL,AH OUT DX,AL; then MS byte.REPEAT; wait for counter 1 count to reach Waiting Count in BX IN AL,DX; Read LS byte of counter 1 (goes as AL) XCHG AL,AH; Put it in AH IN AL,DX; Read MS byte of counter 1 (goes as AL) XCHG AL,AH; swap AL and AH to put things back to order.UNTIL BX == AX MOV DX,CNT0;program counter 0 MOV AX,COUNT; to generate a set (#PS) for Q (free-running) after that waiting ; delay by Counter 1. Note you also load COUNT as with Counter 1 OUT DX,AL; Actual outputting LS byte then MS byte MOV AL,AH OUT DX,AL RET SPEEDENDP For Control Reg Always LS then MS Counter 0 starts free running at 260 Hz, delayed by waiting count from the start of Counter 1 Counter 0 starts, free running at 260 Hz, Repeatedly read counter 1 Until it reaches “Waiting Count”
55
The 16550 UART Universal Asynchronous Receiver Transmitter Baud rates up to 1.5 M bauds (signal elements/s) = Data rate (bps) for binary data Compatible with Intel and other Processors Includes: - A programmable baud rate generator - 16-byte FIFO buffers at input and output to help processor deal with data bursts WK 12
56
Asynchronous Serial Data Communication Data sent asynchronously using the format illustrated below We often use one start bit and one stop bit to frame the data, which is usually 8-data bits with or without parity Usually a byte of data
57
The 16550 UART: Functional Description 40 pin DIP Totally independent Transmitter (TX) and Receiver (RX) Sections This allows communication in the following modes: - Simplex: Only TX or RX is used (one direction all the time) - Half Duplex: TX then RX (two directions at different times) - Full Duplex: TX and RX simultaneously (two directions at the same time) Can control a modem using six signals, e.g. #DSR (Data Set Ready) input, #DTR (Data Terminal Ready) output…. Here the UART is the data terminal and modem is the dataset.
58
The 16550 UART: Typical Configuration UART PP Control Receiver Transmitter Serial Comm. Link SIN SOUT Memory Data DMA Data Transfers: Memory UART Directly Without going through the P 16-byte FIFO Input Buffer 16-byte FIFO Output Buffer PSPS PSPS Serial to Parallel Or Parallel to Serial Converters
59
The 16550 UART: Pin Assignments 40 pin DIP 3 I/O Address bits from Processor (Table 11-5) Baud rate Clock output Chip Select Inputs (Multiple I/Ps) Modem Interface: Inputs & Outputs Data bus to Processor Interrupt Processor Master Reset (tie to P Reset I/P) Serial data INput from RX Serial data OUTput to TX Receiver Clock input Read & Write Control inputs from P (with complements for versatility User defined outputs Crystal or External Clock Input TX ready for data. Put data into UART by DMA RX ready with data. Take data from UART by DMA Address Strobe (not needed with Intels)
60
UARTs in the PC Used to control the COM ports of the PC - UART at I/O address 3F8-3FF: COM Port 0 - UART at I/O address 2F8-2FF: COM Port 2
61
Programming the UART Two Stages: a. Initialization Dialog: (Setup) - Follows RESET - Has two steps: 1. Program the line control register (Set asynchronous transmission parameters: # of stop, data, and parity bits, etc.) 2. Program the baud rate generator for the required baud rate b. Operation Dialog: (Actual Communication)
62
A2A1A0Function 000Receiver buffer (read data from RX) and transmitter holding (write data to TX). Also write LS byte of baud rate divisor 001Interrupt enable. Also write MS byte of baud rate divisor 010Interrupt identification (read) and FIFO control Register (write) - Used for operation dialog programming 011Line control Register (Write into the line control register to program asynchronous communication at initialization) 100Modem control 101Line status LSTAT (Read the line status register to see if TX or RX are ready and to check for errors ) 110Modem status 111Scratch The 8 I/O Byte Locations on the UART
63
1. Programming the Line Control Register I/O Address: A2 A1 A0 = 011 Data Length = 5 bits Data Length > 5 bits Parity Control See next slide To allow programming The baud rate generator See Table on next slide A break is a minimum of 2 frames of 0’s a. Initialization Dialog Programming DL bit must be set before you can load the divisor for the baud generator
64
STPPEFunction 000No parity 001Odd parity 010No parity 011Even parity 100Undefined 101Send/receive 1 (send 1 in place of the parity bit) 110Undefined 111Send/receive 0 (send 0 in place of the parity bit) The 3 Parity Control Bits in the Line Control Register
65
Baud RateDivisor Value 11010,473 3003840 1200920 2400480 4800240 9600120 19,20060 38,40030 57,60020 115,20010 Baud rate is programmed by loading a 16-bit divisor for the crystal oscillator (or external input) frequency into the I/O port addresses: {A2 A1 A0} = 000: LS Byte of divisor {A2 A1 A0} = 001: MS Byte of divisor Divisor value is determined by the Oscillator frequency and the baud rate required: Divisor = Oscillator frequency / (16 * Baud rate) Table shows divisor values required for various baud rates for osc frequency = 18.432 MHz 2. Programming the Baud rate Generator
66
(Active Low)
67
;Initialization dialog for Figure 11-45 ;Baud rate 9600, 7 bit data, odd parity, 1 stop bit LINEEQU0F3H; A2 A1 A0 = 011 for the Line Control Register LSBEQU0F0H; A2 A1 A0 = 000 for LSB of divisor MSBEQU0F1H; A2 A1 A0 = 001 for MSB of divisor FIFOEQU0F2H; A2 A1 A0 = 010 for the FIFO Control Register INITPROCNEAR MOV AL,10001010B OUT LINE,AL ; Enable Baud rate programming See slide 108 ; program Baud 9600 ; Divisor = 120d (see Table on slide 110) MOV AL,120; LSB of divisor OUT LSB,AL MOV AL,0; MS Byte of divisor OUT MSB,AL MOV AL,00001010B;program 7 bit data, odd OUT LINE,AL;parity, 1 stop bit ;(& disable baud rate programming?) MOV AL,00000111B;enable transmitter and receiver OUT FIFO,AL;by writing into the FIFO control Reg. RET INITENDP Must write this into FIFO Register to enable communication and operation dialog programming
68
16550 FIFO Control Register (Write) 1 1 1 I/O Address: A2 A1 A0 = 010 Required to enable actual communication (Operation Dialog)
69
16550 Line Status Register (LSTAT) I/O Address: A2 A1 A0 = 101 Before writing data for transmission, Ensure TX is ready to take it [TH (bit 5) = 1] Before reading data from receiver, ensure RX has data [DR (bit 1) = 1] Error status bits Any being 1 indicates An error b. Operation Dialog Programming
70
;A procedure that transmits the byte in AH serially ;via the 16650 UART LSTATEQU0F5H; The Line status register (LSTAT) (A2 A1 A0 = 101) DATAEQU0F0H; TX/RX Data Register at (A2 A1 A0 = 000) SENDPROCNEAR USES AX.REPEAT ;test the TH bit (bit 5) in to see if TX is available IN AL,LSTAT TEST AL,20H;20H is the mask for the TH bit.UNTIL !ZERO? MOV AL,AH OUT DATA,AL ;send data to TX RET SENDENDP (LSTAT)
71
; Procedure receives byte from UART into AL if no comm. error ; If error detected, it load Al with ‘?’ as an alert LSTATEQU0F5H ; The Line status register (LSTAT) (A2 A1 A0 = 101) DATAEQU0F0H ; TX/RX Data Register at (A2 A1 A0 = 000) REVCPROCNEAR.REPEAT IN AL,LSTAT;test DR bit TEST AL,1.UNTIL !ZERO? TEST AL,0EH;test for any error.IF ZERO?;no error IN AL,DATA;Read RX Data Register into AL.ELSE;any error MOV AL,’?’;Put “?” in AL to indicate error.ENDIF RET RECVENDP
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.