Melanie posted an excellent article on indexing bits, bytes, words, etc. here
http://www.picbasic.co.uk/forum/showthread.php?t=544
Someone asked how to index port pins in a similar way, so here's one way.
Port file register addresses are sequential. For instance on the 16F876A file
register address for porta is at location 05h, portb is at 06h, and portc is at
07h. All you need is a pointer to the base of the bit index.
Example; SYMBOL PORT_PIN = PORTA ' <- start of bit index or pointer
Now using something like this, you can index all port bits.
Code:
X VAR BYTE ' For loop & bit index pointer
ADCON1 = 7 ' All digital
PORTA = 0 ' Clear all port pins
PORTB = 0
PORTC = 0
TRISA = 0 ' Make them all outputs
TRISB = 0
TRISC = 0
FOR X = 0 TO 23
PORT_PIN.0[X] = 1 ' Set all porta, portb, and portc pins high
NEXT X
There aren't 8 pins on porta that can go high, but it's still an 8-bit file register
so we have to treat it as 8-bit and index from 0 to 23 for 24-bits total.
You can do this on any PIC to index all port pins individually just using porta
as the starting index pointer.
PBP doesn't perfom bounds checking, so you can get away with indexing
out of bounds.
Note that also means you could write to a register you may not intend to if
you're not careful.
You could drop porta, and just use portb as the starting address also.
Code:
SYMBOL PORT_PIN = PORTB
X VAR BYTE ' For loop & bit index pointer
PORTB = 0
PORTC = 0
' Make them all outputs
TRISB = 0
TRISC = 0
FOR X = 0 TO 15 ' 16-bits total
PORT_PIN.0[X] = 1 ' Set all portb, and portc pins high
NEXT X
Bookmarks