Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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
**broken link removed**

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?
 
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)
 
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)
 
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.

New Articles From Microcontroller Tips

Back
Top