High

writes a high (1) to the specified pin.

This is the equivalent of the assembly-language command setb pin, where pin is the bit address of the pin. PBASIC also changes the pin's data direction to output by writing a 0 to the appropriate bit of the TRIS register. (In assembly, a 0 in TRIS means makes a pin an output; a 1 makes it an input. This is the opposite of the BASIC Stamp.) The assembly-language example here changes only the output latch, not TRIS.

Of course, using setb means that you cannot choose which pin to write while the program is running. The pin address is coded as part of the machine-language instruction. The accompanying program listing is a port- and pin-independent version of setb. Write the pin number to pin, the port number to w, and call High.

High, like the other routines that accept pin and port arguments, requires the short table Pinz. Remember that tables must be located in the first 256 words of a 512-word program memory page.

One hint on using High: always assign the port number to w immediately before calling the routine. If you don't, some other instruction that uses w may change its contents, causing an error.

Demonstrating High.

To see High in operation, either run the program with the PSIM simulator, or connect the circuit below to an erasable PIC or PIC emulator, such as the Parallax downloader. Assemble and run HIGH.SRC. When you apply power to the PIC, the LEDs will light up one at a time until all are lit.


; HIGH port (in w), pin
; Outputs a high to the specified port and pin.

        org     8
pin     ds      1       ; Pin number to set (0-7).

; Variables for the demo program--not required by High.
p_index         ds      1       ; Copy of pin number, port number.
temp    ds      1       ; Temporary variables for time delay
temp2   ds      1       ; between outputs.

; Device data and reset vector
        device  pic16c55,xt_osc,wdt_off,protect_off
        reset   start
        org     0

; Table to convert pin number (0-7) into bit mask (00000001b to 10000000b).
Pinz    jmp     pc+w
        retw    1,2,4,8,16,32,64,128

; The following demonstration code should show the value of being able to
; set a particular bit of a port based on a calculation performed while the
; program is running. As the variable p_index is incremented from 0 to 15,
; it is used to specify the pin and port to set using High.

start   mov     !rb, #0 ; All outputs on rb.
        mov     !rc, #0 ; All outputs on rc.
        clr     rb      ; Start with 0s on rb, rc (all LEDs off).
        clr     rc
        clr     p_index ; Clear p_index.
:loop   mov     pin,#7  ; Copy three lsbs of p_index into
        AND     pin,p_index     ; variable pin.
        mov     w,#1    ; IF p_index.3 = 0
        snb     p_index.3       ; THEN w = 1
        mov     w,#2    ; ELSE w = 2
        call    High
        call    delay   ; Wait a while between highs. 
        inc     p_index ; Next pin/port.
        sb      p_index.4       ; IF p_index < 15 (rc.7)
        jmp     :loop   ; THEN :loop ELSE done
done    jmp     $       ; Endless loop.


; HIGH (cont)
; To use the routine High, put the pin number into the variable pin
; and the port number into w. Then call High. 

High    mov     fsr,w   ; Point to the port number.
        ADD     fsr,#RA ; Add offset for port RA.
        mov     w,pin
        call    Pinz    ; Get bit mask from the table.
        OR      indirect,w      ; Turn on the selected bit
        ret

; Delay routine for demonstration. Not required by High.

delay   djnz    temp,delay      ; Time delay to provide spacing
        djnz    temp2,delay     ; between highs.
        ret