As an Amazon Associate we earn from qualifying purchases.
Mikro C is a user-friendly C compiler for PIC microcontrollers that ships with many ready-made libraries. Instead of setting registers bit by bit, you can call functions for the LCD, ADC, UART, I2C, SPI and more. This page covers programming the PIC16F877A with Mikro C: the basic circuit, how to set up a project, and what to learn next.
About the PIC16F877A
The PIC16F877A is an 8-bit microcontroller from Microchip in a 40-pin package. It is a common choice for students and hobby projects because it has plenty of I/O and built-in peripherals, and it is well documented. Key features include:
- Five I/O ports: PORTA, PORTB, PORTC, PORTD and PORTE
- An 8-channel, 10-bit analog-to-digital converter (ADC)
- Three timers and two capture/compare/PWM (CCP) modules
- A USART for serial communication
- An MSSP module for SPI and I2C
- Flash program memory, RAM and a small EEPROM
- A clock of up to 20 MHz
Basic circuit
Here is the basic circuit used in the examples. Every PIC16F877A project needs the same few connections before any code will run.
The minimum setup is:
- Power: 5 V on the VDD pins (11 and 32) and ground on the VSS pins (12 and 31). Connect both pairs.
- Reset: the MCLR pin (pin 1) is normally pulled up to 5 V through a resistor, often with a reset push button to ground.
- Clock: a crystal between OSC1 and OSC2 (pins 13 and 14), with two small capacitors to ground. A 20 MHz or 8 MHz crystal is common.
Once these are in place, you can connect LEDs, switches, an LCD or sensors to the I/O pins.
Setting up a project in Mikro C
The steps are similar in every Mikro C for PIC version, though menu names may differ slightly.
- Create a new project and choose PIC16F877A as the device.
- Enter the oscillator frequency that matches your crystal. Delay functions and UART baud rates depend on this value, so a wrong number gives wrong timing.
- Check the configuration bits. For simple projects you will usually turn off the watchdog timer and low-voltage programming, and select the oscillator type that matches your crystal (for example, HS for a high-speed crystal).
- Write your code in the main source file, then build the project to produce a HEX file.
- Load the HEX file into the chip with a programmer, or into a simulator such as Proteus.
Basic program structure
A typical Mikro C program has a main function that configures the pins once and then runs an endless loop. To set pin directions, write to the TRIS registers. A 0 makes a pin an output and a 1 makes it an input. For example, setting TRISB = 0 makes all PORTB pins outputs, and writing to PORTB then changes the pin levels.
The built-in Delay_ms function pauses the program for a given number of milliseconds. Combining a PORTB output, a delay and a while(1) loop is enough to blink LEDs, which is the usual first test of a new circuit.
A note on analog pins
On the PIC16F877A, many pins on PORTA and PORTE start up as analog inputs. If you want to use them as ordinary digital pins, set the ADCON1 register accordingly (for example, ADCON1 = 0x07 makes them all digital). Forgetting this is a common reason why a pin on PORTA or PORTE does not respond as expected.
Basic examples
Each of these topics builds on the basic circuit above:
- DIGITAL: turn pins on and off, and read switches
- LCD: show text and numbers on a character display
- ADC: read analog voltages from sensors or a potentiometer
- UART: send and receive serial data
- ULTRASONIC SENSOR
Each topic has its own step-by-step page. Start with the digital output example to control LEDs, then move on to the LCD example to display text. After that, read sensors with the ADC example and send data to a computer with the UART example. If you want to test without hardware, the Proteus simulations show the same code running in a virtual circuit.
Projects
After the basics, you can combine several peripherals into complete projects:
Tips for getting reliable results
- Make sure the oscillator frequency in the project matches the real crystal. Mismatches cause wrong delays and garbled serial output.
- Add a 0.1 µF decoupling capacitor close to the power pins to reduce noise.
- Test with a simple LED blink before adding more parts, so you know the chip, clock and programmer all work.
- Add one peripheral at a time. If something fails, you will know which part caused it.
- Simulate first when possible. It is quicker to fix a wiring or code mistake in software.
FAQ
Is Mikro C good for beginners?
Yes. Its libraries handle much of the register setup for common peripherals, so you can get an LCD or UART working quickly. It still helps to learn what the underlying registers do, because you will need them for less common tasks.
Do I need an external crystal for the PIC16F877A?
The chip needs a clock source. A crystal with two capacitors is the most common choice for this chip and gives accurate timing. Whatever you use, set the same frequency in your Mikro C project.
Why does my LED code not work on PORTA?
The PORTA pins start as analog inputs on this chip. Set the ADCON1 register to make them digital and set the matching TRIS bits as outputs.
Can I run Mikro C programs without a physical board?
Yes. You can load the compiled HEX file into a simulator such as Proteus to check the logic before building the circuit. Final testing on real hardware is still a good idea, since timing and electrical behavior can differ.