# Glow LED on specified counter values 0-500 in 89c52 uC

1. ## Glow LED on specified counter values 0-500 in 89c52 uC

Please let me know I have 89c52 and I want to program in this manner that counter counts by external interrupt by push button 0 to 500.
When it reaches 10 glow LED1 and at 15 glow LED2 at 25 glow LED1 again at 30 glow LED2 and so on till 500.

Code:
```unsigned int Counter = 0;
void ext0_isr(void) interrupt 0
{.
Counter++;
}

main()
{.
if(Counter == 10)
LED1 = 1;
else if(Counter == 15)
LED2 = 1;
else if(Counter == 25)
LED1 = 1;

and so on till 500
}```
In above for 0 to 500 LED1 and LED2 glow alternatively with gap of 10 and 5.

I want to know is there any possibility for shorter code size.  Reply With Quote

2. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

It looks as though you light led1 every 15 counts starting at 10.
Likewise led2 lights every 15 counts starting at 15.

This can be done by a simple formula, for each led.

1 members found this post helpful.  Reply With Quote

3. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Try this.

Code:
```unsigned int Counter = 0;

void ext0_isr(void) interrupt 0
{
Counter++;
}

main()
{
if(Counter % 10)LED1 = 1;
else 	LED2 = 1;
}

or

main()
{
if(Counter % 10)LED1 = 1;
elseif(!(Counter % 10))	LED2 = 1;
}```

1 members found this post helpful.  Reply With Quote

4. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

- - - Updated - - -

I will try it, you gave in previous thread great answer this answer also looks good I will try and let you know.  Reply With Quote

•

5. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Hi,

In the given code I miss how the LEDs are set to "0".

Klaus

1 members found this post helpful.  Reply With Quote

6. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC Originally Posted by KlausST Hi,

In the given code I miss how the LEDs are set to "0".

Klaus
Code:
```unsigned int Counter = 0;
unsigned char flag = 0;
void ext0_isr(void) interrupt 0
{.
Counter++;
}

main()
{.
if(Counter == 10 && flag == 0)
{
LED1 = 1;
delay(1000);
LED1 = 0;
flag = 1;
}
else if(Counter == 15 && flag == 1)
{
LED2 = 1;
delay(1000);
LED2 = 0;
flag = 0;
}
else if(Counter == 25 && flag == 0)
{
LED1 = 1;
delay(1000);
LED1 = 0;
flag = 1;
}
else if(Counter == 30 && flag == 1)
{
LED2 = 1;
delay(1000);
LED2 = 0;
flag  = 0
}
else if(Counter == 40 && flag == 0)
{
LED1 = 1;
delay(1000);
LED1 = 0;
flag = 1;
}

and so on till 500
}```
I want to make it shorter by using formula or FOR LOOP or other LOOP, any hint?  Reply With Quote

•

7. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Hi,

As "counter" is used both in main and isr, it should be declared "volatile".

If "counter" needs multiple access to be processed (i.e. 16 bit variable on a 8 bit microcontroller) you should use "atomic" access.
For hobbyists this often is no problem, but for reliable industrial solutions it's very important to avoid malfunction.

If you want repeated function of LED glow every x events,
* consider to reset (or better: subtract x from) the counter
* or use the modulo operator

Klaus

1 members found this post helpful.  Reply With Quote

8. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC Originally Posted by KlausST Hi,

As "counter" is used both in main and isr, it should be declared "volatile".

If "counter" needs multiple access to be processed (i.e. 16 bit variable on a 8 bit microcontroller) you should use "atomic" access.
For hobbyists this often is no problem, but for reliable industrial solutions it's very important to avoid malfunction.

If you want repeated function of LED glow every x events,
* consider to reset (or better: subtract x from) the counter
* or use the modulo operator

Klaus

I also used while loop for forever operation.I will try your suggestions and will let you know.

Code:
```unsigned int Counter = 0;
unsigned char flag = 0;
void ext0_isr(void) interrupt 0
{
Counter++;
}

main()
{

while(1){
if(Counter == 10 && flag == 0)
{
LED1 = 1;
delay(1000);
LED1 = 0;
flag = 1;
}
else if(Counter == 15 && flag == 1)
{
LED2 = 1;
delay(1000);
LED2 = 0;
flag = 0;
}
else if(Counter == 25 && flag == 0)
{
LED1 = 1;
delay(1000);
LED1 = 0;
flag = 1;
}
else if(Counter == 30 && flag == 1)
{
LED2 = 1;
delay(1000);
LED2 = 0;
flag  = 0
}
else if(Counter == 40 && flag == 0)
{
LED1 = 1;
delay(1000);
LED1 = 0;
flag = 1;
}

and so on till 500
}}```  Reply With Quote

9. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Subtract the base values 10 and 15 from the Counter value and if the result modulo 10 and 15 is zero, turn the LED on, otherwise turn it off.

Make sure 'Counter' is declared volatile.

Brian.  Reply With Quote

10. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Ok doing it.  Reply With Quote

•

11. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Try this.

Code:
```volatile unsigned int Counter = 0;
volaile unsigned int oldCounter = 0;

void ext0_isr(void) interrupt 0
{
Counter++;

if(oldCounter != Counter) {
oldCounter = Counter;
LED1 = 0;
LED2 = 0;
}
}

main()
{
if(Counter % 10)LED1 = ~LED1;
elseif(!(Counter % 10))	LED2 = ~LED2;
delay(1000);
}```  Reply With Quote

12. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC Originally Posted by baileychic Try this.

Code:
```volatile unsigned int Counter = 0;
volaile unsigned int oldCounter = 0;

void ext0_isr(void) interrupt 0
{
Counter++;

if(oldCounter != Counter) {
oldCounter = Counter;
LED1 = 0;
LED2 = 0;
}
}

main()
{
if(Counter % 10)LED1 = ~LED1;
elseif(!(Counter % 10))	LED2 = ~LED2;
delay(1000);
}```
LED1 should start from 10 and with gap 15 it will lit-up, means 10,25,40,55,70....... till 500.

and

LED2 should start from 15 and with gap 15 it will lit-up, means 15,30,45,60,75........till 500.

- - - Updated - - -

I tried this but LED was glowing at start of Counter means at Counter value 1.

- - - Updated - - - Originally Posted by betwixt Subtract the base values 10 and 15 from the Counter value and if the result modulo 10 and 15 is zero, turn the LED on, otherwise turn it off.

Make sure 'Counter' is declared volatile.

Brian.
I tried this, but only at value 10 of counter LED was litting-up but I also want to lit-up to whole sequence 0-500 first start at 10 then 25 then 40 then 55 then 70.....so on 500.
Same as applies for 15 but how to resolve please more hints needed.

Code:
```	if((!(10 - Counter) % 10) && flag == 0)
{
LED1 = 1;
delay(1000);
LED = 0;
flag = 1;
}```  Reply With Quote

13. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Very simple. Try this.

Code:
```volatile unsigned int Counter = 0;
volaile unsigned int oldCounter = 0;

void ext0_isr(void) interrupt 0
{
Counter++;
}

main()
{
if(Counter >= 10) && ((Counter % 15) == 10) { LED2 = 0; LED1 = ~LED1; }
elseif((Counter >= 15) && ((Counter % 10) == 0)){ LED1 = 0; LED2 = ~LED2;}
delay(1000);
}```  Reply With Quote

14. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC Originally Posted by baileychic Very simple. Try this.

Code:
```volatile unsigned int Counter = 0;
volaile unsigned int oldCounter = 0;

void ext0_isr(void) interrupt 0
{
Counter++;
}

main()
{
if(Counter >= 10) && ((Counter % 15) == 10) { LED2 = 0; LED1 = ~LED1; }
elseif((Counter >= 15) && ((Counter % 10) == 0)){ LED1 = 0; LED2 = ~LED2;}
delay(1000);
}```

It is working only Counter value of 10 only after 10 onwards it is not working.
I tried only for if(Counter >= 10) && ((Counter % 15) == 10) this condition for LED1 only.  Reply With Quote

15. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Missing brackets.

Code:
```volatile unsigned int Counter = 0;
volaile unsigned int oldCounter = 0;

void ext0_isr(void) interrupt 0
{
Counter++;
}

main()
{
if((Counter >= 10) && ((Counter % 15) == 10)) { LED2 = 0; LED1 = ~LED1; }
elseif((Counter >= 15) && ((Counter % 10) == 0)){ LED1 = 0; LED2 = ~LED2;}
delay(1000);
}```  Reply With Quote

16. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

from my post #9:

Subtract the increment from the counter then check the modulo for zero.
You did it the other way around! Subtracting Counter from 10 results in a 'negative' rollover of the unsigned int!

if(((Counter - 10) % 10) == 0) LED1 = 1; else LED1 = 0;
if(((Counter - 15) % 15) == 0) LED2 = 1; else LED2 = 0;

Brian.  Reply With Quote

17. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Hi,

(Counter - 10) % 10)
(Counter - 10) % 10) is the same as (Counter % 10)
(Counter - 15) % 15) is the same as (Counter % 15)

Klaus  Reply With Quote

18. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

Problem Solved

I used simple formula not huge coding needed but unfortunately no one in thread could provide proper answer, however problem solved.

Code:
```volatile unsigned int Counter=0, h=10, v=15;

isr(void) interrupt 0
{
Counter++;
}

main()
{
while(1)
if(Counter == h)         // for LED1
{
LED1=1;
delay(1000);
LED1=0;
h+=15;
}

if(Counter == v)         // for LED2
{
LED2=1;
delay(1000);
LED2=0;
v+=15;
}
}
}               											}```  Reply With Quote

19. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC

I used simple formula not huge coding needed but unfortunately no one in thread could provide proper answer, however problem solved
Problem 'solved' with a bad programming style, consider next time using state machine handling delays with flags instead of closed loop delays. I would not be surprised if you reopen the discussion reporting that with the above working code you are unable to do well other tasks besides this LED blinky itself. By the way, with the code above, you perform the required task as expected only in the first iteration.  Reply With Quote

20. ## Re: Glow LED on specified counter values 0-500 in 89c52 uC Originally Posted by andre_teprom Problem 'solved' with a bad programming style, consider next time using state machine handling delays with flags instead of closed loop delays. I would not be surprised if you reopen the discussion reporting that with the above working code you are unable to do well other tasks besides this LED blinky itself. By the way, with the code above, you perform the required task as expected only in the first iteration.
Yes you saying alright but please let me know how to do in best way.How to use state machine handling delays with flags?  Reply With Quote