The source code below combines pieces from Dallas
1302 RTC and Dallas 1621 temperature sensor
experiments to create a simple temperature logger application:
MTOP=05FFFH
10 REM data logger v1
20 GOSUB 1900:GOSUB 2000
30 GOSUB 1900:LM=MI
100 GOSUB 1900:IF LM=MI THEN GOTO 100
110 LM=MI
120 GOSUB 2100
150 GOSUB 800
151 PRINT ",",
170 print T
190 GOTO 100
799 END
800 REM print formatted time
810 X=D : GOSUB 900 : PRINT "/",
812 X=M : GOSUB 900 : PRINT "/20",
814 X=Y : GOSUB 900 : PRINT ",",
820 X=H : GOSUB 900 : PRINT ":",
822 X=MI : GOSUB 900 : PRINT ":",
825 X=S : GOSUB 900
830 RETURN
900 REM print pure BCD
905 X=X.AND.255
910 Z=INT(X/16)
915 GOSUB 930
920 Z=X.AND.15
930 IF Z>9 THEN Z=Z+7
940 Z=Z+48 : PRINT CHR(Z),
950 RETURN
1000 REM lines 1000-2000 contain Dallas 1302 RTC support
1005 REM send CMD as command
1030 FOR I=1 TO 8 : CN=CMD.AND.1 : CMD=CMD/2
1050 PORT1=0FCH.OR.CN : PORT1=0FEH.OR.CN : PORT1=0FCH.OR.CN
1090 NEXT : PORT1=0FDH : RETURN
1100 REM Pull RST low
1102 PORT1=0FDH : PORT1=0BDH : RETURN
1200 REM pull clock low, RST high
1210 PORT1=0BDH : PORT1=0FDH : RETURN
1300 REM Read first byte after command to CMD
1328 CMD=0 : FOR I=1 TO 7 : CN=PORT1.AND.1
1340 CMD=CMD/2 : CMD=CN*128+CMD
1360 PORT1=0FFH : PORT1=0FDH : NEXT
1381 CN=PORT1.AND.1 : CMD=CMD/2 : CMD=CN*128+CMD
1390 RETURN
1400 REM Read next byte in burst to CMD
1410 PORT1=0FFH : PORT1=0FDH : GOTO 1300
1500 REM start charging
1505 GOSUB 1600 : GOSUB 1200 : CMD=090H : GOSUB 1000
1520 CMD=0A5H : GOSUB 1000 : GOSUB 1100 : GOSUB 1700 : RETURN
1600 REM enable writing to clock
1610 GOSUB 1200 : CMD=08EH : GOSUB 1000 : CMD=0 : GOSUB 1000
1630 GOSUB 1100 : RETURN
1700 REM disable writing to clock
1710 GOSUB 1200 : CMD=08EH : GOSUB 1000 : CMD=080H : GOSUB 1000
1730 GOSUB 1100 : RETURN
1800 REM change time
1810 Y=001H
1811 M=003H
1812 D=031H
1813 H=022H
1814 MI=030H
1815 S=000H
1816 W=6
1825 GOSUB 1600 : GOSUB 1200
1830 CMD=0BEH : GOSUB 1000 : CMD=S : GOSUB 1000
1832 CMD=MI : GOSUB 1000 : CMD=H : GOSUB 1000
1834 CMD=D : GOSUB 1000 : CMD=M : GOSUB 1000
1836 CMD=W : GOSUB 1000 : CMD=Y : GOSUB 1000
1838 CMD=0 : GOSUB 1000 : GOSUB 1100
1860 GOSUB 1700 : RETURN
1900 REM read date/time to vars
1910 GOSUB 1200 : CMD=0BFH : GOSUB 1000
1930 GOSUB 1300 : S=CMD : GOSUB 1400 : MI=CMD
1932 GOSUB 1400 : H=CMD : GOSUB 1400 : D=CMD
1934 GOSUB 1400 : M=CMD : GOSUB 1400 : W=CMD
1936 GOSUB 1400 : Y=CMD : GOSUB 1400 : GOSUB 1100 : RETURN
2000 REM lines 2000-3000 contain Dallas 1621 temp sensor support
2010 REM initialize sensor interface
2011 REM build PORT3 access functions
2012 XBY(6000H)=085H : XBY(6001H)=0B0H : XBY(6002H)=020H : XBY(6003H)=022H
2013 XBY(6004H)=085H : XBY(6005H)=020H : XBY(6006H)=0B0H : XBY(6007H)=022H
2015 REM do the interface init
2020 GOSUB 2200: GOSUB 2300
2025 BY=090H : GOSUB 2600 : BY=0EEH : GOSUB 2600 : GOSUB 2400
2028 RETURN
2100 REM read the temperature to T and return
2110 GOSUB 2200 : GOSUB 2300: BY=090H : GOSUB 2600
2136 BY=0AAH : GOSUB 2600
2144 GOSUB 2400
2146 GOSUB 2300
2150 BY=091H : GOSUB 2600
2160 GOSUB 2700 : GOSUB 2800
2165 BH=BY
2166 GOSUB 2700 : GOSUB 2900: GOSUB 2400
2190 T=BH*256+BY: T=T/256:return
2200 REM idle
2210 DBY(20H)=255 : CALL 6004H : RETURN
2300 REM start
2310 DBY(20H)=0EFH : CALL 6004H : DBY(20H)=0CFH : CALL 6004H : RETURN
2400 REM stop
2410 DBY(20H)=0CFH : CALL 6004H : DBY(20H)=0EFH : CALL 6004H
2415 DBY(20H)=0FFH : CALL 6004H : RETURN
2500 REM get ack
2510 DBY(20H)=0CFH : CALL 6004H : DBY(20H)=0DFH : CALL 6004H
2530 DBY(20H)=0FFH : CALL 6004H : CALL 6000H
2540 A=DBY(20H).AND.16 : DBY(20H)=0DFH : CALL 6004H
2550 DBY(20H)=0CFH : CALL 6004H : RETURN
2600 REM send BY, get ack to A
2610 FOR I=1 TO 8 : BT=BY.AND.128 : BY=BY+BY : BT=BT/8
2640 DBY(20H)=0CFH.OR.BT : CALL 6004H : DBY(20H)=0EFH.OR.BT : CALL 6004H
2660 DBY(20H)=0CFH.OR.BT : CALL 6004H : NEXT I : GOSUB 2500 : RETURN
2700 REM read BY
2710 BY=0 : DBY(20H)=0DFH : CALL 6004H
2720 FOR I=1 TO 8 : DBY(20H)=0FFH : CALL 6004H
2740 CALL 6000H : BT=DBY(20H).AND.16
2745 DBY(20H)=0DFH : CALL 6004H : BT=BT/16 : BY=BY+BY+BT
2770 NEXT I : RETURN
2800 REM send ACK
2810 DBY(20H)=0CFH : CALL 6004H : DBY(20H)=0EFH : CALL 6004H
2820 DBY(20H)=0CFH : CALL 6004H : RETURN
2900 REM send NAK
2910 DBY(20H)=0DFH : CALL 6004H : DBY(20H)=0FFH : CALL 6004H
2920 DBY(20H)=0DFH : CALL 6004H : RETURN
The data logger outputs a timestamped temperature reading once every
minute, in a format DD/MM/YYYY,HH:MM:SS, TT.T where TT.T is temperature
reading. As BASIC-52 does not have an internal function to output BCD
numbers without 'H' added to it, there is a subroutine at line 900 which
outputs a two-digit BCD number from a number contained in variable X.
Subroutine at line 800 then uses it to output the current date and time.
Temperature is output by using a built-in PRINT and thus it has spaces
around it.
If you change line 100 to read just
100 GOSUB 1900
then you will get contignous temperature readings. I used this change
to track the temperature change speed, captured the output to file,
opened it in Excel, and made a chart:
I used an icecube in plastic bag to cool the chip down to +6 degC,
then let it raise to room temperature of +27 degC. As you can see, the
process took about 13 minutes - DS1621 is not a very fast responding
device.
|