'////////////////////////////////////////////////////////////// '/// FastAVR Basic Compiler for AVR by MICRODESIGN /// '/// SHT11/15 Temperature and Humidity measurement /// '/// 12/8 bit, Humy nonlin comp for 0.8% err /// '////////////////////////////////////////////////////////////// $Device= 8515 $Stack = 32 ' stack size $Clock = 7.3728 $Baud=9600 $ShiftOut Data=PORTB.1, Clk=PORTB.0, 1 $LeadChar=" ", Format(2,1) $Def dDta=DDRB.1 $Def inDta=PINB.1 $Def Dta=PORTB.1 $Def Sck=PORTB.0 Declare Sub Init() Declare Sub StartTX() Declare Sub CommReset() Declare Function WriteByte(db As Byte) As Byte Declare Function ReadByte(ack As Byte) As Byte Declare Function Measure(what As Byte) As Word Dim tmp As Integer Dim n As Byte Dim Cta As Byte, Ctb As Integer Const True=1, False=0 Const ACK=1, NAK=0 Const TmpSHT=&h03 Const HumSHT=&h05 Const ResSHT=&h1e Const Rstat =&h07 Const Wstat =&h06 Init() Do tmp=Measure(TmpSHT) ' measure Temperature tmp=10*tmp/25-400 Print "Temp:"; tmp n=Measure(HumSHT) ' measure Humidity If n<108 Then ' non lin corr (0.8%err) Cta=143 Ctb=-512 Else Cta=111 Ctb=2893 End If tmp=(n*Cta+Ctb)/128*5 Print "Humy:"; tmp Print Wait 1 Loop '////////////////////////////////////////////////////////////// Sub Init() WaitMs 20 CommReset() WriteByte(Wstat) ' write to Status Set dDta WriteByte(1) ' 12bit for T, 8 bit for RH End Sub '////////////////////////////////////////////////////////////// Function Measure(what As Byte) As Word Local tmp As Word CommReset() If WriteByte(what)=0 Then ' isue Cmd BitWait inDta, 0 ' wait for data ready tmp=ReadByte(ACK) ' read first byte Shift(Left, 8, tmp) ' make it MSB tmp=tmp Or ReadByte(NAK) ' read second byte and combine 'n=ReadByte(NAK) ' no CRC for now Else Print "No sensor!" tmp=0 End If Return tmp End Function '////////////////////////////////////////////////////////////// Sub StartTX() Set dDta Set Sck Reset Dta Reset Sck: Set Sck Set Dta Reset Sck End Sub '////////////////////////////////////////////////////////////// Sub CommReset() Local i As Byte Set Dta For i=0 To 8 Set Sck: Reset Sck Next StartTX() End Sub '////////////////////////////////////////////////////////////// Function WriteByte(db As Byte) As Byte Local mask As Byte ShiftOut db ' send data Reset dDta ' back to input Set Sck:Reset Sck ' clock ACK Return inDta ' return ACK (0=SHT present) End Function '////////////////////////////////////////////////////////////// Function ReadByte(ack As Byte) As Byte Local i As Byte, db As Byte db=ShiftIn ' receive byte Set dDta ' data to output for ACK Dta=Not ack ' place ACK or NAK Set Sck: Reset Sck ' clock AVK Reset dDta ' back to input Return db End Function