How to blink an LED using PIC microcontroller

Microcontroller Topics
Post Reply
User avatar
Shehani
Lieutenant
Lieutenant
Posts: 61
Joined: Mon Aug 19, 2013 2:11 pm

How to blink an LED using PIC microcontroller

Post by Shehani » Tue Oct 01, 2013 3:59 pm

Introduction

Hi friends…………….my first step in the field of microcontrollers is blinking an LED using PIC microcontroller and MikroC. I used PIC 16F877A for that. It is the one of the most popular PIC microcontroller and comes in a 40 pin DIP pinout. I use MikroC Pro for PIC as the C compiler because it has a lot of built in functions which makes our task very simpler.


Using MikroC

You can buy mikroC form mikroElectronika or download a trial version form here. Trial Version is sufficient for most of our needs.

1. Download and Install mikroC.

2. Open microC Pro For PIC
1.jpg
1.jpg (23.56 KiB) Viewed 3315 times
3. Click on Project>> New Project
2.jpg
2.jpg (55.98 KiB) Viewed 3315 times
4. Click NEXT
3.jpg
3.jpg (28.62 KiB) Viewed 3315 times
5. Select PIC16F877A and Click NEXT
4.jpg
4.jpg (29.29 KiB) Viewed 3315 times
6. Set Device Clock as 8Mhz and Click NEXT
5.jpg
5.jpg (32.55 KiB) Viewed 3315 times
7. Set path of your project folder. It is better to create a new folder for each project. Click NEXT
6.jpg
6.jpg (40.34 KiB) Viewed 3315 times
8. In this step you can add your on header files etc… if you want to use them in this project, otherwise leave it. Click NEXT
7.jpg
7.jpg (44.02 KiB) Viewed 3315 times
9. It is better to select ‘Include All’ for beginners. Click NEXT
8.jpg
8.jpg (37.91 KiB) Viewed 3315 times
10. Thus you have successfully completed New Project Wizard in mikroC
9.jpg
9.jpg (27.73 KiB) Viewed 3315 times

Circuit Diagram and MikroC Programming


mikroC Source Code:

Code: Select all

void main()
 {
      TRISB.F0 = 0  // set direction of RBO to output
                 //or TRISB = 0xFE (0xFE = 11111110)

      /* 0 at a bit of TRIS register makes corresponding
     pin output while 2 makes it input*/
      do // To set infinite loop
      {
        PORTB.F0 = 1; // Set RB0 to high
        Delay_ms(500); // 500 mili seconds delay
        PORTB.F0 = 0; // Set RB0 to low
        Delay_ms(500); // 500 mili seconds delay
     }while(1); // To set infinite loop
 }
11. Then enter the above source code.

12. Save it

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

The hex file will be generated in you Project Folder.

Circuit Diagram:
10.png
10.png (45.59 KiB) Viewed 3315 times
Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.

PIC 16F877A has no internal oscillator, so we should connect external oscillator. Two 22pF capacitors are used to stabilize the operation of the Crystal Oscillator. A low on MCLR pin will reset the PIC, so we tie it to VDD to avoid accidental RESET.




Simulating in Proteus

The project can be simulated in your PC by using the software Proteus. You can buy Proteus from Labcenter Electronics.

1. Download and Install Proteus

2. Open Proteus, named as ISIS Professional
11.jpg
11.jpg (203.45 KiB) Viewed 3315 times
3. Click P (Pick from Libraries) in the Devices column on the Left side of the IDE
12.jpg
12.jpg (63.75 KiB) Viewed 3315 times
4. From here you can add the devices that you uses in the project. You can easily find out the devices using the keyword search on the top left side. Add all the devices 16F877A, Crystal, 22pF, Battery, LED, resistor.
13.jpg
13.jpg (204.07 KiB) Viewed 3315 times
5. Select components from the Devices List and place it on the Root Sheet, then you can easily wire them using your mouse. You can edit the properties of each devices by double clicking on it.
14.jpg
14.jpg (187.79 KiB) Viewed 3315 times
Note: VDD and VSS pins of PIC are hidden, so for the proper working we should define VDD and VSS as shown above by placing wire label (you can place this by right clicking on a wire).

6. Then give the path of hex file to the PIC, by double clicking on it.
15.jpg
15.jpg (65.71 KiB) Viewed 3315 times
7. Click on the play button on the bottom left side of the IDE to simulate the circuit.
16.jpg
16.jpg (187.81 KiB) Viewed 3315 times
Here is a video tutorial too..

[media]http://www.youtube.com/watch?v=h5FlUZdQo-M[/media]


Article courtesy of electrosome.com
Post Reply

Return to “Microcontrollers”