electroSome

Getting Started with MPLAB XC8 Compiler – LED Blinking

LED

LED

Contents

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.

Open MPLAB X IDE
New Project – Step 1
New Project – Step 2
New Project – Step 3
New Project – Step 4
New Project – Step 5
New Project Wizard Completed
Adding a Source File
Name and Location – New C Main File
New Source File Created

MPLAB XC8 Programming

PORT and TRIS Register in PIC Microcontroller

Writing Registers

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

Writing Entire Register

You should be familiar with following C Programming concepts.

Let’s see some examples…

Decimal Binary Octal Hexadecimal
0 0b00000000 00 0x00
1 0b00000001 01 0x01
128 0b10000000 0200 0x80
255 0b11111111 0377 0xFF
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

Code – LED Blinking

#define _XTAL_FREQ 8000000

#include <xc.h>

// BEGIN CONFIG
#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)
//END CONFIG

int main()
{
  TRISB0 = 0; //RB0 as Output PIN
  while(1)
  {
    RB0 = 1;  // LED ON
    __delay_ms(1000); // 1 Second Delay
    RB0 = 0;  // LED OFF
    __delay_ms(1000); // 1 Second Delay
  }
  return 0;
}

First statement #define _XTAL_FREQ 8000000 defines the clock frequency of the microcontroller which is used to calculate delays in __delay_ms() function. Second statement #include <xc.h> includes the header file xc.h which contains the definition of __delay_ms() function and TRIS, PORT registers.

Next is #pragma config directives, which is used to tell the compiler to set Configuration Bits of PIC Microcontroller. You can generate it using the MPLAB IDE as following.

PIC Memory Views – Configuration Bits
PIC Memory Views – Configuration Bits
Generated Configuration Bits Source
Building the Project

Circuit Diagram

Blinking LED using PIC Microcontroller – Circuit Diagram

VDD and VSS of the PIC Microcontroller is connected to +5V and GND respectively. 8MHz crystal oscillator is used to provide necessary clock for the operation of the microcontroller. 22pF capacitors are used to stabilize the clock generated by crystal oscillator. An LED is connected to RBO (Pin 33) via a 470Ω resistor to limit the current.

Download Here

You can download entire project files here…

Exit mobile version