LED not blink

Author
teacher mips
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2014/06/15 10:02:47
  • Location: 0
  • Status: offline
2014/06/15 10:14:16 (permalink)
0

LED not blink



I am a pure beginner to PIC microcontrollers and want to Blink a LED with it (as my first test).
 
But at the end the LED did not blink, I performed the following steps,
 
1. Generate Hex file from MPLAB X IDE v2.05
2. Flash the PIC16F877A with this hex file using a PIC programmer.
3. Connect a LED to PIC16F877A on breadboard to test my work.
 
Please have a look at the detailed steps with screen shots at
 
My code is as

#include <xc.h>

void delay( unsigned int );

void main()
{
 unsigned int DELAY = 100;

 TRISC=0x00;

 while(1)
 {
  PORTC = 0x00; //led off
     delay(DELAY);

  PORTC = 0x01; //led on
  delay(DELAY);
 }
}

//DELAY FUNCTION ( mS )
void delay( unsigned int time )
{
    unsigned int i;
   unsigned int j;
  for( i=0; i<time; i++ )
  for( j=0; j<1275; j++ ) {;}
}


 
and my breadboard circuit is,
 

But at the end I could not get the LED blinking (LED glows but not blinks).
So where I was wrong? Does the PIC16F877A not have an  on chip clock? Whether I have to connect an external crystal?? Or my program is incorrect/incomplete?? Or anything I am missing….??
 
Please help me I am in a great need of it. Please help.
 
Thanks in advance.
#1

18 Replies Related Threads

    Horacio T
    Senior Member
    • Total Posts : 66
    • Reward points : 0
    • Joined: 2013/09/10 16:57:57
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/06/16 10:49:23 (permalink)
    0
    Hi, if the circuit is as shown , the external clock  is missing. You may use crystal or the simpler RC oscilator. Refer to your  micro datasheet. bye!
    #2
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: LED not blink 2014/06/16 13:10:50 (permalink)
    0
    And anyway I cannot see your CONFIG settings...

    GENOVA :D :D ! GODO
    #3
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: LED not blink 2014/06/16 13:29:06 (permalink)
    +1 (1)
    and you need a pullup on MCLR, and a small bypass capacitor (say 100nF) mounted as close as possible to each pair of power pins.
    Which "PIC programmer" are you using?
    post edited by ric - 2014/06/16 13:34:09

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    tomsedlack
    Super Member
    • Total Posts : 152
    • Reward points : 0
    • Joined: 2010/01/14 09:01:53
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/06/17 04:22:49 (permalink)
    +1 (1)
    If DELAY=100 is in milliseconds, you may not see a flash as it's too fast. On my third edit, I finally noticed that you said the LED glows. It is probably running too fast to see a blink so you need to increase your delay. The rest of the stuff below can be taken as a tip rather than a solution.
     
    I might suggest that you just try and turn the LED on to start. This will allow you to concentrate on the proper CONFIG settings, clock, etc. Once you can get the LED to turn on, you'll know that the PIC is running so you can then add the ON/OFF code.
     
    Since you are new to PIC (and possibly programming), try to debug in small pieces. Your program is relatively simple yet it doesn't work. Break it up into smaller pieces and debug each separately. Start with the code that sets up the PIC to actually run your code (no sense debugging the blink part if you're not sure that the PIC is even running). Once you get that section working, add the other parts; the enhancements. Use this same methodology throughout your programming career. Break the debug up into manageable pieces. Once all the pieces work, you can then put them all together and debug the system if it's even needed.
     
     
    post edited by tomsedlack - 2014/06/17 04:34:28
    #5
    pcbbc
    Super Member
    • Total Posts : 516
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/06/17 04:52:21 (permalink)
    0 (1)
    Also, if you plan on doing even a modest amount of work on PICs, the investment of £30/$50 in a basic PICkit3 debugger is more than worth it.
     
    Debugging in the simulator is useful (had you used the stopwatch and a breakpoint in the above code it would have immediately shown you a 100ms delay), but for some projects there's nothing quite as useful as being able to debug on the real hardware.
    #6
    teacher mips
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2014/06/15 10:02:47
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/06/19 21:49:50 (permalink)
    0
    Lots of thanks you all, I modified my circuit and code as,
     
    #include <xc.h>

    // Configuration word for PIC16F877
    __CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);

    void delay( unsigned int );

    void main()
    {
       unsigned int DELAY = 100;

            TRISD=0x00;
            PORTD = 0x00;
           
       while(1)
       {
          PORTD = 0x00;       //led off
               delay(DELAY);

          PORTD = 0x01;       //led on
          delay(DELAY);
       }
    }

    //DELAY FUNCTION ( mS )
    void delay( unsigned int time )
    {
        unsigned int i;
         unsigned int j;
          for( i=0; i<time; i++ )
          for( j=0; j<1275; j++ ) {;}
    }
    But...Huh........bad luck, again I did not get the LED even turned ON.

    Please help me.

    Waiting for your reply...........
    Thanks in advance.
    #7
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: LED not blink 2014/06/19 21:54:49 (permalink)
    0
    What change did you make to your circuit?
    Do you have a pullup on MCLR now?
    Do you have bypass capacitors on both pairs of power pins?
    Did you connect a crystal and load capacitors to the OSC1/OSC2 pins?

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: LED not blink 2014/06/19 21:58:49 (permalink)
    +1 (1)
    When your hardware works, you may still have a problem with your delay routine.
    As it doesn't do anything useful, the compiler is free to optimise it away altogether.
    You are better off using the built in function for delays.
    Look up __delay_ms(x) on page 330 of the XC8 user manual.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    teacher mips
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2014/06/15 10:02:47
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/06/20 23:45:01 (permalink)
    0
    I changed my circuit as this, but no luck even now LED (tested OK) does not even turned ON
    Please help me.......
    Thanks in advance.
    #10
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: LED not blink 2014/06/21 01:58:04 (permalink)
    0
    Do you have access to a logic probe or an oscilloscope to check if your crystal is oscillating?
    You never answered the question about "which PIC programmer are you using?"

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    teacher mips
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2014/06/15 10:02:47
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/06/21 09:51:07 (permalink)
    0
    Oh sorry, The programmer I am using is a JDM programmer purchased from ‘’ site.
    You can view the detailed steps with screen shots at : (my google drive public link)
     
    Note that the program and circuit connection diagram is now the updated one as
    #include <xc.h>

    // Configuration word for PIC16F877
    __CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);

    void delay( unsigned int );

    void main()
    {
       unsigned int DELAY = 100;

            TRISD=0x00;
            PORTD = 0x00;
           
       while(1)
       {
          PORTD = 0x00;       //led off
               delay(DELAY);

          PORTD = 0x01;       //led on
          delay(DELAY);
       }
    }

    //DELAY FUNCTION ( mS )
    void delay( unsigned int time )
    {
        unsigned int i;
         unsigned int j;
          for( i=0; i<time; i++ )
          for( j=0; j<1275; j++ ) {;}
    }
     
    and the circuit..........

    Actually I am a hobbyist and student also, so I can't afford an oscilloscope, but I will try to get in my college lab.
     
    But as far as if your crystal is oscillating? I used the same crystal with the same pair of 22pF caps with my AVR Atmega16 and 8051 MCU and it works fine.
     
    I think the problem lies in my connection or I am writing the code incorrectly (specially config bits)
     
    Please help me to fix the problem.........
     
    Thanks in advance.
     
    #12
    teacher mips
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2014/06/15 10:02:47
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/06/21 10:06:16 (permalink)
    0
    I was unable to post links to my JDM programmer site and detailed steps file,
     My JDM programmer site : www.elesof.com/electronics/product/picipro
     
    My detailed steps file: drive.google.com/file/d/0B5NYQC3oiHcHeGJERzBaZ1NDSnc/edit?usp=sharing
    (do no take code and circuit from this file, it contains old code and circuit, I have posted my updated code with configuration bits anfd updated curcuit with crystal, decoupling caps etc in my just previous reply, please get it there)
     
    Please help me........
    Thanks in advance
    #13
    Victor Bordoloi
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2014/10/04 10:54:56
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/10/24 02:44:20 (permalink)
    0 (1)
    Try connecting a 4148 diode instead of 10K resistor at the MCLR pin.
    #14
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: LED not blink 2014/10/24 02:56:26 (permalink)
    +1 (1)
    Victor Bordoloi
    Try connecting a 4148 diode instead of 10K resistor at the MCLR pin.


    Don't!
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #15
    Victor Bordoloi
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2014/10/04 10:54:56
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/10/24 03:19:43 (permalink)
    0 (1)
    ric
    Victor Bordoloi
    Try connecting a 4148 diode instead of 10K resistor at the MCLR pin.


    Don't!
     


    i am using a 4148 diode cathode towards the Vdd.
    #16
    ric
    Super Member
    • Total Posts : 22101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: LED not blink 2014/10/24 03:31:06 (permalink)
    +1 (1)
    In which case it may as well not be there. It is the built-in pullup doing all the work.
    Don't bother with the diode, the 10k resistor IS the recommended  component to put there.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #17
    Victor Bordoloi
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2014/10/04 10:54:56
    • Location: 0
    • Status: offline
    Re: LED not blink 2014/10/24 04:58:27 (permalink)
    0
    yes 10k res also works just fine.i am not able to what mistake he has done.prog and config looks ok. and also the circuit.
    #18
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: LED not blink 2014/10/24 05:05:10 (permalink)
    0
    Microchip used to have that in its schematics Smile

    GENOVA :D :D ! GODO
    #19
    Jump to:
    © 2018 APG vNext Commercial Version 4.5