RE: [bascom] SV: I2C problem


From "Michael W. Akers" <mwakers@home.com>
Date Mon, 27 Sep 1999 15:37:00 -0700
Cc "[bascom] LISTSERVER" <bascom@grote.net>

Anders,
Here is the working (in simulation) code for you to try out. Let me know how 
it works.
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.
' Brent Nelson         09/27/1999  Usage uf assemble routines for byte/word
'                                  manipulation. Reduces resource 
requirements.
'------------------------------------------------------------------------------
' Define all subroutines.
' Define variables and constants
    Dim Lm75temp As Word
    Dim Lm75write As Byte
    Dim Lm75read As Byte
    Dim Lm75high As Byte
    Dim Lm75low As Byte
    Dim Dispval As Single
' 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
    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
' This code section to be removed when compiling for actual device. For 
Simulation Only.
'    Lm75high = &B01111100
'    Lm75low = &B10000000
' Create temperature word
'   Put Lm75high in high byte position in Lm75temp word.
    mov {Lm75temp+1},{Lm75high}
'   Put Lm75low in low byte position in Lm75temp word.
    mov {Lm75temp},{Lm75low}
' Now strip unused lower bits (least significant 7 bits)
    Rotate Lm75temp , Right , 7
    Dispval = Lm75temp * 0.5
' Now print the temp to the LCD
    Cls
    Lcd Dispval ; " Deg. C"
Goto Start