Blinking LED using PIC Microcontroller – MikroC


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.

  • Download and Install MikroC Pro
  • Create a Folder for this project in any of your drives
  • Open microC Pro For PIC
MikroC Pro Welcome Screen

MikroC Pro Welcome Screen

  • Click on Project >> New Project
Welcome - New Project Wizard

Welcome – New Project Wizard

  • Click Next
Project Settings - New Project Wizard

Project Settings – New Project Wizard

  • Enter Project name, path (created folder path), clock frequency, microcontroller and click Next. Clock Frequency is the frequency of oscillator used with microcontroller. Here we use PIC 16F877A microcontroller with 8MHz crystal.
Add Files to Project - New Project Wizard

Add Files to Project – New Project Wizard

  •  Here you can add your subprogram files or user defined header files in large projects. Hence we are dealing with simple LED Blinking in this tutorial, ignore it and click Next.
Library Manager - New Project Wizard

Library Manager – New Project Wizard

  •  Here you can add MikroC’s built in libraries such as UART, PWM, LCD etc… You may include All Libraries or include None. If you select None, then you can selectively include required libraries later. Then click Next.
Finish - New Project Wizard

Finish – New Project Wizard

  • Click Finish, to complete the New Project Wizard.
  • Then you will see the editor, where you can enter the MikroC Code.

MikroC Programming

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

  • Output pins of a PIC Microcontroller is divided in to different PORTS containing a group of GPIO (General Purpose Input Output Pins) pins.
  • In 16F PIC Microcontrollers, there are two registers associated with a port, TRIS and PORT. eg: TRISB, PORTB, TRISC, PORTC
  • TRIS stands for Tri-State, which determines the direction of each GPIO pin. Logic 1 at a particular bit of TRIS register makes the corresponding pin Input and Logic 0 at a particular bit of TRIS register makes the corresponding pin Output. An Input pin of PIC Microcontroller have very high input impedance, thus it may said to be in Hi-Impedance state.
  • PORT register is used to read data from or write data to GPIO pins. Logic 1 at a particular bit of PORT register makes the corresponding pin at Logic High (VDD) and Logic 0 at a particular bit of PORT register makes the corresponding pin at Logic Low (VSS) if that pin is an Output pin (TRIS bit is 0).
  • PORT register can be used to read digital data from an Input pin. Logic 1 indicates the pin is at Logic High and Logic 0 indicates that the pin is at Logic Low.
PORT TRIS Register PIC Microcontroller

PORT TRIS Register PIC Microcontroller

  • You can write to PORT and TRIS register entirely or bit by bit.

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.

  • 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…

Decimal Binary Octal Hexadecimal
0 0b00000000 00 0×00
1 0b00000001 01 0×01
128 0b10000000 0200 0×80
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 the above MikroC code
Enter Your Code Here - MikroC Pro

Enter Your Code Here – MikroC Pro

  • Save it
  • Then Compile it. Click Build >> Build (or Ctrl+F9)

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

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.

Pages: 1 2



You may also like...

  • Khaba

    When I run any simulation with pic16f877 on proteus, nothing happens but when I use 89C51 and write a code in assembly language the simulation on proteus works, I don’t understand why.

  • Khaba

    When I run any simulation with pic16f877 on proteus, nothing happens but when I use 89C51 and write a code in assembly language the simulation on proteus works, I don’t understand why.

  • http://www.electrosome.com/ Ligo George

    Which pic is selected in mikroC ? 16F877A and 16F877 are different pic microcontrollers…

  • Khaba

    I am using 16F877, couldn’t find 16F877A in proteus 6 I am using. I solved the problem but I noticed that it stemmed from the fact that I used a battery source. When I use a power terminal, it works perfectly well. Also the 16F877 has been behaving well with a few modifications I did e.g. CMCON omitted where you used it.

  • http://www.electrosome.com/ Ligo George

    When using a battery source, you must label VDD and Vss of pic..

  • SOURAV SARKAR

    Good evening Sir. I am having some problem in running the programs in mikroC. Please help me. Actually I am a beginner. Can you please give me your contact number?

  • http://www.electrosome.com/ Ligo George

    Please post your problems here… such that it will helpful to other visitors too… It is not good to share my contact details publically here…

  • Geo
  • http://www.electrosome.com/ Ligo George

    You can download it from mikroelectronica’s website.

  • Nanaiah

    sir can you give me same program for mplab8.80v using hi-tec c compiler or mplabx.

  • Weldan Jamili

    Hi I am using sk40c board with pic16f877a. Looking at the board how do I know which port is A,B,C, or D?

  • http://www.electrosome.com/ Ligo George

    Just look at the pin diagram of pic 16f877a… Then make a continuity test using multimeter..

  • http://www.electrosome.com/ Ligo George
  • Copper Masud

    hello sir,when i run my code in simulation ,it works .But practically it’s not working…..
    i am using pic16f877a & 20mhzcrystal
    void main() {
    TRISB = 0×00;
    PORTB = 0×00;
    while(1)
    {
    PORTB = 0×00;
    Delay_Ms(500);
    PORTB = 0xFF;
    Delay_Ms(500);
    }
    }

  • http://www.electrosome.com/ Ligo George

    You haven’t connected 1st pin (MCLR) to VDD (5V) and you should use 680 or 470 ohm series resistance with LED.

  • hari

    a good thermostate

  • Copper Masud

    thanks for your advice …….. it worksSSSS>>>>>>>>>>>>>
    thank you so mucchhh

  • Pingback: Using Push Button Switch with PIC Microcontroller - MikroC