LED Burning with PIC and PIC16 from scratch

In our previous article, we also gave information about PIC, now we are burning LED with PIC16 series in this series. To compile our code we will use the "MPLAB X IDE" program to create an "MPAB XC8 Compiler" scheme, a multifunctional compiling/IDE program offered by Microchip to developers for Windows, Linux and Mac. For both programs to work, Java must be installed on your computer, the project used 16F877A from the PIC16 series, let's move on to the first steps;

Preparatory Department

We start by opening MPLAB X IDE.

We continue with the File >> New Project.


  • We choose Microchip Embedded >> Standalone Project and continue with "Next"…


  • We choose the Family and Device section according to the PIC chip we have.


  • From here we choose our Programmers, if not supported, we will have the chance to install "burn" again when the project is finished.


·         We choose Compiler XC8 from here and continue…


  • Our project; we fill in information such as name, location.


  • We right-click Source Files from Project Tree.


  • We pick new >> C Main File and move on.


  • We're re-naming and location


  • We'll write our codes in the incoming .c file.

Coding Department

Pic microprocessors' I/O pins are divided into different groups, 16F877A, an 8-bit microprocessor , so each PORT 8 contains I/O, and these PORTs are associated with two registers, one of which is TRIS and the other is PORT (TRISB, PORTB, TRISD, PORTD). We use "PIC 16F877A" is a microprocessor that works with 5V, so VDD output 5V and VSS output 0V round.

What is TRIS Register?

TRIS is used for Tri-State activities, which determines the value of each GPIO pin, in the case of Logic 1, the TRIS record pin INPUT outputs, in the case of Logic 0, the TRIS record pin outputs, and all Input pins are waiting in the case of Hi-Impedance, i.e. high impedance.

What is PORT Register?

Port record is used to read or write data from the pin, output pin is TRIS Bit "0", port logic 1, so the record makes that pin Logic High (VDD) and PORT makes the same pin Logic Low (VSS) while Logic 0. PORT looks at the voltage value of the relevant pin when reading, and if it is potentially close to the VDD, the PORT bit becomes Logic 1, and if it is potentially close to VSS, the PORT bit becomes Logic 0.


Defining Registers in Code

We can identify PORT and TRIS registers per pin or all at once.

Bit Identification

TRISC0 = 1; 0th bit of PORTC Input

TRISC5 = 0; PORTC Output 5th bit

RB3 = 1; PORTB at Logic High 3rd bit

RB7 = 0; PORTB at Logic Low 7th bit

Defining the Entire Register

Numbers with prefix '0b' are binary numbers.

Numbers with prefix '0' are octal numbers.

Numbers with prefix '0x' are hexadecimal numbers.

numbers without prefixes are decimal numbers.

This table may make more sense;


PORTB = 0xFF; Makes all pins of PORTB Logic High

TRISC = 0x00; Outputs TRISC pins.

PORTD = 128; Logic High makes the 7th bit of PORTD.


Our code

#define _XTAL_FREQ 8000000
#pragma config FOSC = HS // HS os oscitor
#pragma config WDTE = ON // Turn on watchdog WDT timer
#pragma config PWRTE = OFF // Power-up PWRT shutdown
#pragma config BOREN = ON // Brown-out BORON opening
#pragma config LVP = OFF // We keep it off at low voltages
#pragma config CPD = OFF // We turn off EEPROM write protection
#pragma config WRT = OFF // FLASH We get memory protection
#pragma config CP = OFF // We turn off code protection
int main()
 TRISB0 = 0; We defined it as RB0 OUTPUT
 RB0 = 1; LED ON
 RB0 = 0; LED OFF
 return 0;

With _XTAL_FREQ, we adjusted our clock speed and defined the line __delay_ms() with xc.h.

Bit Settings in the Config Section

We set the pins in the "CONFIG SETTING" section here. With MPLAB IDE, we adjust bit mid-range settings to our microprocessor. Check out our article here for more detailed understanding and pin-bit meanings

Window >> PIC Memory Views >> Configuration Bits


  • We press the Generate Source Code to Output button.

Pic Pic

After creating the Mid-Range Bit setting, we paste it directly into our code and "BUILD" our project.


With Run Project, we prepare our code and print PICe directly



If we wish, we only create the .hex file with Build Project to write or transfer later. the .hex file is in the folder that we specified in the first place

Circuit Diagram


We have come to the end of this series of articles, feel free to report your comments and questions!