RE: [bascom] SV: I2C problem


From "Michael W. Akers" <mwakers@home.com>
Date Sun, 26 Sep 1999 23:42:09 -0700

Anders,
Here is the program that I think will work for you. The only thing I don't 
know about is how you have the LCD 40*4 connected to the processor. If it is 
using the standard connection configuration then this program should work.  

Michael Akers


'------------------------------------------------------------------------------
' Program Name           : lm75read.bas
' Program Date           : September 26,1999
' Program Written By     : Michael W. Akers
'                          3800 Vineyard Ave. Apt. #E
'                          Pleasanton, California 94566
'                          Voice: +1 925 484 4750
'                          Email: mwakers@home.com
' Program Purpose        : This program will demonstrate how to interface to,
'                          and communicate With The National Semiconductor 
LM75
'                          Digital Temperature Sensor.
'------------------------------------------------------------------------------
' Programmer           Date        Comments
' -------------------  ----------  
--------------------------------------------
' Michael Akers        09/26/1999  Initial creation of this program.
'------------------------------------------------------------------------------
' Define all subroutines.
' Define variables and constants
    Dim Lm75temp As Word
    Dim Lm75value As Word
    Dim Lm75write As Byte
    Dim Lm75read As Byte
    Dim Lm75high As Byte
    Dim Lm75low As Byte
    Dim Dispval As Byte
' Define configurations
    Config Scl = P3.5
    Config Sda = P3.7
    Config I2cdelay = 1
    Config Lcd = 40 * 4
'   Assumption is that the 89C2051 uses Port1 to communicate with the LCD.
' Define Port and pin presets.
' Initialize variables as needed.
    Lm75write = &B10010000
    Lm75read = &B10010001
    Lm75value = 0
    Lm75temp = 0
' Program start
Start:
' Gather the temperature data from the LM75.
'   Start the I2C system.
    I2cstart
'   Place the device in readback mode.
    I2cwbyte Lm75read
'   Read the High Byte. And ACK.
    I2crbyte Lm75high , 8
'   Read the Low Byte. And NACK.
    I2crbyte Lm75low , 9
'   Stop the I2C system.
    I2cstop
' Create temperature word
'   Put Lm75high in high byte position in word.
    Lm75temp = Lm75high * 256
    Lm75temp = Lm75temp Or Lm75low
'   Shift everything 6 bits right!
    Lm75value = Lm75temp * 64
'   Calculate the temperature. I am making no allowances for Minus 
temperatures
'   so anything with bit 9 set will show a bad value.
    Dispval = Lm75value * 0.5
' Now print the temp to the LCD
    Cls
    Lcd Dispval ; " Deg. C"
Goto Start