Electronics
Btrieve
Motorcycling
Software

Using DS1302 timekeeper from BASIC-52

Relevant part of schematics


You can replace battery with 'supercap' if you want to.

Example Source Code

10 REM DS1302 timekeeper v2 100 GOSUB 11000 150 PH0. Y,M,D,H,MI,S, 151 PRINT CHR(13), 160 FOR I=1 TO 100:NEXT I 190 GOTO 100 999 END 1000 REM send CMD as command 1030 FOR I=1 TO 8 1040 CN=CMD.AND.1 1045 CMD=CMD/2 1050 PORT1=0FCH.OR.CN 1060 PORT1=0FEH.OR.CN 1070 PORT1=0FCH.OR.CN 1090 NEXT 1095 PORT1=0FDH 1099 RETURN 2000 REM Pull RST low 2002 PORT1=0FDH 2005 PORT1=0BDH 2010 RETURN 3000 REM pull clock low, RST high 3010 PORT1=0BDH 3030 PORT1=0FDH 3040 RETURN 4000 REM Read first byte after command to CMD 4028 CMD=0 4029 FOR I=1 TO 7 4030 CN=PORT1.AND.1 4040 CMD=CMD/2 4045 CMD=CN*128+CMD 4060 PORT1=0FFH 4070 PORT1=0FDH 4080 NEXT 4081 CN=PORT1.AND.1 4082 CMD=CMD/2 4083 CMD=CN*128+CMD 4090 RETURN 4500 REM Read next byte in burst to CMD 4510 PORT1=0FFH 4520 PORT1=0FDH 4530 GOTO 4000 5000 REM start charging 5005 GOSUB 6000 5006 GOSUB 3000 5010 CMD=090H 5015 GOSUB 1000 5020 CMD=0A5H 5030 GOSUB 1000 5035 GOSUB 2000 5040 GOSUB 7000 5050 RETURN 6000 REM enable writing to clock 6010 GOSUB 3000 6020 CMD=08EH:GOSUB 1000:CMD=0:GOSUB 1000 6030 GOSUB 2000 6040 RETURN 7000 REM disable writing to clock 7010 GOSUB 3000 7020 CMD=08EH:GOSUB 1000:CMD=080H:GOSUB 1000 7030 GOSUB 2000 7040 RETURN 10000 REM change time 10010 Y=001H 10011 M=003H 10012 D=031H 10013 H=022H 10014 MI=030H 10015 S=000H 10016 W=6 10025 GOSUB 6000 10028 GOSUB 3000 10030 CMD=0BEH : GOSUB 1000 10031 CMD=S : GOSUB 1000 10032 CMD=MI : GOSUB 1000 10033 CMD=H : GOSUB 1000 10034 CMD=D : GOSUB 1000 10035 CMD=M : GOSUB 1000 10036 CMD=W : GOSUB 1000 10037 CMD=Y : GOSUB 1000 10038 CMD=0 : GOSUB 1000 10050 GOSUB 2000 10060 GOSUB 7000 10999 RETURN 11000 REM read date/time to vars 11010 GOSUB 3000 11020 CMD=0BFH : GOSUB 1000 11030 GOSUB 4000 : S=CMD 11031 GOSUB 4500 : MI=CMD 11032 GOSUB 4500 : H=CMD 11033 GOSUB 4500 : D=CMD 11034 GOSUB 4500 : M=CMD 11035 GOSUB 4500 : W=CMD 11036 GOSUB 4500 : Y=CMD 11040 GOSUB 4500 11050 GOSUB 2000 11060 RETURN

When run, this program reads RTC values in loop and displays current date and time. However, before you can do that, you have to set the time in RTC, and enable it. Modify values on lines 10010..10016, then enter GOTO 10000. You will get C-STACK error because of bogus RETURN, but it does not matter. You will probably want to enable the trickle charger for you backup power source by entering GOTO 5000.

Copyright © Madis Kaal 2000-