P
P
Pavel K2015-04-04 01:19:07
Arduino
Pavel K, 2015-04-04 01:19:07

Arduino/ATmega328 how best to distribute execution time?

Good afternoon.
So far I'm learning on Arduino, do not throw slippers.
There is some device with which there is communication through jerking of pins, but at the same time timings are rigidly set.
In this case, it is necessary to do other useful actions - to receive values ​​from different sensors.
If I push all this into the main loop, then the timings do not match, which is not surprising.
I tried it through timer interrupts, but then the execution can be interrupted at the most inopportune moment,
and the calculation goes both in the main loop and in the interrupt (just a little, but not without it), so there is confusion in the calculations (old values ​​​​mixed with new ones) and variables in structures when using interrupts generally turn into a mess ...
(Correctly I think that when changing the value for variables, several cycles are required??)
If interrupts are stopped for the duration of calculations, then it is generally pointless to use interrupts.
So far, I just came up with a millis() to divide the time in half, i.e. first we get the values, then we communicate with the device.
But here the timings break down!
I do not use digitalWrite, I write directly to ports.
How to make the MK be Roughly speaking half of the second in loop1 and the other half in loop2
while not paying attention to anything else (xs to what, but something breaks the timings)
, while I know for sure that both functions are executed faster, than half a second.
Damn, vaguely written, but I hope it's clear ...
UPD1: There is no way to use 2 timers, because. PWM is needed on these ports.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel K, 2015-04-04
@PavelK

In general, I solved the issue - I took out all the calculations that take a variable number of cycles from the main loop into an interrupt, and also wrapped it in noInterrupts () when setting the structure; and interrupts(); at the same time, millis () really stopped working, but this is nothing.
almost achieved the ideal, but there was another catch -
for optimization I wrote:
loop() {while(1) { }
}
digitalWrite), not by much, but it was decisive.
In general, it's time to move from the ArduinoIDE to something more complicated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question