Yes Jan, you are right. Just a minor note. The float
should be cast to long long int
(or int64_t
). But that matters only for very big numbers when narrowing could kick in.
Tried calback but this doesn’t work either.
chrono has to find out first. attached the code
#include “mbed.h”
Ticker Tik;
DigitalOut myled(LED1);
void Pwm()
{
myled = !myled;
}
int main()
{
Tik.attach_us(callback(&Pwm),100);
while(1) {
}
}
Gr Jos v. Engelen
Hello Jos,
As Jan suggested replace
Tik.attach_us(callback(&Pwm),100);
with
Tik.attach(callback(&PWM), 100us);
The addition us immediately gives an error message
- Make sure you use Mbed 6.16
- Make sure you call the
attach
function rather than theattach_us
one - Sorry for using
&PWM
in my advice. It should be&Pwm
.
I have made a test using the online compiler (the old one) and Mbed 6.16 library. Compiles and works fine. You can find it here.
This indeed works. Thank you very much for the cooperation.
But may I ask one more question. I had a variable time this in connection with making a frequency converter.
I have tried the below.
#include “mbed.h”
AnalogIn Meting(A0);
Ticker Tik;
DigitalOut myled(LED1);
void Pwm()
{
myled = !myled;
chrono::microseconds chronoPer{long(Meting.read() * 100000 + 10000)};
Tik.attach(callback(&Pwm),chronoPer);
}
int main()
{
chrono::microseconds chronoPer{100000};
Tik.attach(callback(&Pwm),chronoPer);
while(1) {
}
}
- I think in this case the Timeout API is more suitable than the Ticker.
- To keep the ISR as short as possible I would recommend something as follows:
#include "mbed.h"
const uint32_t PWM_EVENT = (1UL << 0); // position 0
AnalogIn Meting(A0);
Timeout timeout;
DigitalOut myled(LED1);
EventFlags eventFlags;
void Pwm()
{
eventFlags.set(PWM_EVENT);
}
int main()
{
eventFlags.set(PWM_EVENT); // sets the flag for first time (you can replace it with Pwm(); if you like so)
while (1) {
eventFlags.wait_all(PWM_EVENT); // while blocking this thread, it waits for PWM_EVENT to become set
// then it clears the PWM_EVENT (by default)
chrono::microseconds chronoPer { long(Meting.read() * 100000 + 10000) };
printf("chronoPer = %lli us\r\n", chronoPer); // only for debugging, comment out to get accurate timing
myled = !myled;
timeout.attach(callback(&Pwm), chronoPer); // Pwm is called (only once) when chronoPer us elapsed
}
}
- For more info on how to use EventFlags see this thread.
Note: To format the source code on forum pages put a line with three back-ticks (like these ```) in front and after the code.
It seems very difficult. I had the frequency converter working in the old mbed ide.
And not only for 1 phase but for 3 phases. That’s why I’m not that motivated for the keil studio. This takes a lot of time to get it working again.
You could take your original code and just add one line for converting time from the float
type to the chrono time
type. After that, just edit the syntax of the attach method.
You still mixing two things together. The Keil Studio is little bit different than Online compiler, but basically it is same tool with few additional features (deploy, debug, intellisense and so on).
Issues what you are facing are in the context of migrating MbedOS version and not Keil Studio. You can downgrade MbedOS to version 5.15.9 (which is more close to old Mbed2) and 95% of your issues are gone.
BR, Jan