hi,
i m doing simle adc sample pgm.(using pic 18f, in c18 compiler). im my pgm,i m using potentiometr in portA1.and varying it.then i printed that integer, aftr adc conversion.since i m using 10 bit adc, i m getting values from 0 to 1023 while changing the potentiometer.ie, in below pgm, im getting xvalue as 0-1023.
but, aftr the calculation
Code:
z = (100*(xvalue-164));
y = z/634;
i printed y on hyper.when the value of xvalue increases, y is also increasing w.r.t the formula,up to xvalue = 820.ie, at xvalue 820 , im getting 103. that is ok. but when i varied potentiometr, ie, when i made xvalue to 849, i m getting y as 4. actual value after calculation should be 108.but we are getting 4. and like that at xvalue 1023, in hyper its displaying 32.bt actually we are expecting 135. why the displayed value is changing from xvalue 830?upto that the calculated value is displayed exactly on hyper.but aftr 830, it is changing.
here is my code...
Code:
unsigned int z = 0;
unsigned int y = 0;
char rstr[8];
unsigned char xvalueASCII[8];
while(1)
{
xvalue = ADC_Convert();
z = (100*(xvalue-164));
y = z/634;
putrsUSART( "\n\rread adc value is......");
itoa(xvalue, rstr);
putsUSART( rstr );
putrsUSART( "\n\rxvalueASCII is......");
itoa(y, xvalueASCII);
putsUSART( xvalueASCII );
im getting in hyper
Code:
read adc value is......619
xvalueASCII is......71
read adc value is......793
xvalueASCII is......99
read adc value is......802
xvalueASCII is......100
read adc value is......818
xvalueASCII is......103
////////// up to this, output is ok.....
read adc value is......849
xvalueASCII is......4 ///////////////actualll 108
read adc value is......872
xvalueASCII is......8 /////////////////// 111
read adc value is......896
xvalueASCII is......12 ///////////////// 115
read adc value is......921
xvalueASCII is......16
read adc value is......947
xvalueASCII is......20
read adc value is......965
xvalueASCII is......22
read adc value is......974
xvalueASCII is......24
read adc value is......1023
xvalueASCII is......32
please give me one solution to solve this different out displayed in hyper..
putrsUSART( "\n\rread adc value is......");
itoa(xvalue, rstr);
putsUSART( rstr );
putrsUSART( "\n\rxvalueASCII is......");
ultoa(((100*(xvalue-164))/634), xvalueASCII); // convert binary value to a series of printable characters
putsUSART( xvalueASCII );
then also i m getting same result.
and then i tried unsigned long int. then also there is no change in output.
here i used y= ((100*(xvalue-164))/634), then the maximum value of y is 135.then i tried by setting y as both unsigned int and unsigned long int. in both case i m getting same output
While what you are trying is along the right lines, you need to very specifically tell the compiler what to do within calculations if you are changing from one type of variable to another.
should work (depending on your compiler). The important thing is that all values of of the same type in the calculation otherwise the compiler will decide whether to make larger variables smaller or vice versa. You need to make those decisions.
in this i have to send Vout and sensor_RH via usart. since these are float type, for converting these to string, i used sprintf function. but sprintf is not working properly.. how can i convert float to char..
please help me ...
itoa means integer to asccii .. so we suppose that ADC_Convert return an integer value.
so result Vout can be integer.
or you must use float for sprintf !
try this:
sprintf(Voutstr,"%f",(float) Vout);
but, i had myself some probleme with sprintf , not successfull result,
so i a m using another subroutines wich woks fine to convert float to ascii.
you need to declare a buffer to receive the string!
you can find it in the attached file
so i get this kind of result ADC integer convert to float
with
integer xvalue;
float Vout;
Vout= (float)(xvalue)*4/1024.0;
MPLABIDE V8.84 C18: mcc18.exe v3.41
Test sprintf PIC18F46K22 RS232 19200 bds Fclock=8Mhz
0<0> output in volt=0.000<0>
66<0> output in volt=0.258<0>
132<0> output in volt=0.516<0>
198<0> output in volt=0.773<0>
264<0> output in volt=1.031<0>
330<0> output in volt=1.289<0>
396<0> output in volt=1.547<0>
462<0> output in volt=1.805<0>
528<0> output in volt=2.062<0>
594<0> output in volt=2.320<0>
660<0> output in volt=2.578<0>
726<0> output in volt=2.836<0>
792<0> output in volt=3.094<0>
858<0> output in volt=3.352<0>
924<0> output in volt=3.609<0>
990<0> output in volt=3.867<0>