Hello guys,
This question might be better suited for a beginner C forum but i think you guys can help :).
What I am trying to do is extract the BCD of a decimal value. For example; I'm using the ADC12 on a MSP 2xx series MCU to represent 12v of a battery.(voltage indicator).
So, I'll have 4095 points measuring 12v, or .003/volt.
Now, here is the code for outputting the integer format of the battery voltage:
void LCD_output_result(unsigned char cursorpos, float data) //data is the contents of ADC12MEM0 x slope(12/4095)
{
int tmp_result, decimal1;
float decimal;
unsigned char bcd0,bcd1,bcd2,bcd3;
tmp_result = (int)data;
bcd0 = (tmp_result / 1000) + 48; // divide by 1000 and add 48 for ASCI
tmp_result = tmp_result % 1000;
bcd1 = (tmp_result / 100) + 48; // divide by 100 and add 48 for ASCI
tmp_result = tmp_result % 100;
bcd2 = (tmp_result / 10) + 48; // divide by 10 and add 48 for ASCI
bcd3 = (tmp_result % 10) +48;
LCD_cursor(cursorpos);
if (!bcd0)
{
LCD_char(0x20);
LCD_char(0x20);
}
else
{
LCD_data(bcd0);
}
LCD_data(bcd1);
LCD_data(bcd2);
LCD_data(bcd3);
So as you can see a series of modulus and division operators are used to get the bcd values of the voltage in order to send each character to the LCD_Data function. My question has to do with trying to extract the .x value from that voltage. Say my voltage is 9.5v. I'll put my code and logic down here:
decimal =fmod(data,1); // i use Fmod operator since normal modulus doesn't work with floats example (9.504 % 1 = .504
decimal = decimal * 1000.0; // .504 * 1000 = 504
decimal = decimal / 100.0; // 504 / 100 = 5.0
decimal1 = (int)decimal; // 5.0> 5
decimal1 = decimal1+48; // add 48 for ascii
LCD_data(decimal1); //send digit to LCD_Data function
Is my logic/code correct here? Something doesn't seem to be working as my digit after the decimal doesn't change when changing my input to the ADC pin. Any tips/suggestions are appreciated!
Thanks,