Getting Started with MPLAB XC8 Compiler – LED Blinking
In this tutorial we will learn How to Blink an LED with PIC Microcontroller using MPAB XC8 Compiler. Recently Microchip released a series of development tools including MPLAB X IDE and MPAB XC Compilers. MPLAB X IDE is a software that runs on a computer intended to develop applications for Microchip’s Microcontrollers and Digital Signal Controllers. It can be used with Windows, Mac and Linux Operating Systems. It is called an Integrated Development Environment as it provides comprehensive facilities to the developers. Unlike previous versions of MPLAB, MPLAB X IDE is based on open source NetBeans IDE by Oracle.
MPLAB XC Compilers are general solutions for all Microchip PIC Microcontrollers and it can be used for any Project. It replaces all MPLAB C and Hi-Tech C compilers. Microchip recommends every developers to use MPLAB XC Compilers. These compilers integrates with MPLAB X IDE to provide full graphics front end.
In this example project we will blink an LED using PIC 16F877A Microcontroller. For that we will use MPLAB X IDE and MPLAB XC8 Compiler. You can download MPLAB X IDE and XC8 Compiler from the respective pages.
You should install Java before installing MPLAB X.
- Download and Install MPLAB X IDE.
- Download and Install MPLAB XC8 Compiler.
MPLAB XC8 Programming
- Input Outputs pins of a PIC Microcontroller is divided into different PORTS containing a group of GPIO (General Purpose Input Output) pins.
- Since PIC 16F877A is an 8-bit microcontroller, each PORT contains 8 Input Output pins.
- In 16F Microcontrollers, each port is associated with two registers : TRIS and PORT. Eg : TRISB, PORTB, TRISD, PORTD.
- TRIS stands for Tri-State, it determines the direction of each GPIO pins. Logic 1 at a particular bit of TRIS register makes the corresponding pin Input while Logic 0 at a particular bit makes the corresponding pin Output.
- All Input pins will be in Hi-Impedance state.
- PORT Register is used to Read Data from or Write Data to Input Output pins.
- For an Output pin (TRIS bit is 0), Logic 1 at PORT register makes the corresponding pin Logic High (VDD) and Logic 0 at PORT register makes the corresponding pin Logic Low (VSS).
- Since PIC 16F877A is a 5V device, VDD = 5V and VSS = 0V.
- PORT Read operation reads the Physical State (actual Voltage Level) of IO pins. If an IO pin is at a potential near to VDD, corresponding PORT bit will be Logic 1 and if it at a potential near to VSS, corresponding PORT bit will be Logic 0.
You can write to PORT and TRIS Registers entirely or bit by bit.
Writing Bit by Bit :
TRISC0 = 1; //Makes 0th bit of PORTC Input TRISC5 = 0; //Makes 5th bit of PORTC Output RB3 = 1; //Makes 3ed bit of PORTB at Logic High RB7 = 0; //Makes 7th bit of PORTB at Logic Low
You should be familiar with following C Programming concepts.
- A number with a prefix ‘0b’ indicates a binary number.
- A number with a prefix ‘0’ indicates an octal number.
- A number with a prefix ‘0x’ indicates a hexadecimal number.
- A number without prefix is a decimal number.
Let’s see some examples…
PORTB = 0xFF; //Makes all pins of PORTB Logic High TRISC = 0x00; //Makes all pins of TRISC Output PORTD = 128; //Makes 7th bit of PORTD Logic High
For more detail: Getting Started with MPLAB XC8 Compiler – LED Blinking
We recommend EasyEDA for electronic circuit design.
From Schematic Drawing to PCB Production, Just Need One Tool
Cheap PCB Prototype: 10 pcs 2 layers only $10, quick delivery, 100% E-test
Current Project / Post can also be found using:
- xc blink
- mplab x ide sample program for proteus
- mplab xc led blinking source code
- mplab xc8 example code