Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Using the LM75/LM75-A I2C sensor

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Sat Aug 17, 2013 6:54 am    Post subject: Using the LM75/LM75-A I2C sensor Reply with quote

Here are some functions that will set-up and read the LM75/LM75A I2C buss temperature sensors they can set-up and read any of the 8 available addresses for the sensor you can have all 8 connected if you want.
Note I am using the LM75A from NXP which has 11-bit ADC that offers a temperature resolution of 0.125 °C
if you only have 9 bits as in the older versions you will have to shift right 7 bits.

Regards Paul

Code:

'----------------------
'-----------------
'--There can be 8 LM75A devices on a buss
'-- HEX  90, 92, 94, 96, 98, 9A, 9C, 9E
'--so lets make it so we can read any one of them
'--we can use this function to return the address
'--or if we have space put the addreses in an EEPROM
'--array of 1 to 8 then use that, this saves 164 bytes of flash
'--eg Dim Lm75add(Cool As Eram Byte


 'Declare Sub Lm75setup(byval Num As Byte)

 'Declare Function Lm75read(byval Num As Byte) As Single

 'Declare Function Getlm75add(byval 75num As Byte)as Byte



'Function Getlm75add(byval 75num As Byte)as Byte

     'Select Case 75num

      'Case 1
      'so Getlm75add(1) will return &H90

      'Getlm75add = &H90

     ' Case 2

     ' Getlm75add = &H92

     ' Case 3

      'Getlm75add = &H94

     ' Case 4

     ' Getlm75add = &H96

     ' Case 5

     ' Getlm75add = &H98

      'Case 6

      'Getlm75add = &H9A

      'Case 7

      'Getlm75add = &H9C

      'Case 8

      'Getlm75add = &H9E


     'End Select



'End Function



Sub Lm75setup(byval Num As Byte)

Local Waddress As Byte

 'Waddress = Getlm75add(num)        'using the function

 Waddress = Lm75ad(num)                                     'using address stored in EEPROM

I2csend Waddress , &H01


I2csend Waddress , &H18

End Sub


'You need to loop this, to keep reading the temperature
'Do

'Read Temperature
Function Lm75read(byval Num As Byte) As Single

Local Lm75temp As Integer
Local Waddress As Byte
Local Raddress As Byte

 'Waddress = Getlm75add(num)                                 'using the function


Waddress = Lm75ad(num)                                      'using address stored in EEPROM
Raddress = Waddress + 1


I2csend Waddress , 0

I2creceive Raddress , Lm75temp , 0 , 2



   Swap Lm75temp                                            'swap the bytes as I2creceive reverses them
  If Lm75temp.15 = 1 Then                                   'its a negative number

    Shift Lm75temp , Right , 5                              ' 5 unused bits plus the sign bit above
    ' Shift Lm75temp , Right , 7 'for older 9 bit devices

   'as its a neagative number the first 5 bits are "1"
    Toggle Lm75temp.15
    Toggle Lm75temp.14
    Toggle Lm75temp.13
    Toggle Lm75temp.12
 '-for 9 bit devices
    'Toggle Lm75temp.11
    'Toggle Lm75temp.10


  Else                                                      'its a positive number

    Shift Lm75temp , Right , 5                              ' 5 unused bits plus the sign bit above
    ' Shift Lm75temp , Right , 7 'for older 9 bit devices

  End If



  Lm75read = Lm75temp * 0.125                               'now we have the binary value

End Function
 
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum