View Single Post
  #21   Spotlight this post!  
Unread 24-01-2006, 00:22
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Cañada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Cañada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: Analog-to-Digital Converter Code

Quote:
Originally Posted by Joel J.
Yes, it samples faster than the rate the data is being sent.

I have another question for Kevin (of course, I'm trying to optimize everything, but I don't want to make a bad move while doing this):

Is there a specific reason for switching from doing both the initiation of and saving of an ADC sample in the timer interrupt? I remember in last year's gyro code that both functions were done in the same place, whereas now you are using both the timer interrupt, and the ADC interrupt to take the samples. Is it safe to kinda shift the ADC functionality to the timer interrupt and reduce the extra overhead by removing the ADC interrupt altogether? Or do the two of them together execute faster, somehow, than the single timer?

Before you:
- Started the initial A/D conversion in Initialize_XXXX()
- Saved the value at the start of the timer2 interrupt
- Did the if/else/etc to put the value in the right place
- Started another A/D conversion by setting the GO bit.
- Exited the timer2 interrupt.

And if I get the go ahead, I'll just do this again.


This is a great question. When I modified the code last Summer to separate the ADC and gyro functionality, I added the ability to sample multiple ADC channels using a simple round-robin algorithm. If you read the pic18f8520 data sheet you'll notice the second paragraph on page 217 which states:
"After the A/D module has been configured as desired, the selected channel must be acquired before the conversion is started...To determine acquisition time, see Section19.1 “A/D Acquisition Requirements”. After this acquisition time has elapsed, the A/D conversion can be started."

What this means is that you need to allow sufficient time for the sample-and-hold capacitor to charge between the time you change the channel and can actually start the conversion. Providing this delay within an ISR is, to say the least, un-cool. My solution was to start the conversion using the timer 2 ISR and manage data and change the ADC channel in the ADC ISR. The sample-and-hold capacitor then has ample time to charge between the ADC and timer 2 ISRs. This is how the currently posted code works.

The pic18f8722, used in the current robot controller, has added circuitry to automatically provide the acquisition delay before conversion begins (see page 277 of the p18f8722 data sheet). In the next revision of the ADC code I suspect that I'll be able to do away with the ADC interrupt and do everything in the timer 2 ISR. Perhaps this would be a good programming exercise for you?

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org