' Kemence homerseklet szabalyzo program

Dim Celtemp As Single
Dim U0 As Single

Celtemp = 81
U0 = 10




Dim Kuszoba As Single
Dim Kuszobf As Single
Dim Tart As Single
Kuszoba = Celtemp - U0
Kuszobf = Celtemp + U0
Tart = U0 * 2
Dim Tempc As Single


'Routine to read the  SHT11 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 SHT11 really cleans up the data
'a pullup is required on "data" pin as shown in the data sheet

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

Const C1 = -4
Const C2 = 0.0405
Const C3 = -0.0000028
Const T1c = .01
Const T2 = .00008
Const T1f = .018

Sck Alias Portd.2
Dataout Alias Portd.3
Datain Alias Pind.3


Declare Sub Getit()

Ddrd = &B11111111                                           'all port d are output
Config Pind.2 = Output                                      'sck
Config Pind.3 = Output                                      'datain
Config Pind.1 = Output

'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

Cls

Dim Delta As Single
Dim Hanyados As Single
Dim Ihanyados As Word
Dim Idelta As Integer
Dim Itart As Integer
Dim Arany As Long
Dim Ido As Long

Waitms 20                                                   'wait for SHT11 setup'
Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Down , Prescale = 1024

Itart = Int(tart)
Ido = 0

Locate 1 , 1

Lcd "Start"

Config Watchdog = 2048
Start Watchdog

Do
   Reset Watchdog
   Config Lcdpin = Pin , E = Portb.3 , Rs = Portb.2 , Db7 = Portb.7 , Db6 = Portb.6 , Db5 = Portd.0 , Db4 = Portb.4

   If Ido = 0 Then
' lekerdezzuk a homersekletet
     Command = &B00000011
     Call Getit                                             'Get the temperature, puts result in "dataword" for us
     Tempc = T1c * Dataword                                 'get celcius for later calculations and for "the rest of the world"
     Tempc = Tempc - 40
' most Tempc-ben van a homerseklet

' Beallitjuk a PWM-et
     If Tempc < Kuszoba Then
       Compare1a = 1023
       Goto Cimke1
     End If

     If Tempc > Kuszobf Then
       Compare1a = 0
       Goto Cimke1
     End If

     Delta = Kuszobf - Tempc
     Hanyados = 1024 * Delta
     Hanyados = Hanyados / Tart
     Ihanyados = Int(hanyados)
     If Ihanyados > 1023 Then Ihanyados = 1023
     Compare1a = Ihanyados

     Cimke1:

' kiirjuk a homersekletet
     Dis = Fusing(tempc , "###.##")
     Locate 1 , 1
     Lcd Dis
     Lcd Chr(223)
     Lcd "C "

' lekerdezzuk a paratartalmat
     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

     Calc = T2 * Dataword
     Calc = Calc + T1c
     Calc2 = Tempc - 25
     Calc = Calc2 * Calc
     Rhlintemp = Calc + Rhlinear
     Dis = Fusing(rhlintemp , "##.##")

' kiirjuk a paratartalmat
     Lcd Dis
     Lcd "%   "
     Locate 2 , 1
     Lcd "heat:"

' kiirjuk, hogy mennyire futunk (%-ban)
     Delta = Delta * 100
     Idelta = Int(delta)
     Arany = Idelta / Itart
     If Tempc < Kuszoba Then Arany = 100
     If Tempc >= Kuszobf Then Arany = 0
     Lcd Arany
     Lcd "% "

' kiirjuk, hogy milyen homersekletre szabalyzunk
     Lcd "85"
     Lcd Chr(223)
     Lcd "C  "

   End If

   Ido = Ido + 1
   If Ido = 300 Then Ido = 0
Loop


Sub Getit()                                                 ' ez a rutin kerdezi le a homersekletet es a paratartalmat

   Local Datavalue As Word
   Local Databyte As Byte

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

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

   Ddrd = &B11111101                                        'datain is now input
   Config Pind.3 = Input                                    'datain
   Set Sck                                                  'click one more off
   Waitus 100
   Reset Sck
   Waitus 100                                               'no idea why, but it doesn't work without it!


   Bitwait Pind.3 , Reset                                   'wait for the chip to have data ready

'   Config Ping.4 = Input
'   Waitus 100
'   Bitwait Ping.4 , Reset

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

   Ddrd = &B11111111
   Config Pind.3 = Output

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

   Ddrd = &B11111101                                        'datain is now input
   Config Pind.3 = 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.3 = Output

   Reset Dataout
   Waitus 100
   Set Sck
   Waitus 100
   Reset Sck
   Waitus 100

   Ddrd = &B11111101                                        'datain is now input
   Config Pind.3 = 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.3 = Output

   Set Dataout
   Waitus 100
   Set Sck
   Waitus 100
   Reset Sck
   Waitus 100


End Sub

End