Wire.beginTransmission(AM2320_address);
Wire.endTransmission();
// запрос 4 байт (температуры и влажности)
Wire.beginTransmission(AM2320_address);
Wire.write(0x03);// запрос
Wire.write(0x00); // с 0-го адреса
Wire.write(0x04); // 4 байта
if (Wire.endTransmission(1) != 0) return 1;
delayMicroseconds(1600); //>1.5ms
// считываем результаты запроса
Wire.requestFrom(AM2320_address, 0x08);
for (int i = 0; i < 0x08; i++) buf[i] = Wire.read();
#Picaxe 18M2
#Terminal 4800
#No_Data
TestProgram:
Do
Pause 2000
For b13 = 1 To 5
Gosub ReadData
If w0 = $0304 Then
SerTxd( "Okay " )
Else
SerTxd( "FAIL " )
End If
Next
SerTxd( CR, LF )
Loop
ReadData:
; .-----.-----.-----.
; | $03 | $00 | $04 |
; `-----^-----^-----'
; Read Reg Len
;
; w0 w1 w2 w3
; .-----------.-----------.-----------.-----------.
; | $03 | $04 | $hh | $hh | $tt | $tt | crc | crc |
; `-----------^-----------^-----------^-----------'
; b1 b0 b3 b2 b5 b4 b7 b6
HI2cSetup I2CMASTER, $B8, I2CSLOW, I2CBYTE
HI2cOut ( $03, $00, $04 )
HI2cIn ( b1,b0, b3,b2, b5,b4, b7,b6 )
Return
TestProgram:
Do
Pause 2000
Do
Gosub ReadData
Loop Until w0 = $0304
Gosub ReadData
SerTxd( "T=", #w2, TAB, "H=", #w1, CR, LF )
Loop
0xB8 is 0x5C shifted once to the left to accommodate the read/write flagAM2320 sensor I2C address of the (SLAVEADDRESS) 0xB8, on the basis of I2C standard protocol
main:
gosub read_sensors ; read the sensors
pause 1000
goto main ; take next reading
; ****************************
; ***** Read sensor data *****
; ****************************
read_sensors:
high B.3 ; flash green LED
readtemp C.7, data7 ; read Temperature - DS18B20 - one wire
low B.3 ; end of green LED flash
sertxd ("DS Temp = ",#data7,CR,LF)
read_time:
hi2csetup i2cmaster, %11010000, i2cslow, i2cbyte ;Addr $D0
hi2cin 0,(secs,mins,hours,temp_byte,day,month)
pause 100
sertxd ("H = ",#hours," Min = ", #mins,CR,LF)
;AM2320 - temp and humidity I2C
hi2csetup i2cmaster, $B8, i2cslow, i2cbyte ;Addr $B8
hi2cout $B8,($B8) ;Wake Up AM2320
;pause 1
hi2cout ($03,$00,$04) ;Addr+W (=$10), FnCode, StartAddr, No of Reg
;pauseus 1600 ;AM2320 states delay >1.5mS, Arduino code 1600uS - but works without a delay on PICAXE (guess it is inherent in PICAXE constructs)
hi2cin (b20,b21,b22,b23,b24,b25,b27,b26) ;Am2320 response Address+R - $03,$04,RHHi,RHLo,THi,TLo,CRCLo,CRCHi
;AM reply
sertxd ("AM reply = ",#b20," FCode = ",#b21,CR,LF)
;RH Calc
sertxd ("RH = ",#b22," ",#b23,CR,LF) ;RH to PC screen
;T Calc
sertxd ("Temp = ",#b24," ",#b25,CR,LF) ;Temp to PC screen
return
Word variables are formed from two byte variabless, 'w0' is also bytes 'b1' and 'b0', 'w1' is also 'b3' and 'b2'. The correlation is 'wN' is fromed from 'b(N*2+1)' and 'b(N*2)'. The odd numbered byte variables are the most significant byte of the word variable, the even numbered byte variables the least significant byte.if temp is b24 and b25 why w1 ( b3.b2)?
Symbol header = w0 ; b1:b0
Symbol header.lsb = b0
Symbol header.msb = b1
Symbol humidity = w1 ; b3:b2
Symbol humidity.lsb = b2
Symbol humidity.msb = b3
Symbol temp = w2 ; b5:b4
Symbol temp.lsb = b4
Symbol temp.msb = b5
Symbol crc = w3 ; b7:b6
Symbol crc.lsb = b6
Symbol crc.msb = b7
HI2cIn ( header.msb,header.lsb, humidity.msb,humidity.lsb, temp.msb,temp.lsb, crc.msb,crc.lsb )
SerTxd( "T=", 'temp, CR, LF )
temp = temp + 5
SerTxd( "T=", 'temp, CR, LF )
Hello,Hi All - done a forum search and seems no one has done anything with the AOSONG AM2320 I2C based temp and humidity sensor.
That right - or have I missed something ?
Thanks
Bob
Symbol header = w0 ; b1:b0
Symbol header.lsb = b0
Symbol header.msb = b1
Symbol humidity = w1 ; b3:b2
Symbol humidity.lsb = b2
Symbol humidity.msb = b3
Symbol temp = w2 ; b5:b4
Symbol temp.lsb = b4
Symbol temp.msb = b5
Symbol crc = w3 ; b7:b6
Symbol crc.lsb = b6
Symbol crc.msb = b7
TestProgram:
Do
Pause 2000
Do
Gosub ReadData
Loop Until w0 = $0304
SerTxd( "T=", #temp, TAB, "H=", #humidity, CR, LF )
Loop
ReadData:
; .-----.-----.-----.
; | $03 | $00 | $04 |
; `-----^-----^-----'
; Read Reg Len
;
; header humidity temp checksum
; .-----------.-----------.-----------.-----------.
; | $03 | $04 | $hh | $hh | $tt | $tt | crc | crc |
; `-----------^-----------^-----------^-----------'
; msb lsb msb lsb msb lsb msb lsb
HI2cSetup I2CMASTER, $B8, I2CSLOW, I2CBYTE
HI2cOut ( $03, $00, $04 ) ; Wake-up
HI2cOut ( $03, $00, $04 ) ; Request data
HI2cIn ( header.msb,header.lsb, humidity.msb,humidity.lsb, temp.msb,temp.lsb, crc.msb,crc.lsb )
Return
temp = 261 ; = 26.1C
SerTxd( "T=", #temp,"C", TAB, "H=", #humidity, CR, LF )
temp = temp * 9 / 5 + 320
SerTxd( "T=", #temp,"F", TAB, "H=", #humidity, CR, LF )
T=261C H=0
T=789F H=0
temp = -415 ; = -41.5C
If temp >= $8000 Then
w0 = -temp
SerTxd( "T=-", #w0,"C", TAB, "H=", #humidity, CR, LF )
Else
SerTxd( "T=", #temp,"C", TAB, "H=", #humidity, CR, LF )
End If
If temp >= $8000 Then
temp = -temp * 9 / 5
temp = -temp + 320
Else
temp = temp * 9 / 5 + 320
End If
If temp >= $8000 Then
w0 = -temp
SerTxd( "T=-", #w0,"F", TAB, "H=", #humidity, CR, LF )
Else
SerTxd( "T=", #temp,"F", TAB, "H=", #humidity, CR, LF )
End If
First, open the Terminal from PE6. Then, in Terminal, select the Settings menu, Special Control Functions, and tick "Clear screen on FF [0C]" - FF means Form-Feed and $0C is the byte value for that character.Question: how do I put a clear display in a program using the pic terminal?
Do
SerTxd( #w0, CR, LF )
Pause 1000
w0 = w0 + 1
SerTxd( $0C ) ; <-- Clear Terminal Screen
Loop