I am posting this code because I have seen that some people have had problems working with this device.
Code: Select all
' This program was made to Read a LM75 and display the temp on a LCD display.
' It was compile with mikroBasic version 7.3B
' This program was compile for a Pic 18F4520 on an EasyPic4 board with the
' LM75 connected on PortA pins 0 and 1 with an address of 0 (000).
'
' LM75 address is 1001 + A2,A1,A0 + R/W bit
' EX. 1001 + 000 + 0 (W) = 10010000 or 0x90
' EX. 1001 + 000 + 1 (R) = 10010000 or 0x91
'
program LM75
include mb_aditional_string_util ' YO2LIO aka Florin Medrea's string Library
dim LM75_H as short
dim LM75_L as byte
dim text as string[3]
dim text1 as string[3]
main:
' Initialize PIC
CMCON = CMCON or 0x07 ' turn off comparators
ADCON1 = ADCON1 or 0x0F ' turn off analog inputs
TRISA = 0x00 ' PortA all outputs
TRISB = 0x00 ' PortB all outputs
TRISC = 0x00 ' PortC all outputs
TRISD = 0x00 ' PortD all outputs
LATA = 0x00 ' Make PortA Low
LATB = 0x00 ' Make PortB Low
LATC = 0x00 ' Make PortC Low
LATD = 0x00 ' Make PortD Low
delay_10ms '
LCD_Init(PORTD) ' LCD connected to PortD
LCD_Cmd(LCD_CLEAR) ' clear display
LCD_Cmd(LCD_CURSOR_OFF) ' turn cursor off
delay_100ms
LATB = 0x01 ' Test bit
delay_100ms
Soft_I2C_Config(PORTA, 0, 1) ' SDA is PORTA bit 0. SCK is bit 1
LATB = 0x00 ' Test bit
While true ' Endless loop
Soft_I2C_Start() ' Issue I2C signal: start
Soft_I2C_Write(0x90) ' Send Address byte
Soft_I2C_Write(0x00) ' Send Pointer Reg. 0x00 = Temp
' 0x01 = Config.
' 0x02 = HYST Set Point
' 0x03 = OS Set Point
Soft_I2C_Start() ' Issue a Re-start
Soft_I2C_Write(0x91) ' Issue a Read Command
LM75_H = Soft_I2C_Read(1) ' Read the high data byte, (1) = Mster ACK's
LM75_L = Soft_I2C_Read(0) ' Read the low data byte , (0) = Master No ACK's
Soft_I2C_Stop() ' Issue I2C signal: stop
' Convert the data to strings for LCD
short2str(LM75_H,text) ' Had to use Florin's string library,
byte2str(LM75_L,text1) ' because ME's library did something unusal
' on the LCD display.
' Write the two data bytes to LCD
LCD_Out(1,1, "H") '
Lcd_Out(1,3, text) '
LCD_Out(1,9, "L") '
Lcd_Out(1,11, text1) '
delay_10ms '
' Write the Temperature to the LCD
LCD_Out(2,1, "Temp = ")
LCD_Out_CP(text)
LCD_Out_CP(".")
If TestBit(LM75_L, 7) = 1 then
LCD_Out_CP("5")
Else LCD_Out_CP("0")
End if
LCD_Chr_CP(223)
LCD_Out_CP("C")
' delay 1 second and loop
delay_1sec
LCD_Cmd(LCD_CLEAR) ' clear display
delay_1sec
'*** Read the Configuration Register*****
Soft_I2C_Start() ' Issue I2C signal: start
Soft_I2C_Write(0x90) ' Send Address byte
Soft_I2C_Write(0x01) ' Send Pointer Reg. 0x00 = Temp
' 0x01 = Config.
' 0x02 = HYST Set Point
' 0x03 = OS Set Point
Soft_I2C_Start() ' Issue a Re-start
Soft_I2C_Write(0x91) ' Issue a Read Command
LM75_H = Soft_I2C_Read(1) ' Read the high data byte, (1) = Mster ACK's
LM75_L = Soft_I2C_Read(0) ' Read the low data byte , (0) = Master No ACK's
Soft_I2C_Stop() ' Issue I2C signal: stop
' Convert the data to strings for LCD
short2str(LM75_H,text) ' Had to use Florin's string library,
byte2str(LM75_L,text1) ' because ME's library did something unusal
' on the LCD display.
' Write the two data bytes to LCD
LCD_Out(1,1, "Config") '
LCD_Out(2,1, "H") '
Lcd_Out(2,3, text) '
LCD_Out(2,9, "L") '
Lcd_Out(2,11, text1) '
delay_10ms '
' delay 1 second and loop
delay_1sec
LCD_Cmd(LCD_CLEAR) ' clear display
delay_1sec
'*** Read the HYST Register*****
Soft_I2C_Start() ' Issue I2C signal: start
Soft_I2C_Write(0x90) ' Send Address byte
Soft_I2C_Write(0x02) ' Send Pointer Reg. 0x00 = Temp
' 0x01 = Config.
' 0x02 = HYST Set Point
' 0x03 = OS Set Point
Soft_I2C_Start() ' Issue a Re-start
Soft_I2C_Write(0x91) ' Issue a Read Command
LM75_H = Soft_I2C_Read(1) ' Read the high data byte, (1) = Mster ACK's
LM75_L = Soft_I2C_Read(0) ' Read the low data byte , (0) = Master No ACK's
Soft_I2C_Stop() ' Issue I2C signal: stop
' Convert the data to strings for LCD
short2str(LM75_H,text) ' Had to use Florin's string library,
byte2str(LM75_L,text1) ' because ME's library did something unusal
' on the LCD display.
' Write the two data bytes to LCD
LCD_Out(1,1, "HYST") '
LCD_Out(2,1, "H") '
Lcd_Out(2,3, text) '
LCD_Out(2,9, "L") '
Lcd_Out(2,11, text1) '
delay_10ms '
' delay 1 second and loop
delay_1sec
LCD_Cmd(LCD_CLEAR) ' clear display
delay_1sec
'*** Read the OS Register*****
Soft_I2C_Start() ' Issue I2C signal: start
Soft_I2C_Write(0x90) ' Send Address byte
Soft_I2C_Write(0x03) ' Send Pointer Reg. 0x00 = Temp
' 0x01 = Config.
' 0x02 = HYST Set Point
' 0x03 = OS Set Point
Soft_I2C_Start() ' Issue a Re-start
Soft_I2C_Write(0x91) ' Issue a Read Command
LM75_H = Soft_I2C_Read(1) ' Read the high data byte, (1) = Mster ACK's
LM75_L = Soft_I2C_Read(0) ' Read the low data byte , (0) = Master No ACK's
Soft_I2C_Stop() ' Issue I2C signal: stop
' Convert the data to strings for LCD
short2str(LM75_H,text) ' Had to use Florin's string library,
byte2str(LM75_L,text1) ' because ME's library did something unusal
' on the LCD display.
' Write the two data bytes to LCD
LCD_Out(1,1, "OS") '
LCD_Out(2,1, "H") '
Lcd_Out(2,3, text) '
LCD_Out(2,9, "L") '
Lcd_Out(2,11, text1) '
delay_10ms '
' delay 1 second and loop
delay_1sec
LCD_Cmd(LCD_CLEAR) ' clear display
delay_1sec
Wend
end.
It will read the temp and the other registers of the LM75.
Note if the I2C is not present it will hang in the test bit section of the code.
Also if the address does not match you will see " -1 and 255 " for data.
Have fun
Durango