How to write a C program to make blinking LED with 1 sec delay?

Status
Not open for further replies.
Re: Blinking LED

ryusgnal said:
How to write C program to make LED blinking with 1 sec delay.

what you mean by 1 second delay, the delay between on and off state??
 

Re: Blinking LED

Put time loop from this:
[ **broken link removed** ]
into this:
Code:
int main(void) {
    while(1) {
        led_on();
        for(int i = 0; i < 0xffff; i++);
        led_off();
        for(int i = 0; i < 0xffff; i++);
    }
    return 0;
}
[ **broken link removed** ]
 

Re: Blinking LED

hi
why dont you use delay calculator for it, if you need I will upload it.

regards
 

Re: Blinking LED


Yes. Can you help?

Added after 3 minutes:


Where should I connect the LED if I want to build the hardware with this code.
 

Re: Blinking LED

Code:
#include <reg51.h>

void timer0_init( void );

/******************************************************************************/
/*                                     Define area                                          */
/******************************************************************************/
#define  	time_high 0xFB                                      //Change time here
#define  	time_low  0xB4                                      //Change time here

sbit 	Pulse  = P1^ 0;

/******************************************************************************/
/*                               main routine area                                 */
/******************************************************************************/
void main( void ) {

      Pulse = 0;                                  
	
      timer0_init ();			// Init timer0
      while(1) {                                     // Repeat at here
      }
}
/******************************************************************************/
/*                          timer0 interrupt routine area                             */
/******************************************************************************/
void int_timer0(void) interrupt 1 {
	TF0 = 0;
	TR0 = 0;

	Pulse = ~Pulse;

	TH0 = time_high;
	TL0 = time_low;
	TR0 = 1;
}
/******************************************************************************/
/*                                Init function  area                                      */
/******************************************************************************/
void timer0_init( void ) {
	TMOD &= 0xF0;
	TMOD |= 0x10;
	TH0 = time_high;
	TL0 = time_low;
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}
The best way by using interrupt timer to do it. Try at this code by adjust the time_high and time_low define value.
 

Blinking LED

Try to use interrupt timer interval, then you can put a blinking of LED inside a interrupt service routine.
 

Re: Blinking LED

You will need CCS compiler for my code.
 

Blinking LED

;Copy this codes and assemble
;///Program starts here///
; Connect your led to Portb,0

list p=16f877A
#include <p16F877A.inc>

bsf STATUS,RP0
clrf TRISB ; all TRISB pins OUTPUT
bcf STATUS,RP0

main
bsf PORTB,0 ; Led connected PB0
call Wait1_sec
bcf PORTB,0
call Wait1_sec
goto main

call Wait1_sec

cblock
d0
d1
d2
endc
Wait1_sec ;!!! 1sec for 4mhz XTAL !!!!
movlw 0x08 ; You OSC is not indefinite , i'm sorry
movwf d0 ; please tell me
movlw 0x2F
movwf d1
movlw 0x03
movwf d2
Delay_0
decfsz d0, f
goto $+2
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_0

goto $+1
return

end
;//// THE END ////
;This way is very strong, keep your hair on CSS ....
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…