atmel - Page 1
Forum section: electronics / general

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 07:17:04 am
content: 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:ADC->INPUTCTRL.reg = (ADC_INPUTCTRL_MUXPOS_TEMP |ADC_INPUTCTRL_MUXNEG_GND|ADC_INPUTCTRL_GAIN_1X);Thanks

author: dannyf
author_rank: Super Contributor
timestamp: August 10, 2016, 10:40:27 am
content: Thats a nice function. Looks good.

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 04:35:11 pm
content: the floats do not work, must I check a very specific option?

author: Andy Watson
author_rank: Super Contributor
timestamp: August 10, 2016, 04:44:25 pm
content: 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" ?

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 04:51:00 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 04:53:26 pm
content: 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.

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 04:57:31 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 04:59:28 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 05:09:41 pm
content: Quote from: hyre on August 10, 2016, 05:03:01 pmThis appnote is for D21, which has more flash and RAM. What is your current Falsh/RAM usage?Quote from: hyre on August 10, 2016, 05:03:01 pm"Does not work" is not a proper description.Have you tried a simple program:Code:float a, b;void main(void){a = 5.0f;b = 6.0f;volatile float c = a + b;while (1) ;}and check what "c" is.

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 05:15:49 pm
content: 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.

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 05:20:25 pm
content: 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% FullData Memory Usage: 1096 bytes 26.8% FullBut 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?

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 05:25:12 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 06:00:27 pm
content: 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 VTemperature sensor slope = 2.16 mV / CI'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 = ADC * 1000000 / 4096 = ADC * 244And the code to get temperature reading isCode: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.

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 06:25:08 pm
content: 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 isCode:int main(void){SystemInit();ADC_Init();uint32_t temp = ADC_Result();uint32_t temp2 = 25+((temp-2818)*244)/216;while (1){}}

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 06:30:01 pm
content: Quote from: hyre on August 10, 2016, 06:25:08 pmWhat 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.

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 06:43:59 pm
content: 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 degreesWhen the converter reached 4095 Temp2 variable will be from 1467 upI don't understand the result

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 07:51:25 pm
content: So what are your ADC settings? And that is the raw reading? You should start with that, not some complicated formulas.

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 07:57:16 pm
content: What are those raw results (exact numbers)? Are they stable?

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 08:02:51 pm
content: temp=4095 ; 2460 ; 4095temp2=-6838658 ; -4889368 ; -7166954

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 08:07:32 pm
content: 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.

author: hyre
author_rank: Contributor
timestamp: August 10, 2016, 08:25:47 pm
content: I made some adjustments in my functions it seems more stable in adc_result function () I get the results:a = 330 result = 3088a = 320 result = 3080a = 324 and result = 3083But these values are high and do not reflect the room temperature

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 08:56:21 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 09:05:09 pm
content: Quote from: hyre on August 10, 2016, 09:03:22 pmWhat 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.

author: ataradov
author_rank: Super Contributor
timestamp: August 10, 2016, 09:07:27 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 04:40:07 pm
content: 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.

author: hyre
author_rank: Contributor
timestamp: August 11, 2016, 04:47:06 pm
content: I think it would be simpler to include <asf.h> at the top of the code right?

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 04:49:25 pm
content: I don't have ASF, I have simple bare-metal projects (https://github.com/ataradov/mcu-starter-projects/tree/master/samd10). And ASF is not really freely searchable, so I can't use it unless I create a project in AS, which I don't have either.

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 05:01:57 pm
content: Now I'm missing NVMCTRL_TEMP_LOG.BTW, what file did this come from? I may need to update my header files.

author: hyre
author_rank: Contributor
timestamp: August 11, 2016, 05:06:26 pm
content: This Software Temperature Log row can be read at address 0x00806030.it comes from nvmctrl.h file

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 05:22:58 pm
content: Ok, I have those definitions for all devices, but D10. And when I define them, manually, I get clearly invalid (all 0xff) calibration data on my device. So it is possible that this is device revision dependent.Let me figure out what is going on here.

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 05:49:47 pm
content: My header files were way old. After updating, I have all those defines.It also looks like my device does not have any calibration data. I assume you use SAM D10 Xplained Pro? Can you try attached project and see what it prints on the COM port?In my case I get:Code:Hello, world!0xffffffff 0xffffffff 255 15 255 15 -1 -14053 21474836474056 21474836474055 21474836474056 21474836474057 21474836474057 21474836474058 2147483647

author: hyre
author_rank: Contributor
timestamp: August 11, 2016, 06:23:05 pm
content: which update did you do and what is the manipulation you have achieved?

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 06:25:27 pm
content: Update of header files in my project to the most recent ones from AS. This does not affect you, since you created your project recently. I had my projects created when this device first came out.What do you mean by "manipulation"?

author: hyre
author_rank: Contributor
timestamp: August 11, 2016, 06:36:43 pm
content: At the terminal I have not the "hello world" only appears I only 3 digits

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 06:39:06 pm
content: Please be more specific. I'm not going to pull information from you, it is a waste of my time.Can you copy exactly what you see?Keep terminal program open and restart the project.

author: hyre
author_rank: Contributor
timestamp: August 11, 2016, 06:43:50 pm
content: I changed of terminalQuote

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 06:46:30 pm
content: Ok, so your board also returns 0xffffffff. Can you check what those values are on your project? It is possible that they are not accessible until something is enabled.Also, values 223-225 are way too low for some reason, but it is not important right now.

author: hyre
author_rank: Contributor
timestamp: August 11, 2016, 06:53:20 pm
content: This address 0x00806030 contains no calibration data so this is a big problem

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 06:55:16 pm
content: So you have confirmed that even your demo application does not contain anything there? Don't trust my program, it is possible that you have to enable some clock that is not enabled, although I don't see anything obvious.

author: hyre
author_rank: Contributor
timestamp: August 11, 2016, 06:58:14 pm
content: the memory is :

author: ataradov
author_rank: Super Contributor
timestamp: August 11, 2016, 07:57:19 pm
content: Yeah, looks like there is no calibration data in those devices. They may be engineering samples or something.For the actual temperature reading, you will get better results if you use lookup table anyway. Just collect what ADC readings correspond to what temperatures and use that table to lookup the temperature based on the ADC reading. This table will be non-linear, so you will get better results than any linear approximation.There may be some variability between the devices, so you will need to check that as well.