Just for the heck of it, I tried your code snippet. As expected, it won't execute in 5 uS. I was able to make it go in 6uS, however, just to show how close to the edge it is :-)
I suggest you try Andy's approach of having the ticker simply set a flag, then shift the bulk of the processing to the main program.
Here's what I used for the test:
#include "mbed.h"
DigitalOut myled(LED1);
Ticker testticker;
Serial pc(USBTX, USBRX); // tx, rx
float level = 0;
float low;
float high;
int oc;
void setup () {
wait_us(1);
}
void check() {
float a;
a=level;
low=(a<0.24);
high=(a>0.61);
oc=(a >= 1.24 && a<=0.61);
if (pc.readable()) setup();
}
int main() {
// Hangs if time is < 6 us
testticker.attach_us(&check, 6);
while(1) {
myled = 1;
wait(0.2);
level += 0.1;
myled = 0;
wait(0.2);
}
}
When the ticker is set to run at 6 uS or above, LED1 flashes continually. When set to 5 uS or below, LED1 stays on constantly, indicating the stream of ticker calls is chewing up all the CPU cycles.
-
hb
Hi, I was wondering whether anybody else is struggling using the Ticker object? I setup a Ticker function which seems to work for a while then after several minutes will stop working. Also if I try to change an attached functions parameters the Ticker also seems to stop working although if I reset mbed it will start to work again. I'm using two Ticker objects, one called every 5us and the other variable, which may be the problem. Any ideas? If not then is there anyway to reset/restart my program running from main() again after I've altered the parameters?