writes a low (0) to the specified pin.
This is the equivalent of the assembly-language command clrb 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 clrb means that you cannot change the pin being written 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 clrb. Write the pin number to pin, the port number to w, and call Low.
Low, 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 Low: 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.
To see Low 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 LOW.SRC. When you apply power to the PIC, the LEDs, initially lit, will go out one at a time until all are dark.
; ; *************************************************************************** ; *** Bubble Software Parallax to PIC Source Converter. Copyright 1999. *** ; *** http://www.bubblesoftonline.com email: sales@picnpoke.com *** ; *************************************************************************** ; ; LOW port (in w), pin ; Outputs a low to the specified port and pin. ; Device data and reset vector P = pic16c55 #include <16c55.inc> ; processor assembler definitions _CONFIG _xt_osc & _wdt_off & _protect_off reset start org 8 pin Res d'1' ; Pin number to set (0-7). ; Variables for the demo program--not required by Low. p_index Res d'1' ; Copy of pin number, port number. temp Res d'1' ; Temporary variables for time delay temp2 Res d'1' ; between outputs. org 0 ; Table to convert pin number (0-7) into bit mask (00000001b to 10000000b). Pinz ADDWF pcl RETLW d'1' RETLW d'2' RETLW d'4' RETLW d'8' RETLW d'16' RETLW d'32' RETLW d'64' RETLW d'128' ; The following demonstration code should show the value of being able to ; clear 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 clear using Low. start MOVLW d'0' ; All outputs on rb. TRIS 6h MOVLW d'0' ; All outputs on rc. TRIS 7h MOVLW d'255' ; Start with 1s on rb, rc (all LEDs on). MOVWF 6h MOVLW d'255' MOVWF 7h CLRF p_index ; Clear p_index. start_loop MOVLW d'7' ; Copy three lsbs of p_index into MOVWF pin MOVF p_index ; variable pin. ANDWF pin MOVLW d'1' ; IF p_index.3 = 0 BTFSC p_index,d'3' ; THEN w = 1 MOVLW d'2' ; ELSE w = 2 CALL Low CALL delay ; Wait a while between lows. INCF p_index ; Next pin/port. BTFSS p_index,d'4' ; IF p_index < 15 (rc.7) GOTO start_loop ; THEN :loop ELSE done done GOTO $ ; Endless loop. ; To use the routine Low, put the pin number into the variable pin ; and the port number into w. Then call Low. Low MOVWF fsr ; Point to the port number. MOVLW 5h ; Add offset for port RA. ADDWF fsr MOVF pin,w CALL Pinz ; Get bit mask from the table. XORLW 0xFF ; Invert it so there's a 0 in selected bit. ANDWF indirect ; Turn off the selected bit RETLW 0h ; Delay routine for demonstration. Not required by Low. delay DECFSZ temp ; Time delay to provide spacing GOTO delay DECFSZ temp2 ; between highs. GOTO delay RETLW 0h end
; LOW port (in w), pin ; Outputs a low 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 Low. 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 ; clear 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 clear using Low. start mov !rb, #0 ; All outputs on rb. mov !rc, #0 ; All outputs on rc. mov rb,#255 ; Start with 1s on rb, rc (all LEDs on). mov rc,#255 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 Low call delay ; Wait a while between lows. 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. ; To use the routine Low, put the pin number into the variable pin ; and the port number into w. Then call Low. Low 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. NOT w ; Invert it so there's a 0 in selected bit. AND indirect,w ; Turn off the selected bit ret ; Delay routine for demonstration. Not required by Low. delay djnz temp,delay ; Time delay to provide spacing djnz temp2,delay ; between highs. ret
See also:
file: /Techref/microchip/seepicsrc/psbpix/low.htm, 9KB, , updated: 2011/1/8 14:42, local time: 2024/11/28 12:20,
3.142.212.119:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/Techref/microchip/seepicsrc/psbpix/low.htm"> The PIC Source Book: Low</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.