'ds1302 clock DEVICE P8X32A, XTAL1, PLL16X FREQ 80_000_000 CS1302 PIN 0 low 'sel - chip select active high DS1302 pin 5 DataIO PIN 1 input 'I/O - Performs R/W to DS1302 pin 6; 2k2 resistor i/face Clock PIN 2 low 'clk - Connects to DS1302 pin 7 ' -----[ DS1302 Constants ]------------------------------------------------------- 'A/n Address bit 'R/C ram/clock 1=ram 1RAA_AAAR 'R/W read/write 1=read 1C43_210W CWPr CON $8E ' Write Protect Register 1000_1110 WPr1 CON $80 ' Set Write Protect-write to cwpr 1000_0000 WPr0 CON $00 ' Clear Write Protec-write to cwpr 0000_0000 WrBurst CON $BE ' Write Burst Of Data 1011_1110 RdBurst CON $BF ' Read Burst Of Data 1011_1111 WrRam CON $C0 ' Write to RAM Data 1100_0000 RdRam CON $C1 ' Read from RAM Data 1100_0001 RdDate CON $87 ' Read Calendar Date - numerical 1000_0111 wrramburst con $FE rdramburst con $FF ' -----[ clock Variables ]------------------------------------------------------- 'table of hub bytes for converting to binary/bcd as required 'dtable data secs HUB byte ' Seconds mints HUB byte ' Minutes hrs HUB byte ' Hours date HUB byte ' Date (numerical) month HUB byte ' Month (numerical) day HUB byte ' Day (numerical - linked to EEPROM data Sunday =1) year HUB byte ' Year mthstr data "JanFebMarAprMayJunJulAugSepOctNovDec",0 daystr data "MonTueWedThuFriSatSun".0 datestr hub string(30) tmpstr hub string(10) strslt hub string(20) index VAR long Counter VAR long ' Loop counter Char VAR long ' Character Program_State VAR long ' Program State Reg VAR long ' Read/Write Address IoByte VAR long ' Data To/From DS1302 value VAR long adr var long stmp1 var long stmp2 var long stmp3 var long tvar1 var long tvar2 var long tvar3 var long htmp var long idx var long x var long y var long show_time sub 0 pcrlf SUB 0 sertx SUB 1 print SUB 1 pstr SUB 1 phex sub 1 RTC_In SUB 0 RTC_Out SUB 0 Read_Time SUB 0 BCD_Convert SUB 0 Write_Date_Time SUB 0 ' ====================================================================== ' PROGRAM START PROGRAM Start LMM ' ====================================================================== START: 'write decimal date to hub { '#### uncomment to write new date/time 'set these values to suit new date/time ' sc,mn,hr,dt,mt,dy,yr wrbyte secs,00,36,23,29,09,02,20 'secs,mins,hrs,date,month,day,year 'convert hub date to bcd date then write back to hub bcd_convert 'write clock regs from hub in bcd format 'have to clock in 8 bytes or the data will not latch!!! HIGH CS1302 SHIFTOUT DataIO, Clock, LSBFIRST,wrBurst for adr=0 to 6 rdbyte secs(adr),stmp1 '3 'adr+1 SHIFTout DataIO, Clock, LSBFIRST, stmp1 print stmp1 next SHIFTout DataIO, Clock, LSBFIRST, 0 'eighth byte essential low CS1302 } '#### uncomment to write new date/time pause 8000 'time to switch terminal software to com port pcrlf 'print ram regs 0-30 pcrlf pstr "Data in clock ram" pcrlf for x=0 to 30 iobyte=0 reg=x<<1 reg=reg or rdram rtc_in strslt=str iobyte,4 pstr strslt next pcrlf pause 2000 ' main loop printing date and time to serial at 9600bd do 'note:- hub ram clock registers ' 0 1 2 3 4 5 6 'secs,mins,hrs,date,month,day,year 'read clock regs to hub and convert to decimal from bcd 'read clock burst mode HIGH CS1302 SHIFTOUT DataIO, Clock, LSBFIRST,RdBurst for adr=0 to 6 'convert from bcd to decimal SHIFTIN DataIO, Clock, LSBPRE, tvar1 tvar2=tvar1>>4 tvar2=tvar2*10 '10's tvar1=tvar1 and $0f tvar2=tvar2 + tvar1 Wrbyte secs(adr),tvar2 'secs is first item in hub ram table. adr is index to others next low CS1302 datestr="" rdbyte secs(5),tvar1 'day dec tvar1 tvar2=tvar1<<1 tvar1=tvar1+tvar2 'times 3 to index string day 3 chars each inc tvar1 tmpstr=mid daystr,tvar1,3 'get three letter day from daystr pstr tmpstr rdbyte secs(3),tvar1 'date sertx "/" print tvar1 sertx "/" rdbyte secs(4),tvar1 dec tvar1 tvar2=tvar1<<1 tvar1=tvar1+tvar2 'times 3 inc tvar1 tmpstr=mid mthstr,tvar1,3 'month pstr tmpstr pstr " 20" rdbyte secs(6),tvar1 print tvar1 'year pstr " " rdbyte secs(2),tvar1 print tvar1 sertx ":" rdbyte secs(1),tvar1 print tvar1 sertx ":" rdbyte secs(0),tvar1 print tvar1 pause 2000 pcrlf loop '==================== SUBS ================================================ 'show time in hub registers sub show_time for x=0 to 6 rdbyte secs(x),stmp1 print stmp1 next endsub '------------------------------ sub Write_Date_Time HIGH CS1302 'Select DS1302 SHIFTOUT DataIO, clock, LSBFIRST, WrBurst 'DS1302 Burst Write GOSUB BCD_Convert for x=0 to 6 'secs, mins, hrs, date, month, day, year rdbyte secs(x),stmp1 print stmp1 SHIFTOUT DataIO, Clock, LSBFIRST,stmp1 next pcrlf LOW CS1302 ' Deselect DS1302 endsub '----------------- SUB RTC_In HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, reg SHIFTIN DataIO, Clock, LSBPRE, ioByte LOW CS1302 ' Deselect DS1302 endsub '----------------- SUB RTC_Out HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, reg SHIFTOUT DataIO, Clock, LSBFIRST, ioByte LOW CS1302 ' Deselect DS1302 endsub '------------------ sub pstr 'send serial string stmp1=__param1 do rdbyte stmp1,stmp2 if stmp2=0 then exit sertx stmp2 inc stmp1 loop endsub '---------------- sub print strslt=str __param1,2 pstr strslt endsub '------------------------ sub phex htmp=__param1 stmp1= htmp and $F0 stmp1=stmp1>>4 stmp1=stmp1+48 if stmp1>57 then stmp1=stmp1+7 endif sertx stmp1 stmp1=htmp and $0F stmp1=stmp1+48 if stmp1>57 then stmp1=stmp1+7 endif sertx stmp1 endsub '----------- sub sertx 'send serial byte SEROUT 30,T9600,__param1 endsub '--------------------------- sub pcrlf sertx 13 sertx 10 endsub '--------------------------- 'convert binary to bcd in clock data table. SUB BCD_Convert FOR index = 0 TO 6 rdbyte secs(index),value ' example value=23 char=value/10 'char=23/10=2 char=char<<4 'mov 2 to msnibble char stmp1=value/10 'stmp1=2 stmp1=stmp1*10 'stmp1=20 stmp1=value-stmp1 'stmp1=23-20=3 char=char or stmp1 'or into lsnibble char making BCD version wrbyte secs(index),char NEXT endsub '------------------------------ 'secs, mins, hrs, date, month, day, year SUB Read_Time ' DS1302 Burst Read HIGH CS1302 ' SELECT DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, RdBurst SHIFTIN DataIO, Clock, LSBPRE, stmp1 'secs wrbyte secs,stmp1 SHIFTIN DataIO, Clock, LSBPRE, stmp1 'mints wrbyte mints,stmp1 SHIFTIN DataIO, Clock, LSBPRE, stmp1 'hrs wrbyte hrs,stmp1 SHIFTIN DataIO, Clock, LSBPRE, stmp1 'date wrbyte date,stmp1 SHIFTIN DataIO, Clock, LSBPRE, stmp1 'month wrbyte month,stmp1 SHIFTIN DataIO, Clock, LSBPRE, stmp1 'day wrbyte day,stmp1 SHIFTIN DataIO, Clock, LSBPRE, stmp1 'year wrbyte year,stmp1 LOW CS1302 ' Deselect DS1302 endsub '==================== END ====================