Electronics Btrieve Motorcycling Software If you find this site useful and want to support, click on few ads to throw some money into hat.

DS1621 temperature sensor with BASIC-52

As you can see, hookup it pretty trivial. So would be the code if BASIC-52 would have PORT3 access operator as for PORT1. As I wanted to keep the PORT1 pins for DS1302 clock hookup, I decided to use T0 and T1 pins for I2C interface. Dallas calls it 2-wire serial bus, but it is the same thing. They probably just can't use I2C name or Philips will sue their heads off.

Anyway, the first thing I needed was a way to read and write port 3 pins. BASIC-52 has a DBY() operator, but this accesses data RAM, not SFR address space where the P3 register is. So I created two very simple functions in shared RAM area (this means that you must have shared ram at 6000H, like this core conf). Before you actually run the program, you will need to tell BASIC-52 that you need the RAM for yourself by entering MTOP=05FFFH.

The code itself is very simple

6000:	85 b0 20	read:	mov	020h,0b0h
	22			ret
6004:	85 20 b0	write:	mov	0b0h,020h
	22			ret

This code is stored into shared RAM at lines 10000..10030. CALL operator is then used to read or write the port 3 value, with memory location 020h used as data exchange storage. The example source code below just runs in loop and outputs the current temperature reading. It does work but is very very slow. This kind of a functionality desperately calls for machine code implementation, but the BASIC code serves better as an example.

MTOP=05FFFH

10 REM DS1621 control
20 GOSUB 10000
25 GOSUB 2000 
28 GOSUB 3000 : BY=090H : GOSUB 6000 : BY=0EEH : GOSUB 6000 : GOSUB 4000 : GOSUB 2000
30 GOSUB 3000
35 BY=090H : GOSUB 6000
36 BY=0AAH : GOSUB 6000
44 GOSUB 4000
46 GOSUB 3000
50 BY=091H : GOSUB 6000
60 GOSUB 7000 : GOSUB 8000
65 BH=BY 
66 GOSUB 7000 : GOSUB 9000
70 GOSUB 4000
90 T=BH*256+BY
100 T=T/256
110 PRINT T
999 GOTO 30

1000 REM read P3
1010 CALL 6000H
1020 P3=DBY(020H)
1030 RETURN 

1100 REM write P3
1110 DBY(020H)=P3
1130 CALL 6004H
1140 RETURN 

2000 REM idle
2010 P3=255 : GOSUB 1100
2020 RETURN 

3000 REM start
3010 P3=0EFH : GOSUB 1100
3015 P3=0CFH : GOSUB 1100
3020 RETURN 

3100 REM repeated start
3110 P3=0DFH : GOSUB 1100
3115 P3=0FFH : GOSUB 1100
3116 P3=0EFH : GOSUB 1100
3117 P3=0CFH : GOSUB 1100
3120 RETURN 

4000 REM stop
4010 P3=0CFH : GOSUB 1100
4012 P3=0EFH : GOSUB 1100
4015 P3=0FFH : GOSUB 1100
4020 RETURN 

5000 REM get ack
5010 P3=0CFH : GOSUB 1100
5020 P3=0DFH : GOSUB 1100
5030 P3=0FFH : GOSUB 1100
5040 GOSUB 1000
5045 A=P3.AND.16
5050 P3=0DFH : GOSUB 1100
5060 P3=0CFH : GOSUB 1100
5070 RETURN 

6000 REM send BY, get ack to A
6010 FOR I=1 TO 8
6020 BT=BY.AND.128
6025 BY=BY*2
6030 BT=BT/8
6040 P3=0CFH.OR.BT
6045 GOSUB 1100
6050 P3=0EFH.OR.BT
6055 GOSUB 1100
6060 P3=0CFH.OR.BT
6065 GOSUB 1100
6070 NEXT I
6080 GOSUB 5000
6090 RETURN 

7000 REM read BY
7010 BY=0
7015 P3=0DFH : GOSUB 1100
7020 FOR I=1 TO 8
7035 P3=0FFH : GOSUB 1100
7040 GOSUB 1000 : BT=P3.AND.16
7045 P3=0DFH : GOSUB 1100
7050 BT=BT/16
7060 BY=BY*2+BT
7070 NEXT I
7080 RETURN 

8000 REM send ACK
8010 P3=0CFH : GOSUB 1100
8020 P3=0EFH : GOSUB 1100
8030 P3=0CFH : GOSUB 1100
8040 RETURN 

9000 REM send NAK
9010 P3=0DFH : GOSUB 1100
9020 P3=0FFH : GOSUB 1100
9030 P3=0DFH : GOSUB 1100
9040 RETURN 

10000 REM build PORT3 access functions
10010 XBY(6000H)=085H : XBY(6001H)=0B0H : XBY(6002H)=020H : XBY(6003H)=022H
10020 XBY(6004H)=085H : XBY(6005H)=020H : XBY(6006H)=0B0H : XBY(6007H)=022H
10030 RETURN
 

Copyright © Madis Kaal 2000-