Tuesday, October 30, 2007

CONTROLLING THE PORT OF PIC16F84

Preface







Microcontroller capable to control everything. The limitation is how we can write program for microcontroller. The program is an instruction that send to microcontroler and will be save on ROM ( Flash Memory ) on the microcontroller. Microcontroller will do as exactly as the program.
Below is the Work Flow of microcontroller.











Figure 1. Microcontroller’s work flow


Microcontroller will receive signal from input line and will processed this signal base on program on it’s Flash memory. After finished processed, it will send signal to the output line. In actual conditions, microcontroller have ports that can be made as input ports or output ports. Microcontroller ports normally called as I/O ports.
As the name of I/O ( Input/Output ) ports, each port have a bi-directional capability that’s mean each port individually can be input port or output port.





Figure 2 Two Ports of the PIC16F84


PIC16F84 have 2 ports that have bi-directional capability on each line. Port A have 5 lines named RA0,RA1,RA2,RA3,RA4. Port B have 8 lines named RB0,RB1,RB2,RB3,RB4,RB5,RB6,RB7.



Figure 3 Name of each port on Port A and Port B

Controlling the Port
Before we can use the port, we must tell to the microcontroller which port as input port and which port as output port. To do so, we must understand register that controlling the port.



List 1 Register that controlling the port
Register Name Purpose Address
PORTA Content of Port A 05 H
TRISA Direction of Port A 85 H
PORTB Content of Port B 06 H
TRISB Direction of Port B 86 H

Note :
A “1” on PORTA / PORTB means that the line has “High Logic” or +5 Volt.
A “0” on PORTA / PORT B means that the line has “Low Logic” or ground ( 0 Volt)

A “1” on TRISA / TRISB means that the line become “Input Line”
A “0” on TRISA / TRISB means that the line become “Output Line”


Suppose we want to make condition on PORTA as follow.
RA0 = input line
RA1 = output line
RA2 = input line
RA3 = output line
RA4 = output line




Figure 4. Example Conditions of PORTA


To make that conditions, we must adjust register ( using instruction as stated on assembly language ) as follow.

;--------------------------------------------------------------------------
; program name : sample.asm
; by : your name
; date :
;--------------------------------------------------------------------------
processor 16f84 ; to tell the compiler , processor used

porta equ 05h ; equivalency porta = 05h
trisa equ 85h ; equivalency trisa = 85h
status equ 03h ; equivalency status = 03h
rp0 equ 05 ; equicalency bit rp0 (inside status ) = 05

org 0h ; start program locations

initialize
bsf status,rp0
movlw b’00101’
movwf trisa
bcf status,rp0


next
bla…
bla…


This instruction will make microcontroller same as what we want. Here’s the explanations.



a. Instructions : bsf status,rp0 ; ( register = status, bit = rp0 )
This instructions mean “bit set file register”
The bit in that instruction is “rp0” ( bit 5th of register status )
This instruction make us will go to Bank1 to allow change TRISA register

b. Instruction : movlw b’00101’
This instructions mean “move literal to W register”
This register will make W = 00101 B (Binary)

Why we use b’00101’ ? This is as we want to control port a directions!

RA4 RA3 RA2 RA1 RA0
Output Output Input Output Input
0 0 1 0 1

c. Instruction : movwf trisa
This instruction will copy content of register W to TrisA
After executed trisa will content = 00101B (Binary)

d. Instruction : bcf status.rp0
This instruction mean “ bit clear file register” in that case bit rp0 in the register status will be cleared to low (“0”). This instruction will make us go back to Bank 0 after chang trisa register.

The same action also can be done to PORTB and TRISB


Download PDF

No comments: