Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] 8051 blinking led for 5 seconds and turn on led by switch

Status
Not open for further replies.

bagusdj

Member level 3
Member level 3
Joined
Nov 25, 2012
Messages
67
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,766
Hi guys, i want to ask about programming my at89s52 using Keil c51 with clock frequency 11.0592MHz

the idea is to interface led so it will blink for 5 second,and then off for 5 second as well. in the same circuit, i also required to interface led that will light on if I hit a switch.

here is my circuit :

blink.jpg

as seen above i want to blink led on P0. then the led on P1 should on when i hit switch on P2.


I search through internet and get 2 codes. each is for each function (blink and switch). Can anyone help me how to make it into a single file?

here is the code for blink:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<reg52.h>
 
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)
     {
          P0=0xff;
          delay(660);
          P0=0x00;
          delay(660);
     }  
}




and here is for switch:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<reg52.h>
 
void main()
{
    unsigned char temp;
    P1=0Xff;
    P2=0Xff;
    while(1)
    {
        temp=P2;
        P1=temp;
        while(P2==temp);
    }
}



Thank you in advance :)
 
Last edited by a moderator:

just try this code..


Code:
#include <reg51.h>

void delay()
{
unsigned int i,j;

for(i=0;i<5000;i++)
for(j=0;j<120;j++);
}

void main()
{
P2=0xff;
P0=0x00;
P1=0x00;
while(1)
{
if(P2^0==0)
{
P0=0xff;
delay();
P0=0;
P1=0xff;
delay();
P1=0x00;
}
}
}

hope this works
 

just try this code..


Code:
#include <reg51.h>

void delay()
{
unsigned int i,j;

for(i=0;i<5000;i++)
for(j=0;j<120;j++);
}

void main()
{
P2=0xff;
P0=0x00;
P1=0x00;
while(1)
{
if(P2^0==0)
{
P0=0xff;
delay();
P0=0;
P1=0xff;
delay();
P1=0x00;
}
}
}

hope this works

unfortunately not working :(. with this code, now the Leds on and off for 5 seconds instead 1 blink, and 1 by switch. I tried to combined 2 codes that i found, and the result is the switch also delayed.

thanks anyway for replying :D
 

Try this

Code:
#include<reg52.h>

unsigned char temp;

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)
	{
		P0=0xff;
		delay(660);
		P0=0x00;
		delay(660);

		if(P2 == 1) {
			P1 = 1;
		}
	}	
}
 

no luck also.. the same as I tried before. different way but same result. the switch is not working. it just on and off between them for 5 seconds each. this is frustrating :D
 

remove the 10k resistor going to led. It will work.

Your led connections are wrong. zip and post your proteus file and i will send the proper circuit.

Which led is not lighting, the D1? You have to use pullups on port 0

Try this code

Code:
#include<reg52.h>

unsigned char temp;

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)
	{
		P0=0xff;
		delay(660);
		P0=0x00;
		delay(660);

		if(P2 == 1) {
			P1 = 0;
		}
	}	
}
 
Last edited:

no luck also.. the same as I tried before. different way but same result. the switch is not working.

The problem is with the checking.

Try with this:

Code:
#include<reg52.h>

unsigned char temp;
unsigned char k;

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)
	{
		P0=0xff;
		P3=0xff;
		for (k = 0; k < 22; k++){
			if (P2 == 1){
				P1 = 1;
			}
			else{
				P1 = 0;
			}
			delay(30);
		}
		P0=0x00;
		P3=0xff;
		for (k = 0; k < 22; k++){
			if (P2 == 1){
				P1 = 1;
			}
			else{
				P1 = 0;
			}
			delay(30);
		}

	}	
}

I've added P3 along with P0. Remember that P0 needs pull-up. Connect LED like this:

5574429100_1353947117.png


I think this should work.

Hope this helps.
Tahmid.
 
Last edited:

remove the 10k resistor going to led. It will work.

Your led connections are wrong. zip and post your proteus file and i will send the proper circuit.

Which led is not lighting, the D1? You have to use pullups on port 0

i think i configure the led by turning the mcu into common ground. many sources tell me to put it that way even my lecturer.
i have the code for blink and switch. by that design, both of them works perfectly. but i have to make it as 1 code. thats my problem. the switch should light up D2. D1 is for blinking 5 sec and its works fine. but D2 not work properly when i tried to mix the code..

View attachment blinking led.rar
 

i think i configure the led by turning the mcu into common ground. many sources tell me to put it that way even my lecturer.
i have the code for blink and switch. by that design, both of them works perfectly. but i have to make it as 1 code. thats my problem. the switch should light up D2. D1 is for blinking 5 sec and its works fine. but D2 not work properly when i tried to mix the code..

View attachment 83506

Did you try with my code in post #7?
 

Try this code. It will work.

Code:
#include<reg52.h>

unsigned char temp;

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)
	{
		P0=0xff;
		delay(660);
		P0=0x00;
		delay(660);

		if(P2 == 1) {
			P1 = 0;
		}
	}	
}
 

Try this code. It will work.

Code:
#include<reg52.h>

unsigned char temp;

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)
	{
		P0=0xff;
		delay(660);
		P0=0x00;
		delay(660);

		if(P2 == 1) {
			P1 = 0;
		}
	}	
}

Problem here is that the LEDs are blinked and then switch is checked. So, while the LEDs are being blinked, status of switch can not be checked. Thus switch response is slow.
 

The problem is with the checking.

Try with this:

Code:
#include<reg52.h>

unsigned char temp;
unsigned char i;

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)
	{
		P0=0xff;
		P3=0xff;
		for (i = 0; i < 22; i++){
			if (P2 == 1){
				P1 = 1;
			}
			else{
				P1 = 0;
			}
			delay(30);
		}
		P0=0x00;
		P3=0xff;
		for (i = 0; i < 22; i++){
			if (P2 == 1){
				P1 = 1;
			}
			else{
				P1 = 0;
			}
			delay(30);
		}

	}	
}

I've added P3 along with P0. Remember that P0 needs pull-up. Connect LED like this:

5574429100_1353947117.png


I think this should work.

Hope this helps.
Tahmid.

okay so i configures all led that way, and now the Led that will be switched (P1) keep light up. the 5 seconds delay one works fine. yeah i think that way also. it just not check the switch somehow
 

So, you tried with this code?

Code:
#include<reg52.h>

unsigned char temp;
unsigned char k;

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)
	{
		P0=0xff;
		P3=0xff;
		for (k = 0; k < 22; k++){
			if (P2 == 1){
				P1 = 1;
			}
			else{
				P1 = 0;
			}
			delay(30);
		}
		P0=0x00;
		P3=0xff;
		for (k = 0; k < 22; k++){
			if (P2 == 1){
				P1 = 1;
			}
			else{
				P1 = 0;
			}
			delay(30);
		}

	}	
}

okay so i configures all led that way, and now the Led that will be switched (P1) keep light up. the 5 seconds delay one works fine. yeah i think that way also. it just not check the switch somehow

Can you clarify this? What do you mean by "not check the switch"?
 
Last edited:

So, you tried with this code?



Can you clarify this? What do you mean by "not check the switch"?

yeah i tried. the result is led (switch) always on, and led(delay) normal. i mean, when i combine the codes,or use codes in this thread, the delay works normal but the switch just doesnt work.
 

Actually using an interrupt for the change of state of the switch and a timer interrupt for the flashing LED would be a more efficient method of coding your project.

The use of interrupts have distinct advantages over both polling for switch change of state and software delays.

Both polling and software delays force the code execution to pause or enter an inactive state, in effect burning cycles waiting for an event to occur.

While interrupts allow the system to service an event in a timely manner and return quickly to process other tasks.


BigDog
 

Realized the mistake in the code.

Try with this now:

Code:
#include<reg52.h>

unsigned char temp;
unsigned char k;

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()
{
        P2=0xff;
	while(1)
	{
		P0=0xff;
		P3=0xff;
		for (k = 0; k < 22; k++){
			if ((P2 & 0x01) == 1){
				P1 = 1;
			}
			else{
				P1 = 0;
			}
			delay(30);
		}
		P0=0x00;
		P3=0x00;
		for (k = 0; k < 22; k++){
			if ((P2 & 0x01) == 1){
				P1 = 1;
			}
			else{
				P1 = 0;
			}
			delay(30);
		}

	}	
}

However, as bigdogguru mentions, use of interrupt and timer would be a more efficient method.

Hope this helps.
Tahmid.
 
Realized the mistake in the code.

Try with this now:



However, as bigdogguru mentions, use of interrupt and timer would be a more efficient method.

Hope this helps.
Tahmid.

it works like a charm. thanks man. yeah i think it should be done using interrupt for efficient result. gonna study about it.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top