Wednesday, September 29, 2010

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

1 comment:

  1. i have so many doubts in pic can you help me
    i am a btech student i want to do a project with pic but i dont know how to do it let me help.....
    my email id:shabeebparambil@gmail.com
    shabeebmoosa@yahoo.com

    ReplyDelete