'
' Test sensirion SHT11
'
' r.0 21/11/03
'
'
$regfile = "Reg51.dat"
'$map
'$sim
$crystal = 11059200
$large
$noramclear
'
' uso Port
'
'P1.0
'P1.1
'P1.2 Rs display
'P1.3 En "
'P1.4 Db4 "
'P1.5 Db5 "
'P1.6 Db6 "
'P1.7 Db7 "
'
'
'P3.0 Rx
'P3.1 Tx
'P3.2 SHT_clock
'P3.3 SHT_data
'P3.4 Led
'P3.5
'P3.6
'P3.7
'
' constant
'
Const P1dis = &B11111111 'disattiva port P1
Const P3dis = &B11111111 'disattiva port P3
'
' addr command r/w
Const STATUS_REG_W = &B00000110 '000 xxxx x
Const STATUS_REG_R = &B00000111 '000 xxxx x
Const MEASUR_TEMP = &B00000011 '000 xxxx x
Const MEASUR_HUMI = &B00000101 '000 xxxx x
Const SOFT_RESET = &B00011110 '000 xxxx x
Const Maxtim = 150 'timeout SHT answer 300ms
'
Const C1 = -4 'humidity x 12bit
Const C2 = 0.0405
Const C3 = -0.0000028
Const T1 = 0.01
Const T2 = 0.00008
'
Const D1 = -40 'temperature x 14bit @ 5v
Const D2 = 0.01
'
'
Dim Fl_Err As Bit
Dim Ack_ret As Bit
'
Dim Tmp1 As Byte
Dim Txx As Byte
'Dim Crc As Byte
'
Dim TmpW As Word
'
Dim TmpSing As Single
Dim Tmp2Sin As Single
Dim Result As Single
'
Dim Strin As String * 5 'usato per display lcd
'
'RoLed Alias P3.4
SHT_data Alias P3.3
SHT_clk Alias P3.2
'
'
Config Lcd = 16 * 2
Config LCDPIN = PIN,DB4=P1.4,DB5=P1.5,DB6=P1.6,DB7=P1.7,E=P1.2,RS=P1.3
'
P3 = P3dis 'predispone stato iniziale
P1 = P1dis
'
waitms 100
cls
waitms 2
cursor off
waitms 2
lcd "Test SHT11"
wait 2
Stay:
call SHT_reset
call SHT_Txstart
Acc = SOFT_RESET
call SHT_write
if Ack_ret = 1 then goto Wrong
waitms 50 'tempo per soft reset
'
do
cls
waitms 2
Txx = MEASUR_TEMP 'send temperature read command
call Read_SHT
if Fl_Err = 1 then exit do 'no ack
TmpSing = TmpW * D2
TmpSing = TmpSing + D1
Result = TmpSing 'save x Humidity
Strin = fusing(TmpSing,###.#)
lcd "T. "; Strin
locate 1,11
waitms 2
lcdhex TmpW
'
waitms 100
locate 2,1
waitms 2
Txx = MEASUR_HUMI 'send Humidity read command
call Read_SHT
if Fl_Err = 1 then exit do
TmpSing = TmpW * C3
TmpSing = TmpSing + C2
TmpSing = TmpSing * TmpW
TmpSing = TmpSing + C1 ' RHlinear
Result = Result - 25
Tmp2Sin = TmpW * T2
Tmp2Sin = Tmp2Sin + T1
Tmp2Sin = Tmp2Sin * Result
TmpSing = TmpSing + Tmp2Sin
Strin = fusing(TmpSing,##.#)
lcd "H. "; Strin
locate 2,11
lcdhex TmpW
wait 3
loop
'
Wrong:
cls
waitms 2
lcd "Err";
if Ack_ret = 1 then
lcd " NACK"
endif
If Txx > Maxtim then
lcd " Timeout"
endif
wait 4
goto Stay
'
'********************************
' PROCEDURE
'********************************
'
Sub Read_SHT
Fl_Err = 1 'default error ON
TmpW = &HFFFE
call SHT_reset
call SHT_Txstart
Acc = Txx 'passa comando
call SHT_write
if Ack_ret = 1 then exit sub 'uscita per NACK
call SHT_answer
if Txx > Maxtim then exit sub 'uscita per timeout
call SHT_read 'riceve high byte
high(TmpW) = Acc
call SHT_ack 'invia ack di risposta
call SHT_read 'riceve low byte
low(TmpW) = Acc
call SHT_ack
' P3.4 = 1
call SHT_read 'riceve crc
' Crc = Acc
call SHT_nack 'chiude comunicazione
' P3.4 = 0
Fl_Err = 0 'no errors
End sub
'
'
Sub SHT_answer
Txx = 0
do
if SHT_data = 0 then exit sub
incr Txx
waitms 2
loop until Txx > Maxtim '300ms per rispondere
End sub
'
' ------------------------------------
'
$asm
'
SHT_reset:
push b
clr {SHT_clk} 'clock pin low
setb {SHT_data} 'data pin high
mov b,#9
res1:
setb {SHT_clk} 'clock = 1
nop
nop
clr {SHT_clk} 'clock = 0
nop
djnz b,res1
pop b
ret
'
'
SHT_TXstart:
setb {SHT_data} 'data = 1
clr {SHT_clk} 'clock = 0
nop
setb {SHT_clk} 'clock = 1
nop
clr {SHT_data} 'data = 0
nop
clr {SHT_clk} 'clock = 0
push acc
pop acc
setb {SHT_clk} 'clock = 1
nop
setb {SHT_data} 'data = 1
nop
clr {SHT_clk} 'clock = 0
ret
'
'
'scrittura dato in A su sht
SHT_write:
push b
mov b,#8
Wr1:
rlc a 'db7 > Cy
mov {SHT_data},c
setb {SHT_clk} 'clock = 1
nop
nop
clr {SHT_clk} 'clock = 0
djnz b,Wr1
setb {SHT_data} 'data = 1
setb {SHT_clk} 'clock = 1
'
mov c,{SHT_data} 'read ack
mov {Ack_ret},c 'return ack value
clr {SHT_clk} 'clock = 0
pop b
ret
'
'lettura dato da SHT
SHT_read:
push b
setb {SHT_data} 'data = 1
mov b,#8
clr a
Rd1:
setb {SHT_clk} 'clock = 1
mov c,{SHT_data}
rlc a 'cy > db7
clr {SHT_clk} 'clock = 0
nop
djnz b,Rd1
'ritorna dato in acc
pop b
ret
'
SHT_nack:
setb {SHT_data} 'data = 0
sjmp SHT_na
SHT_ack:
clr {SHT_data} 'data = 1
nop
SHT_na:
setb {SHT_clk} 'clock = 1
nop
clr {SHT_clk} 'clock = 0
nop
setb {SHT_data} 'data = 1
ret
'
'
'
$end asm
'
|