News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

LM75AD

Started by tolyan249, Today at 01:28 PM

Previous topic - Next topic

tolyan249

proc _loop()

Dim i As Byte
Dim k As Byte


Dim Temp_Raw As Word
Dim Temperature As Float
Dim Degrees As Byte
Dim Tenths As Byte
Dim Frac As Float
Dim Temp_Pos As Float
Dim flac As Float
Dim Remainder As Byte
Dim rawValue As Word
Dim temperatureTimes10 As Byte
Dim tempSigned As Word
Dim tempAbs As Byte

dim REG_TEMP as byte

REG_TEMP=0


    '*********************************** READ TEMPERATURE WITH 2 BYTES **************************************

    Temp_Raw = I2C_ReadRegisterW(REG_TEMP)
    Temperature = (Temp_Raw >> 5) * 0.125 ' Calculate temperature: shift right by 5 and multiply by 0.125

    ' Separate into degrees and decimal part
    ' For positive temperatures: multiply by 100, take integer part, subtract
    Temp_Pos = Abs(Temperature) * 100  ' Multiply by 100 for precision
    Degrees = Temp_Pos / 100            ' Integer part of temperature (truncation)
    Frac = Abs(Temp_Pos - Degrees * 100) / 100  ' Decimal part
    Tenths = Frac * 100 + 0.5            ' Multiply by 10 and round (for digits 0-9)

    Cls
    Print At 1, 1, "TEMP: ", Dec Degrees, ".", Dec Tenths
EndProc

I get a positive temperature, but I can't calculate a negative one


;===================================================

It worked , I did it like this

If  Temp_Raw.15=1 Then
      Temperature=2048-Temperature
      sig=1
      EndIf


Cls
    If sig=0 Then
    Print At 1,1,"TEMP: ", Dec Degrees,".",Dec  Tenths 
    Else
    Print At 1,1,"TEMP: ","-", Dec Degrees,".",Dec  Tenths 
    EndIf





'==========================================================================================

RGV250

Hi,
If you test the 11th bit (bit 10 as it starts from 0) and it is 1 then the temperature is negative.
Invert all the bits and add 1, then treat it the same as a positive value.

Regards,
Bob

tolyan249


Pepe

demo proteus with negative temperature

tolyan249