imranahmed
Advanced Member level 3
- Joined
- Dec 4, 2011
- Messages
- 817
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Location
- Karachi,Pakistan
- Activity points
- 6,492
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
}
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;
}
Hi,
In the given code I miss how the LEDs are set to "0".
Klaus
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
}
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
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
}}
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);
}
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); }
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.
if((!(10 - Counter) % 10) && flag == 0)
{
LED1 = 1;
delay(1000);
LED = 0;
flag = 1;
}
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);
}
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); }
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);
}
(Counter - 10) % 10) is the same as (Counter % 10)(Counter - 10) % 10)
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;
}
}
} }
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.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?