'--------------------------------------------------------------
'
' ---=== NAME ===---
'
'--------------------------------------------------------------
'
' Rudimentary clock using a DS1307 RTC and I2C
'
'Note 1....Program is elementry but shows using a DS1307.
' Code is getting convoluted and needs complete rewrite.
' The time setup sort-of works.
'Note 2....Has non synchronous update due to cumulative LCD delays,
' it needs to just update seconds then when the minute value
' changes then update it etc etc....eh!
'Note 3....Many thanks to some German guy for showing how to talk to the DS1307
'Note 4....Using ZS5110 development board...came as a nuts and bolts...great fun building it.
'Note 5....Run out of 4k so I'm stopping - proved a point
'-----------------------------------------------------------------------
'
' ---=== Definitions ===---
'
'-----------------------------------------------------------------------
$regfile = 8052.dat 'Actually I'm using a AT89S052
'but there doesn't seeem to be a DAT file??
$crystal = 11059200 ' Baud rate accurate Xtal
$baud = 2400 'Just thought Hey !!! 2400
$large
Config Lcdpin = Pin , Db4 = P2.4 , Db5 = P2.5 , Db6 = P2.6 , Db7 = P2.7 , E = P1.0 , Rs = P1.2
'4 bit LCD connection with R/W grounded
Config Lcd = 20 * 4 'configure lcd screen
Config Debounce = 30ms 'TRS80s used to use 50mS
'Configure I2c Communications pins on 8052
Config Sda = P3.6
Config Scl = P3.7
Dim Addrs As Byte 'RTC Address *IN* DS1307
Dim _weekday As Byte
Dim _day As Byte
Dim _month As Byte
Dim Month As Byte
Dim _year As Byte
Dim _sec As Byte
Dim _min As Byte
Dim _hour As Byte
Dim Bcd_sec As Byte
Dim Bcd_min As Byte
Dim Bcd_hour As Byte
Addrs = 0 'Use 0 to read/write RTC registers and
' &H08-&H3f to read/write 1307 ram
'------------------------------------------------------------------------
'
'
' ---=== PROGRAM ===---
'
'
'------------------------------------------------------------------------
Begin_main:
Cls
Cursor Off 'Clear Lcd and wait
Wait 1
Do
Gosub Read_time
Debounce P3.2 , 0 , Main_men , Sub 'Button starts Main Menu
Loop
End 'Main Program
'-------------------------------------------------------------
'
'
' ---=== Routines & Subroutines ===---
'
'
'-------------------------------------------------------------
Read_time:
'Set DS1307 to address 0(RTC registers)
I2cstart
I2cwbyte &B11010000 'DS1307 write register address (ie. 8051-->1307)
I2cwbyte Addrs
I2cstop
'Read RTC registers
I2cstart
I2cwbyte &B11010001 'DS read data (ie. DS1307-->8051)
I2crbyte _sec , Ack
I2crbyte _min , Ack
I2crbyte _hour , Ack
I2crbyte _weekday , Ack
I2crbyte _day , Ack
I2crbyte _month , Ack
I2crbyte _year , Nack
I2cstop
'Display it all on 3 lines of my LCD...4th line for menu prompt
Locate 1 , 1
Lcd Bcd(_hour) ; " : " ; Bcd(_min) ; " : " ; Bcd(_sec)
'Print the day name
Locate 2 , 1
Select Case _weekday
Case 1 : Lcd "It is Monday "
Case 2 : Lcd "It is Tuesday "
Case 3 : Lcd "It is Wednesday"
Case 4 : Lcd "It is Thursday "
Case 5 : Lcd "It is Friday "
Case 6 : Lcd "It is Saturday "
Case 7 : Lcd "It is Sunday "
End Select
'Print the date with month name
Locate 3 , 1
Select Case _month
Case &H01 : Lcd Bcd(_day) ; " : Jan : 20" ; Bcd(_year) ; " "
Case &H02 : Lcd Bcd(_day) ; " : Feb : 20" ; Bcd(_year) ; " "
Case &H03 : Lcd Bcd(_day) ; " : March : 20" ; Bcd(_year) ; " "
Case &H04 : Lcd Bcd(_day) ; " : April : 20" ; Bcd(_year) ; " "
Case &H05 : Lcd Bcd(_day) ; " : May : 20" ; Bcd(_year) ; " "
Case &H06 : Lcd Bcd(_day) ; " : June : 20" ; Bcd(_year) ; " "
Case &H07 : Lcd Bcd(_day) ; " : July : 20" ; Bcd(_year) ; " "
Case &H08 : Lcd Bcd(_day) ; " : Aug : 20" ; Bcd(_year) ; " "
Case &H09 : Lcd Bcd(_day) ; " : Sept : 20" ; Bcd(_year) ; " "
Case &H10 : Lcd Bcd(_day) ; " : Oct : 20" ; Bcd(_year) ; " "
Case &H11 : Lcd Bcd(_day) ; " : Nov : 20" ; Bcd(_year) ; " "
Case &H12 : Lcd Bcd(_day) ; " : Dec : 20" ; Bcd(_year) ; " "
End Select
Locate 4 , 1
Lcd "Press P32 for Menu"
Wait 1
Return
'________________________________________________________________
'Clear screen and Present Main Menu and prompt to show or adjust time
Main_men:
Cls
Wait 1
Locate 1 , 1
Lcd " -= Main Menu =-"
Locate 2 , 1
Lcd "Press P33 to Display"
Locate 3 , 1
Lcd "Press P34 to Set"
Wait 1
Men1:
Debounce P3.3 , 0 , Begin_main
Debounce P3.4 , 0 , Go_set
Goto Men1
'_______________________________________________________________
'
' Routines called by Main Menu to prompt and use buttons to
' adjust Seconds, Minutes and Hours
Go_set:
'Without stuffing around with BCD decoding I have just set _hour,_min,_sec
'at midpoint.
_hour = 12
_min = 30
_sec = 30
Adj_sec:
Cls
Wait 1
Locate 1 , 1
Lcd "P32 Minute Menu"
Locate 2 , 1
Lcd "P33 plus seconds"
Locate 3 , 1
Lcd "P34 minus seconds"
Locate 4 , 1
Lcd "Seconds = " ; _sec ; " "
Wait 1
'
Sec_butns:
Debounce P3.2 , 0 , Adj_min
Debounce P3.3 , 0 , Plus_sec
Debounce P3.4 , 0 , Minus_sec
Goto Sec_butns
Adj_min:
Cls
Wait 1
Locate 1 , 1
Lcd "P32 Hour Menu"
Locate 2 , 1
Lcd "P33 plus minutes"
Locate 3 , 1
Lcd "P34 minus minutes"
Wait 1
'
Min_butns:
Debounce P3.2 , 0 , Adj_hr
Debounce P3.3 , 0 , Plus_min
Debounce P3.4 , 0 , Minus_min
Goto Min_butns
Adj_hr:
Cls
Wait 1
Locate 1 , 1
Lcd "P32 Giveup-Goback"
Locate 2 , 1
Lcd "P33 plus hours"
Locate 3 , 1
Lcd "P34 minus hours"
Locate 4 , 1
Lcd "P35 Write to RTC"
Wait 1
'
Hr_btns:
Debounce P3.2 , 0 , Begin_main
Debounce P3.3 , 0 , Plus_hr
Debounce P3.4 , 0 , Minus_hr
Debounce P3.5 , 0 , Do_it_now
Goto Hr_btns
Do_it_now:
Gosub Write_time
Return
'_______________________________________________________________
'
'These routines tweak hours, minutes and seconds Vars and solve modulus problems
'
Plus_sec:
_sec = _sec + 1
If _sec = 61 Then _sec = 0
Goto Adj_sec
Minus_sec:
_sec = _sec - 1
If _sec = 0 Then _sec = 60
Goto Adj_sec
Plus_min:
_min = _min + 1
If _min = 61 Then _min = 0
Goto Adj_min
Minus_min:
_min = _min - 1
If _min = 0 Then _min = 60
Goto Adj_min
Plus_hr:
_hour = _hour + 1
If _hour = 25 Then _hour = 1
Goto Adj_hr
Minus_hr:
_hour = _hour - 1
If _hour = 0 Then _hour = 24
Goto Adj_hr
'_______________________________________________________________
'
' Write Hours, Minutes and Seconds into DS1307 (no date stuff- No room)
Write_time:
'Change to binary coded decimal
Bcd_sec = Makebcd(_sec)
Bcd_min = Makebcd(_min)
Bcd_hour = Makebcd(_hour)
'Write it to DS1307
'Set DS1307 to address 0(RTC registers)
I2cstart ' Generate start code
I2cwbyte &B11010000 ' 1307 write mode
I2cwbyte 0 ' RTC address in 1307
I2cwbyte Bcd_sec ' Write Data to SECONDS
I2cwbyte Bcd_min ' Minutes
I2cwbyte Bcd_hour ' Hours
I2cstop
Return
|