' tempr.bas 22-8-06
' ds-1821 chip with 12 mhz xtal
' by K.S.Sankar www.mostek.biz for EFY Magazine
' written in embedded basis- Bascom-51
' Language downloaded from http://Www.mcselec.com Holland
' p3.0 =data wire
' 1 wire communication with Dallas temperature sensor Ds1821
' small PR35 Package, 3 pin , see data sheet for more Details
' Temperatures are in degres when >-1 , <125
' for temperature <0 , 256 - tempdegree will give
' from -55 to -1
' count range
' - - - - - - - - - - -0+++++++++++++
' -55 -54 -3 -2 -1 0 1 2 3 ...125
'201 202 253 254 255 0 1 2 3 ...125
' port p1-yellow
' = two 7seg display thru two 4511 bcd-7decoder ics
' port-p3 blue
' p3.0= 1 wire interface
' p3.1= increase set value
' p3.2= decrease set value
' p3.3= display set value
' p3.4= '1' for hundreds
' p3.5= '-' minus segment for negative
' p3.7= relay out
'define xtal speed
$crystal = 12000000

$regfile = "89s8252.dat"
'select chip used

P1 = 0
P0 = 0
P2 = 0
P3 = 0
'all ports off
P3 = &B01001110
' input port high for switches
' declare function used
Declare Sub Fn7seg(_i As Byte)
Dim _i As Byte
Config Timer0 = Timer , Gate = Internal , Mode = 2
'Timer0 use timer 0
'Gate = Internal no external interrupt
'Mode = 2 8 bit auto reload
' set t0 internal interrupt 4000 times a sec with 12mhz
Xtal
On Timer0 Timer_0_overflow_int
Load Timer0 , 250
Priority Set Timer0
Enable Interrupts
Enable Timer0
' do not start timer0 here
Config 1wire = P3.0
'use P3.0 for 1 wire communication
Dim Sec_count As Byte
Dim Clock_word As Word
Dim I As Byte , J As Byte
Dim Tempdegree As Byte , Stat_buf As Byte ,
Disp_temp As Byte
Dim Settemp As Byte , Set_disp_temp As Byte
Dim Set_mode As Bit
Dim Ans As Byte
Dim Comp_temp As Byte , Comp_set As Byte
Relay_out Alias P3.7
Sw_set_up Alias P3.1
Sw_set_down Alias P3.2
Sw_set_disp Alias P3.3
Sw_in_port Alias P3
Display_port Alias P1
' set default max temperature for relay to activate
Settemp = 40
Set_mode = 0
Begin:
1wreset
1wwrite &H0C
' write status
1wwrite &B01000010
' continue convertion
1wreset
1wwrite &HEE
' start conversion
1wreset
1wwrite &HAA
' get temperature
Tempdegree = 1wread()
1wreset
Gosub Disptemp
'-----------------
Rem check if set keys pressed
' if pressed stay in set loop for 3 seconds
' after inactivity and display will be in flicker mode
Ans = &B01001110 And Sw_in_port
If Ans <> &B01001110 Then
   ' some input key pressed
   Start Timer0
   Sec_count = 0
   Set_mode = 1
Begin2:
   While 1 = 1
      If Sec_count >= 3 Then
         Set_mode = 0
   Exit While
      End If
   If Sw_set_up = 0 Then
   Sec_count = 0
   Settemp = Settemp + 1
   ' - - - - - - - - - - -0+++++++++++++
   ' -55 -54 -3 -2 -1 0 1 2 3 ...125
   '201 202 253 254 255 0 1 2 3 ...125
' plus range
      If Settemp <= 200 Then
         If Settemp >= 125 Then
            Settemp = 125
            ' limit + reached
         End If
      End If
End If
'-----------------
If Sw_set_down = 0 Then
   Sec_count = 0
   Settemp = Settemp - 1
   ' - - - - - - - - - - -0+++++++++++++
   ' -55 -54 -3 -2 -1 0 1 2 3 ...125
   '201 202 253 254 255 0 1 2 3 ...125
      If Settemp < 201 Then
         If Settemp >= 200 Then
            Settemp = 201
            ' (-55 degrees)
            ' limt exceeded
         End If
      End If
'-----------------
End If
If Sw_set_disp = 0 Then
   Sec_count = 0
End If
'-----------------
Gosub Dispset
Wend
Stop Timer0
End If
' - - - - - - - - - - -0+++++++++++++
' -55 -54 -3 -2 -1 0 1 2 3 ...125
'201 202 253 254 255 0 1 2 3 ...125
' check if real temperature is higher than set value
' add 55 to both sides to avoid negative errors for
Comparison
' byte variable does not support (-) values
Comp_temp = Tempdegree + 55
Comp_set = Settemp + 55
''''If Tempdegree >= Settemp Then
If Comp_temp >= Comp_set Then
   Relay_out = 1
   'relay on
Else
   Relay_out = 0
   'relay off
End If
Goto Begin
' -==-=-=-=-=-=-=- subroutines below-----
Disptemp:
Disp_temp = Tempdegree
Rem display real temperature
' display on 7 seg
P3.4 = 0
P3.5 = 0
' - - - - - - - - - - -0+++++++++++++
' -55 -54 -3 -2 -1 0 1 2 3 ...125
'201 202 253 254 255 0 1 2 3 ...125
If Tempdegree >= 100 Then
   If Tempdegree <= 125 Then
      Disp_temp = Tempdegree - 100
      P3.4 = 1
      ' switch on hundred segment b/c
   End If
End If
If Tempdegree >= 201 Then
   Disp_temp = 256 - Tempdegree
   P3.5 = 1
   ' switch on minus [-] segment g
End If
Call Fn7seg(disp_temp)
Return
'--------------------------------
Dispset:
Rem display preset temperature
Set_disp_temp = Settemp
P3.4 = 0
P3.5 = 0
' - - - - - - - - - - -0+++++++++++++
' -55 -54 -3 -2 -1 0 1 2 3 ...125
'201 202 253 254 255 0 1 2 3 ...125
If Settemp >= 100 Then
   If Settemp <= 125 Then
      Set_disp_temp = Settemp - 100
      P3.4 = 1
      ' switch on hundred segment b/c
   End If
End If
If Settemp >= 200 Then
   Set_disp_temp = 256 - Settemp
   P3.5 = 1
   ' switch on minus [-] segment g
End If
Set_mode = 1
Call Fn7seg(set_disp_temp)
Waitms 50
Return
'=-=-=-=-=-= function below---- - -- -
Sub Fn7seg(_i As Byte)
Dim _ans As Byte
' display on two 7 seg
_ans = Makebcd(_i)
Display_port = _ans
If Set_mode = 1 Then
   ' if in set mode make display flicker
   Display_port = 255
   ' blankout the display
   Waitms 10
   ' turn it on again
   Display_port = _ans
   Waitms 10
End If
End Sub
' interrupt subroutine -----------------
Timer_0_overflow_int:
' program comes here 4000 times a sec
' with a 12mhz xtal
Incr Clock_word
If Clock_word > 4000 Then
   Clock_word = 0
   Incr Sec_count
End If
Return
End
' prog size = 914 bytes
' end of program