go away spammer

Author Topic: Analog Reading Method, Good or Bad?  (Read 2428 times)

0 Members and 1 Guest are viewing this topic.

Offline frank26080115Topic starter

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Analog Reading Method, Good or Bad?
« 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
}

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Analog Reading Method, Good or Bad?
« Reply #1 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.

Offline frank26080115Topic starter

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: Analog Reading Method, Good or Bad?
« Reply #2 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.

 


Get Your Ad Here