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

help with my code

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



Joined: 28 Nov 2007
Posts: 3

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

help with my code
PostPosted: Tue Dec 04, 2007 3:27 am     Reply with quote

Sad I have a program problem , this program is not working like iI was expecting in it's simulation and I do not know what is going wrong with it.
This program is designed to take frequently sampled analog input through pin A1, and to use the value obtained in mathematical function,and to output a subsequent analog output when some conditions are satisfied.
Can somebody help me on ? Smile

[code]# include<18F458.h>
# device ADC=10
# use delay (clock=10000000,RESTART_WDT)
# Fuses HS,NOWDT
# include<stdio.h>
# use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)

float F1(float b1,float I1, float G1) // function 1 definition
{ float y;
y=(-4.16*1e-5+5.83*1e-7*G1-1.66*1e-9*G1*G1)*b1;
return y;
}
float F2(float b1,float I1, float G1) // function 1 definition
{ float y;
y=(3*1e-2*b1*G1*G1)/(2*1e4+G1*G1)-0.3*I1;
return y;
}
float F3(float b1,float I1, float G1) // function 1 definition
{ float y;
y=0.6-(1e-3+5*1e-4*I1)*G1;
return y;
}

void main (void)
{ float F1(float,float,float );
float F2(float,float,float );
float F3(float,float,float );
void basal(void);
unsigned int du;
int A,B;
float I1,I2,b1,b2,G0,G1,L1,L2,K1,K2,H1,valeur,h,u;
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
set_tris_d(0x00);
set_tris_b(0x0F);
set_adc_channel(0);
output_low (PIN_B0);
A=0;B=0;valeur=0;h=1;
set_adc_channel(1);
debut: while(1)
{ I1=10;b1=300; // initial condition before entering the loop do
do
{delay_ms(30000);
G0=read_adc(1);
delay_ms(30000);
G1=read_adc(1);
output_low(PIN_B1);
L1=h*F1(b1,I1,G1);
K1=h*F2(b1,I1,G1);
H1=h*F3(b1,I1,G1);
L2=h*F1(b1+L1,I1+K1,G1+H1);
K2=h*F2(b1+L1,I1+K1,G1+H1);
b2=b1+(L1+L2)/2;
I2=I1+(K1+K2)/2;
u=7.35*1e-4*(I2-I1); // calibration
I1=I2;b1=b2;
if (u>0)
{du=255-(int16)u; // digital value send for output
output_high(PIN_B1);
output_d(du);
}
}while ((G1/100)>=1.01); // end of the loop do condition


}
}
Ttelmah
Guest







PostPosted: Tue Dec 04, 2007 5:10 am     Reply with quote

When posting code, you must have both the start code, and end code buttons. You have only got the 'start' one, so the code has posted as normal lext.

I'd suspect your problem is simply the read_adc function. You appear to be trying to send the function a channel number, as well as setting it earlier!. It does not work like this. You have to select the channel you want to read, with the 'set_adc_channel' function, wait long enough for the internal capacitor to charge to the value (typically about 10uSec), and then read the value. You can give the read function a control value, but this alters what it performs. A value of '1', means 'start a conversion, but do not return the value', which means your ADC reads, will not be returning the required numbers...

As a separate comment, don't use ADC_CLOCK_INTERNAL, if you want accurate readings. Use the master CPU clock, with a suitable divider. The problem is that the internal RC clock is asynchronous to the processor clock, making the results have a higher degree of variation, according to what the processor is doing at the time. If using the internal RC clock, you really need to put the chip to sleep, while the conversion is performed, or values wll be relatively poor. On most chips, this is stated in the data sheet, but it is omitted for the 458, because it has a very slow RC rate, which makes this somewhat less of a problem. It does however still exist.
ADC_CLOCK_DIV_16, will give faster conversions, and slightly better results.

Final comment, think what your formulae are doing.
If you look at the first equation:

y=(-4.16*1e-5+5.83*1e-7*G1-1.66*1e-9*G1*G1)*b1;

In the centre two parts, you have:

5.83*1E7*G1

-1.66*1e9*G1

These sum together as:

-1.6017E9*G1

Two things apply. Don't put a * sign between the value and the exponent. Floating point numbers just have the exponent after them. The compiler _ought_ to be smart enough to work this out, and get rid of the unneeded multiplication, but it is better not to rely on this. The same applies with the terms shown, where simplifying the term, may save uneccessary code and arithmetic.
Also, I would not pass unused values to functions. Again the copiler should be smart enough to remove these, but if not, it involves extra storage space being used...

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