## Pesky XMEGA A/D - GOTCHA, ya bugger!

7 posts / 0 new
Author
Message

For what it's worth, today I excelled, after a week of expelling in getting the XMEGA 256A3U's A/D to finally do what I wanted. It was an experience akin to herding cats, but here's my results.

Firstly, I am to programming what Rudolph Nureyev was to bricklaying. I'm a hardware engineer, but I persevere, as well as perspire, as I'm the only muppet in the company for whom I work (very part-time, mostly unpaid) who knows which way round to install a resistor...(I kid you not - I have been asked! "Does the black band go to ground?")
Concept: Low current application, battery/solar powered, and need to send via GSM its results.
Need to measure the battery. Circuit snippets in attached files:

My previous attempts resulted in me buying numerous lottery tickets, using the number it spewed out. They were random enough...However, thanks to inspiration from a bloke at http://barefootelectronics.com/x... I got a result.
Need to measure a nominal 12V battery, powered from a solar cell. Need only a few percent resolution. I used a potential divider of a 1.78 megohm resistor in series with a 93.1 Kilohm. Minimising current, even that takes 7 microamps...
I use the internal 1V reference, and my code looks like:

```/*****************************
*
*	Author:	Andy
*	Created:	9/11/2015
*	Modified	9/11/2015
*
*
*	Purpose: To read the ADC in the logger to determine the battery voltage. For the ehp-data service, the
*	battery voltage is returned 10* actual, as the 'old' DL6 could only handle positive integers.
*	The battery is connected to channel ADC1 (pin 63/PA1) by means of a potential divider consisting
*	of a 1M78 resistor from battery, and a 93K1 resistor to ground.
*	This gives a theoretical(!) voltage to the ADC of:
*	0.5964V for 12V battery, or 0.9941V for a 20V unit.
*	Reference is the internal 1V bandgap.
*	NOTE; The clock speed for the ADC is 100kHz - 2MHz, so need to run CPU at 2MHz and
*	(try) prescale by 4
****************************/

uint16_t get_battery()
{
{
// UNSIGNED Mode, High Z, no current limit, not freerunning, resolution 12 bit, right justified.
ADCA.EVCTRL = 0 ; // no events
ADCA.PRESCALER = ADC_PRESCALER_DIV8_gc ; // Divide clock by 4 (2MHz/4 = 500KHz)

// The following affect only Chanel 1 (Where the attenuated battery goes)
}

for(uint8_t Waste = 0; Waste<2; Waste++)
{
while (ADCA.INTFLAGS == 0 ) ; // Wait for complete
; // Do nothing!
}

float temp_f;
temp_f = (20.14 * ((result-200) / 4096.0)) * 10; // 20.14 is the battery potential divider ratio

return ( (int) temp_f);
}
```

Please don't laugh, I learnt at the Blunt Force Trauma School of Programming...I'm sure I'll be ripped to shreds, but...I'm far better with a blowtorch soldering iron than a keyboard.
(Calculated voltage at a Vbatt of 12.95 is 0.643, I measured 0,638V with my Fluke 10Megohm impedance, and almost bugger all with my (t)rusty 45-year-old AVO-8 (200K/volt) )

Oh, the 10* multiplication is because the previous machine couldn't handle floats, it was a PIC BASIC Stamp.

(Replying to oneself is probably considered Bad Form, but I thought I'd publish an early - scrapped - design I still use for testing). Final is unrecognisable.
For those cheapskates who don't put pullups on their I2C/TWI lines - Good News! The TCA9406 level shifter has them built-in (5K) and the OE pin is 5V-tolerant, so those four 10K resistors underneath it are not needed.
Again, hope it helps. That's what a forum is for, aint it?

-Andy

## Attachment(s):

However, thanks to inspiration from a bloke at http://barefootelectronics.com/x... I got a result.

That would be our  own "Torby" or Tom to his friends.

solar powered

in Finland... you are setting difficult objectives aren't you? OK, OK, I know, I've been there many times even have a T-shirt.

Ross McKenzie ValuSoft Melbourne Australia

I'm the barefoot bloke. Glad to be of service.

Your resistance value might be a little high. The xMega has to charge a capacitor through it in a pretty short time. You might use a p channel mosfet to turn off the power to the divider so it's not drawing current until you want to measure it.

I also find the ADC REALLY smooths out if you put like 10uH between AVCC and the digital supply to keep all that digital noise out. Look at the "schematic checklist" document from Atmel.

Now, which way does this resistor go?

The largest known prime number: 282589933-1

Thanks! You helped me immensely!
Yeah, did think about a P-channel/N-channel combo. - which already works, see bottom right of the schematic I posted..

But - code seems to give me as much resolution as the crappy ADC can, I guess. Finding ALL my prototypes (5) have miraculously fixed the A/D errors (Must have - the signature bytes in all cases read zero), I can live with 100mV error - it's mainly to decide if the battery has enough 'grunt' to mess about with a GSM network, or should I wait 6 months before the sun comes up again...

Resistor positioning? Like most of my designs.

They go south, of course...

Last Edited: Sun. Mar 13, 2016 - 08:45 PM

OH, I wish, I wish, that there is a way to "flag" threads of high interest (per user). I know that I am going to "need" this, a few months down the road. Andy crofts - could you post your "Battery_ADC.h"?

Thanks

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

andycrofts wrote:
it's mainly to decide if the battery has enough 'grunt' to mess about with a GSM network, or should I wait 6 months before the sun comes up again...
fyi

Embedded Experience: First LoRa network now open

May 7, 2015

http://embeddedexperience.blogspot.com/2015/05/first-lora-network-now-open.html

Espotel has released first open LoRa network in Finland for developers' and hackers' use.

...

... or winter blocks the energy source of the gateway,

...

The gateway is solar powered in order to emphasis the low power nature of LoRa RF technology.

...

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