Αστέρια ΑνενεργάΑστέρια ΑνενεργάΑστέρια ΑνενεργάΑστέρια ΑνενεργάΑστέρια Ανενεργά
 

Writing To the Ports.

In the last tutorial, we I showed you how to set up the IO port pins on the PIC to be either input or output.  In this tutorial, I am going to show you how to send data to the ports.  In the next tutorial, we will finish off by flashing an LED on and off which will include a full program listing and a simple circuit diagram so that you can see the PIC doing exactly what we expect it to.  Don’t try and compile and program your PIC with the listings here, as they are examples only.

First, let us set up Port A bit 2 as an output:

bsf                    03h,5              ;Go to Bank 1
movlw              00h                  ;Put 00000 into W
movwf              85h                  ;Move 00000 onto TRISA – all pins set to output
bcf                   03h,5               ;Come back to Bank 0

This should be familiar from the last tutorial.  The only difference is that I have set all of the pins on Port A as output, by sending 0h to the tri-state register.

Now what he have to do is turn an LED on.  We do this by making one of the pins (the one with the LED connected to it) high.  In other words, we send a ‘1’ to the pin.  This is how it’s done (note the comments for an explanation of each line):

movlw              02h      ;Write 02h to the W register.  In binary this is 00010, which
                                     ;puts a ‘1’ on bit 2 (pin 18) while keeping the other pins to ‘0’

movwf              05h      ;Now move the contents of W (02h) onto the PortA, whose
                                     ;address is 05h

So, now our LED is on, we now need to turn it off:

movlw              00h      ;Write 00h to the W register.  This puts a ‘0’ on all pins.

movwf              05h      ;Now move the contents of W (0h) onto the Port A, whose
                                     ;address is 05h

So, what we have done is turn the LED on then off once.

What we want is for the LED to turn on then off continuously. We do this by getting the program to go back to the beginning.  We do this by first defining a label at the start of our program, and then telling the program to keep going back there. 

We define a label very simply.  We type a name, say START, then type the code:

Start                movlw              02h      ;Write 02h to the W register.  In binary this is
                                                              ;00010, which puts a ‘1’ on pin 2 while keeping
                                                              ;the other pins to ‘0’

movwf              05h      ;Now move the contents of W (02h) onto the
                                                 ;PortA, whose address is 05h

movlw              00h      ;Write 00h to the W register.  This puts a ‘0’ on
                                                 ;all pins.

movwf              05h      ;Now move the contents of W (0h) onto the Port
                                                 ;A, whose address is 05h

                                            goto               Start      ;Goto where we say Start

As you can see, we first said the word ‘Start’ right at the beginning of the program.  Then, right at the very end of the program we simply said ‘goto Start’.  The ‘goto’ instruction does exactly what it says.

This program will continuously turn the LED on and off as soon as we power up the circuit, and will stop when we remove power.

I think we should look at our program again:

             bsf                    03h,5
                         movlw              00h
                         movwf              85h
                         bcf                   03h,5
Start                 movlw              02h
                         movwf              05h
                         movlw              00h
                         movwf              05h
                         goto                 Start

OK, I know I have left the comments off.  But, do you notice that all we can see are instructions and numbers?  This can be a little confusing if you are trying to debug the program later, and also when you write the code you have to remember all of the addresses.  Even with the comments in place, it can get a bit messy.  What we need is to give these numbers names.  This is accomplished by another instruction: ‘equ’.

The ‘equ’ instruction simply means something equals something else.  It is not an instruction for the PIC, but for the assembler.  With this instruction we can assign a name to a register address location, or in programming terms assign a constant.  Let us set up some constants for our program, then you will see how much easier to read the program is.

STATUS          equ  03h           ;this assigns the word STATUS to the value of 03h,
                                                   ;which is the address of the STATUS register.

TRISA             equ 85h            ;This assigns the word TRISA to the value of 85h,
                                                  ;which is the address of the Tri-State register for PortA

PORTA           equ 05h            ;This assigns the word PORTA to 05h which is the
                                                  ;address of Port A.

So, now we have set up our constant values, let us put these into our program.  The constant values must be defined before we can use them, so to be sure always put them at the start of the program.  I will re-write the program without comments again, so that you can compare the previous listing to the new one:

STATUS          equ 03h
TRISA              equ 85h
PORTA            equ 05h

                         bsf                    STATUS,5
                         movlw              00h
                         movwf              TRISA
                         bcf                   STATUS,5

Start                 movlw              02h
                         movwf              PORTA
                         movlw              00h
                         movwf              PORTA
                         goto                 Start

Hopefully, you can see that the constants make following the program a little easier, even though we still have not put the comments in.  However, we are not quite finished.

  

Προσθήκη νέου σχολίου


Κωδικός ασφαλείας
Ανανέωση