Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong ADV REFERENCE when computing Vcc #3

Closed
guiguid opened this issue Apr 13, 2013 · 10 comments
Closed

Wrong ADV REFERENCE when computing Vcc #3

guiguid opened this issue Apr 13, 2013 · 10 comments

Comments

@guiguid
Copy link

guiguid commented Apr 13, 2013

Hi,
In CosaVCC.ino

wrong m_vcc = 1126400L / AnalogPin::sample(VBG, AnalogPin::A1V1_REFERENCE);
good m_vcc = 1126400L / AnalogPin::sample(VBG, AnalogPin::AVCC_REFERENCE);

wrong VCC lowPower(1150);
good VCC lowPower(4900) ; // trigger alarm when below 4.9 Volts

We know with 5V VCC we have 1023, and so 1.1V ref is 225 (1023/5*1.1)
Now if we donc know VCC, if we sample 1.1REF, with VCC as ref,
we have vcc = 1126400 / (sample 1.1V with Vcc as REF)

now we can do :
// if vcc under 4.9 Volts = lowbatt trigged

VCC lowPower(4900);

guillaume

PS: as you can see, I'm porting my current project to your libs ;-)
PS: I'll try using pull request for new issue ...

@mikaelpatel
Copy link
Owner

With this I get very strange readings. Over 5000 mV, as high as 8000 mV. Need to check AVCC_REFERENCE.

Cheers!

@mikaelpatel
Copy link
Owner

Please see 5f2eaae. There was a problem with the logic in AnalogPin::sample when using the VBG mux address. Solved this with a new function; AnalogPin::bandgap().

@guiguid
Copy link
Author

guiguid commented Apr 13, 2013

Hi

It seem's to be a delay problem when changing ADMUX register.
It need some time to have the new REF stabilised, so don't launch convertion until
we are shure the new REF is stabilised.

from: Datasheet Atmega 328P

If the user has a fixed voltage source connected to the AREF pin, the user may not use the other
reference voltage options in the application, as they will be shorted to the external voltage. If no
external voltage is applied to the AREF pin, the user may switch between AVCC and 1.1V as reference
selection. The first ADC conversion result after switching reference voltage source may
be inaccurate, and the user is advised to discard this result.

also : http://capnbry.net/blog/?p=167

a delay of 10ms after changing ADMUX value is need.

Pins.cpp

uint16_t
AnalogPin::sample(uint8_t pin, Reference ref)
{
if (sampling_pin != 0) return (0xffffU);
if (pin >= Board::A0) pin -= Board::A0;
loop_until_bit_is_clear(ADCSRA, ADSC);
if (ADMUX != (ref | pin)) {
ADMUX = (ref | pin);
// if ADMUX change, need to wait ms in order to let the new voltage reference stabilise
delay(10);
}
bit_mask_set(ADCSRA, _BV(ADEN) | _BV(ADSC));
loop_until_bit_is_clear(ADCSRA, ADSC);
return (ADCW);
}

Guillaume

@guiguid
Copy link
Author

guiguid commented Apr 13, 2013

Yes, you pacth fix it, but without my post above, you'll have a first wrong result :
CosaVCC: started
1:3264 mV <--- without delay
2:5143 mV
3:5143 mV
4:5143 mV
5:5143 mV
6:5143 mV

@guiguid
Copy link
Author

guiguid commented Apr 13, 2013

well, you close this issue when I has testing ....
I'll open a new one ?

@mikaelpatel
Copy link
Owner

I have fixed this with a dummy sample in the VCC class. Please see 0fb3f39. Thanks for the great feedback and sorry about missing your latest comment.

BW: I have my test setup with ATtiny running CosaVWItempsensor with DS18B20 in parasite mode at 3.3V talking to an Arduino Mini Pro running CosaVWItempmonitor at 8 Mhz and 3.3V. I had to change the pullup resistor for 1-Wire on the ATtiny to 1Kohm to get it working.

@guiguid
Copy link
Author

guiguid commented Apr 13, 2013

Well,

If could tell you : I don't think it's the best choice.
In lot of project battery powered, we need to know battery voltage (VCC) and some others
ADC inputs. So vcc monitor is often a slow monitoring (once by minute)
and other can be very fast ADC conversion.
With your solution, the very fast ADC convertion can be corrupt by an exit of vcc monitor (ADMUX was changed...)
my patch test for an ADMUX change and make an delay (but you can replace by a dummy convertion if you wants).
It's your code, of course you can do that you wants.
cheers

guillaume

@guiguid
Copy link
Author

guiguid commented Apr 13, 2013

an other solution is to backup ADMUX before doing vcc stuff, and restore it after !

@mikaelpatel
Copy link
Owner

Sounds like a good idea. I haven't considered the scenarios for the bandgap function and restoring is maybe the best solution. It seems to be the ATtiny that requires additional time (1ms) when reference voltage is changed. I will have to read more before coming to a decision on which way to go. Your input is valuable.

I will keep this issue open until there is a better solution.

@mikaelpatel mikaelpatel reopened this Apr 13, 2013
@mikaelpatel
Copy link
Owner

It seems to be the 1V1 internal reference voltage generator that needs additional startup time. I added this to AnalogPin::bandgap().

@ghost ghost assigned mikaelpatel Nov 25, 2013
@mikaelpatel mikaelpatel removed their assignment Jul 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants