View previous topic :: View next topic |
Author |
Message |
shardul
Joined: 12 Nov 2010 Posts: 8
|
ADC sampling and pulse at same time |
Posted: Fri Nov 12, 2010 1:06 am |
|
|
hi,
I am developing an application that requires a continuous pulse at 1pin of microcontroller and simultaneous ADC scanning (which can't be interrupted).
So is there any controller which does multiple tasks at a time?
Last edited by shardul on Fri Nov 12, 2010 2:50 am; edited 1 time in total |
|
|
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Fri Nov 12, 2010 2:06 am |
|
|
A lot of (most) PICs have built in hardware timers that can generate clock signals completely independent of the rest of the program. So you can have the hardware timer pin generating a pulse that will not affect or be affected by the adc readings and other programmed functions. What is the pulse going to be used for? Clock signal? Led blinking? This will help determine what kind of hardware timer you want to use (PWM, Timer2, etc). _________________ Vinnie Ryan |
|
|
shardul
Joined: 12 Nov 2010 Posts: 8
|
|
Posted: Fri Nov 12, 2010 2:44 am |
|
|
Thanks vinnie.
I am scanning an AC input (via ADC channel), and the time I am taking the sample is very important and as far as I know any timer holds current state of program and runs interrupt sub-routine then comes back to held state of program.
My view is.... timer overflows and generates interrupt when I am taking ADC sample then my ADC sample time will be pushed forward by (the time taken by) timer ISR routine.
In short if I am taking ADC sample after every 1ms then I can't even tolerate 1.01ms delay in that. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Nov 12, 2010 3:09 am |
|
|
This is possibly one of the few cases where the ADC interrupt might be used.
The PIC CCP, has the ability to set or reset an output pin. It separately has the ability to generate a 'special event trigger', and _start_ the ADC. You would need to use both CCP's, and start them immediately after one another, using the same clock. Then the sequence would be:
1) CCP1 triggers and starts the ADC conversion. At this instant, the internal sampling capacitor is disconnected from the input.
2) CCP2 triggers and sets the output pin, on the same clock.
3) ADC interrupt fires to signal that the ADC _conversion_ is complete
4) You call the ADC interrupt routine, and read the value. Possibly clearing the CCP2 output at this point.
Now, even if other things are going on, the 'worst' that would happen, is if another interrupt fires before the ADC interrupt. The actual transfer of the ADC value would then be delayed, and the output pulse would end up being wider than it should be. However the actual ADC _sampling_ will still have occurred at the programmed instant. If other interrupts are not used, the timings would be exact.
The point is that the ADC 'sampling', takes place at an instant specified by the hardware. The actual _reading_ of this value, occurs later, and may well vary in time, but by triggering the ADC using the CCP, you get precise synchronous timing of the sample event.
Now, the only thing I'm not 'sure' about - would need to be tested, is what happens if you set two CCP's to trigger on the same clock cycle?. Do both events occur, or does one 'take precedence'?.....
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Nov 12, 2010 7:06 am |
|
|
The PIC PWM hardware does NOT use interrupts to generate its output. You should be able to generate your pulse using PWM and have the rest of the PIC to do your AC line monitoring without being disturbed. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Nov 12, 2010 7:46 am |
|
|
The problem then is how to make the ADC 'synchronous' to the PWM. Remember that the PWM uses a different timer to the CCP, and there is nothing about the PWM, to trigger the ADC.
A combination of one PWM, the other CCP, and the ADC interrupt, ought to be able to meet all the criteria. If you set the CCP timer, to reset at the same number of clocks as the PWM timer, and trigger the start of the ADC conversion at this point, the timings should all 'line up'. Downside would be that you'd either have to start the PWM timer with an offset, or it'd be the falling edge of the PWM, that lines up with the ADC starting. On chips with the ECCP though, you could invert this output.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Fri Nov 12, 2010 3:53 pm |
|
|
Though for the first cases, you are then 'back' to the actual start of the ADC conversion not occurring till the 'interrupt latency' after the PWM event, and if anything else is interrupting, the accuracy will be gone. This is the whole point of using the CCP's ability to trigger the actual start of the conversion, since it will be triggered by hardware without this delay.
Generally, much easier, but for a totally 'synchronous' result, using the hardware trigger will be the more accurate route.
YPYMATYC
The real answer is bkamen's post for chips with the power PWM, at the end of the posted thread. This allows the PWM, to be used as the trigger of the ADC conversion. Much better.
So, going for a chip with this ability, is the 'answer'.
Best Wishes |
|
|
|