A
A
Anatoly2016-02-03 11:07:21
Programming
Anatoly, 2016-02-03 11:07:21

Why is the AVR buggy on calculations in the main program when interrupts are enabled?

Good afternoon. I've run into a problem that I don't understand.
The ATmega32a controller runs on internal 8 MHz. IAR Embedded Workbench for Atmel AVR 6.12. Debugger Chinese noun JTAG adapter.
There is a timer2 interrupt with a frequency of 20kHz, which reads the value of the ADC and does not count anything inside, is executed in microseconds, most of the time the main program is free.
Another interrupt on timer0 at a frequency of 30Hz (!) Is responsible for blinking LEDs, it also doesn’t count anything, it’s fast, everything was controlled not by an oscilloscope.
There are moments in the main program that are very time consuming. Fast Fourier transform, arc cosine with arc tangent and square root, like this:

if (FFTfull){
             cli();
             fft_input(capture, bfly_buff);   
             fft_execute(bfly_buff);          
             fft_output(bfly_buff, spektrum); 
             FFTfull=0;
             sei();
      }
cli();
SKV=0*X+0*Y+1*Z; //скалярное произведение векторов
DLV=sqrt(Y*Y+X*X+Z*Z); //длина вектора
COSV=SKV/DLV; //косинус угла 
Alpha=acos(COSV)*pi180; //угол
sei();

So, if I don’t enclose calculations by prohibiting cli () interrupts, then the controller behaves inappropriately, jumps to incomprehensible memory addresses and eventually goes to reboot. Is this normal, or is there something wrong somewhere? As I understand it, he cannot normally return from interruption to calculations.
UPD: Wrote a small note for those whom Google will bring here. Solving the problem with CStack and RStack in the IAR environment for AVR driveelectronics.ru/soft/prevyshenie-razmerov-csta...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
iv_k, 2016-02-03
@oWart

most likely someone ate the stack. try increasing it all the way.
and rewrite the math into tables

A
Armenian Radio, 2016-02-03
@gbg

Arc cosine on AVR? Are you seriously?
FFT in real time on the AVR is done only according to ready-made tables, and then it fits back-to-back in cycles.

A
Alexander Volkov, 2016-02-03
@a_volkov1987

Most likely, you have overlapping memory areas that are used inside the interrupt, which is responsible for updating the LED indication and the memory areas that you use in the main program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question