The 16F877 and 16F874 have the same pin layouts. These chips only differ internally by the amount of memory they contain, otherwise they function identically. The description of the pins and port access discussed below for the 16F877 also applies to the 16F874.The 16F877 has 33 I/O pins
These pins are accessed via port A, B, C, D and E
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 16F877, 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, B for PORTB etc., and j will be the bit number within the port (in the range 0 to 7)
On the 16F877 port A bit 0 connects to the pin labelled RA0 which is pin 2, port A bit 3 connects to the pin labelled RA3 which is pin 5
NOTE: most of the I/O pins on the 16F877 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 2 RA1 3 RA2 4 RA3 5 RA4 6 RA5 7 RB0 33 RB1 34 RB2 35 RB3 36 RB4 37 RB5 38 RB6 39 RB7 40 RC0 15 RC1 16 RC2 17 RC3 18 RC4 23 RC5 24 RC6 25 RC7 26 RD0 19 RD1 20 RD2 21 RD3 22 RD4 27 RD5 28 RD6 29 RD7 30 RE0 8 RE1 9 RE2 10
To write to pin 5 of a 16F877 lookup the label on pin 5 in the pin diagram. The label is RA3. This translates to PORTA bit 3.
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 7 6 5 4 3 2 Clear the corresponding bit in the PORTA data direction register (called TRISA) to 0, that is clear TRISA bit 3 to 0. This configures pin 5 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 0 x x x PIN LABEL RA7 RA6 RA5 RA4 RA3 RA2 RA1 RA0 PIN NUMBER N/A N/A 7 6 5 4 3 2 Then write the required value (0 or 1) to PORTA bit 3
BIT NUMBER 7 6 5 4 3 2 1 0 PORTA x x x x 0 or 1 x x x TRISA x x x x 0 x x x PIN LABEL RA7 RA6 RA5 RA4 RA3 RA2 RA1 RA0 PIN NUMBER N/A N/A 7 6 5 4 3 2 This can be done in XCSB as:
The 0x08 value is a hex mask (the equivalent of binary 00001000 see binary to hex).// configure pin 5 as output by writing 0 to TRISA bit 3 TRISA = TRISA & ~0x08 // set pin 5 low (to 0) by writing 0 to PORTA bit 3 PORTA = PORTA & ~0x08 // set pin 5 high (to 1) by writing 1 to PORTA bit 3 PORTA = PORTA | 0x08A 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 17 to 1 use
More about bit manipulation and binaryTRISA = TRISA & ~((1 << 2) | (1 << 0)) PORTA = PORTA | ((1 << 2) | (1 << 0))
To read from pin 5 of a 16F877 lookup the label on pin 5 in the pin diagram. The label is RA3. This translates to PORTA bit 3.
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 7 6 5 4 3 2 Set the corresponding bit in the PORTA data direction register (called TRISA) to 1, that is set TRISA bit 3 to 1. This configures pin 5 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 1 x x x PIN LABEL RA7 RA6 RA5 RA4 RA3 RA2 RA1 RA0 PIN NUMBER N/A N/A 7 6 5 4 3 2 Then read from PORTA and mask out bit 3
This can be done in XCSB as:
The 0x08 value is a hex mask (the equivalent of binary 00001000 see binary to hex).// configure pin 5 as input by writing 1 to TRISA bit 3 TRISA = TRISA | 0x08 // read 0 or 1 into the variable called result result = (PORTA & 0x08) != 0 // NOTE: the expression (PORTA & 0x08) return 0x00 or 0x08 // the expression X != 0 return 0 if X is 0x00 or 1 if X is 0x08A 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 TRISA bit 3 TRISA = TRISA | (1 << 3) // read 0 or 1 into the variable called result result = (PORTA & (1 << 3)) != 0 // NOTE: the expression (PORTA & (1 << 3)) return 0x00 or 0x08 // the expression X != 0 return 0 if X is 0x00 or 1 if X is 0x08More about bit manipulation and binary