Blink an LED with PIC16F877A in PICSimLab
Blinking an LED is the “hello world” of microcontrollers, and it is the fastest way to prove your whole PICSimLab workflow works: board selection, project generation, compiling with XC8, loading the hex file and running the simulation. This tutorial uses a PIC16F877A, the most documented PIC in PICSimLab, on one of the two boards that officially support it.
What you need
- PICSimLab 0.9.3 installed — see the download page for the official GitHub and SourceForge files.
- MPLAB X IDE and the XC8 compiler. PICSimLab does not edit source code itself; for PIC16F and PIC18F boards its Code Project Wizard generates MPLAB X projects, and MPLAB X with XC8 must be installed first.
- Board: McLab2 or PICGenios. Both officially support the PIC16F877A (along with the PIC16F777, PIC18F452, PIC18F4520, PIC18F4550 and PIC18F4620).
- One LEDs spare part (added in the steps below) so the output pin is visible even if you use a minimal board configuration.
Steps
1. Select the board and the microcontroller
Start PICSimLab and select the McLab2 board (or PICGenios) with the PIC16F877A as the microcontroller. Every later step depends on this choice, because the wizard generates the project for the selected chip.
2. Generate the MPLAB X project
Use the Code Project Wizard to generate a project for the board. For PIC16F and PIC18F boards the wizard creates an MPLAB X project that uses the XC8 compiler. Alternatively, open one of the ready-made board examples from the Help → Examples menu — the McLab2 and PICGenios example sources use MPLAB X and XC8 — and save a copy as your own project.
3. Replace main.c with the example code
Paste the example code below into your project. It configures RB0 as a digital output and toggles it every 500 ms. The _XTAL_FREQ value tells XC8 how fast the chip runs so __delay_ms() can compute delays; set it to match the clock configured for the board in PICSimLab (this example assumes 20 MHz).
4. Build in MPLAB X
Build the project in MPLAB X. A successful build produces a .hex file in the project output folder. If the build fails, fix the compiler errors in MPLAB X first — PICSimLab can only run a hex file that actually built.
5. Add the LED in Spare Parts
In PICSimLab, open Modules → Spare parts. From the Outputs menu add LEDs (a bank of 8 red LEDs). Right-click the part, open Properties and connect the first LED to pin RB0 of the microcontroller. Right-clicking any spare part also gives you Move, Rotate, Delete and Help options.
6. Load the hex and run
Load the compiled .hex file into PICSimLab and start the simulation. The LED connected to RB0 should turn on and off twice per second. Stop the simulation before loading a different hex file.
Example code (XC8)
// Example: blink an LED on RB0 - PIC16F877A, XC8
#pragma config FOSC = HS // High-speed oscillator
#pragma config WDTE = OFF // Watchdog off
#pragma config PWRTE = ON // Power-up timer on
#pragma config BOREN = ON // Brown-out reset on
#pragma config LVP = OFF // Low-voltage programming off
#pragma config CPD = OFF // Data EEPROM protection off
#pragma config WRT = OFF // Flash write protection off
#pragma config CP = OFF // Code protection off
#include <xc.h>
#define _XTAL_FREQ 20000000 // Example: match the board clock in PICSimLab
void main(void) {
TRISBbits.TRISB0 = 0; // RB0 as output
PORTBbits.RB0 = 0; // LED off at start
while (1) {
PORTBbits.RB0 = 1; // LED on
__delay_ms(500);
PORTBbits.RB0 = 0; // LED off
__delay_ms(500);
}
}
What you should see
A steady half-second blink. If you later open Modules → Oscilloscope and probe RB0, you will see a square wave with a period of about one second — a good first check that the simulation clock matches your _XTAL_FREQ assumption.
Try next
- LED on RB0 and RC0 with a push button — control this LED with a spare-parts button instead of a delay.
- Timers and interrupts — blink from a Timer0 interrupt instead of a delay loop.
- Using the PICSimLab oscilloscope — measure the blink waveform properly.
- Need the simulator itself? Get it from the download page.
Troubleshooting
The LED never changes. Check the spare part’s Properties first: the LED must be connected to RB0, and the pin must match the pin toggled in code. Then confirm you loaded the newest hex file after your last build.
The blink is much faster or slower than 500 ms. Your _XTAL_FREQ does not match the clock configured for the board. Adjust the define to the board clock, rebuild and reload the hex.
