CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

ADC sampling and pulse at same time

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
shardul



Joined: 12 Nov 2010
Posts: 8

View user's profile Send private message

ADC sampling and pulse at same time
PostPosted: Fri Nov 12, 2010 1:06 am     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Fri Nov 12, 2010 2:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 12, 2010 2:44 am     Reply with quote

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: 19359

View user's profile Send private message

PostPosted: Fri Nov 12, 2010 3:09 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 12, 2010 7:06 am     Reply with quote

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: 19359

View user's profile Send private message

PostPosted: Fri Nov 12, 2010 7:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 12, 2010 12:02 pm     Reply with quote

Also see this thread on how to "Sync ADC Reading with PWM":
http://www.ccsinfo.com/forum/viewtopic.php?t=42791
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Fri Nov 12, 2010 3:53 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group