|
|
View previous topic :: View next topic |
Author |
Message |
platon16382
Joined: 26 Oct 2005 Posts: 4
|
analog to digital conversion with 12f675 |
Posted: Wed Jan 11, 2006 2:24 pm |
|
|
hi , i trying to make a analog to digital conversion with 12f675, the porpuse is to make a close loop control .
i have to make two ADC almost consecutively, what considerations i have to do for improve my code ....
thanks for all
#include "C:\motor control mecanica\prub3.h"
int8 referencia, realimentacion=0, error;
void main()
{
setup_adc_ports(sAN0|sAN1|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_comparator(NC_NC);
setup_vref(FALSE);
while(1)
{
// set_adc_channel(0);
referencia=read_adc();
/* set_adc_channel(1);
realimentacion=read_adc();
*/ error=referencia-realimentacion;
if(error>=0)
{
output_low(PIN_A4);
output_high(PIN_A5);
delay_ms(50);
}
else
{
output_low(PIN_A4);
output_low(PIN_A5);
delay_ms(50);
}
}
} |
|
|
Ttelmah Guest
|
|
Posted: Wed Jan 11, 2006 4:24 pm |
|
|
I'd suggest rethinking your control loop.
Basically, when you change ADC channels, you must wait about 10uSec (and longer if your analog source does not have a low impedance), before starting the conversion. Then the ADC conversion itself, takes twelve cycles of the conversion 'clock'. Now if working with critical timings, it is better to get these 'right', and then take advantage of the delays this code involves to actually do these tasks.
I have done a number of such loops in the past, and the basic 'sequence' used, is similar in each case (though what is used as the 'filler' varies). So, I work like this:
Select channel.
Now perform any arithmetic needed to normalise last set of results.
Now trigger the read (read_adc(ADC_START_ONLY)).
Now calculate the coefficients from the last results.
Read the value (read_adc(ADC_READ_ONLY)).
Loop back.
The basic idea is to use the delays inherent in things like calculating servo coefficients, to provide the delays needed, and to never actually 'wait' for anything. This way, you can get the total loop time as low as possible. On some systems (with chips offering hardware CCPs), I synchronise the loop, to the actual PM. So for instance, I have a power unit here, which uses a 50KHz PWM, and does one operation on each loop (selecting an ADC, then on the next loop starting the read, then on the next pass, reading the value etc..). The total 'servo loop' in this, takes 8 cycles of the CCP, giving over 6KHz on the corrections.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jan 11, 2006 8:13 pm |
|
|
Generally as soon as I read one A/D channel I set the A/D to the next channel I will need. That way the A/D has maximum time to settle to the new voltage.
Read #1
Select #2
Do math on #1
Half of pacing delay
Read #2
Select #1
Do math on #2
Half of pacing delay
Loop _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|
|
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
|