Friday, August 28, 2020

Reading Digital Inputs From PortB With PortD Outputs Of PIC16F877A With MikroC

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.

Reading Digital Inputs From PortB With PortD Outputs
PIC16F877A Pins Diagram

PORT Registers 

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.



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.



Reading Digital Inputs From PortB With PortD Outputs
Schematic Diagram

PORTB is internally wired to HIGH. PORTD wire to 8-bit LED output.

MikroC source code:


A simulation screen shot:

Reading Digital Inputs From PortB With PortD Outputs
A Simulation Screen Shot

Click here to download the complete example. I use my own development board to test this program.

Reading Digital Inputs From PortB With PortD Outputs Of PIC16F877A With MikroC
Tested Program

It also has a video of my experiment.


No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)