Led not blinking using 8051!

Thread Starter

astrotom

Joined Jul 14, 2012
16
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.

Rich (BB code):
#include<reg52.h>
sbit led = P0^0;

void delay(int time)        //This function produces a delay in msec.
{
    int i,j;
    for(i=0;i<time;i++)
     for(j=0;j<1275;j++);
}

void main()
{
     while(1)
     {
          led=0;
          delay(500);
          led=1;
          delay(500);
     }
}
 
Last edited by a moderator:

Thread Starter

astrotom

Joined Jul 14, 2012
16
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.
 

shubham161

Joined Jul 22, 2012
47
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.

Rich (BB code):
/* This program is written by Shubham */
#include <stdio.h>
#include <reg52.h>

sbit led = P1^0;

void delay(unsigned int msec)  // Function to provide time delay in msec.
{
int i,j ;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}

void main()
{
           while(1)
            { 
                if(led == 1)
                {
                   led = 0;
                   delay(50);
                }   

               else{
                      led = 1;
                      delay(50);
               }
           }
}
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.

Rich (BB code):
/* This program is written by Shubham */
#include <stdio.h>
#include <reg52.h>

sbit led1 = P1^0;
sbit led2 = P1^1;

void delay(unsigned int msec)  // Function to provide time delay in msec.
{
int i,j ;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}

void main()
{
           while(1)
            { 
                if(led1 == 1)
                {
                   led1 = 0;
                   led2 = 1;
                   delay(50);
                }   

               else{
                      led1 = 1;
                      led2 = 0;
                      delay(50);
               }
           }
}
 
Last edited:

Thread Starter

astrotom

Joined Jul 14, 2012
16
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?
 

shubham161

Joined Jul 22, 2012
47
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?
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

Rich (BB code):
P1 = 0x00;
P2 = 0x00;
P3 = 0x00;
remember that the longer terminal of your led is anode and shorter terminal is cathode.
 

Papabravo

Joined Feb 24, 2006
21,094
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.
 

Thread Starter

astrotom

Joined Jul 14, 2012
16
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.
 

shubham161

Joined Jul 22, 2012
47
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.
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

if you want to light an LED with an open drai pin, the anode of the LED must be tied to say, +5 volts, through a series resistor, and then you can drive the cathode of this LED low (lighting the LED) with an open-drain pin. This is called "sinking" current.

You cannot, however, tie the cathode of the LED to ground through a series resistor, and drive the anode of the LED with the open drain pin. That would be called "sourcing" current, and an open-drain pin cannot do this.
and from the AT89C51 datasheet

Port 0 is an 8-bit open drain bidirectional I/O port.
 
Last edited:

Thread Starter

astrotom

Joined Jul 14, 2012
16
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.
 

shubham161

Joined Jul 22, 2012
47
@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.
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.
 
Top