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.

LED blinking program help on c program

Status
Not open for further replies.

vead

Full Member level 5
Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Visit site
Activity points
3,815
HI
I wrote code for LED blinking

LED blinking c code
start
turn ON LED
wait for some time (delay )
turn OFF LED

Code:
include<regx51.h>		        // header file for 8051 
include<delay.h>				// header file for delay 
#define LED_ON  00000001		// LED ON
#define LED_off 00000000        // LED OFF      			
void main()					   //main function starts
{
while (1)				       //infinite loop
{		                  
setb P1_1=00000001;
delay (1000);
setb P1_1=00000000;
}
}
there is no error but I am not sure that code will be work ?

"LED" - 0 Error(s), 2 Warning(s).
 

You are also not using the #defines you added. You don't have to to use them but if you don't you can remove both the lines.

The problem Easyrider83 pointed out is that without the extra delay you turn the LED off then immediately on again as the loop repeats. It will be so fast that you won't be able to see the LED turn off and it will appear to be on all the time.

Brian.
 

How can i coding,

when 1st switch press 1st led off 2nd led on, again 1st switch press 1st led on 2nd led off.

when 2nd switch press 1st led off 3rd led on, again 2nd switch press 1st led on 3rd led off.


this like coding....
 

another delay after "setb P1_1=00000000;" missed

Now
Code:
include<regx51.h>		        // header file for 8051 
include<delay.h>				// header file for delay 

void main()					   //main function starts
{
while (1)				       //infinite loop
{		                  
setb P1_1=00000001;
delay (1000);
setb P1_1=00000000;
delay (1000);
}
}

Is it right ?

- - - Updated - - -

You are also not using the #defines you added. You don't have to to use them but if you don't you can remove both the lines.
Brian.
when does we define port pins?
 

Like this. #define is used to create aliases.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
include<regx51.h>               
include<delay.h>                
 
#define LED P1_1
#define LED_ON  1       
#define LED_off 0 
 
void main()                    
{
    while (1)                      
    {                         
        LED = LED_ON;
        delay (1000);
        LED = LED_OFF;
        delay (1000);
    }
}

 

Miss # on includes!!!

Which the compiler used???

Keyl uVision C Code:
Code:
#include<regx51.h>
//#include<delay.h>    // I dont have it!!!	

#define LED 		P1_1
#define LED_ON	        1
#define LED_OFF	0

void delay(void)			// make it, because I dont have delay.h lib
{
	unsigned int i;
	for (i = 0; i < 40000; i++)
	{
		;
	}
}

void main()
{
	P0 = 0x00;
	P1 = 0x00;
	P2 = 0x00;
	P3 = 0x00;

	while (1)
	{
		LED = LED_ON;
		delay();
		LED = LED_OFF;
		delay();
	}
}
 
Last edited:

Miss # on includes!!!

Which the compiler used???

Keyl uVision C Code:
Code:
#include<regx51.h>
//#include<delay.h>    // I dont have it!!!	

#define LED 		P1_1
#define LED_ON	        1
#define LED_OFF	0

void delay(void)			// make it, because I dont have delay.h lib
{
	unsigned int i;
	for (i = 0; i < 40000; i++)
	{
		;
	}
}

void main()
{
	P0 = 0x00;
	P1 = 0x00;
	P2 = 0x00;
	P3 = 0x00;

	while (1)
	{
		LED = LED_ON;
		delay();
		LED = LED_OFF;
		delay();
	}
}
what is meaning of following line
unsigned int i;
for (i = 0; i < 40000; i++)
{
;
}
what is i and 40000 ?
 

It is a code to generate delay. i is an unsigned integer type variable and so it can hold values from 0 to 65535. In the for loop i is incremented from 0 to 39999 with a step of one. So the for loop executes 40000 times.
 

what is meaning of following line
unsigned int i;
for (i = 0; i < 40000; i++)
{
;
}
what is i and 40000 ?

"i" is a variable used in FOR loop .. Since FOR loop requires variable in it Syntax, "i" is used here .. 40000 is the number of times the loop is going to be repeated, since "i" is an unsigned int which ranges from 0 to 65535, here 40000 is used which is well under the range ..
 

It is correct!!!

Because i dont have the lib delay.h to call a delay (1000) function in my sorce code!!!
 

It is correct!!!

Because i dont have the lib delay.h to call a delay (1000) function in my sorce code!!!

If LED is connected with switch
when switch is on , Led will ON
wait
when switch is off , LED will OFF
Code:
 #define LED 		P1_1
#define LED_ON	        1
#define LED_OFF	0
#define switch     P1_2
#define switch_ON
#define switch_OFF

void delay(void)			//
{
	unsigned int i;
	for (i = 0; i < 40000; i++)
	{
		;
	}
}

void main()
{
	P0 = 0x00;
	P1 = 0x00;
	P2 = 0x00;
	P3 = 0x00;

	If  (switch_ON )
	{
		LED = LED_ON;
		delay();
		else (switch_OFF)
		LED = LED_OFF;
		delay();
	}
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top