Home Code PIC16F873 flashing LED example

PIC16F873 flashing LED example

by shedboy71

This is the usual introduction to a different pic, this time we will start to show you some examples with the PIC16F873 microcontroller, you can find out more about this micro from Microchip but to summarise its a 28 pin sdip package with 5 10 bit ADC channels, 2 x 8 bit timers , 1 x 16 bit timer and various other functionality.

We’ll start with the usual flash led’s on and off, since we used the QL200 development board to verify this PIC was supported, we switched on/off 8 LEDs connected to PORTB

Lets look at the schematic


All resistors were 470 ohms

pic16f873 and leds

pic16f873 and leds


This time I decided to write an example using MIkroC and Mplab X with the XC8 compiler, I like to play around like this. So 2 code examples



void main()
TRISB = 0x00;
do {
PORTB = 0x00;
} while(1);
} // end main


Mplab X


#define _XTAL_FREQ 4000000
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
int main(int argc, char** argv)
TRISB = 0x00; //RB0 as Output PIN
__delay_ms(1000); // 1 Second Delay
PORTB = 0x00; // LED OFF
__delay_ms(1000); // 1 Second Delay
return 0;
return (EXIT_SUCCESS);



10PCS PIC16F873 DIP28 IC

QL200 PIC16F877A-I/P PIC16F877 PIC 8-bit Development Board 4 in 1 Onboard Programmer Integrated Demo System + LCD1602


You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More