The 16F688 has 12 I/O pins
These pins are accessed via port A and C
To identify which bit within which port is responsible for driving a particular PIC pin, look at the pin diagram below (this diagram is only good for the 16F688, refer to other pin diagrams for other PICs). Locate the pin you are interested in and read off the label.
The label will be of the form Rxj
where x will be A for PORTA and C for PORTC, and j will be the bit number within the port (in the range 0 to 7)
On the 16F688 port A bit 0 connects to the pin labelled RA0 which is pin 13, port A bit 3 connects to the pin labelled RA3 which is pin 4
NOTE: most of the I/O pins on the 16F688 have multiple functions and multiple labels to indicate these functions. When using a pin it must be configured to perform the required function. This is done internally by the program when it starts running. The program writes to special function registers which are responsible for configuring the pins.
sample projects (ciruits, sample code and chip programming)
LABEL PIN PIN DIAGRAM RA0 13 RA1 12 RA2 11 RA3 4 RA4 3 RA5 2 RC0 10 RC1 9 RC2 8 RC3 7 RC4 6 RC5 5
To write to pin 11 of a 16F688 lookup the label on pin 11 in the pin diagram. The label is RA2. This translates to PORTA bit 2.
BIT NUMBER 7 6 5 4 3 2 1 0 PORTA x x x x x x x x TRISA x x x x x x x x PIN LABEL RA7 RA6 RA5 RA4 RA3 RA2 RA1 RA0 PIN NUMBER N/A N/A 2 3 4 11 12 13 Clear the corresponding bit in the PORTA data direction register (called TRISA) to 0, that is clear TRISA bit 2 to 0. This configures pin 11 as an output.
BIT NUMBER 7 6 5 4 3 2 1 0 PORTA x x x x x x x x TRISA x x x x x 0 x x PIN LABEL RA7 RA6 RA5 RA4 RA3 RA2 RA1 RA0 PIN NUMBER N/A N/A 2 3 4 11 12 13 Then write the required value (0 or 1) to PORTA bit 2
BIT NUMBER 7 6 5 4 3 2 1 0 PORTA x x x x x 0 or 1 x x TRISA x x x x x 0 x x PIN LABEL RA7 RA6 RA5 RA4 RA3 RA2 RA1 RA0 PIN NUMBER N/A N/A 2 3 4 11 12 13 This can be done in XCSB as:
The 0x04 value is a hex mask (the equivalent of binary 00000100 see binary to hex).// configure pin 11 as output by writing 0 to TRISA bit 2 TRISA = TRISA & ~0x04 // set pin 11 low (to 0) by writing 0 to PORTA bit 2 PORTA = PORTA & ~0x04 // set pin 11 high (to 1) by writing 1 to PORTA bit 2 PORTA = PORTA | 0x04A simpler way to access a bit within a byte is to use a constant expression of the form
(1 << n)where n is the bit number and << is the left shift operatorTo access multiple bits within a byte, combine them with the bit wise OR operator
e.g.(1 << n) | (1 << m) | (1 << j)where n, m and j are the bit numbersTo set pins 1 and 13 to 1 use
More about bit manipulation and binaryTRISA = TRISA & ~((1 << 2) | (1 << 0)) PORTA = PORTA | ((1 << 2) | (1 << 0))
To read from pin 11 of a 16F688 lookup the label on pin 11 in the pin diagram. The label is RA2. This translates to PORTA bit 2.
BIT NUMBER 7 6 5 4 3 2 1 0 PORTA x x x x x x x x TRISA x x x x x x x x PIN LABEL RA7 RA6 RA5 RA4 RA3 RA2 RA1 RA0 PIN NUMBER N/A N/A 2 3 4 11 12 13 Set the corresponding bit in the PORTA data direction register (called TRISA) to 1, that is set TRISA bit 2 to 1. This configures pin 11 as an input.
BIT NUMBER 7 6 5 4 3 2 1 0 PORTA x x x x x x x x TRISA x x x x x 1 x x PIN LABEL RA7 RA6 RA5 RA4 RA3 RA2 RA1 RA0 PIN NUMBER N/A N/A 2 3 4 11 12 13 Then read from PORTA and mask out bit 2
This can be done in XCSB as:
The 0x04 value is a hex mask (the equivalent of binary 00000100 see binary to hex).// configure pin 11 as input by writing 1 to TRISA bit 2 TRISA = TRISA | 0x04 // read 0 or 1 into the variable called result result = (PORTA & 0x04) != 0 // NOTE: the expression (PORTA & 0x04) return 0x00 or 0x04 // the expression X != 0 return 0 if X is 0x00 or 1 if X is 0x04A simpler way to access a bit within a byte is to use a constant expression of the form
(1 << n)where n is the bit number and << is the left shift operatorThe above example could then be re-written as:
// configure pin 11 as input by writing 1 to TRISA bit 2 TRISA = TRISA | (1 << 2) // read 0 or 1 into the variable called result result = (PORTA & (1 << 2)) != 0 // NOTE: the expression (PORTA & (1 << 2)) return 0x00 or 0x04 // the expression X != 0 return 0 if X is 0x00 or 1 if X is 0x04More about bit manipulation and binary