|
|
|
|
|
|
|
|
|
|
| Sourcecode |
|
|
|
'------------------------------------------------------
$regfile = "m8def.dat"
'$lib "mcsbyte.lbx" ' use the byte lib since we do not need longs
'$crystal = 4000000
$crystal = 3579545
'$baud = 19200
'declare used subs
Declare Sub Writebcdtime(byval S1 As Byte , Byval M1 As Byte , Byval H1 As Byte , Byval D1 As Byte , Byval Month1 As Byte)
Declare Sub Writetime
Declare Sub Gettime
Declare Sub Normalzeitzeigen
Declare Sub Stundensignal
Declare Sub Zeitstellen
Declare Sub Dezimalzeit
Soundpin Alias Pinb.0
'Declare variables
Dim Tm(5) As Byte At &H60
'These are pointers to tm() for simple handling.
Dim S As Byte At &H60 Overlay
Dim M As Byte At &H61 Overlay
Dim H As Byte At &H62 Overlay
Dim D As Byte At &H63 Overlay
Dim Month As Byte At &H64 Overlay
Dim I As Byte , Temp As Byte
Dim Str_char As String * 2
Dim D_stunde As Byte
Dim D_minute As Byte
Dim D_sekunde As Byte
Dim Sekunden As Long
Dim Cache As Long
Dim Bweekday As Byte , Strweekday As String * 2
Dim Strdate As String * 8
Config Date = Dmy , Separator = . ' ANSI-Format
Config Clock = User ' we use I2C for the clock
Timesetpin Alias Pinb.1
Config Timesetpin = Input
'configure LCD-Display
Config Lcd = 24 * 2
Config Lcdpin = Pin , Db4 = Portc.0 , Db5 = Portc.1 , Db6 = Portc.2 , Db7 = Portc.3 , E = Portc.5 , Rs = Portc.4
'configure the used port pin for I2C
Config I2cdelay = 5 ' default slow mode
Config Scl = Portd.6 'Configure i2c SCL
Config Sda = Portd.7 'Configure i2c SDA
' not needed since the pins are in the right state
'I2cinit
'Call Settime(ss , mm ,hh , DD , MM) 'set time
Call Gettime
If H = 0 Then
If S = 0 Then
Call Writebcdtime(50 , 01 , 23 , 17 , 12) 'set time
End If
End If
Config Int1 = Rising
Enable Interrupts
Enable Int1 'enable the interrupt
On Int1 Getclock Nosave 'jump to getclock on INT0
Cls 'clear screen
Cursor Off
Do ' endless loop
Idle ' Energie sparen
Loop
End
' ______________________ Ende Hauptprogramm _________________________________
Getclock:
Call Zeitstellen
Call Gettime
Call Normalzeitzeigen
Call Stundensignal
Call Dezimalzeit
Return 'generates a RETI because it is the first RETURN
Return 'generates a RET because it is the second RETURN
Sub Dezimalzeit
' Vergangene Sekunden des Tages
Str_char = Hex(s)
Sekunden = Val(str_char)
Str_char = Hex(m)
Cache = Val(str_char)
Cache = Cache * 60
Sekunden = Sekunden + Cache
Str_char = Hex(h)
Cache = Val(str_char)
Cache = Cache * 3600
Sekunden = Sekunden + Cache
' Umrechnen ins Dezimalsystem
Sekunden = Sekunden * 625 ' Dezimaltag hat 100*100*100 Sekunden 10000=2*2*2*2 *5*5*5*5
Sekunden = Sekunden / 54 ' ein Tag hat 86400 Sekunden 864=2*2*2*2*2*2*3*3*3
Cache = Sekunden / 10000
D_stunde = Cache
If D_stunde < 10 Then
Lcd " "
End If
Lcd D_stunde ; ":";
Cache = Cache * 10000
Sekunden = Sekunden - Cache
Cache = Sekunden / 100
D_minute = Cache
If D_minute < 10 Then
Lcd "0"
End If
Lcd D_minute ; ":";
Cache = Cache * 100
D_sekunde = Sekunden - Cache
If D_sekunde < 10 Then
Lcd "0"
End If
Lcd D_sekunde ; " "
End Sub
Sub Zeitstellen
If Timesetpin = 1 Then
Sound Soundpin , 20 , 40
If S > 31 Then
Incr M
If M > 59 Then
M = 0
Incr H
End If
End If
S = 0
Call Writetime
End If
End Sub
Sub Normalzeitzeigen()
Locate 1 , 1
Lcd "Dezimaluhr "
Strdate = Hex(d) + "." + Hex(month) + ".06"
Bweekday = Dayofweek(strdate)
Strweekday = Lookupstr(bweekday , Weekdays)
Locate 2 , 1
Lcd Strweekday ; " " ; Strdate ; " " ; Hex(h) ; ":" ; Hex(m) ; ":" ; Hex(s)
Locate 1 , 13
End Sub
Sub Stundensignal
If S = 0 Then
If M = 0 Then
Sound Soundpin , 20 , 40
End If
End If
End Sub
Sub Gettime()
'there are 2 ways to get the time. With low level i2c calls or with a high level call
'first the high level call
Tm(1) = 2 ' point to second register
I2creceive &HA0 , Tm(1) , 1 , 5 ' write the second address and get 5 bytes back
'i2creceive will first write 1 byte from tm(1) which is 2, and then will read 5 bytes and store it onto tm(1)-tm(5)
'and optional with low level calls
' For I = 1 To 5
' Temp = I + 1
' I2cstart
' I2cwbyte &HA0 'write addres of PCF8583
' I2cwbyte Temp 'select register
' I2cstart 'repeated start
' I2cwbyte &HA1 'write address for reading info
' I2crbyte Tm(i) , Nack 'read data
' Next
' I2cstop
End Sub
Sub Writebcdtime(s1 As Byte , M1 As Byte , H1 As Byte , D1 As Byte , Month1 As Byte)
'values are stored as BCD values so convert the values first
Tm(1) = Makebcd(s1) 'seconds
Tm(2) = Makebcd(m1) 'minutes
Tm(3) = Makebcd(h1) 'hours
Tm(4) = Makebcd(d1) 'days
Tm(5) = Makebcd(month1) 'months
Call Writetime
End Sub
Sub Writetime
I2cstart 'generate start
I2cwbyte &HA0 'write address
I2cwbyte 0 'select control register
I2cwbyte 8 'set year and day bit for masking
I2cstart 'repeated start
I2cwbyte &HA0 'write mode
I2cwbyte 2 'select seconds Register
For I = 1 To 5
I2cwbyte Tm(i)
Next 'write seconds
I2cstop
End Sub
Weekdays:
Data "Mo" , "Di" , "Mi" , "Do" , "Fr" , "Sa" , "So"
|
|
|
|
|
|
|
|