'---------------------------------------------------------------------------------------- ' Name: I2CL_DS1307_DEMO_001.TIG ' Type: Tiger-BASIC(tm) Source Code ' Purpose: Demo for writing to and reading from a Dallas DS1307 realtime clock ' with the Tiger's I2CL functions ' ' (C) - Copyright Wilke Technology, P.O.Box 1727, D-52018 Aachen, Germany '---------------------------------------------------------------------------------------- ' ' Thank you for using BASIC Tigers in your products. If you have questions, ideas ' or special needs, please contact your next distributor or the Tiger support team ' and visit our web site: ' ' Wilke Technology GmbH ' The Tiger Support Team ' P.O.Box 1727, D-52018 Aachen, Germany ' Krefelder Str. 147, D-52070 Aachen, Germany ' ' email: support@wilke-technology.com (english) ' email: support@wilke.de (german) ' Phone: +49 (241) 918 900 Mo to Fr, 7:00 to 16:00 (GMT) ' Fax: +49 (241) 918 9068 ' ' New information, new drivers and free downloads see: ' ' www.wilke-technology.com (english) ' www.wilke.de (german) ' ' Sincerely, ' ' Your Tiger Support Team ' '---------------------------------------------------------------------------------------- ' ' This demo was compiled with Tiger-BASIC V5.3.2 and TAC files 1.16e to run on a ' Plug & Play Lab with a BASIC-Tiger AXI-4/4. ' ' Connect the DS1307 clock line to pin L70 and the DS1307 data line to pin L71 of the ' Plug & Play Lab and connect also a pull-up resistor to each line, connect VCC and GND ' to the DS1307. ' ' !----------O-------- Vcc = +5V ' ! ! ' ! ! ' !---! !---! ' !------------------! ! ! ! ! ' ! BASIC-Tiger ! ! R ! ! R ! ' ! TINY-Tiger ! ! ! ! ! ' ! ECONO-Tiger ! !---! !---! ' ! TINY-Tiger 2 ! ! ! ' ! ! ! ! !----------! ' ! ! ! ! ! ! ' ! L70 = CLOCK !-------O------------------! SCL ! ' ! ! ! ! ! ' ! ! ! ! DS1307 ! ' ! ! ! ! ^^^^^^ ! ' ! L71 = DATA !------------------O-------! SDA ! ' ! ! ! ! ' ! ! ! ! ' ! ! !----------! ' ! ! ' !------------------! ' ' ' The program first writes time and date given in variables to the DS1307 (after ' converting the data to BCD format used in the DS1307 registers) using the I2CL ' functions. If you just want to read the DS1307 RTC, you can comment out the define ' "SET_DS1307" at the beginning of the source code. Without this defined, writing to ' the DS1307 will NOT be executed. ' ' After that the program, in an endless loop, reads the DS1307 RTC until there is a ' change in at least one of the 7 register addresses of the DS1307. If a change is ' found, the registers content is converted back from BCD to "standard" numbers and ' time and date are put out on the LCD. ' ' On writing, always the 24-hour format is used. When reading, the DS1307 can be in ' 12-hour format - the conversion routine does take this into account. ' ' In addition to the 8 register bytes at addresses 0 to 7, the DS1307 has also 56 ' RAM bytes at addresses 08h to 3Fh. These can be written an read just like the RTC ' registers and give a little non-volatile storage space. ' '---------------------------------------------------------------------------------------- USER_VAR_STRICT ' variables must be declared #INCLUDE DEFINE_A.INC ' general defines #INCLUDE UFUNC4.INC ' user function codes #DEFINE SET_DS1307 ' make to comment if clock NOT to be set TASK Main ' begin task MAIN BYTE Temp, Hour, Minute, Second ' BYTE vars for time BYTE Weekday, Day, Month, Year ' BYTE vars for date LONG EVER, FLG, A ' STRING Read$, Write$, I2C$, Result$ ' STRING vars for I2C messages STRING Temp$, OldResult$ ' help variables INSTALL_DEVICE #LCD, "LCD1.TDD" ' install LCD-driver: BASIC Tiger PRINT #LCD, "<27>c<0><240>"; ' cursor OFF ' (Port, Clock_Pin, Data_Pin, Speed_Reduction: 1=no ... 20=slower ) I2CL_SETUP (7, 0, 1, 1) ' setup I2C bus for Tiger Write$ = "" ' DevSel for write operations Read$ = "" ' DevSel for read operations ' ----- Time and date to be set Hour = 10 ' 10:25:30 (AM), 24 hour mode is used Minute = 25 ' Second = 30 ' Weekday = 5 ' Friday (Monday = 1, ..., Sunday = 7) Day = 30 ' 30 Month = 11 ' Nov Year = 07 ' 2007 ' ----- build string for writing time and date to DS1307 I2C$ = Write$ + "<0>" ' DevSel for write + start register addr. Temp = ((Second/10) SHL 4) + MOD(Second, 10) ' BCD format: Bit 6..4 = 10s, Bit 3..0 = 1s I2C$ = I2C$ + CHR$(Temp) ' add seconds to string (reg. addr. 00h) Temp = ((Minute/10) SHL 4) + MOD(Minute, 10) ' BCD format: Bit 6..4 = 10m, Bit 3..0 = 1m I2C$ = I2C$ + CHR$(Temp) ' add minutes to string (reg. addr. 01h) Temp = ((Hour/10) SHL 4) + MOD(Hour, 10) ' BCD format: Bit 5..4 = 10h, Bit 3..0 = 1h I2C$ = I2C$ + CHR$(Temp) ' add hours to string (reg. addr. 02h) I2C$ = I2C$ + CHR$(Weekday) ' add weekdays to string (reg. addr. 03h) Temp = ((Day/10) SHL 4) + MOD(Day, 10) ' BCD format: Bit 5..4 = 10day, Bit 3..0 = 1day I2C$ = I2C$ + CHR$(Temp) ' add days to string (reg. addr. 04h) Temp = ((Month/10) SHL 4) + MOD(Month, 10) ' BCD format: Bit 4 = 10month, Bit 3..0 = 1month I2C$ = I2C$ + CHR$(Temp) ' add months to string (reg. addr. 05h) Temp = ((Year/10) SHL 4) + MOD(Year, 10) ' BCD format: Bit 7..4 = 10year, Bit 3..0 = 1year I2C$ = I2C$ + CHR$(Temp) ' add years to string (reg. addr. 06h) I2C$ = I2C$ + "<0>" ' add control register (reg. addr. 07h): ' no output (low level) on SQW/OUT pin #IFDEF SET_DS1307 ' ----- writing time and date (first 8 registers) I2CL_START (0) ' set START condition on bus FLG = I2CL_WRITE (I2C$) ' write slave addr. for writing, pointer and 8 databytes I2CL_STOP (0) ' set STOP condition on bus #ENDIF ' ----- reading back time and date (first 7 registers) Result$ = "" ' initialize target string oldResult$ = "" ' initialize compare string FOR EVER = 0 TO 0 STEP 0 ' -------------- endless loop -------------> WHILE Result$ = OldResult$ ' read until new result (time changed) I2C$ = Write$ + "<0>" ' DevSel for write + start register addr. I2CL_START (0) ' set START condition on bus FLG = I2CL_WRITE (I2C$) ' write slave addr. for writing and pointer I2CL_START (0) ' set repeated START condition on bus FLG = I2CL_WRITE (Read$) ' write slave address for reading Result$ = I2CL_READ$ (7) ' READ 7 bytes from I2C-Bus (registers 0 to 6) I2CL_STOP (0) ' set STOP condition on bus ENDWHILE ' OldResult$ = Result$ ' copy to compare string ' ----- evaluate string for displaying time and date on LCD Temp = ASC(MID$(Result$,0,1)) ' Extract value of reg. addr. 00h (seconds) Second = 10*(Temp SHR 4) + (Temp BITAND 15) ' BCD format: Bit 6..4 = 10s, Bit 3..0 = 1s Temp = ASC(MID$(Result$,1,1)) ' Extract value of reg. addr. 01h (minutes) Minute = 10*(Temp SHR 4) + (Temp BITAND 15) ' BCD format: Bit 6..4 = 10m, Bit 3..0 = 1m Temp = ASC(MID$(Result$,2,1)) ' Extract value of reg. addr. 02h (hours) IF (Temp BITAND 64) = 0 THEN ' if 24 hour format Hour = 10*(Temp SHR 4) + (Temp BITAND 15) ' BCD format: Bit 5..4 = 10h, Bit 3..0 = 1h ELSE ' if 12 hour format Hour = 10*(Temp BITAND 16) + (Temp BITAND 15) ' BCD format: Bit 4 = 10h, Bit 3..0 = 1h IF (Temp BITAND 32) = 0 THEN ' if AM (ante meridiem) Hour = MODULO_INC(Hour,0,11,0) ' if hour=12 then hour=0 (12 AM = 0 in 24h) ELSE ' if PM (post medidiem) Hour = MODULO_INC(Hour,12,23,12) ' add 12 for PM, if hour=24 then hour=12 ENDIF ' (12 PM = 12 in 24h) ENDIF ' Weekday = ASC(MID$(Result$,3,1)) ' extract value of reg. addr. 03h (weekday) Temp = ASC(MID$(Result$,4,1)) ' extract value of reg. addr. 04h (days) Day = 10*(Temp SHR 4) + (Temp BITAND 15) ' BCD format: Bit 5..4 = 10day, Bit 3..0 = 1day Temp = ASC(MID$(Result$,5,1)) ' extract value of reg. addr. 05h (months) Month = 10*(Temp SHR 4) + (Temp BITAND 15) ' BCD format: Bit 4 = 10month, Bit 3..0 = 1month Temp = ASC(MID$(Result$,6,1)) ' extract value of reg. addr. 06h (years) Year = 10*(Temp SHR 4) + (Temp BITAND 15) ' BCD format: Bit 7..4 = 10year, Bit 3..0 = 1year ' ----- output receive string and converted time and date USING "UH<2><2> 0.0.0.0.2" ' display bytes in HEX PRINT #LCD, "<0>"; ' cursor home FOR A = 0 TO 6 ' loop over all read bytes PRINT_USING #LCD, ASC(MID$(Result$,A,1)); " "; ' output byte value in HEX NEXT ' USING "UD<2><2>0 0.0.0.0.2" ' display numbers two digits each PRINT_USING #LCD, "<13>Date: ";Day;"-";Month;"-";Year ' output date PRINT_USING #LCD, "Time: ";Hour;":";Minute;":";Second ' output time PRINT #LCD, "Weekday (1=Mo):";Weekday ' output weekday NEXT ' <------------- endless loop -------------- END ' end of task MAIN