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.
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.
Schematic Diagram
PORTB is internally wired to HIGH. PORTD wire to 8-bit LED output.
PIC16F877A is an old PICMicro device released in 2003. At the period that this device was very popular due to their large amount of I/O pins, a large memory, and a rich of peripherals to fit most of embedded applications. It was listed at the top of its mid-range 8-bit PICMicro.
However, there are many new released mid-range 8-bit devices. They have a lot of new features and more advantage over PIC16F877A. But the PIC16F877A is still in use in academic purposes and still useful in many typical electronic control projects.
A piece of PIC16F877A-I/P I used for prototyping
A screen shot of the running program
C Compilers For PICMicro
Many C compilers designed to target PIC devices. At the time I was at university, the popular C compiler are Hi-tech PICC, CCS PICC, and MikroC.
MikroC from Mikroelectronika offers a free version of coding below the size of 2 KB, otherwise we will need to buy the compiler's license.
MikroC is not only support for PICMicro, but it's designed to fit many other architecture, ARM, AVR, and legacy 8051.But they are sold separately. The coding of C in this compiler remain almost the same in syntax and function libraries for all targeted architecture.
Simulation And Hardware Prototyping
Proteus is a simulating software based on SPICE. It has a lot of model and libraries for many analog, digital and microprocessor devices including PICMicro. Using this software could save a lot of time of doing the prototyping project in real hardware, especially a breadboard prototyping. Additionally, it could cut down the devices burning due to any wrong connection or over voltage during the prototyping.
Getting Start With MikroC Pro For PIC
MikroC Pro for PIC support all 8-bit PIC devices at the specific time of released version. It has a help file that demonstrates how to use C coding and function libraries.
In this example, I show a simple LED blinking from the 8-bit of PORTD. PORTD of PIC16F877A is a pure digital input outputs port, without any multiplexing with analog peripherals.
MikroC source code lists below.
Circuit diagram made with Proteus.
Circuit Diagram
I have been using PICKit2 for device programming many years now. It still useful for any programming for most of the earlier version of PICMicro.
The MAX series CPLD from Altera ( now become a part of Intel ), is a low power and low cost CPLD released since 1993. They shipped with non-volatile memory (especially Flash memory ) for programming.
For educational purpose, there are many development boards released by the device's vendor itself and other third party vendors. However, some MAX CPLD device come with PLCC package allowing the use of additional socket adapter. It enables an easy way for students and hobbyists to make a DIY prototyping.
I made a CPLD board using EPM7128SCL84-15 of the flash and JTAG-based MAX series.
The EPM7128SCL84 is a CPLD in the MAX 7000S series. It bases on Flash technology allowing the user to store program permanently even the system is power off. It contains 2,500 usable gates within its 128 macrocells.
A device comparison table of many part numbers within the flash-based device in the MAX 7000S.
The MAX 7000S families, come with ISP and JTAG interface. Here, I don't mention the MAX 7000 series due to irrelevance.
An Altera USB Blaster is a device allowing the programming from a host computer to the target Altera CPLD/FPGA device via its JTAG port interfaces.
An Altera USB Blaster I got in 2014 from E Bay.
I bought some Altera CPLD chip from E Bay since 2014 with a USB blaster. An Altera chip costed around 5 $ from online market. Altera USB blaster costs about 6 $ from the same market.
Before Starting to program this device, I get used to a Xilinx XC9500 series. I code them using VHDL and a little of Verilog. I was aware of burning the Xilinx chip and its USB JTAG because those Xilinx devices costs far more expensive than the Altera one's.
A Xilinx XC95108 device has a similar capabilities with an Altera EPM7128S one's, but costing nearly 20 $. A Xilinx USB DLC9G USB JTAG costed around 30 $ at that time.
Designing A DIY Experimental Board For MAX 7000S
In 2014, I had enough working skills on CPLD/FPGA. I decided to prototype my own board. It was very hard due to lack of components in the local market.
Referring to EDA tools, KiCAD and Altium have a lot of components library and footprints. However it took a lot of time to design this kind of device in those EDA tools. Due to my long term working in using Proteus, I decided to make my own component symbols, an making a schematic and PCB design in that tool.
It consumed nearly one week of a design job in the computer, PCB fabrication and assembling. But before this finalized stuff, I spent many months working on drafting.
Schematic Diagram Of Main Sheet
Schematic Diagram Sheet 2
Within this design I putted many peripherals:
A regulated +5 V DC power supply with LED indicator
A 25.178 MHz oscillator
A JTAG socket
Two push buttons
Four slide switches
Eight output LEDs
A main EPM7128SCL84 CPLD with its pin headers
A legacy PS/2 Interface
A VGA port
A four-digit 0.36 inch common cathode multiplex 7-segment display
A 3D Model Of This Design
Copper Side
Components Side
Click here if you wish to download its completed design.
I made a lot of works on VHDL and Verilog coding with CPLD/FPGA. From now, I will post all those finished studies.
TFT display module is
widely available from online stores. The programming interface could make from
scratch with Arduino ready to use libraries. It has many examples included.
The esp8266 IoT module
is a low cost development platform with Arduino compatible.
In this example I use an
esp8266 IoT module to scan all nearby WiFi networks, and display them on a
ST7735 1.8" 128x160 SPI LCD module. It is very low cost from online store.
A Wi-Fi Scanning Using My Home Router
I use hardware SPI of esp8266. But
it will need more control pins such as D/C, CS and Reset pin.
A 1.8 inches SPI 128x160 TFT LCD
Connect the TFT LCD the
esp8266 to module as below:
GND to GND
VCC and BLK to Vin(+5V)
D/C to D1
CS to D8
RST to D2
CLK to D5 (HSCLK)
DIN to D7 (HMOSI)
The programming requires ucblib
library for TFT from github. You can install it via Arduino library manager.