The 12F683 has 6 I/O pins
These pins are accessed via the GPIO port
To identify which bit within the port is responsible for driving a particular PIC pin, look at the pin diagram below (this diagram is only good for the 12F683, 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 GPj
where j will be the bit number within the port (in the range 0 to 7)
On the 12F683 the GPIO port bit 0 connects to the pin labelled GP0 which is pin 7, the GPIO port bit 3 connects to the pin labelled GP3 which is pin 4
NOTE: most of the I/O pins on the 12F683 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 GP0 7 GP1 6 GP2 5 GP3 4 GP4 3 GP5 2
To write to pin 5 of a 12F683 lookup the label on pin 5 in the pin diagram. The label is GP2. This translates to GP bit 2.
BIT NUMBER 7 6 5 4 3 2 1 0 GPIO x x x x x x x x TRISIO x x x x x x x x PIN LABEL GP7 GP6 GP5 GP4 GP3 GP2 GP1 GP0 PIN NUMBER N/A N/A 2 3 4 5 6 7 Clear the corresponding bit in the GP data direction register (called TRISIO) to 0, that is clear TRISIO bit 2 to 0. This configures pin 5 as an output.
BIT NUMBER 7 6 5 4 3 2 1 0 GPIO x x x x x x x x TRISIO x x x x x 0 x x PIN LABEL GP7 GP6 GP5 GP4 GP3 GP2 GP1 GP0 PIN NUMBER N/A N/A 2 3 4 5 6 7 Then write the required value (0 or 1) to GPIO bit 2
BIT NUMBER 7 6 5 4 3 2 1 0 GPIO x x x x x 0 or 1 x x TRISIO x x x x x 0 x x PIN LABEL GP7 GP6 GP5 GP4 GP3 GP2 GP1 GP0 PIN NUMBER N/A N/A 2 3 4 5 6 7 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 5 as output by writing 0 to TRISIO bit 2 TRISIO = TRISIO & ~0x04 // set pin 5 low (to 0) by writing 0 to GPIO bit 2 GPIO = GPIO & ~0x04 // set pin 5 high (to 1) by writing 1 to GPIO bit 2 GPIO = GPIO | 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 7 to 1 use
More about bit manipulation and binaryTRISIO = TRISIO & ~((1 << 2) | (1 << 0)) GPIO = GPIO | ((1 << 2) | (1 << 0))
To read from pin 5 of a 12F683 lookup the label on pin 5 in the pin diagram. The label is GP2. This translates to GP bit 2.
BIT NUMBER 7 6 5 4 3 2 1 0 GPIO x x x x x x x x TRISIO x x x x x x x x PIN LABEL GP7 GP6 GP5 GP4 GP3 GP2 GP1 GP0 PIN NUMBER N/A N/A 2 3 4 5 6 7 Set the corresponding bit in the GP data direction register (called TRISIO) to 1, that is set TRISIO bit 2 to 1. This configures pin 5 as an input.
BIT NUMBER 7 6 5 4 3 2 1 0 GPIO x x x x x x x x TRISIO x x x x x 1 x x PIN LABEL GP7 GP6 GP5 GP4 GP3 GP2 GP1 GP0 PIN NUMBER N/A N/A 2 3 4 5 6 7 Then read from GP 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 5 as input by writing 1 to TRISIO bit 2 TRISIO = TRISIO | 0x04 // read 0 or 1 into the variable called result result = (GPIO & 0x04) != 0 // NOTE: the expression (GPIO & 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 5 as input by writing 1 to TRISIO bit 2 TRISIO = TRISIO | (1 << 2) // read 0 or 1 into the variable called result result = (GPIO & (1 << 2)) != 0 // NOTE: the expression (GPIO & (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