Code not executing properly with xmega256D3

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I am using an Xmega256D3 using Atmel studio 6.2, AVR/GNU C compiler version 4.8.1 and FreeRTOS. I am running into some weird issue. I have the following code:

 

    if (gas_select <= MAX_GAS_INDEX)
    {
        dns = 0;
        for (b = 0; b <= 12; b++)
        {
            val1 = powf(temp_K, (float)b);       //Calculate 12 order polynomial
            dns += (prm[b] * val1);
        }
    }

 

When first entering the loop, temp_K is 273.16 . The result on the first pass should assign val1 to 1 (273.15^0 = 1.0) but I get zero.

When I setup a break point, I get a value of 0 for val1. If I take the statement with val1 and move it into a other task, it works properly. Also the for loop does not show b be incremented

on every loop when using the AVROne debugger.

The code size now is 110986 bytes. I checked all the tasks stack allocations and the total heap size and could not find anything wrong.

 

Is there anybody that ran into similar problem and figure out how to fix this issue?

 

Thank You,

 

Charles

 

This topic has a solution.
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We'll need to know the types for dns, b, val1 and prm.

 

277,232,917 -1 The largest known Mersenne Prime

Measure twice, cry, go back to the hardware store

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dns,val1 and prm are float. (prm[] array of 13 float)

b is a char

 

Thanks,

 

Charles

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The guys will tell you: I'm not actually a C mage.

 

According to math.h, powf is #defined as pow.

 

pow takes 2 doubles and returns a double.

 


double pow	(	double 	__x,
                double 	__y 
            )	

well, that wasn't exactly legible.

 

 

277,232,917 -1 The largest known Mersenne Prime

Measure twice, cry, go back to the hardware store

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thought that powf handles float as parameters and pow handles double. I did not think that AVR8 compiler can handle double, just float as 32 bits. Anyway, I think the code is executing properly but the debugger is having some issue stepping through the code. I tried to set the function with no optimization and still got the same result.

 

void __attribute__ ((optimize("O0"))) function(prm1, prm2)

 

Thank You for the response!

 

Charles

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It wouldn't surprise me if the debugger wasn't able to show you the floats while executing. The compiler seems to be smarter than the debugger, not to mention smarter than me.

 

277,232,917 -1 The largest known Mersenne Prime

Measure twice, cry, go back to the hardware store

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am thinking of getting some better tools for compiler and debugger. Any recommendation? I am thinking of IAR tools set

Thanks,

Charles

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know what polynomial coefficients you have so I tried with all of them set to 1.

int main(void)
{
    // Setup USART and printf
    // ...

    int b;
    float dns = 0, temp_K = 273.15, val1;

    for (b = 0; b <= 12; b++)
    {
        val1 = powf(temp_K, (float) b);
        dns += val1;

        printf("b = %d, val1 = %g, dns = %g\n", b, val1, dns);
    }

    putchar('\n');
    while(1)
        ;
}

and got the following output from a ATXmega128a1

b = 0, val1 = 1, dns = 1                                                        
b = 1, val1 = 273.15, dns = 274.15                                              
b = 2, val1 = 74610.8, dns = 74885                                              
b = 3, val1 = 2.038e+07, dns = 2.04548e+07                                      
b = 4, val1 = 5.56678e+09, dns = 5.58723e+09                                    
b = 5, val1 = 1.52057e+12, dns = 1.52615e+12                                    
b = 6, val1 = 4.15342e+14, dns = 4.16869e+14                                    
b = 7, val1 = 1.13451e+17, dns = 1.13868e+17                                    
b = 8, val1 = 3.0989e+19, dns = 3.11029e+19                                     
b = 9, val1 = 8.46466e+21, dns = 8.49576e+21                                    
b = 10, val1 = 2.31212e+24, dns = 2.32062e+24                                   
b = 11, val1 = 6.31554e+26, dns = 6.33875e+26                                   
b = 12, val1 = 1.72509e+29, dns = 1.73143e+29                                   

                                   

The first val1 looks OK to me. Note that this is not a good way to evaluate polynomials, see for example Horner's method.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

guggech wrote:
Any recommendation?
Before you give up on AVR GCC consider inquiring Embecosm.

Embecosm is doing some effort for Atmel.

Embecosm Logo

http://www.embecosm.com/services/support/

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also the for loop does not show b be incremented

I don't know C, but often when someone mentions something along those lines its because a variable isn't declared volatile.

That may have nothing to do with your issue, but it is perhaps worth a quick check.

 

JC 

Last Edited: Mon. Feb 2, 2015 - 05:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What DocJC wrote might be correct, but we really don't know since you posted almost no code...

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Snigelen,

It turnout that the problem is with the debugger that is showing the wrong value when stepping throuh the code. On the top of that there was some incorrect value in the prm table. The calcualtion computed by an Excel shhet does agree with the value calculated by the code.

 

Thank you for yourv response.

 

Charles