View previous topic :: View next topic |
Author |
Message |
ashrau
Joined: 25 Feb 2009 Posts: 9
|
sampling timing help |
Posted: Wed Feb 25, 2009 2:33 am |
|
|
Hi,
I need to sample an analog signal on channel 0 of PIC18F4220. My problem is that i need to take 104 samples, and each of them needs to be spaced exactly 192 microseconds apart. This is for a frequency filter, so the time b/w samples has to be exact.
I am unsure as to how long the a/d acquisition and conversion takes, so that i can accordingly set the delay between samples so that the total delay is exactly 192 us.
Any code along with explanation of what is going on will be greatly appreciated. I would also appreciate a simple explanation of how I can find out the time it takes for the a/d acquisition and conversion process. I am relatively new to microcontrollers. Thanks in advance.
ashrau _________________ ashrau |
|
|
Sydney
Joined: 13 Feb 2009 Posts: 71
|
|
Posted: Wed Feb 25, 2009 3:33 am |
|
|
I would setup an rtcc interrupt every 192us, which would be fairly exact. Eg with an 8mhz xtal, and a prescalar of 2 the rtcc will increment every 1us. Or you could clear the rtcc before the conversion, and wait until the rtcc reaches 192 before looping to the next conversion. Either way it doesnt matter how long the conversion takes. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Feb 25, 2009 8:46 am |
|
|
If you still want to know the time the A/D conversion takes, the very best way is to read the datasheet. It takes a little time to study it but you learn just what is going on. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah Guest
|
|
Posted: Wed Feb 25, 2009 9:33 am |
|
|
Realistically, Sydney is right, except possibly for chosing the RTCC.
Provided you are using a PIC, that has a timer2 module, far easier to use this, since you can program it to automatically interrupt/reset at a specified number of counts.
Trying to 'cycle count', to get exactly 192uSec, will involve you in working out not only the ADC timing, but how long every memory transfer takes etc.. Easiest way, is probably to use the MPLAB smulator, and just run the stopwatch function in this,then you can tell exactly how long everything takes if needed.
Best Wishes |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Wed Feb 25, 2009 10:14 am |
|
|
Ttelmah wrote: | ...Trying to 'cycle count', to get exactly 192uSec, will involve you in working out not only the ADC timing, but how long every memory transfer takes etc... |
You can do cycle counting without knowing exactly how long an ADC conversion takes. You just need to ensure that you are waiting "long enough". Suppose the ADC conversion was taking 11 usec. Then just make sure you wait at least 50 usec before reading the result. There will be no need to check the DONE bit. In fact I would prefer cycle counting to using a timer if there is nothing else to be done during the sampling. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
Guest
|
|
Posted: Wed Feb 25, 2009 10:27 am |
|
|
Yes. However I was more worried about the whole approach to cycle counting to get the 192uSc total. It is too easy to miss things like (for instance), it taking longer to access RAM, after one point in the data array, as an extra bank switch appears. Though ccle counting is very essential when using some low level applications, it is easier to get the whole thing accurate, without cumulative errors, by using the interrupt flag (remember you don't actually have to use an _interrupt_ - a search hee will find lots of examples on doing this).
Best Wishes |
|
|
Sydney
Joined: 13 Feb 2009 Posts: 71
|
|
Posted: Thu Feb 26, 2009 4:29 pm |
|
|
Ttelmah wrote: | Provided you are using a PIC, that has a timer2 module, far easier to use this, since you can program it to automatically interrupt/reset at a specified number of counts. |
Good stuff, I never got as far as timer2, now I can get an exact 20ms interrupt ;) (not that 19.968ms wasn't good enough) |
|
|
|