// Auto start up // Updated for version 1.445 // constant I2_RTC 0xd0 // address of rtc I2c device constant I2_TMP 0x90 // address of temperature device // // ============ I2C Section ================================ // starts i2c at conservative 100k function i2_init i2copen 100000 endf // // ============ Temperature section ======================== // temperature device function i2_temp$ // gets temperature as a string dim t[3], dpl=0 i2cread I2_TMP 0, t[1];2 if t[2] > 128 then dpl=5 endif result str$(t[1])+"."+str$(dpl) endf // // ============ RTC Section ================================ // // starts clock after power down function i2_startrtc i2cwrite I2_RTC 0x0c 0 endf // --------------------------------------------------------- // set time on i2c clock, enter numbers as decimal function i2_settime(h%, m%, s%) s%=dec2bcd(s%,2):m%=dec2bcd(m%,2):h%=dec2bcd(h%,2) i2cwrite I2_RTC 0 0 s% m% h% endf // --------------------------------------------------------- // set time on i2c clock, enter numbers as decimal function i2_setdate(dom%, mon%, yr%, dow%) dom%=dec2bcd(dom%,2):mon%=dec2bcd(mon%,2):dow%=dec2bcd(dow%,2) yr%=dec2bcd(yr%,4)// year is 4 digits yr1%=and(yr%,0xff) // but only 2 used // NOTE rtc MT1T81, registers start from 0 i2cwrite I2_RTC 4 dow% dom% mon% yr1% endf // --------------------------------------------------------- // time returns time on i2c clock as a string function i2_time$ dim t[8], t$[:50] i2cread I2_RTC 0, t[1];8 // time t$=bcd2dec(t[4],2)+":"+bcd2dec(t[3],2)+":"+bcd2dec(t[2],2) result t$ endf function i2_date$ dim t[8], t$[:50] i2cread I2_RTC 0, t[1];8 // date t$=bcd2dec(t[6],2)+"/"+bcd2dec(t[7],2)+"/" yr=2000+bcd2dec(t[8],2) t$=t$+yr+" "+bcd2dec(t[5],2) result t$ endf function i2_clock dim t[2], t$, d$ print "Time at shutdown ";i2_date$;" ";i2_time$ i2_startrtc i2cread I2_RTC 0, t[1];1 // compare values wait 20 i2cread I2_RTC 0, t[2];1 // compare values if t[2]>t[1] then print "Clock running so setting system time" settime i2_time$ setdate i2_date$ endif print print "Current time ";i2_date$;" ";i2_time$ endf function i2_time i2_init // open i2c // test to see if rtc# is plugged in to i2c bus if i2ctest(I2_RTC)=1 then print "The temperature is ";i2_temp$ i2_clock // get time from i2c clock else print time$();" enter to accept or enter new time "; input a$ if a$<>"" then settime a$ endif print date$();" enter to accept or enter new date
"; input a$ if a$<>"" then setdate a$ endif endif endf // just a test for i2c clock, shows all of registers function all dim t%[20],i i2cread I2_RTC 0, t%[1];20 for i=1 to 20 print "[";i;"] ";t%[i] next endf