Try to avoid float. If you have a 10-bit ADC value (0-1023 counts), and a 5v reference voltage, then I assume you want to display volts.I have a ADC result that I am converting to a float. I want to display on a 4 digit 7 segment display via a BCD decoder. How do I break the float into 100’s, 10’s, 1’s and 10th’s to convert to BCD for the decoder
Not to be too picky, but I think maybe your calculator needs a new battery.So if you measure 330 counts, you'd calculate 5 * 330 / 1023 = 1.651 volts. However,
You can also say 5000 * 330 / 1023 = 1651
Don't be afraid to to use floats sparingly if the system has the resources, timing is not critical and fractional scaling is a requirement to make a standard internal data integer storage type (10th's of X parameter) for later processing.I have a ADC result that I am converting to a float. I want to display on a 4 digit 7 segment display via a BCD decoder. How do I break the float into 100’s, 10’s, 1’s and 10th’s to convert to BCD for the decoder
A quick example with made-up numbers:nsaspook not sure I understand your point
#include <stdio.h>
int main()
{
int n = 7569;
int th,h,t,u; // Thousands,hundreds,tens,units
u=n%10;
t=(n/10)%10;
h=(n/100)%10;
th=n/1000;
printf("Thousands = %d , Hundreds = %d , Tens = %d , Units = %d\n",th,h,t,u);
return 0;
}
So no need whatever for floats - which are MUCH slower, take MUCH more memory (neither of which are really a problem in your case) and also less accurate - just do it in integers and manually add a decimal point in the display routine - as in my assembler tutorial routines.Nigel Goodwin & gophert in am using the ADC to read temperature from a thermistor to a tenth of a degree. Nigel Goodwin I'm using c in MPLab
LCD_charD(Thou);
LCD_charD(Hund);
LCD_charD(Tens);
LCD_char(46); //decimal point
LCD_charD(Ones);
void LCD_charD(unsigned char ch)
{
ch+=0x30;
LCD_char(ch); // convert to ascii
}
/*
* assuming the value is to be converted to 5V
* with 1 decimal place
*/
void showADC(uint16_t dat){
uint32_t total;
uint8_t tenths,units,tens,hund;
total=dat*5000; //multiply by millivolts
total>>=10; //divide by ADC resolution (1024)
tenths=total%10+0x30;
total/=10;
units=total%10+0x30;
total/=10;
tens=total%10+0x30;
total/=10;
hund=total+0x30;
}
/*
* assuming the value is to be converted to 5V
* with 1 decimal place
*/
void showADC(uint16_t dat){
uint32_t total;
unit16_t totint;
uint8_t tenths,units,tens,hund;
total=dat*5000; //multiply by millivolts
totint = total >> 10; //divide by ADC resolution (1024)
tenths=totint%10+0x30;
totint/=10;
units=totint%10+0x30;
totint/=10;
tens=totint%10+0x30;
totint/=10;
hund=totint+0x30;
}
uint32_t total;
uint16_t dat;
total=dat*5000; //multiply by millivolts
total=dat*5000ul; //multiply by millivolts
double TempConversion (int TempResult)
{
double Temp;
Temp = log(10000.0*((1024.0/TempResult-1)));
// =log(10000.0/(1024.0/RawADC-1)) // for pull-up configuration
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp -= 273.15; // Convert Kelvin to Celcius
return Temp;
}
Stop thinking of it as a float. You can display as a string and cram a decimal point anywhere you want.rjenkinsgb & Pommie thanks for your information but I am reading the ADC through a thermistor for temperature, so not sure how to apply it.
I am taking the result and type casting to a float to display remotely on a LCD and locally on a four digit 7 segment(that I'm driving w/ a BCD decoder) display to the tenth of a degree.C:double TempConversion (int TempResult) { double Temp; Temp = log(10000.0*((1024.0/TempResult-1))); // =log(10000.0/(1024.0/RawADC-1)) // for pull-up configuration Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); Temp -= 273.15; // Convert Kelvin to Celcius return Temp; }