tag:blogger.com,1999:blog-6245413346375218188.post2609355013849443160..comments2024-03-15T05:42:04.481-07:00Comments on Nerd Ralph: Writing a library for the internal temperature sensor on AVR MCUsRalph Doncasterhttp://www.blogger.com/profile/00037504544742962130noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-6245413346375218188.post-31503232451875635302021-04-08T05:15:23.937-07:002021-04-08T05:15:23.937-07:00Thanks for the tip. My copy of the m328p datashee...Thanks for the tip. My copy of the m328p datasheet was from 2012 and I hadn't looked at the updated datasheet. It's now on my list of things to test.Ralph Doncasterhttps://www.blogger.com/profile/00037504544742962130noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-51527232267805490062021-03-14T15:59:50.441-07:002021-03-14T15:59:50.441-07:00High, just read the ATmega datasheet and found sec...High, just read the ATmega datasheet and found section "23.8.1 Manufacturing Calibration" - it seems that the 328P has some sort of calibration value lasered into silicon. I was too lazy to download the ATtiny datasheet and check for you guys, but perhaps it's worth a look... This is neat, because it allows for cheap power electronics temperature monitoring. In the proximity of a power FETs, inductors close to saturation or rectifier diodes the influence of the MCU losses become pretty negligible...clshttps://www.blogger.com/profile/11657320505865720772noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-53995280010324966122020-09-28T21:21:28.459-07:002020-09-28T21:21:28.459-07:00thanks, that would be very nice too.thanks, that would be very nice too.xiaohttps://www.blogger.com/profile/17842702917321682328noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-15020317116139128422020-09-27T20:01:28.299-07:002020-09-27T20:01:28.299-07:00I wrote it to use my bit-bang uart code so it work...I wrote it to use my bit-bang uart code so it works on tiny AVRs with no UART. I didn't write a sophisticated makefile for it, so it wouldn't properly rebuild the lib when the mcu target changed. I'll consider re-writing it as a header-only library, or maybe as a simple Arduino/Wiring library so it's easier to build for different MCU targets.Ralph Doncasterhttps://www.blogger.com/profile/00037504544742962130noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-60903582684818093672020-09-15T19:48:28.208-07:002020-09-15T19:48:28.208-07:00thank you so much for the prompt reply.
we have t...thank you so much for the prompt reply.<br /><br />we have try and compile to code with avr-gcc under Win10, the make had has no success. we also try manually compilation, still the same error with,<br /> <br />test_temperature.c, failed to comiple with warning and error, test_temperature.o produced.<br />temperature.o compiled, ok.<br /><br />linking was no way to proceeding as long as test_temperature.o was not produced.<br /><br />any idea whether compiler version/option different or the build environment affected ?<br /><br />thanks.<br /><br /><br />log following ---------------------<br /><br /><br />xiao> avr-gcc.exe -dumpversion<br />5.4.0<br /><br />xiao> set main=test_temperature<br /><br />xiao> set mcu=atmega328p<br /><br />xiao> set cpu_speed=8000000<br /><br /><br />xiao> avr-gcc -Wall -g -Os -mmcu=atmega328p -DF_CPU=8000000 -mrelax -Wall -Wno-main -ffunction-sections -Wl,--gc-sections -flto -Wl,-flto -c test_temperature.c -o test_temperature.o<br />In file included from test_temperature.c:11:0:<br />test_temperature.c:23:38: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]<br /> FILE uart_output = FDEV_SETUP_STREAM(TxByte, NULL, _FDEV_SETUP_WRITE);<br /> ^<br />test_temperature.c:23:38: note: (near initialization for 'uart_output.put')<br /><br /><br />xiao> avr-gcc -Wall -g -Os -mmcu=atmega328p -DF_CPU=8000000 -nostdlib -c libs\temperature\temperature.c -o libs\temperature\temperature.o<br />Compiling temperature.c, done<br /><br />xiaohttps://www.blogger.com/profile/17842702917321682328noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-37355584281297867542020-09-14T09:31:17.785-07:002020-09-14T09:31:17.785-07:00I copied it to github before google code shut down...I copied it to github before google code shut down.<br />https://github.com/nerdralph/nerdralph/tree/master/avr/libs/temperature<br />Ralph Doncasterhttps://www.blogger.com/profile/00037504544742962130noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-63347702249702727602020-09-14T00:21:18.230-07:002020-09-14T00:21:18.230-07:00hello, we would like to know whether the code is s...hello, we would like to know whether the code is still available on google code. seems no download capable anymore.xiaohttps://www.blogger.com/profile/17842702917321682328noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-33834036399164187442014-11-07T12:39:53.016-08:002014-11-07T12:39:53.016-08:00Hi Fernando,
When you change ADMUX, you need to wa...Hi Fernando,<br />When you change ADMUX, you need to wait for the sample and hold capacitor to charge before doing the ADC.<br />I recommend you read AVR 125 and this page:<br />http://www.openmusiclabs.com/learning/digital/atmega-adc/<br />Ralph Doncasterhttps://www.blogger.com/profile/00037504544742962130noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-41794483842547505292014-11-06T11:47:22.817-08:002014-11-06T11:47:22.817-08:00Hi Ralph.
I have found what I think is a litlle i...Hi Ralph.<br /><br />I have found what I think is a litlle issue in your library.<br /><br />I'm tryying to do a sensor node that gets both temperature and eBay's ACS712 shield current.<br /><br />What I found is this:<br /><br />a) if I call only analogRead to the ACS712 (20 samples), I get 20 good samples.<br />b) if I call your function and then analogRead (20 times), I get THE FIRST ANALOG VALUE very very bad, and then, 19 more good.<br /><br />Could it be something about the registers or configuration that you make inside your function ?<br /><br />I have this function (it's like yours but with comment the line where your turn off ADC. I have also try uncommenting this line, but the problem is the same, the first value is wrong):<br /><br />static uint16_t doADC()<br />{<br /> // start conversion with ADC clock prescaler 16<br /> ADCSRA = (1<<ADEN) | (1<<ADSC) | (1<<ADPS2); <br /> while (ADCSRA & (1<<ADSC)); // wait to finish <br /> return ADCW;<br />}<br /><br />#define SAMPLE_COUNT ((256/ADC_GAIN)+0.5)<br />// returns signed byte for temperature in oC<br />char temperature()<br />{<br /> // ADMUX = (0<<REFS0) | (1<<MUX3); // ADC8<br /> ADMUX = ADCINPUT;<br /> uint16_t tempRaw = 0;<br /><br /> // take multiple samples then average <br /> for (uint8_t count = SAMPLE_COUNT; --count;) {<br /> tempRaw += (doADC() - 273); <br /> }<br /> //Fernando<br /> //ADCSRA = 0; // turn off ADC<br /><br /> // a known offset could be used instead of the calibrated value<br /> //return ((tempRaw/256) - eeprom_read_byte(&temp_offset) ) ;<br /> return ((tempRaw/256) - temp_offset) ;<br />}<br /><br />Could you help me ? <br /><br />Cheers<br /><br />fernandohttps://www.blogger.com/profile/05009107095998106032noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-30308614950722523332014-10-16T12:20:27.138-07:002014-10-16T12:20:27.138-07:00Thanks, and what about a little delay to settle Vr...Thanks, and what about a little delay to settle Vref ? Do you think that it's necessary ?<br /><br />Cheers,fernandohttps://www.blogger.com/profile/05009107095998106032noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-52584477867532592752014-10-15T15:19:02.062-07:002014-10-15T15:19:02.062-07:00Discarding the first sample might be a good idea. ...Discarding the first sample might be a good idea. You could add a call to doADC before the for loop in the temperature function.<br />Restoring the register values is typically only done in ISRs, in case any code was using the ADC before the ISR ran.Ralph Doncasterhttps://www.blogger.com/profile/00037504544742962130noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-37358183265989288112014-10-13T13:50:42.728-07:002014-10-13T13:50:42.728-07:00Hi Ralph,
Thanks a lot. I finally get it working....Hi Ralph,<br /><br />Thanks a lot. I finally get it working. Just a few last questions:<br /><br />1. other examples that I found recomend a little delay to wait for Vref to settle. (after ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);) What do you think about it ?<br />2. others, also, discard first sample (I don't know if this is a subtitute of that delay)<br />3. Is it worth to save values of ADCSRA and ADMUX to restore them after reading temperature ? Or doesn't matter ?<br /><br />Thanks a lot,<br /><br />Best regards,fernandohttps://www.blogger.com/profile/05009107095998106032noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-86178653530481897962014-10-13T07:10:15.404-07:002014-10-13T07:10:15.404-07:00Hi Fernando,
If you don't want the auto-calibr...Hi Fernando,<br />If you don't want the auto-calibration, delete the calibrate_temp function.<br />Ralph Doncasterhttps://www.blogger.com/profile/00037504544742962130noreply@blogger.comtag:blogger.com,1999:blog-6245413346375218188.post-21931447343250772542014-10-13T07:00:23.816-07:002014-10-13T07:00:23.816-07:00Hi,
Your web page is great. Thanks a lot for your...Hi,<br /><br />Your web page is great. Thanks a lot for your blog.<br /><br />I have download temperatura.h and temperature.c but I want to use them WITHOUT writing anything to EEPROM.<br /><br />I'am a noob, so speaking about "automatically run the calibration when the application code is flashed" fears me.<br /><br />I only want to "play" with them so, I tried to remove that option, but anyway, I get "Y" or 89 value fixed. I'm using a arduino buono (atmega328p).<br /><br />Could you help me ?<br /><br />best regards,<br /><br />fernandohttps://www.blogger.com/profile/05009107095998106032noreply@blogger.com