Documente Academic
Documente Profesional
Documente Cultură
http://www.xcprod.com/titan/XCSB-DOC/physical_io.html
0 0 OUT
0 0 OUT
0 1 IN
0 0 OUT
0 0 OUT
0 1 IN
0 0 OUT
0 1 IN
To access TRISA on the 16F84, 16F628, 16F88, 16F876 or 16F877 the program must access the RAM at address location 133 (this is actually 5 + 128). In XCSB, the ports and data direction registers are refered to by name not by address (although they can also be accessed directly by address).
1 of 4
2/29/2012 1:52 PM
http://www.xcprod.com/titan/XCSB-DOC/physical_io.html
PORTB RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 PIN 13 12 11 10 9 8 7 6 NOTE: on the 16F84 bits 5, 6 and 7 of PORTA do not connect to any pins so they are shown as not connected or N/C 12f683
16F84
16F628
16f874 16f877
16f88
To clear a specific bit (or patern of bits) within a port, the user should AND a 0 into that bit (or patern of bits) e.g.
// set PORTA bit 5 to output
2 of 4
2/29/2012 1:52 PM
http://www.xcprod.com/titan/XCSB-DOC/physical_io.html
TRISA = TRISA & 0xDF // clear PORTA bit 5 -- DETAILED EXPLANATION PORTA = PORTA & 0xDF
An alternate method for setting and clearing pins is to use bit numbers e.g.
// set PORTA bit 5 to output TRISA = TRISA & ~(1 << 5) // set PORTA bit 5 PORTA = PORTA | (1 << 5) // clear PORTA bit 5 PORTA = PORTA & ~(1 << 5)
The expression (1 << 5) may look more complicated that the equivalent mask 0x20, however XCSB will generate the same efficient code for both.
The expression (1 << 5) may look more complicated that the equivalent mask 0x20, however XCSB will generate the same efficient code for both. Sometimes the value of an imput needs to be stored instead of acted upon immediately. This can be done in one of two ways 1. read the input into a variable, mask out the bit (only keep the bit that is of interest) and use the variable only in logical expressions 2. read the input into a vaiable as the integer value 0 or 1 Method 1
// set PORTA bit 5 as input
3 of 4
2/29/2012 1:52 PM
http://www.xcprod.com/titan/XCSB-DOC/physical_io.html
TRISA = TRISA | 0x20 // read PORTA into temp and discard all bits except bit 5 // temp will only be set to 0 or 0x20 temp = PORTA & 0x20
NOTE: in XCSB zero represents FALSE and non-zero represents TRUE Method 1 - using bit numbers
// set PORTA bit 5 as input TRISA = TRISA | (1 << 5) // read PORTA into temp and discard all bits except bit 5 // temp will only be set to 0 or 0x20 temp = PORTA & (1 << 5)
Method 2
// set PORTA bit 5 as input TRISA = TRISA | 0x20 // set temp to 1 if PORTA bit 5 is 1 otherwise set temp to 0 temp = ((PORTA & 0x20) != 0)
NOTE: the statement "temp = ((PORTA & (1 << 5)) != 0)" looks complicated but is compiled into just 4 native PIC instructions. To put this into perpective, a 4MHz PIC could execute this statement 200,000 in 1 second
4 of 4
2/29/2012 1:52 PM