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!