Overview Of PIC16F877A Digital Input/Output
PIC16F877A comes with five digital I/O ports names from PortA to PortE. Since the device is 8-bit, the data bus of each port are 8-bit. From PortA to PortD the I/O are fully 8-bit wide, except PortE that uses only 3-bit of the 8-bit Port register.
Beside the digital I/O, there are some analog input output function multiplexed with any Port that we don't mention here. Any relevance's will be talk on next section.
PORT registers locate in the special function register (SFR). Each port has its corresponding name and address of the SFR.
- PORTA locates at 0x05
- PORTB locates at 0x06
- PORTC locates at 0x07
- PORTD locates at 0x08
- PORTE locates at 0x09
Each port are bi-direction read and write. Each PORT direction (input or output) is configured by its corresponding TRIS register.
For example,
- PORTA=0xFF
- PORTD=0xAA
TRIS Registers
Tri-state (TRIS) register set the direction for its corresponding PORT register. As mention above, this device contains up to five PORT the TRIS register is also have up to five corresponding registers. TRIS are the member of the SFR. They have their own address in the SFR.
- TRISA locates at 0x85
- TRISB locates at 0x86
- TRISC locates at 0x87
- TRISD locates at 0x88
- TRISE locates at 0x89
Assigning '1' to the TRIS, allowing the corresponding PORT as digital input, otherwise configuring it to a digital output.
For example:
- TRISC=0x0F - The lower nibble of PORTC is digital input while the higher nibble is a digital output.
- TRISA=0xF0 - The lower nibble of PORTA is digital output while the higher nibble is a digital input.
PORTB and PORTD
PORTB and TRISB
As mention earlier PORTB is a digital bi-directional I/O port. It has a corresponding direction control register TRISB. Setting all bits of TRISB to '1' assigns all bits of PORTB to digital input, otherwise digital output.
Registers relate to PORTB |
All digital pins of PORTB have their own weak pull up resistor. By clearing the RBPU (PORTB Pull-up Enable Bit) bit of the OPTION_REG, it will enable the pull up resistors to all pins of PORTB. Hence, PORTB is internally wired to VDD via resistors, making it read HIGH.
PORTB is internally wired to HIGH. PORTD wire to 8-bit LED output.
PORTD and TRISD
Similarly, PORTD is a digital bi-directional I/O port. The corresponding direction control is TRISD. It also come with Schmitt Trigger input. This port also work as a microprocessor parallel slave port but I don't mention here.
Registers relate to PORTD |
MikroC Digital I/O Port Programming
Programming the digital input output port for PIC16F877A could make from scratch. In this start up example, I use the controller to read digital input from PORTB and display them on PORTD.
Schematic Diagram |
PORTB is internally wired to HIGH. PORTD wire to 8-bit LED output.
MikroC source code:
A simulation screen shot:
A Simulation Screen Shot |
Click here to download the complete example. I use my own development board to test this program.
Tested Program |
It also has a video of my experiment.
No comments:
Post a Comment