Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20606

Share your working BASCOM-AVR code here : Using the LM75/LM75-A I2C sensor : NEWTOPIC

$
0
0
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. Regards Paul [code:1:d7e385d296] '---------------------- '----------------- '--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(8) 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 'as its a neagative number the first 5 bits are "1" Toggle Lm75temp.15 Toggle Lm75temp.14 Toggle Lm75temp.13 Toggle Lm75temp.12 Else 'its a positive number Shift Lm75temp , Right , 5 ' 5 unused bits plus the sign bit above End If Lm75read = Lm75temp * 0.125 'now we have the binary value End Function [/code:1:d7e385d296]

Viewing all articles
Browse latest Browse all 20606

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>