Вы находитесь на странице: 1из 30

I/O PORTS

In computer hardware, a port serves as an interface between the computer and other computers or peripheral devices. Physically, a port is a specialized outlet on a piece of equipment to which a plug or cable connects. Electronically, the several conductors making up the outlet provide a signal transfer between devices. The term "port" is derived from a Dutch word "poort" meaning gate, entrance or door.

Architecture 16F877A

PORTS in PIC

PORTS in PIC microcontroller The PIC16F877A have the following ports:


PORT A has 6 bit wide, Bidirectional - can be programmed as input or output PORT B,C,D have 8 bit wide, Bidirectional - can be programmed as input or output PORT D - can be programmed as PSP with the help of TRIS E PORT E has 3 bit wide, Bidirectional - can be programmed as input or output Ports have 2 control registers 1. TRIS x sets whether each pin is an input (1) or output (0) 2. PORT x sets their output bit levels or contain their input bit levels

Structure of Bidirectional pin - PORT B pin

PORT A, B, C, D* and E pin configuration

Configuring as output pin Configuring as input pin Knowing the status of the pin Weak pull up RBPU

* Port D can also be programmed as PSP

Configuring as output pin - Output operation Data on the data bus should go to the pin i.e. From PIC to I/O pin Databus 0 WR TRIS high to low TRIS latch L2 outputs the 0 Buffer B2 gets enabled, output of B1 high and PMOS is off (pull up disabled). Data bus data(0 or 1) WRPORT high to low Data Latch L1 outputs the data (0 or 1) Since buffer B2 is enabled data goes to I/O pin.

Timing diagram - Output operation

Configuring as input pin - Input operation


Data appearing on the I/O pin should go to the data bus ie. Peripherals (I/O devices) to PIC

Data bus 1 WRTRIS high to low TRIS latch L2 outputs 1 Buffer B2 gets disabled RD port low for small period Latch L3 gets enabled and gets connected to B3 RD port high so that B3 is enabled and data is available on data bus

Timing diagram - Input operation

Configuring as input / output port B (an example)

Relay

Speed sensor

LED

Switch

Knowing the status of the pin Input or Output


Status of TRIS register is read to know the direction of data flow

RDTRIS high Output of L2 is connected to data bus, indicates whether the port bit is connected to an output device (if its value 0) or connected to input device (if its value 1).

Weak pull up - RBPU

The wire connected to a pin in input mode may not be connected to the source (floating).

It can pick up any stray signals and cause damage to the IC.

A resistance may be connected to that pin---pull up.


A PMOS transistor kept in active mode can act as a pull up. This option is exercised by RBPU = 0.

Weak pull up - Example

Parallel Slave Port - PORT D

Configuring PSP

PORTE Data Direction Bits:


bit 2 Bit 2: Direction Control bit for pin RE2/CS/AN7 1 = Input 0 = Output bit 1 Bit 1: Direction Control bit for pin RE1/WR/AN6 1 = Input 0 = Output bit 0 Bit 0: Direction Control bit for pin RE0/RD/AN5 1 = Input 0 = Output

Configuring PSP
Parallel Slave Port Status/Control Bits:
bit 4 PSPMODE: Parallel Slave Port Mode Select bit 1 = PORTD functions in Parallel Slave Port mode 0 = PORTD functions in general purpose I/O mode bit 3 Unimplemented: Read as 0 bit 7 IBF: Input Buffer Full Status bit 1 = A word has been received and is waiting to be read by the CPU 0 = No word has been received bit 6 OBF: Output Buffer Full Status bit 1 = The output buffer still holds a previously written word 0 = The output buffer has been read bit 5 IBOV: Input Buffer Overflow Detect bit (in Microprocessor mode) 1 = A write occurred when a previously input word has not been read (must be cleared in software) 0 = No overflow occurred

Configuring PSP

READ and WRITE operations in PSP


WRITE operation I/O device writing into PIC CS 0 WR 0, RD 1 RD port high Data from pin goes to data bus READ operation I/O device is reading from PIC CS 0 RD 0, WR 1 Data from PIC will be there in data bus WR port high to low Data from data bus goes to pin

Registers involved in PSP mode

PORT E register

Q#1
Write a program to configure alternate pins of PORT B as input and output pins as shown below. After a delay of 1 msec interchange the configuration.

PORT B

PORT B

Answer Q#1 movlw 0xAA movwf TRISB call delay (1 m sec) movlw 0x55 movwf TRISB call delay (1 m sec)

Q#2 PORT C is connected to anodes of 8 leds through a current limiting resistor and cathodes are shorted and connected to ground. Write a program to turn on LED s one by one from pin 0 to pin 7. Each led should glow for a period of 2 m sec before lighting the next one.

Answer Q#2 Movlw 0x00 Movwf TRISC Start: Bsf PORTC,0 Call delay (2 m sec) Clrf PORTC Bsf PORTC,1 Call delay (2 m sec) Clrf PORTC . . . goto start

Q#3 Write a program to get the data present in pins of PORT C and PORT B send it to port D after adding them (Assume that the sum is always 8 bit only).

Answer Q#3

Banksel TRISA Movlw 0xFF Movwf TRISB.configure PORTB Movwf TRISC..configure PORTC Movlw 0x00 Movwf TRISD Banksel PORTA Movf PORTC,0get the value from PORTC to WREG Addwf PORTB,0 Movwf PORTDmove the added value to PORTB end

Q#4 A door alarm is to be controlled using PIC. It has to work as follows. When the door gets opened, a buzzer needs to be on A sensor which is connected to the door gives out a low signal, when the door gets opened and the Buzzer is made on, by giving a low to high pulse to it and it should be on for 3 m sec only. To turn of, it requires a high to low pulse. Write a program to set the alarm when the door gets opened, if the sensor is connected to RB3 and the buzzer is connected to RC5.

Answer Q#4 Bsf TRISB,3 Bcf TRISC,5 Btfsc PORTB,3 Goto here Bcf PORTC,5 Bsf PORTC,5 call delay (3 m sec) Goto here

here:

Q#5 Write a program to check whether the key is pressed or not in the circuit shown below. If the key is pressed light the led for 2 msec. Repeat the procedure.

Answer Q#5
goto main delay: movlw count movwf countreg rep: decfsz countreg goto rep return
main: movlw 0x10 movwf TRISB movwf PORTB again: btfsc PORTB,4 Goto again bsf PORTB,6 call delay goto again end

IC TD=LD+O LD 1 2 m= [(3*(count-1)) + 8] 4 * 1 1 1/ 2 Count = 163 Decimal = 0xA3 Hex 2 2

Вам также может понравиться