'The ATTiny2313 is used. ' 'Charlos Potma PA3CKR ' '20070220 v00 basic functionality '20070222 v01 test i2c error ' 'Program reads four lm type temperature sensors. 'Temperature is displayed on a LCD and sent to a RS-232 i/f. 'The temperature string sent is completed with a four character hex crc. 'The companion program reads the temperature string and displays 'these on a graphical display. ' $regfile = "ATtiny2313.DAT" $crystal = 4000000 $baud = 9600 'Config LCD Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.3 , Rs = Portb.2 Config Pind.6 = Output Led Alias Portd.6 Config Sda = Portd.5 Config Scl = Portd.4 'Rather slow I2C speed Config I2cdelay = 20 'LM76 I2C write address 'Const Lm76xwrite = &H90 (not used) 'LM76 I2C read addresses Const Lm761read = &H91 Const Lm762read = &H93 Const Lm763read = &H95 Const Lm764read = &H97 'LM76 resolution in degrees celcius Const Lm76resolution = 625 Dim Tempint As Long Dim Temptmp As Long Dim Tempbytelo As Byte Dim Tempbytehi As Byte Dim Tempsign As Byte Dim Tempstr As String * 7 Dim Outstr As String * 7 Dim Strl As Byte Dim Strp As Byte Dim Lmaddr As Byte Dim Crc As Byte Dim Crchexstr As String * 4 Dim Crctot As Word 'Measure temperature Declare Sub Readtemp 'make temperature string Declare Sub Makeoutstr Cls Lcd "cpjp multitemp" Wait 1 Do Crctot = 0 Set Led Wait 1 Reset Led 'sensor 1 Lmaddr = Lm761read Call Readtemp Call Makeoutstr Print Outstr; Crc = Crc8(outstr , 7) Crctot = Crctot + Crc Cls Lcd Outstr 'sensor 2 Lmaddr = Lm762read Call Readtemp Call Makeoutstr Print Outstr; Crc = Crc8(outstr , 7) Crctot = Crctot + Crc Locate 1 , 8 Lcd Outstr 'sensor 3 Lmaddr = Lm763read Call Readtemp Call Makeoutstr Print Outstr; Crc = Crc8(outstr , 7) Crctot = Crctot + Crc Locate 2 , 1 Lcd Outstr 'sensor 4 Lmaddr = Lm764read Call Readtemp Call Makeoutstr Print Outstr; Crc = Crc8(outstr , 7) Crctot = Crctot + Crc Crchexstr = Hex(crctot) Print Crchexstr Locate 2 , 8 Lcd Outstr Wait 1 Loop Sub Readtemp 'Read the two temperature bytes in two's complement I2cstart I2cwbyte Lmaddr I2crbyte Tempbytehi , Ack I2crbyte Tempbytelo , Nack I2cstop 'Calculate temperature Tempint = Makeint(tempbytelo , Tempbytehi) Tempsign = Tempbytehi And 128 If Tempsign = 128 Then Tempint = Not Tempint Tempint = Tempint + 1 End If Shift Tempint , Right , 3 Tempint = Tempint * Lm76resolution Tempint = Tempint / 1000 End Sub Sub Makeoutstr 'Make output string for one temperature If Err = 0 Then Temptmp = Tempint / 10 Temptmp = Temptmp * 10 Temptmp = Tempint - Temptmp Tempint = Tempint / 10 Tempstr = Str(tempint) Strl = Len(tempstr) Strp = -strl Strp = Strp + 6 Outstr = " . " Mid(outstr , Strp , Strl) = Tempstr Tempstr = Str(temptmp) Mid(outstr , 7 , 1) = Tempstr Else 'fill string with - after i2c error (sensor not attached?) Outstr = "-------" End If End Sub End