electroSome

Blinking LED using PIC Microcontroller – MikroC

LED

Contents

Welcome to the world of PIC Microcontrollers. You are in the right place if you are a beginner in the filed of microcontrollers. MikroC is the best compiler for beginners as it contains built in functions for most of the commonly used tasks. But MikroC is less efficient and the hex file generated will be large size compared to other compilers. So I suggest you to use Hi-Tech C compiler by Microchip after you get familiar with microcontrollers. Note that, Hi-Tech C is a bit difficult compared to MikroC as there is no built in functions. You can follow our Hi-Tech C Tutorials.

Getting Started with MikroC Pro

You can buy mikroC form mikroElectronika or download a trial version here. Trial Version is limited to 2k of program words and is sufficient for most of our applications.

MikroC Pro Welcome Screen
Welcome – New Project Wizard
Project Settings – New Project Wizard
Add Files to Project – New Project Wizard
Library Manager – New Project Wizard
Finish – New Project Wizard

MikroC Programming

Before going to the programming you should understand the following things.

PORT TRIS Register PIC Microcontroller

Writing Bit by Bit :

TRISC.F0 = 1; //Makes 0th bit of PORTC Input
TRISC.F5 = 0; //Makes 5th bit of PORTC Output
PORTB.F3 = 1; //Makes 3ed bit of PORTB at Logic High
PORTB.F7 = 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

MikroC Code – Blinking an LED

The following program blinks an LED with a delay of 1 second.

void main()
{
  TRISB.F0 = 0; //Makes PORTB0 or RB0 Output Pin

  while(1) //Infinite Loop
  {
    PORTB.F0 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    Delay_ms(1000); //1 Second Delay
  }
}

Note : Delay_ms(const unsigned long a) is a built in function of MikroC Pro which provides a delay of ‘a’ milliseconds. The variable ‘a’ must be a constant of type unsigned long integer.

Enter Your Code Here – MikroC Pro

A hex file will be generated in your Project Folder. You need to write this file to microcontroller using a programmer.

Circuit Diagram

Blinking LED using PIC Microcontroller – Circuit Diagram

VDD and VSS of PIC Microcontroller is connected to +5V and GND respectively to provide necessary power for the operation of the microcontroller. 8MHz crystal is used to provide necessary clock for the microcontroller. 22pF capacitors stabilizes the oscillations of the crystal. LED is connected to the 0th bit of PORTB and a 470Ω resistor is connected in series to limit the current through the LED.

You can simulate the working in Proteus. If you haven’t yet started with Proteus, please try this tutorial. Also see the following video.

Don’t forget to set the clock frequency and to add the hex file in your project folder by editing the properties of PIC Microcontroller as shown below.

Proteus Edit Component – PIC 16F877A

Hardware Implementation

You can wire this circuit in breadboard after writing the hex file to PIC 16F877A using a programmer and verify the output. I am using the Microchip’s PICKit2 for programming the microcontroller. You may buy PICKit2 or try to make one.

Programming

Connect the PIC 16F877A microcontroller to PICKit2 in the following manner.

Then connect the programmer to PC and open the PICKit2 Software.

After Opening the PICKit2 Programmer Tool
After Erasing – PICKit2
To Import Hex – PICKit2 Programmer Tool
Importing Hex File – PICKit2 Programmer Tool
After Importing the Hex File – PICKit2
After Programming – PICKit2
After Verification – PICKit2 Programmer Tool

Configuration Bits

I suggest you to read the following tutorial to know more about Configuration Bits.

Download Here

You can download the Hex file, MikroC file, Proteus etc from here…

Exit mobile version