PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : AVR Mega8 + SHT75



cni
20.06.2007, 23:03
Hallo,

habe folgendes Problem:

Ich habe einen AVR ATMega8, der einen Sensor SHT75 (Feuchte und Themperatur) auslesen soll. Später soll das ganze dann auf einem Display dargestellt werden und 3 Ausgänge abhängig von der Themperatur bzw. Luftfeuchte geschaltet werden.

Nun, hier mal das Programm:



'************************************************* ****************************'
' Basic application for sensor SHT75. Humidity measuring is 8 bit and tempera-'
' ture is 12 bit resolution. Result is display on 16 char LCD module . '
' Sensor DATA pin is connect on PD1 and SCK on PD0. One LED is connect on PD2 '
' MCU pin to indicate measuring sequence. '
' Program size is 1800 bytes '
' '
'************************************************* ****************************'
$regfile = "m8def.dat"
$crystal = 16000000 ' used crystal frequency
$baud = 9600 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 32 ' default use 10 for the SW stack
$framesize = 32 ' default use 40 for the frame space
'Config Lcd = 16 * 1
'Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.3 , Rs = Portb.2

Dim Command As Byte , Msb As Byte , Lsb As Byte , Humi As Word
Dim I As Byte , Temp As Word , Celi As Word , Deseti As Word , Big_celi As Word

Deflcdchar 0 , 6 , 9 , 9 , 6 , 32 , 32 , 32 , 32 'Creating "°" character

Sck Alias Portd.6 'Serial CLOCK
Sdata Alias Pind.7 'Serial DATA for INPUT
Sdata_out Alias Portd.7 'Serial DATA for OUTPUT

Ddrd.6 = 1 'Data direction for SCK

Config Watchdog = 2048 'Watchdog time setting
Start Watchdog 'watchdog enable
'Cursor Off
'Cls 'LCD must move back to DATA mode after creating chr
'Waitms 20 'Must wait to stabilize LCD



Start1: 'Main program
'Ddrd.2 = 1
Ddrd.6 = 1
'Set Portd.2 'Switch ON LED (measuring start)

Command = &B00011110 'SOFTWARE RESET command
Gosub Trans_start
Gosub Send_cmd

Gosub Write_status
Gosub Conn_reset
Gosub Read_humidity

Gosub Conn_reset
Gosub Read_temp

'Cursor Off
'Reset Portd.2 'Switch OFF LED (measuring finish)
'Cls ' Clear display
'Waitms 10


'Locate 1 , 5
'Lcd "%RH"

'Locate 1 , 2
'Lcd Humi

Print "Luftfeuchte: " ; Humi ; " %"


'If Celi < 10 Then
' Locate 1 , 10
' Lcd Celi
'Else

'Locate 1 , 9
'End If
'Lcd Celi
'Locate 1 , 11
'Lcd "."
'Locate 1 , 12
'Lcd Deseti

'Locate 1 , 14
'Lcd Chr(0)
'Locate 1 , 15
'Lcd "C"

Print "Themperatur: " ; Celi ; "," ; Deseti ; " " ; Chr(167) ; "C"
Print

Reset Watchdog

Powerdown


'***************'
' HUMIDITY READ '
'***************'

Read_humidity:
Command = &B00000101 'Command "READ HUMIDITY"
Gosub Send_cmd
Waitms 20 'Wait to finish measuring
Gosub Sensor_readout

Humi = 0 'Start of humidity calculation (see App. note)
Humi = Humi + Msb
Shift Humi , Left , 8
Humi = Humi + Lsb

If Humi <= 107 Then
Humi = 143 * Humi
If Humi < 512 Then Humi = 512
Humi = Humi - 512
Else
Humi = 111 * Humi
Humi = Humi + 2893
If Humi > 25600 Then Humi = 25600

End If
Shift Humi , Right , 8
Return


'******************'
' TEMPERATURE READ '
'******************'

Read_temp:
Command = &B00000011 'Command "READ TEMPERATURE"
Gosub Send_cmd
Waitms 60
Gosub Sensor_readout
Temp = 0 'Calculation (see App. note)
Temp = Temp + Msb
Shift Temp , Left , 8
Temp = Temp + Lsb
Temp = Temp * 10
Temp = Temp / 25
Temp = Temp - 400


Celi = Temp / 10
Big_celi = Celi * 10
Deseti = Temp - Big_celi
Return


'******************'
' TRANSMISION START'
'******************'

Trans_start:
Config Sdata = Output
Set Sdata_out
Set Sck
Reset Sdata_out
Reset Sck
Waitus 5
Set Sck
Set Sdata_out
Reset Sck
Return


'******************'
' CONNECTION RESET '
'******************'

Conn_reset:

Config Sdata = Output
Reset Sck
Set Sdata_out
For I = 1 To 12
Set Sck
Reset Sck
Next I
Goto Trans_start
Return


'**************'
' COMMAND SEND '
'**************'

Send_cmd:
Config Sdata = Output
Shiftout Sdata_out , Sck , Command , 1 ', 8 , 1 'data change when sck goes low, 8bit out, 1uS delay
Ddrd.7 = 0
Reset Sck
Set Sck
Bitwait Sdata , Reset 'Wait ACK
Reset Sck
Return


Sensor_readout:
Config Sdata = Input
Shiftin Sdata , Sck , Msb , 1 , 8 , 1
Config Sdata = Output
Reset Sdata_out
Set Sck
Reset Sck
Config Sdata = Input
Shiftin Sdata , Sck , Lsb , 1 , 8 , 1
Config Sdata = Output

Set Sdata_out 'pull-up ACK (CRC not use)
Set Sck
Reset Sck
Return


Write_status:
Gosub Trans_start
Command = &B00000110
Gosub Send_cmd
Command = &B00000001 'Switch to 8/12 bit resolution
Gosub Send_cmd
Return

End


Das Problem ist nun, das auf der RS232 Schnittstelle immer das gleiche ankommt:


Luftfeuchte: 0 %
Themperatur: 11,2 °C


Anscheinend wird der SHT nicht richtig angesprochen oder so.

Kann mir evtl. jemand helfen?

Danke

cni
21.06.2007, 21:15
Hallo,

habe jetzt mal den Sensor auf andere Ports gelegt (PD4 und PD5) allerdings mit dem gleichen Ergebnis! :-k

Auch ein anderes Beispiel Programm:


'Routine to read the SHT75 Humidity sensor chip
'By Stuart Leslie
'Contact stu@4sightinc.com with any questions
'Uses BascomAVR
'a .01 uf capacitor across VCC and Ground on the SHT75 really cleans up the data
'a pullup is required on "data" pin as shown in the data sheet

$regfile = "m8def.dat"
$crystal = 16000000 ' used crystal frequency
$baud = 9600 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 32 ' default use 10 for the SW stack
$framesize = 32 ' default use 40 for the frame space


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

Sck Alias Portd.4
Dataout Alias Portd.5
Datain Alias Pind.5
'Redled Alias PortD.2

Declare Sub Getit()

Ddrd = &B11111111 'all port b are output
Config Pind.4 = Output 'sck
Config Pind.5 = Output 'datain

'reset the serial communications first, it is easily confused!
Set Dataout
For Ctr = 1 To 12
Set Sck
Waitus 2
Reset Sck
Waitus 2
Next Ctr


Do 'continually read the tempfature and humidity

Command = &B00000011
Call Getit 'Get the temperature, puts result in "dataword" for us
'
'Tempf = T1f * Dataword
'Tempf = Tempf - 40

Tempc = T1c * Dataword 'get celcius for later calculations and for "the rest of the world"
Tempc = Tempc - 40

Dis = Fusing(tempc , "###.##")
Print "Temperatur = " ; Dis ; Chr(167) ; "C"

Command = &B00000101
Call Getit 'get the humidity
Calc = C2 * Dataword
Calc2 = Dataword * Dataword 'that "2" in the datasheet sure looked like a footnote for a couple days, nope it means "squared"!
Calc2 = C3 * Calc2
Calc = Calc + C1
Rhlinear = Calc + Calc2

'Dis = Fusing(rhlinear , "##.##")
'Print "Humidity adjusted for linear = " ; Dis


Calc = T2 * Dataword
Calc = Calc + T1c
Calc2 = Tempc - 25
Calc = Calc2 * Calc
Rhlintemp = Calc + Rhlinear

Dis = Fusing(rhlintemp , "##.##")
Print "Luftfeuchte = " ; Dis ; " %"

Wait 1
Loop


Sub Getit()

Local Datavalue As Word
Local Databyte As Byte

'start with "transmission start"
Set Sck
Reset Dataout
Reset Sck
Set Sck
Set Dataout
Reset Sck


'now send the command
Shiftout Dataout , Sck , Command , 1

Ddrd = &B11111101 'datain is now input
Config Pind.5 = Input 'datain
Set Sck 'click one more off
Reset Sck
Waitus 10 'no idea why, but it doesn't work without it!
Bitwait Pind.7 , Reset 'wait for the chip to have data ready

Shiftin Datain , Sck , Databyte , 1 'get the MSB
Datavalue = Databyte

Ddrd = &B11111111
Config Pind.5 = Output

Reset Dataout 'this is the tricky part- Lot's of hair pulling- have to tick the ack!
Set Sck
Reset Sck

Ddrd = &B11111101 'datain is now input
Config Pind.5 = Input

Shiftin Datain , Sck , Databyte , 1 'get the LSB
Shift Datavalue , Left , 8
Datavalue = Datavalue Or Databyte
'don't tick the clock or ack since we don't need the CRC value, leave it hanging!
Dataword = Datavalue

Ddrd = &B11111111
Config Pind.5 = Output

Reset Dataout
Set Sck
Reset Sck

Ddrd = &B11111101 'datain is now input
Config Pind.5 = Input

Shiftin Datain , Sck , Databyte , 1 'not using the CRC value for now- can't figure it out! Anybody know how to impliment?
'Print "CRC value was - " ; Databyte

Ddrd = &B11111111
Config Pind.5 = Output

Set Dataout
Set Sck
Reset Sck
End Sub

End


Funktionierte nicht!
Ausgabe war immer:


Temperatur = 612.80°C
Luftfeuchte = -6216.70 %


Was kann das sein?
Der Sensor ist in Ordung, an einer C-Control funktioniert er!!!

cni
21.06.2007, 21:33
Es funktioniert!!!

Wenn ich den Orginal-Code von:
http://www.mcselec.com/index.php?option=com_content&task=view&id=80&Itemid=57
nehme und die Ports benutze, die dort angegeben sind (PB0 und PB1) dann geht es!!!

Ich habe doch nur die Ports in dem Beispiel geändert!
Evtl. habe ich wohl etwas vergessen???

Nur was?
Was muß angepasst werden, wenn ich andere Ports nehmen will?

Danke für Eure Hilfe!!!

peterfido
22.06.2007, 10:53
so wie ich es sehe, musst du die Register auch noch anpassen. (ddrb)

cni
24.06.2007, 21:31
ALso "Command = &B00000011" ändern in "Command = &D00000011" ???
Oder "Ddrd = &B11111111" in "Ddrd = &D11111111" ???

lowtzow
07.08.2007, 21:24
sehr nice

hab auch so nen sensor daheim nur noch nicht das richtige display. das muss ich mir noch zulegen.

bist du schon weiter, würd es auch für temperaturabhängiges schalten benötigen, hätte an einen bestimmten temp bereich gedacht oder gibts hierfür eigenen bascom befehle.

mfg
piri

lowtzow
13.08.2007, 16:02
hallo update

hab jetzt auch meinen sht in betrieb genommen!
hab sogar den sht75 damals gekauft, also rm1,27,
programm funkt bei mir auch.
das einzige was probleme macht dürfte die ausgabe sein (nichts wird angezeigt):


Dis = Fusing(tempf , "###.##")
Print "Temperature = " ; Dis ; " (F)"


wenn ich das durch



Lcd "Temperatu = " ; Tempc
Print
Waitms 999
Cls

ersetze funktioniert es problemlos (also wird was angezeigt)?!

naja jedenfalls der sht funktioniert! und das auch noch sehr gut! hab daneben mein innen/außen termometer liegen und die toleranz liegt zwischen den beiden so bei 1-2%max!

also top teil & problemlose montage an einem µc.
der einzige nachteil ist der preis, naja man gönnt sich ja sonst kaum was.

mfg
piri

Trexis5
02.12.2007, 08:54
Hallo,

ich habe den SHT11 SMD und bei mir bleibt das Programm hängen.
Immer in der Sub bei



Bitwait Pinb.4 , Reset 'wait for the chip to have data ready


Kann es sein das der Sensor defekt ist?
Am Programm und an der Config kann es nicht liegen, weil ich solch einen Sensor schon einmal verbaut habe. Diesen funktionierenden möchte ich aber nicht auslöten zum Testen.
Funktioniert hatte, der wo jetzt nicht geht, schonmal mit dem Programm.

Nur habe ich gestern eine andere Platine gelötet und nun geht er nicht mehr.

cni
02.12.2007, 21:38
Also bei mir läufte es prima!

Habe eine Gewächshaussteuerung daraus gemacht!

Bei bestimmten Temperaturen geht die Heizung oder ein Lüfter an!
Und bei bestimmten Luftfeuchten geht das Fenster auf oder die Luftbefeuchtung springt an!

Sebastian P.
14.11.2011, 15:14
Hallo,

habe jetzt mal den Sensor auf andere Ports gelegt (PD4 und PD5) allerdings mit dem gleichen Ergebnis! :-k

Auch ein anderes Beispiel Programm:


'Routine to read the SHT75 Humidity sensor chip
'By Stuart Leslie
'Contact stu@4sightinc.com with any questions
'Uses BascomAVR
'a .01 uf capacitor across VCC and Ground on the SHT75 really cleans up the data
'a pullup is required on "data" pin as shown in the data sheet

$regfile = "m8def.dat"
$crystal = 16000000 ' used crystal frequency
$baud = 9600 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 32 ' default use 10 for the SW stack
$framesize = 32 ' default use 40 for the frame space


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

Sck Alias Portd.4
Dataout Alias Portd.5
Datain Alias Pind.5
'Redled Alias PortD.2

Declare Sub Getit()

Ddrd = &B11111111 'all port b are output
Config Pind.4 = Output 'sck
Config Pind.5 = Output 'datain

'reset the serial communications first, it is easily confused!
Set Dataout
For Ctr = 1 To 12
Set Sck
Waitus 2
Reset Sck
Waitus 2
Next Ctr


Do 'continually read the tempfature and humidity

Command = &B00000011
Call Getit 'Get the temperature, puts result in "dataword" for us
'
'Tempf = T1f * Dataword
'Tempf = Tempf - 40

Tempc = T1c * Dataword 'get celcius for later calculations and for "the rest of the world"
Tempc = Tempc - 40

Dis = Fusing(tempc , "###.##")
Print "Temperatur = " ; Dis ; Chr(167) ; "C"

Command = &B00000101
Call Getit 'get the humidity
Calc = C2 * Dataword
Calc2 = Dataword * Dataword 'that "2" in the datasheet sure looked like a footnote for a couple days, nope it means "squared"!
Calc2 = C3 * Calc2
Calc = Calc + C1
Rhlinear = Calc + Calc2

'Dis = Fusing(rhlinear , "##.##")
'Print "Humidity adjusted for linear = " ; Dis


Calc = T2 * Dataword
Calc = Calc + T1c
Calc2 = Tempc - 25
Calc = Calc2 * Calc
Rhlintemp = Calc + Rhlinear

Dis = Fusing(rhlintemp , "##.##")
Print "Luftfeuchte = " ; Dis ; " %"

Wait 1
Loop


Sub Getit()

Local Datavalue As Word
Local Databyte As Byte

'start with "transmission start"
Set Sck
Reset Dataout
Reset Sck
Set Sck
Set Dataout
Reset Sck


'now send the command
Shiftout Dataout , Sck , Command , 1

Ddrd = &B11111101 'datain is now input
Config Pind.5 = Input 'datain
Set Sck 'click one more off
Reset Sck
Waitus 10 'no idea why, but it doesn't work without it!
Bitwait Pind.7 , Reset 'wait for the chip to have data ready

Shiftin Datain , Sck , Databyte , 1 'get the MSB
Datavalue = Databyte

Ddrd = &B11111111
Config Pind.5 = Output

Reset Dataout 'this is the tricky part- Lot's of hair pulling- have to tick the ack!
Set Sck
Reset Sck

Ddrd = &B11111101 'datain is now input
Config Pind.5 = Input

Shiftin Datain , Sck , Databyte , 1 'get the LSB
Shift Datavalue , Left , 8
Datavalue = Datavalue Or Databyte
'don't tick the clock or ack since we don't need the CRC value, leave it hanging!
Dataword = Datavalue

Ddrd = &B11111111
Config Pind.5 = Output

Reset Dataout
Set Sck
Reset Sck

Ddrd = &B11111101 'datain is now input
Config Pind.5 = Input

Shiftin Datain , Sck , Databyte , 1 'not using the CRC value for now- can't figure it out! Anybody know how to impliment?
'Print "CRC value was - " ; Databyte

Ddrd = &B11111111
Config Pind.5 = Output

Set Dataout
Set Sck
Reset Sck
End Sub

End


Funktionierte nicht!
Ausgabe war immer:


Temperatur = 612.80°C
Luftfeuchte = -6216.70 %


Was kann das sein?
Der Sensor ist in Ordung, an einer C-Control funktioniert er!!!

Ich hab dasselbe Problem, irgendwie wird hier aber keine Lösung aufgezeigt. Kann sich nochmal einer der Lösung annehmen bitte?
MfG