'----------------------------------------------------------------------------------------- 'name : myTWI_Temperatursensor_Mail.bas 'programmed by : Claus Kuhnel 'date : 2009-08-22 'purpose : myTWI Temperatursensor & mySmartControl M168 write in logfile ' and send messages when temperature crosses an aler set point 'micro : ATmega168 'tested with : V.1.11.9.3 'suited for demo : yes 'commercial addon needed : no '----------------------------------------------------------------------------------------- $regfile = "m168def.dat" $crystal = 20000000 ' used crystal frequency $baud = 9600 ' used baud rate $hwstack = 40 ' default use 32 for the hardware stack $swstack = 40 ' default use 10 for the SW stack $framesize = 60 ' default use 40 for the frame space Debug Off ' Off when connected to Gobetwino Config Scl = Portc.5 ' PC5 is SCL on myTWI Temperatursensor Config Sda = Portc.4 ' PC4 is SDA on myTWI Temperatursensor Config I2cdelay = 10 ' SCL = 100 kHz ' myAVR LCD Configuration Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.3 , Rs = Portd.2 Config Lcd = 16 * 2 Cursor Off Const Device_id = &H9 ' Device ID for LM75 Dim Slave_wa As Byte ' Slave Write Adresse Dim Temp As Byte Dim Temperature As Word Dim Ti As Integer Dim Ts As Single Dim Ta As Single Dim Overtemp As Bit ' Overtemperature flag Ta = 23.0 ' Temperature Alert Set Point Declare Function I2c_read_temperature(byval Swa As Byte) As Word Declare Sub Sendmail() Declare Function Read_return() As Integer Slave_wa = Device_id ' Berechnung der Slaveadresse Shift Slave_wa , Left , 4 Debug "Read Temperature from myTWI Temperature Sensor - Quit by ESC" Debug "Slave Write Address is 0x" ; Hex(slave_wa) Cls Do Temperature = I2c_read_temperature(slave_wa) ' Read temperature from LM75 Debug "Temperature Value is 0x" ; Hex(temperature) Shift Temperature , Right , 7 Debug "Temperature in 0.5 grd steps is 0x" ; Hex(temperature) Ti = Temperature If Ti > &HFF Then Ti = Ti Or $ff00 End If Ts = Ti / 2. Debug "Temperature is " ; Ts ; " grd C" Upperline : Lcd "Temperature is " Lowerline : Lcd " " Lowerline : Lcd Ts ; " grd C" If Ts >= Ta Then If Overtemp = 0 Then Overtemp = 1 Debug "Overtemperature" Sendmail End If Else If Overtemp = 1 Then Overtemp = 0 Debug "No Overtemperature" Sendmail End If End If Wait 1 Loop Print End Function I2c_read_temperature(byval Swa As Byte) As Word Local Sra As Byte Local Thi As Byte , Tlo As Byte Local T As Word Sra = Swa Or 1 I2cstart I2cwbyte Sra I2crbyte Thi , Ack I2crbyte Tlo , Nack I2cstop T = Thi Shift T , Left , 8 T = T + Tlo I2c_read_temperature = T End Function Sub Sendmail() Local Num As Integer Cls : Lcd "Log Temperature" Lowerline : Lcd "Send Message..." Print "#S|LOGINFILE|["; Print Str(ts); Print "]#" Num = Read_return() If Num <> 0 Then Debug "Write Error" Cls : Lcd "Write Error" End If Print "#S|MAILTO|[]#" ' send mail to connected PC Num = Read_return() If Num <> 0 Then Debug "Send Mail Error" Cls : Lcd "Send Mail Error" End If End Sub Function Read_return() As Integer Local Cc As Byte Local Ss As String * 5 Ss = "" Cc = Waitkey() Ss = Ss + Chr(cc) If Ss = "-" Then Cc = Waitkey() Ss = Ss + Chr(cc) End If Read_return = Val(ss) End Function