Led not blinking using 8051!

Discussion in 'Embedded Systems and Microcontrollers' started by astrotom, Jul 23, 2012.

  1. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    Hey guys!

    I setup a breadboard circuit to do the basic single led blinking test using a AT89C52 chip. However, I do not get any blinking, it just lights up until power is stopped. I had only enabled a single bit for port0.0. The code is below. However, the led lights up even if I connect it to any other port. Is it a problem with my code? Please check it. Thank You.

    Code ( (Unknown Language)):
    1.  
    2. #include<reg52.h>
    3. sbit led = P0^0;
    4.  
    5. void delay(int time)        //This function produces a delay in msec.
    6. {
    7.     int i,j;
    8.     for(i=0;i<time;i++)
    9.      for(j=0;j<1275;j++);
    10. }
    11.  
    12. void main()
    13. {
    14.      while(1)
    15.      {
    16.           led=0;
    17.           delay(500);
    18.           led=1;
    19.           delay(500);
    20.      }
    21. }
    22.  
     
    Last edited by a moderator: Jul 24, 2012
  2. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    50ms is too short. Try 500ms.
     
  3. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    It still doesn't work. Also as I said, the led lights up even in other ports when its supposed to light up only at Port0.0.

    Anyways thanks for replying.
     
  4. shubham161

    Member

    Jul 22, 2012
    50
    3
    first thing, don't use port 0. use port 1 or port 2 or port 3.

    and yes, the led will light up in port 1, port 2 and port 3 automatically without any programming because they have a +1 logic on their pins by default. but you have to make them "logic 0" by your code.

    Code ( (Unknown Language)):
    1.  
    2. /* This program is written by Shubham */
    3. #include <stdio.h>
    4. #include <reg52.h>
    5.  
    6. sbit led = P1^0;
    7.  
    8. void delay(unsigned int msec)  // Function to provide time delay in msec.
    9. {
    10. int i,j ;
    11. for(i=0;i<msec;i++)
    12. for(j=0;j<1275;j++);
    13. }
    14.  
    15. void main()
    16. {
    17.            while(1)
    18.             {
    19.                 if(led == 1)
    20.                 {
    21.                    led = 0;
    22.                    delay(50);
    23.                 }  
    24.  
    25.                else{
    26.                       led = 1;
    27.                       delay(50);
    28.                }
    29.            }
    30. }
    31.  
    32.  
    Edit: your code will work fine, if you connect the cathode of your led to your port 0 pin and +5V to anode of your led. try it!!

    and I've made the program using if else because later you may want to connect two leds together to make a dancing light. you can use if else to turn on one led and turn off other led.

    Code ( (Unknown Language)):
    1.  
    2. /* This program is written by Shubham */
    3. #include <stdio.h>
    4. #include <reg52.h>
    5.  
    6. sbit led1 = P1^0;
    7. sbit led2 = P1^1;
    8.  
    9. void delay(unsigned int msec)  // Function to provide time delay in msec.
    10. {
    11. int i,j ;
    12. for(i=0;i<msec;i++)
    13. for(j=0;j<1275;j++);
    14. }
    15.  
    16. void main()
    17. {
    18.            while(1)
    19.             {
    20.                 if(led1 == 1)
    21.                 {
    22.                    led1 = 0;
    23.                    led2 = 1;
    24.                    delay(50);
    25.                 }  
    26.  
    27.                else{
    28.                       led1 = 1;
    29.                       led2 = 0;
    30.                       delay(50);
    31.                }
    32.            }
    33. }
    34.  
    35.  
     
    Last edited: Jul 23, 2012
  5. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    Thanks. I am quite confused by the default values of the bits. If I want to output something, I should set the bit as 0, right?
     
  6. shubham161

    Member

    Jul 22, 2012
    50
    3
    actually, to light an led -
    1) ground the cathode terminal (logic 0)
    2) give +5 volt to the anode terminal (logic 1)

    now there are two ways to achieve this requirement.

    First Way: connect the cathode of your led to ground (i.e logic 0) and anode of your led to your microcontroller pin. when the microcontroller pin will be in "logic 1" the led will glow.

    Second Way: connect the cathode of your led to microcontroller pin and anode of your led to +5V supply (logic 1). When the microncontroller pin will be in "logic 0" the led will glow.

    most probably you were using the first way. you have connected your led cathode to ground. So to light an led you must give "logic 1" to anode terminal of your led. So whenever you connect that terminal to port 1, port 2 and port 3 they light up automatically because they have "logic 1" by default.

    If you don't want your led to automatically light up on port 1, port 2 and port 3, then use this code on your program

    Code ( (Unknown Language)):
    1.  
    2. P1 = 0x00;
    3. P2 = 0x00;
    4. P3 = 0x00;
    5.  
    remember that the longer terminal of your led is anode and shorter terminal is cathode.
     
    astrotom likes this.
  7. Papabravo

    Expert

    Feb 24, 2006
    10,145
    1,791
    Point #1: Port 0 is used for inputs or OPEN DRAIN outputs. An open drain output requires an external pullup resistor. You can connect the pin to the LED to a resistor to Vcc and light the LED with the correct polarity.

    Point #2: Ports #1, #2, and #3 are "quasi bi-directional ports". This is because they can sink more current in the low state than they can supply in the high state. Read the datasheet very carefully looking for specifications on output current under various conditions. Symbols you might find include:

    1. Vol (Vee sub oh ell) -- Voltage with output low and sinking current Iol
    2. Voh (Vee sub oh aitch) -- Voltage with output high and sourcing current Ioh
    Unlike some parts, the outputs on an 8051 do NOT have symmetrical drive characteristics and tradition requires vendors to continue to make them that way.
     
  8. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    I am having no luck in getting the led to blink. Is it a problem with my flasher? My hex/bin flasher shows no error though. It says everything is successful. However I have noticed that when I load the hex file, it shows its file size as less than the actual file size. For eg: A hex file 231 bytes, after loading is shown as 81 bytes.

    Or is it my wiring? Here is my breadboard wiring: Consider the power bus numbered 1-50 with hole '1' above the micro and hole '50' below.

    The left side of the micro controller is connected in Column C from rows 21-40.
    The other side is connected to column G.
    The VCC of micro is connected to power bus 6 to the left of micro.
    The VPP is connected to power bus 11 to the left of the micro.
    The GND pin is connected to power bus 50 (which is to the left and below the MU)
    The anode of LED is connected to P1_0. Cathode is connected to C43
    330ohm resistor is connected to B43 and Power Bus 15 to the left of MU.
    The source is connected to Bus1 of both +ve and -ve rows to the left of MU.

    I have noticed that, halfway through the power bus lines, that is after hole '25', there is no current flowing. So my MCU's gnd pin might not be really grounded. But if I connect the pin to a hole before hole '25', the LED does not light up. Please note, I have connected the the pins in the right power bus, that is +ve one on the red bus, -ve on the blue.
     
  9. Robin Mitchell

    Well-Known Member

    Oct 25, 2009
    734
    200
    Only if it is an open collector. Remember that!
     
  10. shubham161

    Member

    Jul 22, 2012
    50
    3
    hey, do you use any simulation software? i use proteus. They solve most of my problem.

    Edit: And try to understand what papabravo said. Most probably it will solve your problem.

    see, what i find on google search

    and from the AT89C51 datasheet

     
    Last edited: Jul 24, 2012
  11. astrotom

    Thread Starter New Member

    Jul 14, 2012
    16
    0
    Well, thanks guys. Now I have understood what port 0 does. However after implementing the correct code, I still couldn't get the led to blink using a breadboard. I then picked up my friend's 8051 development board, and Voila! It works. So I am guessing its my lack of knowledge with breadboard setup which is causing the problem. Can somebody please suggest a complete functional circuit for 8051 on a breadboard?

    @Shubham, I have downloaded Multisim. Its a breadboard emulator. I am installing it now. Is it good? I didnt choose Proteus because I had no intentions on using a PCB.
     
  12. shubham161

    Member

    Jul 22, 2012
    50
    3
    yes, in our college we use multisim in our simulation lab. I have not attended any of the lab yet so i don't know how is it but since they use it in college, i think its a nice software. And it is created by national instruments and they make great products.
     
Loading...