Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

adc output after calculation is different from that displayed on hyper

Status
Not open for further replies.

td micro

Member level 5
Joined
Jun 26, 2012
Messages
86
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,929
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..
 

If xvalue can be 1023 and you multiply by 100 to get z then it will not fit in a 16 bit signed integer.

Keith
 

thank you
i changed my program like this..
Code:
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

which datatype i have to set here?

please guide me...
 
Last edited:

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.

Something like

y=(unsigned int)((100UL*((unsigned long)xvalue -164UL))/634UL);

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.

Keith.
 
thank you..thank you very much ...
this is the 1st time in my life, i m getting the exact output what we need....
thank you ...

- - - Updated - - -

thank you...


i modified that ADC program for one sensor..
here is my code..
Code:
xvalue =  ADC_Convert();

Vout= (( 4*xvalue)/(1024));
sensor_RH= ((float)(Vout/Vcc)-0.16)/0.0062;
True_RH=(sensor_RH)/(1.0546 –((0.00216)*T));     // T room temp
putrsUSART( "\n\rread adc value is......");
itoa(xvalue, rstr); 
putsUSART( rstr );


putrsUSART("\noutput in volt=");
sprintf(Voutstr,"%f",Vout);
putsUSART(Voutstr);

itoa( sensor_RH, sstr );
putrsUSART("\nSensor RH value=");
putsUSART(sstr);
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 ...
 

hello,


Code:
xvalue =  ADC_Convert();
.....
itoa(xvalue, rstr); 
.....
putrsUSART("\noutput in volt=");
sprintf(Voutstr,"%f",Vout);
putsUSART(Voutstr);

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>
 

Attachments

  • 18F46K22_test_float2ascii.zip
    26.2 KB · Views: 75
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top