GP32 Reading Voltage After Modding


SvOlli

Certified Guru
Joined
May 4, 2004
Messages
161
Location
Hannover, Germany
Website
svolli.org
Hi there,

I think it might be very nice to include the software side of the "voltage detection mod" into SvOrbis. I haven't modded my GP32 (yet - maybe B)), so I need someone providing me with the code and testing it, after it went in.

Anyone could help here. If not it's not going into SvOrbis... :(

Greetings,
SvOlli

P.S.: SvOrbis Version 0.8 is on the way (there are some oddities about the new text drawing routine to check first), hope I'll get the code soon, so voltage detection is already in by then.
 
Hi SvOlli,

I presume you're talking about measuring the battery voltage, using the mod I posted pictures for a few days ago. It's very simple, but probably worth you reading section 16 of the S3C2400 User manual.

The registers you need for the ADC are called ADCCON and ADCDAT, they should be in any of the "gp32.h" files floating around as "rADCCON" and "rADCDAT". The ADC conversion speed depends on PCLK, you might like to look into the prescaler settings in ADCCON if you want to change the speed. I just set it to a fairly slow setting that should be OK with pretty much any clock settings.

So, bits 5:3 of ADCCON select the ADC input channel to be converted. Using my mod, the battery voltage is on AIN3, so bits 5:3 of ADCCON should be 011. When you change the selected ADC channel, I think you should give a little time for the ADC to settle down. If you only ever read the battery voltage, and never change the ADC channel, then there's no need for that.

The ADC voltage reference comes from the 3.3V supply in the GP32, so the ADC result is a 10-bit value representing 0V to 3.3V. In other words, 0V input gives 0 output, 3.3V input gives 1023 output, 1.65 volts input gives 512 output etc etc. An easy conversion to a useful value is to multiply by 330 and then shift right by 10 bits, this gives a range of 0 for 0V to 330 for 3.3V.

So putting it all together, you get something like this:
Code:
void SelectAdcChannel (int channel)
{
   unsigned short adccon;

   adccon = 0x6000; /* prescaler value is 128 */
   adccon += ((unsigned short)channel)<<3;
   rADCCON = adccon;
}

void StartAdc (void)
{
   rADCCON |= 1;
}

int ReadAdcResult (void)
{
   int result;

   while (rADCCON & 1); /* wait for conversion to end */
   result = (((int)rADCDAT) * 330) >> 10;
   return result;
}

If you leave a long enough time (more than a millisecond or so) between calling StartAdc and ReadAdcResult, then there will be no delay in waiting for the ADC to finish.

The next problem is deciding what to do with the result. Freshly charged alkaline batteries will read about 300 (3V), newly charged NiCd or NiMH batteries will read about 240 (2.4V). I don't know enough about battery characteristics to say what level indicates that the batteries should be replaced, although I would imagine that if the reading drops to about 170 (1.7V) for any battery then the gp32 is about to stop working. You'll have to play around with that.

Hope this helps! :)
 
Thanks for the code! I'll hack it in tomorrow.

Robster said:
The next problem is deciding what to do with the result. Freshly charged alkaline batteries will read about 300 (3V), newly charged NiCd or NiMH batteries will read about 240 (2.4V). I don't know enough about battery characteristics to say what level indicates that the batteries should be replaced, although I would imagine that if the reading drops to about 170 (1.7V) for any battery then the gp32 is about to stop working. You'll have to play around with that.
That's what I'm gonna do the easy way. Just print out the number and let the user decide. :p If I'm reading a zero this means there's no mod, or what reading will I get?

Robster said:
Hope this helps! :)
Yes, indeed. :)

Thanks again,
SvOlli
 
Last edited by a moderator:
If the mod isn't there, you should read 0 but remember that it's an input that's not terminated so it will pick up noise easily... you could get anything.

I forgot to say, the resistor in my mod was 3900 ohms, it's not too critical, but probably keep it between 2700 ohms and 4700 ohms. It should be high enough to protect the CPU input from the battery voltage when the gp32 is turned on and the cpu supply isn't stable yet, but not so high that it affects the reading.
 
Rob, out of interest, roughly how sensitive is this mod?

I was just wondering if you could manage to use the extremely minor discharges of voltage when the GP32 is off to actually measure time passed approximately. I mean, obviously, that kind of measurement would have to be configured for each battery - and presumably depending on when you "trained" it - i.e. if the battery is half full, the discharge will be different to if it is newly charged. And of course, variations across different brands would mean anyone trying to use it would *have* to set it up for their own batteries rather than use presets, since the discharges when off would be so small anyway.

Basically, if the mod is actually sensitive enough, what you'd do would be to take the voltage before you quit a tester program. Then turn off the GP32, and leave it off for say 10 hours, maybe a day. Then turn on and take a new reading. And then work use the difference between those - assuming it took the voltage at *every* start up (you'd have to integrate the callendar and especially the "date" file into the firmware for it to be of any use at all) - to work out the ammount of time the GP32 had been off, and suggest a new date accordngly.

Perhaps a little far fetched, and maybe less easy anyway than doing Darkfader's rtc mod, but nonetheless, interesting.
 
Hi Tobriand!

The absolute resolution of the mod is 3.3V / 1024, or about 3.2mV. However, many things affect this and you will never get that resolution.

First, the analog reference is the 3.3V rail, which may be be off by up to a couple of percent, or (say) 50mV. Then there are standard ADC characteristics like integral and differential linearity errors, which add up to about 10mV. These wouldn't have much effect in one GP32, because they're pretty repeatable, but they will certainly cause issues in an application running on different GP32s.

A bigger issue is that the batteries don't discharge much at all when the unit is off. In fact, the biggest change in battery voltage that you will see, when you've been running the GP32 for a while (especially with lighting), is that when the unit is switched off the voltage will increase. This is because the voltage drops when under load, then recovers when the load is removed.

It's a really interesting idea, but unfortunately I don't think it's a runner :)
 
Back
Top