BLINKING LED PROJECT

on Wednesday, 16 October 2013
We start  project section with the simplest project as similar as "hello world" program in C language. As this is our first project we fly slowly as we go further in this section we deal with more complicated projects.

WHAT YOU WILL LEARN FROM THIS PROJECT?
  1. Assigning ports as inputs and outputs
  2. Interfacing LED 
  3. Using tact button as input
  4. Using Delays in mikroC

AIM: If the switch(button) connected to microcontroller is pressed, then Leds should ON for 1sec and OFF for 1sec it should repeat for 10times(ON OFF shows blinking effect), again after 10times of blinking check the switch and repeat the process.

Algorithm: 

Check the switch
if switch is pressed
then ON Led
1sec delay
OFF Led
1sec delay
repeat it for 10 times
go back to check switch

Circuit Diagram:
led blinking with switch

Look at the above circuit diagram, one end of switch is connected to power source & portc.f0 and other end is grounded. Portc pin0 will be always high(i.e 1)  because there is a path from power to portc.f0 pin, when switch is pressed path will be created from power source to ground through switch so portc.f0 will be zero voltage(i.e '0') .Look at the below fig to understand clearly.
current flow through switch

So, in order to know whether switch is pressed or not we need to check its status if its '0' then switch is pressed if its '1' then switch is not pressed.

Program Code: 
Compiler used = MikroC for PIC v 5.6.1
Crystal Oscillator = 4Mhz
void main( )
{
   TRISD=0; //Portd as output
   TRISC=0xFF; //Portc as input
   Delay_ms(500);//Delay of 500ms
   unsigned char switch_status = 1;//declaring variable 'switch_status'
   while(1)//infinite loop
   {
        Switch_status = PORTC.F0;//taking input from portc pin0
        if(Switch_status = = 0)//if switch is pressed
        {
            
             for(i=0;i<10;i++)//ON and OFF Leds for 10times
             {
                 PORTD = 0b11111111;//ON all leds connected to portd
                 Delay_ms(1000);// generate delay of 1000ms i.e 1sec
                 PORTD = 0b00000000;//OFF all leds
                 Delay_ms(1000);//delay 1sec
             } 
          } 
      }
 }

Note: "0b" is binary way of representation and "0x" is the hexadecimal representation.. 0b11111111 is similar as 0xFF. 
We can write the same program in different ways, i have different reasons for chosen the above style of code.
AUTHOR: YOUSTRON SIC

5 comments:

deepak said...

is there no need of using else statement in pic microcontroller

YOUSTRON SIC said...

deepak , else is optional , it is not compulsory to use else statement with if statement...

SUSHANT KUMAR said...
This comment has been removed by the author.
SUSHANT KUMAR said...
This comment has been removed by the author.
Syed Zain Nasir said...

You should also check The Engineering Projects because they have posted a lot of Arduino Projects and PIC Mirocontroller Projects and have also designed many new Proteus Tutorials. I hope you guys are gonna like them.

Post a Comment