Ok, last problem, for now! lol.
I am trying to read the temperature from the DS1822 sensor, the temperature seems to be reading back, although on the C18 and the Swordfish code I dont get a correct reading until the 2nd pass of the reading, maybe it's cause it's being simulated on Proteus? Read HW samples comming soon hopefully!
Anyway, the problem now is that i cant get the code to return negative values!
Posted below the code from the function, the sp data input is the 2 temperature bytes that get read in sp[0] sp[1].
Code:
unsigned char tempC[] = "000.0\r\n";
unsigned char tempF[] = "000.0\r\n";
signed int whole = 0, temp = 0;
signed int LSB = 0;
signed int frac = 0;
int CNeg = 0;
whole = ((sp[1] << 8) + (sp[0] & 0xff) >> 4);
LSB = sp[0] & 0xF;
frac = LSB * 625;
if (whole & 0x8000) {
tempC[0] = '-';
// Negative temp values are stored in 2's complement form
whole = ~whole + 1;
CNeg = 1;
}
else tempC[0] = ' ';
if ((whole/100) && CNeg == 0)
{
tempC[0] = whole/100 + 48;
temp = (int)(whole / 100) * 100;
whole -=temp;
}
tempC[1] = (whole/10) + 48;
temp = (int)(whole / 10) * 10;
whole -= temp;
tempC[2] = whole + 48;
tempC[4] = (frac / 1000) + 48;
PutString(tempC);
I got the negative checking code from somewhere else, though the Dev-C++ version of this code can work without that check!
On Dev-C++
Code:
char bit1 = 0xC0;
char bit2 = 0xFE;
long bigbit = (bit2 << 8) + (bit1 & 0xff) >> 4;
char LSB = bit1 & 0xF;
long frac = LSB * 625;
returns bigbit as -20 and frac as 0, which is -20.0, which is correct.
The same code in C18 produces:
Wilksey