# Debugging problems in AVR studio 4

Status
Not open for further replies.

#### jdh_1984

##### Member level 2
Hi
My program is not working as expected, and of this reason I am trying to find the bug with help of the debugger in AVR studio 4. When I step throw the code the debugger would not step into the If-statement, and I get no response in the output register (PORTB and PORTC). I hope anyone could tell me what I am doing wrong! (See attachment for code, and screen dump of the step with the corresponding add to watch list)

#### Attachments

• Debugging AVR studio.pdf
74.6 KB · Views: 22

#### kalbun

##### Full Member level 2
Which optimisation level are you using?
Unless you use -O0, be aware that debugging source code can be trickier than you think. In case of doubt, I always suggest to view and debug the assembler instead than the source code.

(What I write is correct for GCC, if you use IAR maybe things could be different, but in any case try to inspect assembler)

#### luben111

Are you using the simulator or real debugging with JTAGICEmkII, Dragon, AVRONE or other tool?

#### jdh_1984

##### Member level 2
Which optimisation level are you using?
Unless you use -O0, be aware that debugging source code can be trickier than you think. In case of doubt, I always suggest to view and debug the assembler instead than the source code.

(What I write is correct for GCC, if you use IAR maybe things could be different, but in any case try to inspect assembler)

Hi
Tnx for replay. I am new to atmel/AVR studio so this optimization doesn't give my any meaning. I found in the configuration that I am using -Os optimization. What does this mean? What is the difference between the different optimizations levels? I am using GCC, but how do I debug the assembler instead of the source file? Do you have any good links to tutorials about this topic (debug in AVR studio)?

---------- Post added at 17:16 ---------- Previous post was at 17:15 ----------

I am using the simulator

#### kalbun

##### Full Member level 2
-Os is the maximum optimisation level for GCC. Code is optimised, compiler may decide to put some functions inline even if you did not design them this way, and so on.

You can find a lot of tutorials about GCC explaining how to use switches and various optimisation levels.
For AVR, have a look at help files. Something is accessible from the AVR studio itself, others can be found in the installation directory. There you can find all the needed information.

About your code: I suggest you to set -O0 that means no optimisation. Purists will blame me, as the general rule is "try avoiding -O0 at all costs", but if the code is small and simple using -O0 can simplify a lot the debug, especially if you are new to AVR/GCC

One last thing: be careful with the simulator, not all peripherals are emulated fully and/or correctly. Again, have a look at help files for known issues

Soumyajit

### Soumyajit

Points: 2

#### jdh_1984

##### Member level 2
-Os is the maximum optimisation level for GCC. Code is optimised, compiler may decide to put some functions inline even if you did not design them this way, and so on.

You can find a lot of tutorials about GCC explaining how to use switches and various optimisation levels.
For AVR, have a look at help files. Something is accessible from the AVR studio itself, others can be found in the installation directory. There you can find all the needed information.

About your code: I suggest you to set -O0 that means no optimisation. Purists will blame me, as the general rule is "try avoiding -O0 at all costs", but if the code is small and simple using -O0 can simplify a lot the debug, especially if you are new to AVR/GCC

One last thing: be careful with the simulator, not all peripherals are emulated fully and/or correctly. Again, have a look at help files for known issues

Hi
Tnx for good advice. I will check out the documentation. Do you have any tip where I mess up in my code? I know the interrupts is working and that the ADC value is read, but after this I have a bug. The purpose of the code is to read a accelerometer value, calculate the angle of this and write it out as a 16bit value (use a scaling of 10000 to get a better resolution at the output).
(see attachment for complete code)

#### Attachments

• code akselerometer.txt
1.6 KB · Views: 5

#### kalbun

##### Full Member level 2
Sorry, can't find anything unusual. I also did a quick check with AVR4 and simulator2 and the code works.
Try to execute the code in main() instead than under an ISR and see if something changes.

#### jdh_1984

##### Member level 2
I did some change in the code sequence, and no its debug correctly until the line:

angleFloatBeta = acos(argument); /*calculate the arc cos */

when stepping through this line the debugger jump straight to the end of the code without updating variables/registers. It seems like the debugger doesn't like the acos function(the libm.a is linked to the project). I am using simulator not simulator2 since this one support atmega16, which isn't the case for simulator2. Could this be the problem?

#### alexan_e

I have the same behavior, something happens in this line but the strange thing is that if you put real number instead of variable (for example acos((0.242/1.586)); ) it works fine but probably because it calculates the value in compile time.

I have used the following code to be able to track the values (AVRstudio watch ) and enter virtual ADC values (100,200,300...)

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<avr/io.h>
#include<avr/interrupt.h>
#include <math.h>

volatile uint8_t i;
volatile float angleFloat, In_value;

int main(void)
{
GICR=0x40;          /*Set the INT0 bit to enable ext INT0*/
MCUCR=0x03;         /*Set INT0 to be activate on rising edge */
DDRB=0xff;          /*All B-pins set to output*/
DDRC=0xff;          /*All C-pins set to output*/
DDRD=0xf3;          /*All D-pins set to input*/

for (i=100;i<1000;i+=100) {
In_value = (float)adc_data;               /*converting from int to float*/
In_value*=0.002429;

if (In_value<=1.586)
{
angleFloat = acos((In_value/1.586)); /*calculate the arc cos */
}
else
{
angleFloat = acos((1.586/In_value)); /*calculate the arc cos */
}
angleInt = (int)(angleFloat*10000);         /*using a scale factor of 10000 to get a better resolution at the 16bit output*/

PORTB = angleInt;                         /*Writing the lowest eight bit of angleInt to PORTB???*/
PORTC = angleInt>>8;
}
while(1);

}

Alex

#### kalbun

##### Full Member level 2
The code works. Yes, it works.
I am able to obtain correct results with the code of alexan_e, the simulator does not skip calculation and everything works fine with -O0 and -Os as well.
Are you sure you are using the latest version of AVR4? On my PC I have a 4.18 build 716 (also known as SP3)

Ah, I was just forgotting... The code works even if you don't explicitly link libm.a (possibly the compiler is smart enough to do so for you)

Last edited:

#### alexan_e

You should include the libm.a, it is not done automatically

This is the result with libm.a
Code:
Device: atmega16

Program:    [B]1872 bytes (11.4% Full)[/B]

Data:         [B]13 bytes (1.3% Full)[/B]
(.data + .bss + .noinit)

And without the libm.a
Code:
Device: atmega16

Program:    [B]4108 bytes (25.1% Full)[/B]

Data:        [B]277 bytes (27.1% Full)[/B]
(.data + .bss + .noinit)

I have version 4.18 b684,
you are right, the code i have posted above works so i thought why wasn't it working when i tried...
I have removed the sei(); (because I wasn't using any interrupt) before i post the code , if you add it (before the "for loop") then the code stops working again in the acos line.

Alex

#### alexan_e

I finally found what was wrong,

Typically, system libraries like libm.a are given to the final C compiler command line that performs the linking step by adding a flag -lm at the end. (That is, the initial lib and the filename suffix from the library are written immediately after a -l flag. So for a libfoo.a library, -lfoo needs to be provided.) This will make the linker search the library in a path known to the system.

in AVRSstudio, go to Project -> Configuration Options .
Then go to the custom options button down the side, select linker options,

compile and enjoy.

Alex

jdh_1984

### jdh_1984

Points: 2

#### jdh_1984

##### Member level 2
I finally found what was wrong,

in AVRSstudio, go to Project -> Configuration Options .
Then go to the custom options button down the side, select linker options,

compile and enjoy.

Alex

Thank you Alex

Status
Not open for further replies.