Sunday, 28 December 2014

Glowing LED's through PIC Microcontroller

This is a simple tutorial on how to glow LED's using PIC Microcontroller, this will be helpful to those who have very little knowledge about microcontroller's and want to get started with PIC.
To begin keep in mind that there are several PIC microcontroller's out there you can choose any if you want. Here I am using PIC16F877A (40 pin pic microcontroller) because this controller has plenty of circuits on internet, you can take a look.

Ok keep in mind we write code either in C language or Assembly, here I will use C language, because it is easy to understand. To write your code you need a software or an IDE, The three main softwares to write your code are:

2) CCS        
3) MikroC Pro

MPLAB is a little tough to begin with, I will use MikroC Pro. MikroC is easy to understand plus its help library provides enough information to begin. To burn your code on to a PIC Microcontroller we need a circuit that transfer's your code to PIC, (of course you can't connect your PIC directly to PC).

This circuit is called 'PICKIT' which is designed by microchip to burn your code to PIC. There are many versions available like PICKIT1, PICKIT2 and the latest one PICKIT3. If a PICKIT's is locally manufactured it is called a 'PIC Programmer'. PICKIT or PIC Programmer both have the same function, the difference is the manufacturer. Local PIC Programmer's are inexpensive  as compare to PICKIT. To run this PICKIT or PIC Programmer you need a separate software, the name of software is 'PICKIT2 Programmer' Software although it was developed for PICKIT2 but works with many locally PIC Programmer's as well.

Simple as this:

Some Important factors you should keep in mind:

>> Every PIC Microcontroller has different Capacity, functions and number of Pins, it depends as your requirement which one you choose.

>> There are different bits available like some PIC's are 8 bits some are 16 bits.

>> Pins are divided into Port's. So that we can control them easily through coding like, this particular PIC16F877A has 40 pins. Take a look:

Double sided arrow means pin can be used either for i/p or o/p,
Vdd, Vss and MCLR are just i/p's.

Don't confuse yourself, its just looking difficult but its not.
As I told you Pins are divided into ports like Port A, Port B, Port C and so on. (this PIC16F877A has Port A,B,C,D and E, always read data sheet before buying a PIC)

>> If port C has 8 pins then Port C is 8 bit port and will consist of RC0, RC1, RC2, RC3, RC4, RC5, RC6 and RC7 which are pin15, 16, 17, 18, 23, 24, 25 and 26 respectively. (As you can see in diagram)

>> How much port A ?
Port A has 6 pins means port A is 6 bit. i.e. RA0, RA1, RA2 RA3 RA4 and RA5 which are pin 2, 3, 4, 5, 6 and 7 respectively. (Now you can find Port B, D, and E easily)

>> To run a microcontroller you need a crystal which serves as an oscillator (it is a 2 pin component, it can be 8MHz or 16MHz or any depending on the speed you want the microcontroller to respond). The crystal is connected to OSC1 (pin13) and OSC2 (pin14). (in this PIC)

>> While programming in C, if you want to take input or output from a port, use the keyword "TRIS" followed by the port name.

>> To take input like from port B write: "TRISB = 1;"

>> To give output write "TRISB = 0;", to give output from port B.



STEP1: Write Code in MikroC Pro

To make a new project in mikroC go to the icon shown below:

A dialog box will appear, click next

Select the PIC microcontroller you will use, then click next. I am selecting PIC16F877A.

Enter the value of crystal you will use, and click next. Here I am using 16MHz crystal.

Select the directory for mikroC files,

Click next.

Skip this and click next.

Ok its done, click finish.

This will appear:

before you write code make sure to click "check all" to use all the library functions, as shown:

like this:

Write your code, I have written this one:

/*      Pic microcontroller tutorial
            Shahrukh Saleem         */

unsigned int i = 0;               //UNSIGNED Integer declared

void main()                      //main method
  TRISB = 0;                      //PORTB AS OUTPUT

 {                        //Infinie loop
  for(i=0;i<=255;i++)              //for loop from 0 to 255
    PORTB = i;                     //Send value of i to PORTB Where LED's are connected
    Delay_ms(250);                 //Delay of 250,

(download link of .hex file is provided at the end)

We will discuss about this code, latter.

After your done goto Project-->>Build, short key is Ctrl+F9. This build will generate a .hex file. This is the file which is to be burn on your PIC Microcontroller.

If no errors are found you will see this:

Now, lets test it on proteus. Make the circuit as shown:

Double click on controller, a dialog box will appear, locate this hex file that you generated, it will be in the project's folder. Select 16MHz crystal, since during coding we selected 16MHz.

Double click on crystal, select 16MHz from here as well.

Every thing is done, play the circuit the led's will glow according to binary.

STEP2:(Connect PIC Programmer)

Connect your PICKIT or PIC Programmer to your PC. From image below see the pic configuration of PICKIT2, other PIC Programmer also have similar pin configuration like this, locate the pin of pic programmer and through jumper wires connect it to PIC16F877A,

From diagrams above we can see that:
Pin1 of PICKIT2 = Pin1 of PIC
Pin2 of PICKIT2 = Pin11 and Pin32 since both are Vdd
Pin3 of PICKIT2 = Pin12 and Pin31 since both are Vss (Ground)
Pin4 of PICKIT2 = Pin40 (PGD Pin)
Pin5 of PICKIT2 = Pin39 (PGC Pin)
Pin6 is not connected.


After making these connections open PICKIT2 Programmer Software, (you can download it free from here: )

1) Now click Read button
2) Click Erase 
3) Click Blank Check
4) Go to file-->> import hex and select the hex file that you generated through MikroC Pro
5) Click Write
6) Click Verify

Now you have successfully transferred your code to your PIC. 

STEP4: (Implement on Hardware)

here is the video take a look :

Understanding the CODE:

unsigned int i = 0;               //UNSIGNED Integer declared

void main()                      //main method
  TRISB = 0;                      //PORTB AS OUTPUT

 {                        //Infinie loop
  for(i=0;i<=255;i++)              //for loop from 0 to 255
    PORTB = i;                     //Send value of i to PORTB Where LED's are connected
    Delay_ms(250);                 //Delay of 250,

In this code an integer is declared which is "i", this is an unsigned integer which has range from 0 to 255 signed integer's range from -127 to +127. After his there is the main method, in which Port B is defined as output so TRISB = 0 (discussed before).

After that take a look at the inner for loop since initially "i" = 0 then PORTB = 0 means PORTB = 0000 0000, when "i" = 1 then PORTB = 1 means PORTB will display 0000 0001. For example if we have "i" = 15 then PORTB = 15 this means PORTB = 0000 1111 since PORTB is 8 bit thats why from LSB to MSB only 4 bits are filled. ( Note that RB0 is LSB while RB7 is MSB ) when "i" reaches 165 then PORTB = 165 means PORTB = 1010 0101, this implies only LED's at RB0, RB2, RB5 and RB7 will glow.

Now you must be wondering why I used the "Delay_ms(250);" ? Well if you delete this line then the LED's will turn on and off so quickly that you can't watch it through naked eye. To increase the delay time, this Delay_ms(<value>) is used the value is how much delay you want. Whenever the value of i changes and it goes to PORTB you can watch it through the delay like whats happening on PORTB.

Now this whole is included in an infinite for loop 
{ .....
Obviously you don't want this circuit to stop after the value of i reached to 255, if you want this program to run again and again then this infinite loop is used whenever the compiler terminates and executes the whole program, due to this infinite loop it executes the program again.

OK SO THIS WAS A SIMPLE TUTORIAL ON HOW TO GET STARTED WITH PIC MICROCONTROLLER, FOR MORE INFORMATION YOU CAN READ THE BOOK "Pic Microcontroller and Embedded Systems using Assembly and C for PIC18 family by Muhammad Ali Mazidi"

Download the hex and proteus files: 

You can share this tutorial with your friends so other people can get the most out of it. 

Shahrukh Saleem Qureshi

No comments:

Post a Comment