Friday, March 10, 2006

PIC: Example 5

Example 5

Using subroutine, using push-buttons

In the previous examples the microcontroller executes the program without being influenced in any way its surrounding. In practice, devices operating in this way are very rare (for example, simple neon signs). You guess, among other components, input pins will also be used in this example. There is a schematic in the figure below, while the program is on the next page. Everything is still very simple.
Example 5 - Using subroutine, using push-buttons
At the beginning of the program, immediately upon defining variables, the microcontroller pins* are configured by using registers TRISA and TRISB.
In the main program, one bit on port B is set first. Then the contents of this register is constantly moved by one place to the left (instruction rlf PORTB). It gives us the impression that the lit LED diodes is moving. To make it visible, the whole process must be slow enough. Press on the push-button "STOP" stops the movement and the program remains in loop3. Delay is provided by means of a nested loop. This time, it is placed in a short subroutine "DELAY".
* It is not necessary for PORTA pins since they are automatically configured as inputs after every reset.

Source Code

;****************************************************************************
;                    Header
;****************************************************************************
;************* DEFINING VARIABLES *******************************************

       cblock      0x20            ; Block of variables starts at address 20h
       counter1                    ; Variable "counter1" at address 20h
       counter2                    ; Variable "counter2" at address 21h
       endc                        ; Block of variables ends
       
;************************ MAIN PROGRAM **************************************

       org         0x0000          ; Address of the first program instruction
       banksel     ANSEL           ; Selects bank containing register ANSEL
       clrf        ANSEL           ; Clears registers ANSEL and ANSELH to
       clrf        ANSELH          ; configure all inputs as digital

       banksel     TRISB           ; Selects bank containing register TRISB
       clrf        TRISB           ; All port B pins are configured as outputs
       movlw       B'00000010'
       movwf       TRISA           ; Pin RA1 is input
       
       banksel     PORTB           ; Selects bank containing register TRISB
       movlw       B'00000001'     ; Writes 1 to register W
       movwf       PORTB           ; Number is moved to PORTB
loop
       rlf         PORTB           ; Port B bits rotates by one place left
       call        DELAY           ; Calls subroutine "DELAY"
loop3
       btfss       PORTA,1         ; Tests the firs port A bit
       goto        loop3           ; "0" is applied to pin.Go to label "loop3"
       goto        loop            ; "1" is applied to pin.Go to label "loop"
       
;************************ SUBROUTINES ***************************************
DELAY
       clrf        counter2        ; Clears variable "counter2"
loop1
       clrf        counter1        ; Clears variable "counter1"
loop2
       decfsz      counter1        ; Decrements variable "counter1" by 1
       goto        loop2           ; Result is not 0. Go to label loop2
       decfsz      counter2        ; Decrements variable "counter2" by 1
       goto        loop1           ; Result is not 0. Go to lab loop1
       return                      ; Return from subroutine "DELAY"

       end                         ; End of program