Author Topic: atmel  (Read 11588 times)

0 Members and 1 Guest are viewing this topic.

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
atmel
« on: August 10, 2016, 07:17:04 am »
Hello, I would like the temperature via the ADC module with atmel SAMD10. So I created a function for initializing ADC then for the ADC conversion. But now, if I want to get the temperature just me it must change:

Code: [Select]
ADC->INPUTCTRL.reg = (ADC_INPUTCTRL_MUXPOS_TEMP |
ADC_INPUTCTRL_MUXNEG_GND|
ADC_INPUTCTRL_GAIN_1X);

Thanks
« Last Edit: August 12, 2016, 06:36:33 am by hyre »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: atmel
« Reply #1 on: August 10, 2016, 10:40:27 am »
Thats a nice function. Looks good.
================================
https://dannyelectronics.wordpress.com/
 

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
Re: atmel
« Reply #2 on: August 10, 2016, 04:35:11 pm »
the floats do not work  :-[,  must I check a very specific option?
 

Offline Andy Watson

  • Super Contributor
  • ***
  • Posts: 2340
Re: atmel
« Reply #3 on: August 10, 2016, 04:44:25 pm »
Does your compiler issue warnings about converting between formats?
How does your compiler handle the conversion from float to unint_16 and back to float that occurs when you use "calculate_temperature" ?
 

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
Re: atmel
« Reply #4 on: August 10, 2016, 04:51:00 pm »
The compiler does not emit any mistake linked to the formats yet when the main works, and each time it encounters a float, put it "optimized out" as a value to the variable. It does not cover the floats.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #5 on: August 10, 2016, 04:53:26 pm »
This MCU has no floating point unit, so I'd avoid using floats. At the same time things don't just disappear without warnings, so I'd look at resulting assembly code.

PS: Give topics meaningful titles.
Alex
 

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
Re: atmel
« Reply #6 on: August 10, 2016, 04:57:31 pm »
What is even more surprising is that I followed one of documentations in atmel and if I can not use floating, it is me not possible to recover the temperature.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #7 on: August 10, 2016, 04:59:28 pm »
Sure it is. People have been using fixed point calculations ever since computers existed.

Also, what documentation? And when things don't work, what exactly doe not work?

You really need to give more information if you want meaningful answers.
Alex
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #8 on: August 10, 2016, 05:09:41 pm »
So I followed this documentation with linear interpolation formulas:
This appnote is for D21, which has more flash and RAM. What is your current Falsh/RAM usage?

What does not work is really the handling of floating.
"Does not work" is not a proper description.

Have you tried a simple program:
Code: [Select]
float a, b;

void main(void)
{
  a = 5.0f;
  b = 6.0f;
  volatile float c = a + b;

  while (1) ;
}
and check what "c" is.
Alex
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #9 on: August 10, 2016, 05:15:49 pm »
It actually looks like basic floating point operation don't take that much. Using simple + - / * operations resulted in ~2K increase in flash and they do work as expected.

Also, don't look at "optimized" look, since things can really get optimized out. Assign your value to a volatile variable, which can't be optimized and check that variable.
Alex
 

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
Re: atmel
« Reply #10 on: August 10, 2016, 05:20:25 pm »
For a simple example like this it works.

As regards the use of Flash and Ram at the moment, I only this code so it astonish me to know that the memory becomes full puique when I compile this appears:
Program Memory Usage: 9552 bytes 58.3% Full
Data Memory Usage: 1096 bytes 26.8% Full


But just to get the temperature, I find that the memory is still very overloaded. But how do I will be possible to get the temperature without solicit as much memory?
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #11 on: August 10, 2016, 05:25:12 pm »
Yes, you need to understand exactly what is going on in your system. For instance, what type of temperature sensor you are using? Most analog temperature sensors are non-linear devices, so you will need to have a lookup table. They are also not that accurate, so integer degrees values will be sufficient in most cases.
Alex
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #12 on: August 10, 2016, 06:00:27 pm »
To get stated accuracy out of the internal  sensor you do need to read calibration values and apply corresponding transformations. "34.9.8 Temperature Sensor Characteristics" describes all the formulas. You don't need to use floating point, it all can be done with integer calculations.

But to get basic uncalibrated reading, all you need is data in the table Table 34-28:

Temperature sensor output voltage @ 25 C = 0.688 V
Temperature sensor slope = 2.16 mV / C

I'm assuming you are using 12-bit ADC and 1 V as reference. We will use microvolts for voltages, so we can stick to integer numbers and maintain accuracy of calculations.

ADC reading at 25 C will be 4096 * 0.688 = 2818.
ADC input voltage is Vin[uV] = ADC * 1000000 / 4096 = ADC * 244

And the code to get temperature reading is
Code: [Select]
int32_t temp = 25 + (((int32_t)ADC - 2818) * 244) / 216;

Also, internal temperature sensor is even less accurate than external ones. It has +/- 10 degree accuracy over all operating conditions.
Alex
 

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
Re: atmel
« Reply #13 on: August 10, 2016, 06:25:08 pm »

If I understand correctly, I have to change the way of coding linear interpolation so as to not have floating.

In case I use the uncalibrated basic reading, I must therefore use the following formula:

  int32_t temp = 25 + (((int32_t) ADC - 2818) * 244) / 216;

But I have a question about this case. I must then change the ADC_result on the configuration of the conversion?

But the main is
Code: [Select]
int main(void)
{
        SystemInit();
ADC_Init();
uint32_t temp = ADC_Result();
uint32_t temp2 = 25+((temp-2818)*244)/216;
    while (1)
    {
    }
}

 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #14 on: August 10, 2016, 06:30:01 pm »
But I have a question about this case. I must then change the ADC_result on the configuration of the conversion?
What do you mean?

Your code roughly looks OK, make sure that some of those functions actually start a conversion. And use int32_t instead uint32_t, otherwise you will get incorrect results for negative temperatures.
Alex
 

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
Re: atmel
« Reply #15 on: August 10, 2016, 06:43:59 pm »
OK but the problem is that completely out of me as temperature halucinante temp2 1023 or 1426 ..
This varies enormously .. In the room it has to 22 degrees
When the converter reached 4095 Temp2 variable will be  from 1467 up

I don't understand the  result
« Last Edit: August 12, 2016, 06:38:52 am by hyre »
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #16 on: August 10, 2016, 07:51:25 pm »
So what are your ADC settings? And that is the raw reading? You should start with that, not some complicated formulas.
Alex
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #17 on: August 10, 2016, 07:57:16 pm »
What are those raw results (exact numbers)? Are they stable?
Alex
 

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
Re: atmel
« Reply #18 on: August 10, 2016, 08:02:51 pm »
temp=4095              ;  2460          ; 4095
temp2=-6838658    ; -4889368    ; -7166954
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #19 on: August 10, 2016, 08:07:32 pm »
4095 is a good sign of overflow. Do you have temperature sensor enabled (TSEN in VREF register)?

Also, if you plug in 4095 into the formula, you will get 1467, not that crazy negative number, so there is also something wrong with the way you interpret results of the calculation.
Alex
 

Offline hyreTopic starter

  • Contributor
  • Posts: 16
  • Country: cl
Re: atmel
« Reply #20 on: August 10, 2016, 08:25:47 pm »
I made some adjustments in my functions it seems more stable in adc_result function () I get the results:

a = 330 result = 3088
a = 320 result = 3080
a = 324 and result = 3083
But these values are high and do not reflect the room temperature
« Last Edit: August 12, 2016, 06:39:37 am by hyre »
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #21 on: August 10, 2016, 08:56:21 pm »
I get similar readings on my board.

Well, it is possible that without calibration direct temperature readings are totally useless. So you need to do similar integer math for those calibration formulas.

Just keep your voltage readings in microvolts and you won't need floating point math.
Alex
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #22 on: August 10, 2016, 09:05:09 pm »
I agree with you, but if I want to use this calibration function that atmel do you recommend to tempR et INT1Vr?
What do you mean? Last part of this sentence makes no sense.

I'd recommend go over the section of the datasheet that describes the math, and implement it from scratch.
Alex
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #23 on: August 10, 2016, 09:07:27 pm »
Note that even if you apply calibration procedure, your readings still will be accurate to +/- 10 degrees C. I'm not sure why this would be useful in any way.
Alex
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12498
  • Country: us
    • Personal site
Re: atmel
« Reply #24 on: August 11, 2016, 04:40:07 pm »
I can try this code, but can you please find where NVMCTRL_TEMP_LOG, FUSES_XXX_Msk and FUSES_XXX_Pos, and post the values here. They are not a part of standard headers, so they are probably defined somewhere in ASF, and I'm to lazy to start digging for that stuff.
Alex
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->