Go Down

### Topic: How to show data from I2C sensor on 4digit LED display (Read 1 time)previous topic - next topic

#### takao21106

##### Mar 11, 2012, 01:32 am
I made the I2C code working, and I have LED drive working as well.

Right now I only show integer temperature, using /10 divide, and *10 multiply, which is costly in terms of code size. How can I display the fractional part?

The sensor gives 2 EXP 0 to 2 EXP 6 in the MSB, and 2 EXP -1 to 2 EXP -4 in the LSB.

The data I need for the 7segment drive is 16 bit integer (hexadecimal).
The points are driven seperately.

This is the code I wrote:

Code: [Select]

{unsigned char temp;
led_numbers=0;
i2c_Stop();
i2c_Open(0b10011000,I2C_WRITE);
i2c_SendByte(0x00);

i2c_Stop();
i2c_Stop();
TRISB=0b00011111;
temp&=0x7f;
led_numbers=(temp/10)<<4;
led_numbers|=temp-((temp/10)*10);
led_numbers<<=8;
}

#### jwatte

#1
##### Mar 11, 2012, 01:58 am
Is your question "how do I convert binary to BCD without using division and multiplication"?

#### takao21106

#2
##### Mar 11, 2012, 02:06 am
Well kind of, I don't know properly how to do the fractional part.
I could test the four bits but I was thinking, ask on the forum.

#### jwatte

#3
##### Mar 11, 2012, 09:08 pm
If you logically shift the four fractional bits left by 4, they turn into a range from 0 .. 15.
However, it sounds as if you actually have bits -4 .. -1 in the high nybble of the byte; if so, you actually want to shift it right 4 to get it into the 0 .. 15 range.

#### takao21106

#4
##### Mar 27, 2012, 04:43 pm
Well if I only have one bit for 0.5,
how can I compute 5?
And then I take the next bit, 0.25,
how can I computer 75 (or 25)?

It's maybe trivial but I am really not getting it.
My idea was to use a lookup table but is there any kind of algorithm?
It would really be helpful to to finish my program.
Right now it is only showing integer value.

#### jwatte

#5
##### Mar 27, 2012, 06:24 pm
If you have a fractional byte, the conversion is simple:

Code: [Select]

static const float conversion = 1.0 / 256.0f;

float converted = (unsigned char)theByte * conversion;

If you have only four bits in the lower nybble of the byte, use 1/16 instead of 1/256 for conversion.

#### takao21106

#6
##### Mar 27, 2012, 07:46 pm
OK thanks I solved it:

Code: [Select]

const unsigned char frac[]={6,12,25,50};

led_numbers=(temp/10)<<4;
led_numbers|=temp-((temp/10)*10);
led_numbers<<=8;
temp3=0;
if(temp2&0x80)temp3+=frac[3];
if(temp2&0x40)temp3+=frac[2];
led_numbers|=(temp3/10)<<4;
led_numbers|=temp3-((temp3/10)*10);

Go Up