Blinking LED using PIC Microcontroller with Hi-Tech C

A lot of you may heard about microcontrollers and its applications. Well it is a bit difficult to start learning microcontrollers. And the guides and tutorials also do not start from zero level which makes learning far more difficult than anticipated. I have tried to start from zero level in here also. All you need is the simplest knowledge of electronics or digital circuits. But you surely should have a decent knowledge of C language.

The software for programming Microchip microcontrollers comes by the name MPLAB IDE. MPLAB is the name of the software and IDE stands for Integrated Development Environment. IDE means that the software itself has all or most of the features that is needed. MPLAB IDE can perform the following operations :

  • Source code editing
  • Project management
  • Machine code generation from Assembly
  • Device simulation
  • Device emulation
  • Device programming

Hence it is called an Integrated Development Environment.

Some things should be clear before starting the tutorial. We can write programs for the PIC microcontrollers in MPLAB but the syntax and code depends upon the library, which may be different from family to family and even a lot of libraries are available for the same family of microcontrollers too.

We are discussing about the Hi-Tech C Compiler library (HTC) for MPLAB in this tutorial. It is used for programming the microcontrollers of series 16F which will be enough for the beginner users. Now if you want to have very high performance applications through microcontrollers then you need to go to higher and more powerful family of microcontrollers like 18F, 24F series and for them the programming library is different. But as the 16F series microcontrollers are 8 bit and support clock rates up to 20 MHz, they are useful in most of our non-commercial applications.

So let’s cut directly to the chase. The software MPLAB IDE comes with integrated HTC compiler library from the version 8.56 on wards.

Let’s start our first project, Blinking LED using PIC 16F877A. 16F877A is a very commonly used PIC microcontroller.

  • Download and Install MPLAB IDE with Hi-Tech C compiler
  • Open MPLAB IDE
  • Click on Project >> Project Wizard
Creating New Project - MPLAB

Creating New Project – MPLAB

  • Click on Next
MPLAB - Selecting Microcontroller

MPLAB – Selecting Microcontroller

  • Select PIC 16F877A and click Next
MPLAB - Selecting Language Toolsuite

MPLAB – Selecting Language Toolsuite

  • Select Hi-Tech C compiler as show above and click Next
MPLAB - Giving Project path and File name

MPLAB – Giving Project path and File name

  • Select Project path, give a file name and click Next
MPLAB - Adding files to Project

MPLAB – Adding files to Project

  • Add required files to Project (Not required here) and click Next
MPLAB - Completing the Project Creation

MPLAB – Completing the Project Creation

  • Click Finish to complete project creation
MPLAB - After Creating Project

MPLAB – After Creating Project

Next step is writing the program. All the information about writing programs which include its commands, operations and the microcontroller registers are available on the datasheet and Hi-Tech Toolsuite guide. But I will also try to make you understand the first steps so that the datasheet and guide may prove useful. You can start making your own programs at the end of this tutorial.
Now let us view a program for 16F877A where 8 LED are connected at the PORT B (8 pins of port B, from pin no 33-40). This programs blinks the LEDs which means the LEDs remain on for a second and off for another second.

Hi-Tech C Code

#include <htc.h>
#define _XTAL_FREQ 8000000
void main()
{
  TRISB=0X00;
  PORTB=0X00;
  while(1)
  { 
    PORTB=0XFF;
    _delay_ms(100);
    PORTB=0X00;
    _delay_ms(100);
  }
}

This is the core style of writing the microcontroller program. As you can see, the style is completely similar to that of the normal C programs. Just some additional keywords here. Remember that all the syntax and mathematical and logical operations supported by stdio and conio libraries are accepted here in htc library with some additional ones also.

  1. In the first line, we start the program by including the library. This step is nothing new.
  2.  Now we need to define the frequency of oscillator used in the system. This is the style of defining the oscillator frequency. The value 8000000 means its frequency is 8MHz.
  3. Start of the main program code
  4. TRIS is the command used for initializing the ports of the microcontroller. TRISB is an 8 bit register, in fact every word written in capital represents a register inside the microcontroller and you can get all the information about the register in the datasheet. Using this line will start the use of PORT B and if the value is ‘1’ in the respective register bit, that pin will be made input and if the value is ‘0’, the pin will be made output. One more thing, the use of ‘0x’ in that line represents that we are giving the data in hexadecimal number system. You can use ‘0b’ if you wish to give the data in binary and use nothing if you use decimal number system. So this line makes each 8 lines of port B output. Other way to give the same command is: TRISB=0; (decimal) and TRISB=0b00000000; (binary).
  5. PORTB is also a register like TRISB. This register passes the value out of port B. This means PORTB=0×00 will give low output from each eight line of port B. This is equivalent to clearing of port B at the start from any stray values. This line is not needed because there are no stray values at the start.
  6. Start of while loop. We write the entire program inside the while loop. After all initializations have been done, the main code is written here. This is done because this segment of program has to repeat over and over but the initializations do not need to be repeated. That is why the main program is written inside the while loop.
  7. PORTB is also a register like TRISB. This register passes the value out of port B. This means PORTB=0xff will give high output from each eight line of port B.
  8.  This line is written to have a delay of 1000ms. So high logic will pass from port B pins for 500ms.
  9. This line will make the output from port B get down to 0V, ie logic ‘0’. It is to be considered that until the value in PORTB or any other port register is changed, the output from the corresponding port will also not change. So the output will drop from 5V (logic 1) to 0V (logic 0) only when this line is executed.
  10. Again a delay of 1000ms.
  11. End

So you can now understand why the LEDs connected to the port B will blink. For half a second, 5V or logic 1 is coming from port B and for the other half 0V or logic 0 is coming. See how easy it is to do anything using a microcontroller!

  • Go to File-> New, write the program and save as a C file (*.c).
MPLAB - After Writing and Saving the Program

MPLAB – After Writing and Saving the Program

  • Add this C file to Source group, Right Click on Source File >> Add Files
Adding Program C file to Source Group

Adding Program C file to Source Group

  • Set the Configuration Bits using the Configuration bits tool. Click on Configure >> Configuration Bits
Configuration Bits - MPLAB

Configuration Bits – MPLAB

 

  • Build the project to generate hex file. Click on Project >> Build
MPLAB - After Building the Program

MPLAB – After Building the Program

This was just a basic demonstration. In the same manner, there are registers for numerous operations in the microcontroller. You have to set the register and provide some command and it will be carried out. PIC 16F877A also has general purpose resisters and RAM so you can even use a lot of variables and constants. It supports floating point arithmetic and ASCII values also. Programming in Hi-Tech C is just like programming in any other C compiler with some additional commands.

Circuit Diagram

Blinking LED using PIC Microcontroller - Circuit Diagram

Blinking LED using PIC Microcontroller – Circuit Diagram

8MHz crystal is used to provide the required clock for the PIC 16F877A microcontroller. 22pF capacitors are used to stabilize the oscillation of the crystal. The first pin of the microcontroller (MCLR) is the Reset pin (stands for Memory Clear) which is tied to Vdd since it is an active low input. LEDs are connected to PORTB via 470Ω resistors to limit current through them.

You can simulate the working using Proteus. If you haven’t yet started with Proteus try this tutorial. Here is also a small video, which may help you.

The above generated hex file doesn’t contain Configuration Bits, we should use the Export option in MPLAB to export the hex file with configuration bits.

  • Click on File >> Export
Exporting Hex File - MPLAB

Exporting Hex File – MPLAB

  • Click OK
Exporting Hex File - MPLAB

Exporting Hex File – MPLAB

  • Give the file name and Click Save.
Exporting Hex File - MPLAB

Exporting Hex File – MPLAB

  • Now burn this exported hex file to the microcontroller

You can download Hi-Tech C files and Proteus files here…
Blinking LED PIC Hi-Tech C



You may also like...

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

    Verify the crystal’s frequency.. value of capacitors… It will work.. without capacitors…………

    If the value of capacitor and crystal are correct…. try replacing the crystal……. with a new one..

    You should also use a good power supply… try adding a 100mf capacitor across the supply

    Also… increase the delay to 1000 ms… otherwise .. you might not be able to see .. blinking led..

  • sujith thycattussery

    Replaced crystal, added 100mf capasitor. but not working. no blink/ even not turning on led. my power supply is 12v adapter with 7805 regulator. output is 4.95v

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

    Does it works in proteus??

  • sujith thycattussery

    downloading proteus. what is the minimum working voltage for 16f877a?

  • sujith thycattussery

    proteus simulation failed. error: mixed model pic16.dll failed to authorize. Missing or invalid customer key.

    it’s a demo version of proteus. do you have the full version link??

  • sujith thycattussery

    yes, it works in proteus.

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

    5V,………… Try searching torrent for cracked versions..

  • sujith thycattussery

    yes it works in proteus, not in circuit.

  • sujith thycattussery

    changed power supply to 5v smps(5.08v). still not working.

  • sujith thycattussery

    ??????????????????????????/

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

    It should work… try changing the pin of microcontroller..
    verify that the LED is working .. by connecting it to 5V instead of .. to pic..
    If above solutions not working.. try replacing your microcontroller or programmer..

  • sujith thycattussery

    is it possible to do multi-threading in pic? (Hi tech c way)

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

    Nop….. I think so…
    What is your exact application??

  • sujith thycattussery

    programmed with pickit2. still not working. all components, circuit, hex are ok. it works well in proteus. i can read, write, detect, verify the pic using my pickit and jdm programmers.

  • sujith thycattussery

    when i add this line : __CONFIG(0xFFBA);

    the LED just light up(not blinking).

  • sujith thycattussery

    yaahoooooooooooooo it’s working..

    changed to __CONFIG(0x3F39); //for 4mhz(xt oscillator)

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

    Thanks for the feedback… sorry for the late replay.. I was busy for 2 days..
    I will update the above article with configuration bits and will write a new post on configuration bits..

  • sujith thycattussery

    I’m trying to port this code to pic 12F508. this is my code:
    #include
    #define _XTAL_FREQ 4000000
    __CONFIG(0x0FFD);
    void main()
    {
    TRISA0 = 0;
    TRISA1=0;
    RA0 = 0;
    RA1=0;
    while(1)
    {
    RA0 = 1;
    RA1=0;

    __delay_ms(500);

    RA0 = 0;
    RA1=1;
    }

    but mplab triggers this errors:undefined identifier “TRISA0″, undefined identifier “TRISA1″, undefined identifier “RA0″, undefined identifier “RA1″

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

    Please refer the datasheet of PIC 12F508… there is no PORTA

  • sujith thycattussery

    Instead what? please post the code..

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

    There is no port named A.. only C and B

  • sujith thycattussery

    #include

    #include

    #define _XTAL_FREQ 4000000

    __CONFIG(0x0FFD);

    void main()

    {

    TRISC = 0;

    TRISC1=0;

    RC0 = 0;

    RC1=0;

    while(1)

    {

    RC0 = 1;

    RC1=0;

    __delay_ms(500);

    RC0 = 0;

    RC1=1;

    }
    ERRORS:
    undefined identifier “TRISC”

    undefined identifier “RC0″

    undefined identifier “RC1″

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

    Sorry.. there is no PORTC and PORTB too..

    The pins are labeled as GP0 to GP5. please refer the datasheet ..

    http://ww1.microchip.com/downloads/en/DeviceDoc/41236A.pdf

    There are two registers.. GPIO and TRISGPIO..

  • Sirius

    Hi Tech is no longer available.

  • AnswersOfLife

    Vry useful…bt im using mikroC……>>im 2 a beginnr @ microcontroller programming..