Jump to content
Sign in to follow this  
mityeltu

Float Format For Lcd

Recommended Posts

I am working on a simple (I call it that) project to display the results of the pythagorean theorem. I have the user input 'a' and 'b' and then I want to display 'c'. The problem is getting 'c'. I know the format is |signbit(1)|exp(8)|mantissa(23)| So, my real question is, how can I format this for display on an LCD?

 

I tried the following, just to see if I could use a brute force method on it. The output is as follows: 11001111001 and another 21 zeros. I know this is in reverse order because of the way I'm ripping out the bits, but it doesn't make any sense to me either way. Does anyone have any suggestions?

I'm using the 16f887 for this project.

 

#include <system.h>
#include <lcd_driver.h>
#include <float.h>

#define LCD_ARGS  2, /* Interface type: mode 0 = 8bit, 1 = 4bit(low nibble), 2 = 4bit(upper nibble) */ \
1,	 /* Use busy signal: 1 = use busy, 0 = use time delays */\
PORTB, TRISB,  /* Data port and data port tris register */ \
PORTB, TRISB,  /* Control port and control port tris register */ \
1,	/* Bit number of control port is connected to RS */ \
2,	/* Bit number of control port is connected to RW */ \
3	 /* Bit number of control port is connected to Enable */

float a, b, c, a_sqr, b_sqr, c_sqr;
char i, out, x, y;

void main()
{
intcon = 0;
adcon0 = 0;
ansel = 0;
anselh = 0;

trisa = 0;
trisb = 0;
porta = 0;
portb = 0;

lcd_setup();
//a^2+b^2=c^2
a = 1.0;
b = 1.0;
x = 2;
y = 0;
lprintf("c=");
while (1)
{
a_sqr = float32_mul(a, a);
b_sqr = float32_mul(b, b);
c_sqr = float32_add(a_sqr, b_sqr);
c = float32_sqrt(c_sqr);
//floats are 32 bits
for (i = 0; i <= 31; i++)
	{
	if (test_bit(c,i))
		{
		out = 0x31;
		}
	else
		{
		out = 0x30;
		}
	lcd_gotoxy(x,y);
	lcd_datamode();
	lcd_write(out);
	x++;
	if (x == 18)
		{
		x = 0;
		y = 1;
		}
	}
	while (1)
	{}
}

}

Share this post


Link to post
Share on other sites
I am working on a simple (I call it that) project to display the results of the pythagorean theorem. I have the user input 'a' and 'b' and then I want to display 'c'. The problem is getting 'c'. I know the format is |signbit(1)|exp(8)|mantissa(23)| So, my real question is, how can I format this for display on an LCD?

 

I tried the following, just to see if I could use a brute force method on it. The output is as follows: 11001111001 and another 21 zeros. I know this is in reverse order because of the way I'm ripping out the bits, but it doesn't make any sense to me either way. Does anyone have any suggestions?

I'm using the 16f887 for this project.

You are just displaying the raw binary value, the value is encoded (IEEE 754) as you state signbit, exp, mantissa.

The brute force method (if you know the number is specific range) is:

1) Multiply by a factor of 10 (10, 100, 1000 etc) so that the decimal digits required are on the left side of the decimal point.

2) Convert to an integer.

3) Divide by the multiplication factor used to get the whole part.

4) Modulus (%) by the multiplication factor to get the decimal part.

5) Use lprintf to print out the whole part.

6) Use lprintf to print out a "." and the the decimal part part.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Ok, so you're saying I can handle the floats like any other numerical animal I come across? I'll try it, but it doesn't make sense. If the float is composed of 3 parts and I multiply the entire thing by some factor (say 1000) have I not just changed all the values, or is sourceboost smart enough to handle this manipulation for me?

Share this post


Link to post
Share on other sites

OK, excellent... It DID work that way, but I still have not figured out how this exponent thing will work. Since I know where the exponent lies within the float, do I need to extract it from the float so I know where the decimal point is supposed to be in the number?

 

For instance, if I multiply 100 * 100 I should get 10000, and in the float format, I would assume I would have an exponent of +4. I'm not sure I really sure the format is correct though. According to what I know of math, the mantissa is actualy the fractional part of the number, in which case I would be missing the integer part. Did I miss sonething there?

Edited by mityeltu

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...