Go Down

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

takao21106

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]

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

i2c_Stop();
i2c_Open(0b10011001,I2C_READ);
temp=i2c_ReadByte();
i2c_ReadAcknowledge();
i2c_Stop();
TRISB=0b00011111;
// led_numbers|=i2c_ReadByte();
// i2c_ReadAcknowledge();
temp&=0x7f;
led_numbers=(temp/10)<<4;
led_numbers|=temp-((temp/10)*10);
led_numbers<<=8;
}
I also have PIC related web domain.

jwatte

Is your question "how do I convert binary to BCD without using division and multiplication"?


takao21106

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.
I also have PIC related web domain.

jwatte

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

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.
I also have PIC related web domain.

jwatte

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

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);

I also have PIC related web domain.

Go Up