$regfile = "M8def.dat" $crystal = 3686400 $baud = 9600 $lib "i2c_twi.lbx" Dim B1 As Byte Dim B2 As Byte Dim B3 As Byte Dim B4 As Byte Dim X As String * 10 Dim Y As Integer Dim Z As Byte Dim Ctr As Byte Dim Dataword As Word Dim Command As Byte Dim Dis As String * 20 Dim Calc As Single Dim Calc2 As Single Dim Rhlinear As Single Dim Rhlintemp As Single Dim Tempc As Single Dim Tempf As Single Const C1 = -4 Const C2 = 0.0405 Const C3 = -0.0000028 Const T1c = .01 Const T2 = .00008 Const T1f = .018 Y = 0 Z = 0 Declare Sub Getit() Config Timer1 = Timer , Prescale = 256 On Timer1 On_timer1 Enable Timer1 Enable Interrupts Timer1 = 51135 Do If Z = 3 Then Sck Alias Portc.5 Dataout Alias Portc.4 Datain Alias Pinc.4 Ddrc = &B11111111 Config Pinc.5 = Output Config Pinc.4 = Output Set Dataout For Ctr = 1 To 12 Set Sck Waitus 2 Reset Sck Waitus 2 Next Ctr Command = &B00000011 Call Getit Tempf = T1f * Dataword Tempf = Tempf - 39.35 Tempc = T1c * Dataword Tempc = Tempc - 39.66 Dis = Fusing(tempc , "###.##") Print "Temperatur = " ; Dis ; " (C)" Command = &B00000101 Call Getit Calc = C2 * Dataword Calc2 = Dataword * Dataword Calc2 = C3 * Calc2 Calc = Calc + C1 Rhlinear = Calc + Calc2 Calc = T2 * Dataword Calc = Calc + T1c Calc2 = Tempc - 25 Calc = Calc2 * Calc Rhlintemp = Calc + Rhlinear Dis = Fusing(rhlintemp , "##.##") Print "relative Feuchte = " ; Dis ; " (rF%)" ; Z = 4 End If If Z = 7 Then Gosub Co2messen Z = 0 End If Loop On_timer1: Z = Z + 1 Timer1 = 51135 Return Co2messen: I2cinit Config Twi = 50000 Config Scl = Portc.5 Config Sda = Portc.4 I2cstart ' Send start sequence: Fallende SDA-Flanke wenn SCL high I2cwbyte &HD0 ' Adresse 0x68 mit R/W low = HD0. Read/Write bit0 encoding Read = 1, Write = 0. I2cwbyte &H22 ' command read ram and number of databytes I2cwbyte &H00 ' ram address (for command register?) I2cwbyte &H08 ' ram address I2cwbyte &H2A ' Checksum is calculated as sum of byte 2, 3 and 4. Print I2cstop Waitus 350 I2cstart ' restart I2cwbyte &HD1 ' senden der Slaveadresse mit R/W bit0 = 1 I2crbyte B1 , Ack ' Slave sendet Byte: operation status, where bit 0 tells us if the read command was successfully executed. I2crbyte B2 , Ack ' CO2 value high byte I2crbyte B3 , Ack ' CO2 value low byte I2crbyte B4 , Nack ' beinhaltet checksum I2cstop ' Steigende SDA-Flanke wenn SCL low Waitus 350 X = Hex(b2) X = X + Hex(b3) Y = Hexval(x) Print "CO2-Wert: "; Y ; " (ppm)" ; Y = 0 Return Sub Getit() Local Datavalue As Word Local Databyte As Byte Set Sck Reset Dataout Reset Sck Set Sck Set Dataout Reset Sck Shiftout Dataout , Sck , Command , 1 Ddrc = &B11101111 Config Pinc.4 = Input Set Sck Reset Sck Waitus 10 Bitwait Pinc.4 , Reset Shiftin Datain , Sck , Databyte , 1 Datavalue = Databyte Ddrc = &B11111111 Config Pinc.4 = Output Reset Dataout Set Sck Reset Sck Ddrc = &B11101111 Config Pinc.4 = Input Shiftin Datain , Sck , Databyte , 1 Shift Datavalue , Left , 8 Datavalue = Datavalue Or Databyte Dataword = Datavalue Ddrc = &B11111111 Config Pinc.4 = Output Reset Dataout Set Sck Reset Sck Ddrc = &B11101111 Config Pinc.4 = Input Shiftin Datain , Sck , Databyte , 1 Ddrc = &B11111111 Config Pinc.4 = Output Set Dataout Set Sck Reset Sck End Sub Return End