Advertisement
wiring diagram

PIC Microcontroller Intro – Tutorial #3

This chapter, like its predecessor, focuses both in hardware and software related to PIC microcontrollers. In the second part, we made an attempt to blink one LED through a PIC microcontroller PIC12F675 with the help of a pre-processed (hex) code. Here, we are going to learn more about writing our own code for such a simple LED project.

What is a compiler? Compiler is a program that decodes instructions written in a higher order language and produces an assembly language program. MikroC is a best compiler for beginners as it contains built in functions for most of the commonly used tasks. You can download a trial/limited version of MikroC from here (http://www.mikroe.com/mikroc/pic/). Note that, this trial version is limited to 2K of program words, but is sufficient for most of our learning applications.

First of all, you should install the downloaded MikroC PRO For PIC in your PC and open it. Now click on Project and then New Project. In the New Project Wizard window, click Next, enter Project name, Project Folder, Device Name (microcontroller) , Device Clock (clock frequency), and click Next (here we use PIC 16F877A microcontroller with 8MHz crystal). At this time just click Next in the Add Files to Project window.

Click Next in Library Manager after selecting Include All (default) in Include Libraries, click Finish to complete the New Project Wizard. This will open the editor window where you can enter the code.

What is next? Enter the code (shown at the end of this paragraph), Save it and then Compile it by clicking Build (or Ctrl+F9). This will generate a hex file in your project folder. You need to burn this hex file into the PIC16F877A microcontroller using your PIC programmer, as done earlier. Finally, finish the hardware as per the wiring diagram included here.

wiring diagram

This time, the microcontroller is PIC16F877A! VDD and VSS of PIC Microcontroller is connected to +5V and GND respectively, and the 8MHz crystal is used to provide necessary clock for the microcontroller. Two 22pF capacitors stabilizes the oscillations of the crystal. One Red LED is connected to the one PORTB pin and a 1K Ω resistor is connected in series with it to limit the operating current of the LED.

Our code for PIC 16F877A microcontroller blinks the connected LED with a delay of 1 second. Okay, but how? What is behind this little code?

void main()
{
   TRISB.F0 = 0;
   while(1)
   {
      PORTB.F0 = 1;
      Delay_ms(1000);
      PORTB.F0 = 0;
      Delay_ms(1000);
   }
}

TRIS stands for TriState, which determines the direction of each GPIO pin. Output pins of a PIC Microcontroller is divided in to different PORTS containing a group of GPIO (General Purpose Input Output Pins). 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. And, 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 (H) state and Logic 0 at a particular bit of PORT register makes the corresponding pin at Logic-Low (L) state, if that pin is an Output pin (TRIS bit is 0).

TRISB.F0 = 0;→ Makes 0th bit of PORTB (PORTB0 or RB0) Output while(1)→ An infinite loop
PORTB.F0 = 1;→ Makes 0th bit of PORTB at Logic-High state
PORTB.F0 = 0;→ Makes 0th bit of PORTB at Logic-Low state

The 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. Okay, now our code is well-defined!

void main()
{
   TRISB.F0 = 0; // PORT B0 as output
   while(1) // Infinite Loop
   {
      PORTB.F0 = 1; // LED connected at RB0 ON
      Delay_ms(1000); // 1 Second Delay
      PORTB.F0 = 0; // LED connected at RB0 OFF
      Delay_ms(1000); // 1 Second Delay
   }
}
PIC16F877A 40-pin Enhanced Flash Microcontroller

(PIC16F877A: 40-pin Enhanced Flash Microcontroller)

If you are in doubt, follow this wiring diagram to learn how to connect your PIC Kit 2 programmer with the PIC16F877A microcontroller. Also refer Part-2 of this tutorial!

PIC Kit 2 programmer

→ Part 4 (soon)
Part 2

3 Comments

Join the conversation!

Error! Please fill all fields.
  • i_shalkami

    please
    my apply make the led light but no blinking the code copy and past
    the simulation is working but in actual apply the led light static

    • T.K.Hareendran

      In the forthcoming part of this tutorial, you can find useful tips on setting configuration bits for PIC microcontrollers. Stay tuned…

    • T.K.Hareendran

      When you tried to write the hex file on PIC, any warning or error message raised at that time?

Looking for the latest from TI?