How to use a software timer to handle SPI

I was made the Arduino library for the MLX90363 magnetometer rotery sensor:

I have one question / problem:
The library works perfectly, but i want to remove delayMicroseconds(...); and instead use software timer.
When i'm using software counters or library e.g. chrono the SPI reading is messing up :frowning:

can someone help me with this?

delayMicroseconds() is a software timer (in my opinion). You might have to explain in more detail what exactly you need and why you think you need it.

I want to make a delay without interrupting the whole program, like timers do this.

If your delays were larger you could use millis() (but I'm sure you know this).

But for a 100 or 800 µS delay, if you could use µS like the millis() what would your program accomplish during that time?

i know that.
a) I tried to use the micros () function, it doesnt work properly...
b) The sensor data was incorrectly read or was not read at all
I would like the library to be able to be used for multitasking programs
So I would like to ask what the support for such SPI buses looks like

So I need advice on how this code should look without using the delayMicroseconds() function, as it is in professional libraries with proper support of the SPI bus.

Maybe I did something wrong, I am asking for advice / example.

Hello, i was add a micros() function, and now it working...

i was add old version in another folder.
Thanks for help.
The new library: GitHub - popos123/MLX90363: Arduino library for the MLX90363 magnetometer rotery sensor

I'm surprised that this code works for you, most people would expect the data to be read after one or at a deterministic number of calls of ReadData(). How do you know when a new value was read?

Just like this :slight_smile:

And yes, the function ReadData() needs to be call couple times, but if you using timers instead delay in main loop function there is no problem.
I was write example code with timer for you (interval is 100ms but just for display values on serial, the data is reading < 1ms):

The data from sensor on the SPI bus:

As you can see the data cycle is about 86 + 107 + 84 + 702 = 979us.
The goal was to make about 1kHz dara reading rate for my steering wheel Logitech G29, and it was achieved :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.