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 CCS Technical Support

analog to digital conversion with 12f675

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



Joined: 26 Oct 2005
Posts: 4

View user's profile Send private message Send e-mail MSN Messenger

analog to digital conversion with 12f675
PostPosted: Wed Jan 11, 2006 2:24 pm     Reply with quote

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







PostPosted: Wed Jan 11, 2006 4:24 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 11, 2006 8:13 pm     Reply with quote

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.
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