Hier der komplette code:
Code:
$regfile "m16def.dat" ' use the byte lib since we do not need longs
$crystal = 20000000
$baud = 19200
'declare used subs
Declare Sub Settime
Declare Sub Gettime()
Declare Sub Schreibe
Declare Sub Lese
Declare Sub Löschen
'Declare variables
Dim Tm(5) As Byte At &H60
Dim I As Byte , Temp As Byte
'These are pointers to tm() for simple handling.
Dim S As Byte At &H60 Overlay
Dim M As Byte At &H61 Overlay
Dim H1 As Byte At &H62 Overlay
Dim D As Byte At &H63 Overlay
Dim Month As Byte At &H64 Overlay
Dim X As Byte
Dim Adrcount As Word
Const Ff = &H0C
Const Lf = &H0A
Const Pu = &H2E
Const Dp = &H3A
Const Le = &H20
Const Ko = &H2C
Const Adr = &HA0
'Time Setup
Dim A As Byte
Dim S1 As Byte
Dim Tag As Byte
Dim Monat As Byte
Dim Stunde As Byte
Dim Minute As Byte
Dim B As Word
Dim C As Integer
'configure the used port pin for I2C
Config I2cdelay = 5 ' default slow mode
Config Sda = Portc.1
Config Scl = Portc.0
'Display Setup
Config Lcdpin = Pin , Db4 = Porta.6 , Db5 = Porta.5 , Db6 = Porta.4 , Db7 = Porta.3 , E = Porta.7 , Rs = Porta.2 ' Natürlich so wie es wirklich angeschlossen ist (4-Bit-Modus)
Config Lcd = 20 * 4
Cls
Lcd "test"
' not needed since the pins are in the right state
'I2cinit
Print Chr(27) ; "[2J"; 'clear screen
Print "PCF8583 Clock Sample"
Do
Call Gettime
'since the values are stored in BCD format we can use Hex() to display them
Print Chr(27) ; "[2;2f"; ' VT100 emulation set pos to 2,2
Print Hex(h1) ; ":" ; Hex(m) ; ":" ; Hex(s) ; " Err:" ; Err
Wait 1
If Pind.2 = 1 Then
Cls
Print "Schreibe"
Locate 1 , 1
Lcd "Schreibe"
Call Schreibe
Print "Schreiben beendet ERR" ; Err
Locate 2 , 1
Lcd "Schreiben beendet ERR" ; Err
End If
If Pind.4 = 1 Then
Cls
Print "Lese"
Locate 1 , 1
Lcd "Lese"
Call Lese
Locate 2 , 1
Lcd Str(x)
Locate 3 , 1
Lcd "Lesen beendet ERR" ; Err
Print "Lesen beendet ERR" ; Err
End If
If Usr.rxc = 1 Then 'Wenn Byte empfangen...
I = Udr 'Byte aus UART auslesen
Select Case I
Case "l"
Call Löschen
End Select
End If
Loop
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
End Sub
Sub Settime '
'values are stored as BCD values so convert the values first
Tm(1) = Makebcd(s) 'seconds
Tm(2) = Makebcd(m) 'minutes
Tm(3) = Makebcd(h1) 'hours
Tm(4) = Makebcd(d) 'days
Tm(5) = Makebcd(month) 'months
I2cinit
I2cstart 'generate start
I2cwbyte &HA0 'write address
I2cwbyte &H00 'select control register
I2cwbyte &H08 '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
Sub Schreibe(byval Adressh As Byte , Adressl As Byte)
Call Gettime
Tag = Makebcd(tm(4))
Monat = Makebcd(tm(5))
Minute = Makebcd(tm(2))
Stunde = Makebcd(tm(3))
S1 = 111
I2cinit
I2cstart
I2cwbyte &HA2
I2cwbyte &HA3 'Speicherstelle im EEPROM
I2cwbyte &HA2
I2cwbyte S1
I2cstop
End Sub
'I2cwbyte D 'Tag
'I2cwbyte Pu
'I2cwbyte Month 'Monat
'I2cwbyte Le
'I2cwbyte H1 'Stunde
'I2cwbyte Dp
'I2cwbyte M 'Minute
'I2cwbyte Dp 'Wechselrichter Status (2.Paketbyte)
'I2cwbyte Ff 'Cursor zurück auf Anfang
'I2cwbyte Lf
Sub Lese
I2cinit
I2cwbyte &HA2
I2cwbyte &HA3 'Speicherstelle im EEPROM
I2cwbyte &HA2
I2cstart
I2cwbyte &HA3
I2crbyte X , Nack
I2cstop
End Sub
Sub Löschen
Disable Interrupts
For B = 0 To 32768 Step 256
I2cstart
I2cwbyte &HA2
I2cwbyte &HA3
I2cwbyte &HA2
For C = 1 To 256
I2cwbyte 255
Next C
I2cstop
Waitms 100
Next B
Enable Interrupts
End Sub
End 'end program
Lesezeichen