I'm having an issue with the scoping of a global variable. In this demo code I wrote, LED1 flashes at 100ms on then 100ms off and that is handled in the function TimerFunction() with is called with an attach. However, LED is supposed to be flashing at 1000ms on and then 1000ms off but it does not. It flashes at a rate of 0.3us on then 0.3us off. That is micro seconds!
See attached scope plot of the LED2 pin and code below. The scope is setup for 0.2us per division
Maybe someone can try this code on their mbed and see if it does the same thing. Is there something obviously wrong with this code?
The issue seems to be with the global variable TimerValue. I tried defining it as int, unsigned int, long, unsigned long, volatile, extern,etc and not luck.
For what it's worth, I have the same basic program structure running on an LPC2129 and GNU compiler with no issues. In that code I have TimverValue defines as a long volatile and it works.
Any help would be appreciated.
Thanks.
#include "mbed.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Ticker SchedulerTimer;
long volatile TimerValue;
void TimerFunction()
{
led1 = !led1; //every 100ms
TimerValue = TimerValue + 1;
}
int main()
{
SchedulerTimer.attach(&TimerFunction,0.1);
while(1)
{
if(TimerValue%10 == 0) //every 1000ms
{
led2 = !led2;
}
}
}
I'm having an issue with the scoping of a global variable. In this demo code I wrote, LED1 flashes at 100ms on then 100ms off and that is handled in the function TimerFunction() with is called with an attach. However, LED is supposed to be flashing at 1000ms on and then 1000ms off but it does not. It flashes at a rate of 0.3us on then 0.3us off. That is micro seconds!
See attached scope plot of the LED2 pin and code below. The scope is setup for 0.2us per division
Maybe someone can try this code on their mbed and see if it does the same thing. Is there something obviously wrong with this code?
The issue seems to be with the global variable TimerValue. I tried defining it as int, unsigned int, long, unsigned long, volatile, extern,etc and not luck.
For what it's worth, I have the same basic program structure running on an LPC2129 and GNU compiler with no issues. In that code I have TimverValue defines as a long volatile and it works.
Any help would be appreciated.
Thanks.