Blinking LED using PIC Microcontroller with Hi-Tech C

Blinking LED using PIC Microcontroller with Hi-Tech C

Contents

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(1000);
    PORTB=0X00;
    __delay_ms(1000);
  }
}

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=0x00 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
PORT and TRIS Register in PIC Microcontroller
PORT and TRIS Register in PIC Microcontroller

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.

Video

Here is also a small video, which may help you.

Exporting Hex File

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

Download Here

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

Share this post

  • You have got one thing wrong. To initialize pins as output, TRIS registers need to be set to 0, not one.

  • need a lil help, i have 16F876A, with the folowing code

    start

    banksel TRISB
    banksel TRISC
    movlw 0XF0
    movwf TRISB
    movwf TRISC
    movlw 0XF0
    banksel PORTB
    banksel PORTC

    main

    bcf PORTB,0
    bcf PORTC,3
    ; nop
    ; nop
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b

    bsf PORTB,0
    bsf PORTC,3
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    bcf PORTC,0
    bcf PORTC,1
    bcf PORTC,2
    bcf PORTC,4
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    call delay8b
    goto main

    delay8b

    movlw 0xFF
    movwf TEMP1
    d13 movlw 0xFF
    movwf TEMP2
    decfsz TEMP1
    goto d12
    goto d1r; RET
    d12 decfsz TEMP2
    goto d12
    goto d13
    d1r return
    ; remaining code goes here
    goto main

    END ; directive ‘end of program’
    but I can’t make my rgb to function, can any1 help me ?

  • hi,
    plz help me with this.
    cofig as same it given in above exmple.

    #include
    #define _XTAL_FREQ 4000000
    void main()
    {
    TRISB=0X00;
    PORTB=0X00;
    while(1)
    {
    PORTB=0XFF;
    _delay_ms(100);
    PORTB=0X00;
    _delay_ms(100);
    }
    }

  • How to program the PIC microchip? where will i program it and where will i plug it? new student here.

  • Sir, I am a student.I recently start to program of PIC16f877a.for blink led.I installed
    1. MPLAB IDE v8.92,
    2. picc-9_82 win,
    3. picclite-setupnew,
    4. univesal toolsuite -1.37 on my computer.

    and I learn about c code, like decimal,binary,hexadecimal and octal language.
    I showed some example from web site.I try to built blinkled.c file.but the feedback will built failed.Pls anyone help me to built binkled.c program.wait for feedback.

    thanks you……

  • Help my Proteus 8 shuts down each time I run a simimulation, any assistance pls. Its a cracked version.

  • when i press program the target device to my microcontroller it shows this error. how do i resolve this???

  • Plz tell me Sir.I want correct shift c program file…..16f628a
    void main()
    {

    TRISB = 0x00; // Sets all pins in PORTB as output
    PORTB = 0b00000001; // Set RB0 to high 00000001
    TRISA = 0x00; // Sets all pins in PORTB as output
    PORTA = 0b00000000; // Set RB0 to high 00000001

    CMCON = 0x07; // To turn off comparators
    ADCON1 =0x06; //Turne off adc

    do // To set infinite loop
    {
    Delay_ms(100); // 300 mili seconds delay
    PORTA = PORTA<= 0b10000000) //To reset to 00000001
    { //when the count becomes 10000000
    Delay_ms(100);
    PORTA = 0b00000000
    PORTB = 0b00000001
    }
    if(PORTB >= 0b00000001) //If RB7 =1
    {
    Delay_ms(100);
    PORTB = PORTB<= 0b10000000) //To reset to 00000001
    {
    Delay_ms(100); //
    PORTB =0b00000000; //RB low
    PORTA =0b00000001;
    }
    }while(1); // To set infinite loop
    }

  • Error [800] C:Program FilesMicrochiptprogramf.c; 483. undefined symbol “entry__ms” what does it means in pic10f206

  • hi
    when i compile this code this error occurs can you please help? mplab v8.33 i am using
    Error [499] ; 0. undefined symbol:

    __delay_ms(test del.obj)

  • Dear sir
    Plz guied me I need a simple program for LED1 to LED8.

    task_1
    when S1 unpressed ;

    {LED=11111110;}
    (HOLD IT for 2sec)

    {LED=11111101;}
    (HOLD IT for 2sec)

    {LED=11111011;}
    (HOLD IT for 2sec)

    {LED=11110111;}
    (HOLD IT for 2sec)

    {LED=11101111;}
    (HOLD IT for 2sec)
    Repeat this function every time till S1 unpressed .

    task_2
    but when S1 pressed for 1time within 1sec

    {LED=11111110;}
    (HOLD IT for 20sec)
    & ofter 20 sec exit form task_2 & Repeat this function task_1

    but when S1 pressed for 2time within 1sec

    {LED=11111101;}
    (HOLD IT for 20sec)
    & ofter 20 sec exit form task_2 & Repeat this function task_1

    but when S1 pressed for 3time within 1sec

    {LED=11111011;}
    (HOLD IT for 20sec)
    & ofter 20 sec exit form task_2 & Repeat this function task_1

    but when S1 pressed for 4time within 1sec

    {LED=11101111;}
    (HOLD IT for 20sec)
    & ofter 20 sec exit form task_2 & Repeat this function task_1

  • Read the datasheet of PIC 12F508 carefully.. there is no TRIS, PORT register instead of it have TRISGPIO and GPIO registers..
    Try using GP0, GP1.. .instead of RA0, RA1..

  • Can you please give some detailed explanation for these words 0b00000000, 0X00, 0XFF. ?? or post a tutorial on “use of hexadecimal, decimal, binary values in microcontroller programming”.

  • what that command _CONFIG(0X3F39); doing??
    what are configuration bits ? why we need them?
    and one can u tell me the declaration for _delay_ms(100);

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

  • #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”

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

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

  • yaahoooooooooooooo it’s working..

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

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

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

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

  • 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

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


  • >