Thread Starter

Syeda Zainab Hassan

Joined Jan 27, 2023
2
I design a project using an 8051 microcontroller. A project description is mentioned below:

I used two LEDs with the logic that if led1 is on continuously then led2 will be on for 1 sec and off for a long delay. This logic is running fine in proteus software but it is not working when the hardware is designed.

This is my code, if anyone finds any error in the code kindly help me.

C:
#include<stdio.h>
#include<reg51.h>
sbit led1=P1^0; //led1 pin declared
sbit led2=P2^1;

void period(void); // Function prototype declaration
void Delay(void); // Function prototype declaration

void main()
{
led1=0x00;
led2=0xFF;

if(led1==1){ //led status check
led2=0;
period();
led2=1;
Delay();
}
else
led2=1;
}

void period(void)
{
int l;
int m;
for(l=0;l<10;l++)
{
for(m=0;m<10005;m++)
{
}
}
}

void Delay(void)
{
int i;
int j;
for(i=0;i<10;i++)
{
for(j=0;j<100000;j++)
{
}
}
}
I attached my circuit diagram for your reference.

Can anyone guide me on this?

Thanks in advance.

Mod edit: Code tags - JohnInTX
 

Attachments

Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
Welcome to AAC!

The first thing you must know about Proteus is that it will simulate code correctly even though there are problems with the hardware design.

You must have an RC network on the RST pin to properly start the 8051 after power up.
EA must be pulled up to enable the internal ROM.
There are no current limiting resistors on the LEDs.
Be sure you have a good 5 volt power source.
Use decoupling capacitors on the power pins. 1uF tantalum in parallel with a .1uF ceramic is a good start.

Remember that reading a port sets that pin to an input. That will turn your LEDs off. It is better to save system status in RAM and act on that rather than polling the output lines, especially in the 8051.
You need a 'while' loop in main. As written, your code exits and restarts continuously. Stand-alone embedded code never exits.
Comments and indents will help you visualize your code and find errors sooner.
Post your code using code tags. On the toolbar select Insert->Code, select 'C' language and paste the code in the window.

C:
void main()
{
led1=0x00;    // initialize the system
led2=0xFF;

while(1){    // while loop to keep code from exiting
    if(led1==1){ //led status check
        led2=0;
        period();
        led2=1;
        Delay();
        }
    else
        led2=1;
    }
}// while
Good luck!
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,151
In line 14, your logic expression checks to see if led1 is off and then flashes led2. Isn’t this the opposite of your description?

I think it appears to work because you initialize led1 to be on. This is done before the test in line 14. When the code loops, led1 will always be on. Where or how does the status of led1 change?
 

Thread Starter

Syeda Zainab Hassan

Joined Jan 27, 2023
2
Thanks for explaining, but this logic works when I initialized led1=0 and check the status of led1==1 then led2 will on for 1 sec and off for a longer time. The code is again attached for your reference.

LED interface with 8051 controller:
#include<stdio.h>
#include<reg51.h>
sbit led1=P1^1;  //led pin declared as input from external power source
sbit led2=P2^1; //led pin declared as a trigger output for 1 second

void period(void);           // Function prototype declaration
void Delay(void);           // Function prototype declaration

void main()
{
led1=0;      //led1 as input initialized

    while(1){
if(led1==1){ //LED status check as condition is true or false
led2=0;        //switch on led
period();
led2=1;             //switch off led
Delay();
} 
else
    led2=1; //If noting led off   
break;
}
}

void period(void)
{
    int l;
    int m;
    for(l=0;l<10;l++)
    {
        for(m=0;m<10005;m++)
        {
        }
    }
}

void Delay(void)
{
    int i;
    int j;
    for(i=0;i<10;i++)
    {
        for(j=0;j<1000000;j++)
        {
        }
    }
}
I explain to you the scenario and what I have to do.
We are using as an input in port 1 and pin 0 from the external power source, on the basis of that input port 2 of pin 1 should act like a trigger for 1 second and then should be off.

Any alternate solution you suggest is really helpful for me.

Thanks in advance.
 

JohnInTX

Joined Jun 26, 2012
4,787
led1=0; //led1 as input initialized
Actually, that line initializes the pin to be an OUTPUT.
if(led1==1){ //LED status check as condition is true or false
That line makes the pin an input by reading it. You are changing the direction of the pin at the same time as trying to read its previous state. That may not work. Again, what works in a simulator may not work on actual hardware.

For a test, pull P1.1 high or low with a jumper to Vdd and GND to set the input state and see if your logic works then.

Have fun!
 
Top