Wednesday, October 31, 2007

UNDERSTANDING THE REGISTERS

What is the register?






Register is a memory inside of microcontroller that have a specific function. Register can be read read from or written to. By written to the register, mean we are still controlling some function of the microcontroller. For Example, Port A controlled by 2 register. There are register PORTA and register TRISA. More complex a microcontroller, more register needed to control inside the microcontroller.

Microcontroller PIC16F84/A have several registers. There are divided into 2 location called Bank. Bank 0 and Bank 1. Bank 0 is use to manipulate data and bank 1 is use to control the actual operation of the microcontroler. To change location from Bank 0 to Bank 1, simply by change bit RP0 ( bit 5th of register STATUS ).

RP0 = 0 --> Goto Bank 0
RP0 = 1 --> Goto Bank1




Figure 1 Registers of PIC16F84/A



Here is some information about the register on microcontroller PIC16F84.

W Register

W Register is a "Working" Register. On another type of microcontroller usually called as "Accumulator". This register is common register to use in arithmatic operations. We can put any value on this register. Also we can moved it's content to another registers. We can also add the content of W register with another value and save in the same register. W register doesn't have address.

PORTA Register
PORTA have address of 05H ( hex ). PORTA is a register that control port A. To make one of output pins high on port A, simply by send "1" to corresponding bit on PORTA. To make one of output pins low on port A, simply by send "0" to corresponding bit on PORTA. Port A is a 5 bits wide. Each line have a name, there are RA0, RA1, RA2, RA3, and RA4. RA4 also have another function for input to Timer/Counter.


1 --> PORTA ---> port A High ( +5 Volts )
0 --> PORTA ---> port A Low ( 0 Volt / ground )

PORTB Register
PORTB have address of 06H ( hex ). PORTB is register that control port B. To make one of output pins high on port B, simply by send "1" to corresponding bit on PORT B. To make one of output pins low on port B, simply by send "0" to corresponding bit on PORTB. Port B is a 8 bits wide. Each line have a name, there are RB0, RB1, RB2, RB3, RB4, RB5, RB6, and RB7. RB6 also use as "Clock" input during programming. RB7 also use as "Data" input during programming.

1 --> PORTB --> port B High ( + 5 Volts )
0 --> PORTB --> port B Low ( 0 Volt / ground )

TRISA Register
TRISA have address of 85H ( hex ). This register have functions to control the direction of port A. With this register, we can assign port as input port or output port. To become input port, simply by send "1" to corresponding bit on TRISA. To become output port, simply by send "0" to corresponding bits on TRISA.
On Port A we have 5 pins ( lines ) hence 5 bits.

1 --> TRISA -- > Input Port
0 --> TRISA --> Output Port

TRISB Register
TRISB have address of 86H (hex ). This register have functions to control the direction of port B. With this register, we can assign port as input port or output port. Just same as port a with TRISA. On port B we have 8 pins ( lines ) hence 8 bits.

1 --> TRISB --> Input Port
0 --> TRISB - > Output Port


For Example we want Port B have direction as Follow. RB0 ~ RB3 as input port and RB4 ~ RB7 as output port. So we must assign TRISB with the value of :

RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
out out out out in in in in
0 0 0 0 1 1 1 1 Binary

or 0F H (hex )

Another example we want Port A have direction as follow. RA0 = input, RA1 = output, RA2 = input, RA3= input and RA4 = output So we must assign TRISA with th value of :

RA4 RA3 RA2 RA1 RA0
out in in out in
0 1 1 0 1 Binary.

or 0D H ( Hex )

STATUS Register ( Address : 03 H )
STATUS Register have many functions. But, for this time we just want to explain functions of STATUS register to change from Bank 0 to Bank 1 or vice versa. Bit 5th of STATUS Register is Bit RP0. This bits togeter with RP1 is use to control Bank.

Bank Location Bit RP1 Bit RP0
Bank 0 0 0
Bank 1 0 1

From above explanation, we understand that only bit RP0 is used to control bank selections. Because there are only 2 Banks so bit RP1 not use ( keep "0" ).

Bank Location Bit RP0
Bank 0 0
Bank 1 1


An Example Code
We want to make Port A as input port and Port B as output port.

Here's the code.


bsf STATUS,RP0 ; bit set RP0 ( make RP0 = 1 ) to go to Bank 1
movlw b'11111' ; RA0 to RA4 --> "1" for input port
movwf TRISA ; copy contetn W register to TRISA register

movlw b'00000000' ; RB0 to RB7 -- > "0" for output port
movwf TRISB ; copy content of W register to TRISB register

bcf STATUS,RP0 ; bit clear RP0 ( make RP0 = 0 ) to go back to Bank 0


Download PDF

No comments: