ksraja
Joined: 08 Dec 2014 Posts: 8
|
need adc interrupt code for PIC18F6722 |
Posted: Wed Dec 10, 2014 11:24 pm |
|
|
Hi
My project is in robotics, here i want to read the position of motor where it is, so that I am using Potentiometer to read the position of motor.
Pls can anyone provide the adc interrupt code to complete the application. |
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Dec 11, 2014 1:28 am |
|
|
Start at the beginning. What makes you think you need the interrupt?.
It generally takes longer to get into and out of the interrupt handler, than it does to complete an entire ADC reading. The only reason to use the ADC interrupt, is when you use something else to trigger the actual conversion.
So the sequence becomes:
The event happens (whatever it may be), and starts the ADC.
The ADC then interrupts when the conversion completes.
You take the reading.
The actual reading is simple - one line of code:
val=read_adc(ADC_READ_ONLY);
Nothing else (where 'val' is the variable you want the reading put into).
So the question is, what are you going to use to start the ADC?. This is where the complexity comes, not in the ADC interrupt. You can trigger it from a CCP, which therefore could represent a 'time', or an external trigger. You need to work out what you are going to trigger 'from' before starting to think about ADC interrupts.
If you search the forum, I have in the past posted examples of how to do the ADC readings at a specified time from the CCP.
There are also other ways of taking ADC readings involving time, without the ADC interrupt. For these the sequence is:
In a 'tick' (timer) interrupt:
Read the value from the ADC without performing the conversion
Start a new conversion.
With this at each 'tick' you load the value that was recorded by the ADC which is doing it's conversions 'in the background' between the timer interrupts. This has the big advantage of not involving the overhead of the ADC interrupt, and making the readings synchronous to the 'tick'. If the tick is say a multiple of your PWM frequency, then the readings are synchronous to the PWM, which is often ideal for power control.
Work out 'when' you want your readings to occur then we may be able to help showing you how to do this. |
|