# Explain this to me?

Status
Not open for further replies.

#### Squintz

##### New Member
;****Set up the port****
Code:
            bsf                STATUS,5       ;Switch to Bank 1
movlw              00h            ;Set the Port A pins
movwf              TRISA          ;to output.
bcf                STATUS,5       ;Switch back to Bank 0
movlw              02h            ;Set up our w register with 02h
This is part of the code from a tutorial found
http://www.mstracey.btinternet.co.uk/pictutorial/progtut7.htm

this is what iv gathered from this tutorial. To set up a port you have to switch to bank1 the enter the binary or hex numer for which bits you want on. In this case they want bit 2 of port a on. so the decimal val would be 00010 or 02h and that has to be place in the general register first and the that value is moved to the TRISA register. now TRISA holds the value of which ports are input and which are output.

Why does this code seam to set it to 00h first then go back and change it to 02h? Why not just set it to 02h in the first place?

#### Exo

##### Active Member
Firstly you need to know that W is the main working register
and variables are called files

Code:
        bsf           STATUS,5       ;Bit Set File : Set bit 5 in File STATUS (switch to bank 1)
movlw         00h            ;MOVe Literal to W : Moves a constant value (00h) into W
movwf         TRISA          ;MOVe W to File: Moves the value in W (still 00h) into TRISA
;This makes all pins on portA outputs, if you were to write
;FFh (11111111b) to it all pins would be outputs
bcf           STATUS,5       ;Bit Clear File: Clears bit 5 in file Status (switch to bank 0)

movlw         02h             ;Move literal 02h to W
xorwf         PORTA,1         ;eXclusive OR W with File: performs an exclusive OR
;between the value in PORTA and the Value in W, the 1 indicates
;where to save the result (1 means save it to the file PORTA)
You could also use a F or W to indicate the position to save: for example
XORWF PORTA, W ;save the result to W
XORWF PORTA, F ;save the result to File (portA)

#### Squintz

##### New Member
im not understanding.

the movlw 00h is still throwing me off.

can you explain each step in detail

#### Exo

##### Active Member
edited the previous post to give a step-by-step explanation
(best viewed in a high resolution, otherwise the format seems to get screwed up)

#### Squintz

##### New Member
Oh ok...So by setting the everything to 0 the output can be exored with the number 1 or 0 to turn the output on and off

00 off
01 on
10 on
11 off

so what would i do do make it and input and turn the input on and off?

#### Exo

##### Active Member
To make a pin an input, write a 1 to the corresponding bit in the tris register.

So for example:
-all pins inputs
MOVLW 0xFF (binairy 11111111)
MOVWF TRISA

-pin A0 input, pin A1 output, pin A2 input,....
MOVLW b'01010101'
MOVWF TRISA

a input pin moves the value on the pin to the PORT register
so, if A0 is high (5V) then PORTA bit 0 will be set...

Status
Not open for further replies.