please help

this shouldnt be a difficult question to answer and it gets freaking frustrating to have 40 views and no response of anykind

i'm trying to use the adc, lets forget about EMI and coupling for the time being

assume this

ADC 0 = 0V

ADC 255 = 650V

if i get a 237 from the ADC

the voltage should be 237*650/255=604

at least that's what i want...

but it doesn't works, i assume its due type

if i do this

unsigned char ADCmeasure;

unsigned int Voltage;

ADC1_Measure(1);

ADC1_GetValue8(ADCReadBuffer);

ADCm=ADCReadBuffer[0];

Voltage=(int)ADCm*650/255;

it compiles but is not correct

i think because ADCmeasure*650 might not fit on a int

if i do this

unsigned int ADCm;

long Voltage;

ADC1_Measure(1); //measure

ADC1_GetValue8(ADCReadBuffer); //record

ADCm=(int)ADCReadBuffer[0]; //get first number and cast it to int

Voltage=ADCm*2.54902; //interpret it

ADCm=Voltage; //truncate it

it breaks saying

_DMUL_RC, _DSTRUNC, _DUFLOAT are not defined

if i do this

unsigned int ADCm;

long Voltage;

ADC1_Measure(1); //measure

ADC1_GetValue8(ADCReadBuffer); //record

ADCm=(int)ADCReadBuffer[0]; //get first number and cast it to int

Voltage=ADCm*650/255; //interpret it

ADCm=Voltage; //truncate it

it compiles but still is not correct

i think because it is messing with the sign or something for supposedly negative stuff

so how do i do this?

do i rather make the table of what each adc step mean and just look out for it?

would that be faster than dividing and multiplying on a 8 bit mcu?

thanks

Hello,

It should be possible to stay with a 16-bit calculation, since it will be more efficient than a 32-bit calculation.

In place of the expression

Voltage = (int)ADCm * 650 / 255;

instead use the following equivalent, whose intermediate result will not overflow a 16-bit value.

unsigned int Voltage;

...

Voltage = (unsigned int)ADCm * 130 / 51;

For the case of the 32-bit calculation, another way of forcing this would be -

long Voltage;

...

Voltage = ADCm * 650L / 255;

Regards,

Mac