View previous topic :: View next topic |
Author |
Message |
dmccric
Joined: 07 Dec 2009 Posts: 7 Location: Bradford, West Yorkshire
|
Sync ADC Reading with PWM |
Posted: Thu Jun 10, 2010 3:54 am |
|
|
Hello,
I am using the PIC18F4431 and need to know how to sync the reading of the adc when the PWM pin is high. At the moment I am using the standard adc reading but this is inaccurate.
Cheers
Dave |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 10, 2010 12:06 pm |
|
|
You could poll the INT_TIMER2 interrupt flag in a loop. When it goes
high it means a PWM cycle has started. You could read the ADC at that
time. See this example code which tests the INT_TIMER2 flag:
http://www.ccsinfo.com/forum/viewtopic.php?t=41403&start=5
Or if you want to do it inside an interrupt routine, this code shows how
to use the Timer2 interrupt. The PWM cycle starts when the
#int_timer2 interrupt occurs. But, if your PIC is running at a low
frequency, such as 4 MHz, there can be a 25 to 30 usec delay between
the time the Timer2 interrupt occurs, and time that it enters into the
#int_timer2 interrupt routine. This is the time required by the CCS
interrupt handler to "save the state of the machine" and to route to
the #int_timer2 routine.
http://www.ccsinfo.com/forum/viewtopic.php?t=41473&start=1 |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Jun 10, 2010 9:33 pm |
|
|
If you look closely at the ADCON3 register, you'll see you can trigger the A/D to take a measurement on several source inputs as the trigger. One of them being the PowerPWM.
Then you could have an IRQ for the A/D to just get the data when ready.
I did this in the past and it works nice.
You don't even need to set IRQ's for the PPWM unit.. just enable it and set the trigger for the A/D as needed.
Then it's all done in hardware with the only IRQ being the A/D when the result is ready.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|