Conversion ADC value

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Conversion ADC value

554 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by drvrh on Wed Jul 15 00:12:42 MST 2015
Hello,

I have a problem with a conversion ADC value, I get ADC value (int) from my ADC, but when I would like convert to voltage (float) I cannot.
I have get 0 after calculate and conversion.
ADC value get int number at 0 to 255.

int iValue = 0;
int vrednostADC = 0;
float fVrednost = 0;
char vrednsotADC1[] = "";
char vredADC[] = "";

ADCInit(4400000);    //set 4.4MHz

    while (1) {

    vrednostADC = ADCRead(5);
    fVrednost = vrednostADC * (3.3 / 4096);             //calculate voltage of ADC

    iValue=(int)fVrednost;                       //convert form int to float

    itoa(iValue, vrednsotADC1, 10);
    itoa(vrednostADC, vredADC, 10);

    PotisniChar(16, 5, 1, vrednsotADC1);                       //look conversion value
    PotisniChar(16, 6, 1, vredADC);                                //look value from ADC (int)
    //LCDPocisti();
    }
0 Kudos
3 Replies

434 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Sun Jul 19 03:07:58 MST 2015
Hello,

itoa can't handle floats:

stdlib.h:
extern char * itoa(int value, char *vstring, unsigned int base);

I recommend to use integer arithmetic, this example gives you millivolts:

int vrednostADC;
char buf[10]; 

vrednostADC = ADCRead(5);
itoa(vrednostADC, buf, 10);
PotisniChar(16, 5, 1, buf);

vrednostADC = (vrednostADC * 3300) / 4096;
itoa(vrednostADC, buf, 10);
PotisniChar(16, 6, 1, buf);


Note: it is important to use parenthesis in this calculation: (vrednostADC * 3300) / 4096.
Otherwise the compiler will replace 3300 / 4096 with 0 (integer rounded down) resulting a division by zero.
The parenthesis tell the compiler to not compute the constant value at compiletime.


0 Kudos

434 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Sun Jul 19 01:12:43 MST 2015

Quote: TheFallGuy
It is to do with the order of the conversion. Convert vrednostADC to float first.

(((float) vrednostADC) * 3.3) / 4096.0



I don't think that is the problem.  3.3 / 4096 gives a double, which gives another double when multiplied with vrednostADC, which then is converted to a float. So it should produce correct values.


Quote: drvrh
char vrednsotADC1[] = "";
char vredADC[] = "";


This is horribly wrong, you will get buffer overflows. Better (you need to make sure that the size of the buffer is large enough):
char vrednsotADC1[12];
char vredADC[12];



Quote:
    iValue=(int)fVrednost;                       //convert form int to float


Assuming your ADC has 12 bits and is working correctly, this will give you an int 0...3. Maybe you just have a low input voltage? Also, the comment is wrong.
0 Kudos

434 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Wed Jul 15 00:33:07 MST 2015
It is to do with the order of the conversion. Convertv vrednostADC to float first.

(((float) vrednostADC) * 3.3) / 4096.0
0 Kudos