Society of Robots - Robot Forum

Software => Software => Topic started by: frank26080115 on February 17, 2008, 09:56:24 PM

Title: Analog Reading Method, Good or Bad?
Post by: frank26080115 on February 17, 2008, 09:56:24 PM
Instead of calling functions to convert once, what if you call one function and then it converts the voltages on all the pins for you in the background, and all you need to do is read from an array? I know it's slower but it runs in the background so your normal programming operates faster (the interrupt routine isn't long).

Just wanted to know what you think.

Code: [Select]
uint8_t currentADC; // pin currently being read
// just a flag to indicate that all channels have been read at least once
uint8_t ADCAllFlag;
uint8_t ADCRes[8]; // result array

void analogInit()
{
sei(); // enable interrupts
currentADC = 0;
ADCAllFlag = 0;
ADMUX = 0b01100000; // select reference, channel 0
DIDR0 = 0b11111111; // disable normal port function
DDRA = 0; // all inputs
PORTA = 0; // clear, no pullups
ADCSRA = 0b10001111; // set ADC clk prescaler, enable ADC and interrupt
sbi(ADCSRA, ADSC); // starts conversion
while(bit_is_clear(ADCAllFlag, 0)); // wait for first read to finish
}

ISR(SIG_ADC)
{
ADCRes[currentADC] = ADCH; // load into result array
currentADC++; // increment
if(currentADC > 7)
{
currentADC = 0; // reset counter
ADCAllFlag = 1; // set the flag
}
ADMUX = 0b01100000 + currentADC; // sets channel
sbi(ADCSRA, ADSC); // starts conversion
}
Title: Re: Analog Reading Method, Good or Bad?
Post by: Admin on February 18, 2008, 10:12:27 AM
Quote
Instead of calling functions to convert once, what if you call one function and then it converts the voltages on all the pins for you in the background, and all you need to do is read from an array?
The problem with this method is that the data in your array quickly gets outdated. Its best to read your sensor just right before you use the data.
Title: Re: Analog Reading Method, Good or Bad?
Post by: frank26080115 on February 18, 2008, 11:45:01 AM
it's still only a fraction of a second, shouldn't be a problem if the robot is slow. I think it's about 0.7 ms (calculated by 128 prescaler * cycles per sample * number of pins / clock frequency, 128*14*7/18432000 )to take a sample from every pin, and the robot won't move much during that time.