Pages: [1]   Go Down
Author Topic: How to show data from I2C sensor on 4digit LED display  (Read 640 times)
0 Members and 1 Guest are viewing this topic.
IRL
Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
}


* i2c_thermo.jpg (162.84 KB, 800x673 - viewed 8 times.)
Logged

I also have PIC related web domain.

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

IRL
Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I also have PIC related web domain.

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

IRL
Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I also have PIC related web domain.

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you have a fractional byte, the conversion is simple:

Code:
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.
Logged

IRL
Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK thanks I solved it:

Code:
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);
Logged

I also have PIC related web domain.

Pages: [1]   Go Up
Jump to: