Hey, ik ben al een tijdje aan het stoeien met 1wire-technology chippies. De DS1820 was al gelukt :)

Nu de DS1307 nog.

Ik heb onderstaande code geschreven:

Device 16F628A                                                                   ;PIC
Config INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_ON, LVP_OFF, MCLRE_OFF                 ;Configuratie
ALL_DIGITAL TRUE                                                                 ;Alles digitaal

Symbol SDA PORTa.1                                                               ;Symbolen
Symbol SCL Porta.0                                                               ;Symbolen

Dim RTCYear    as byte                                                           ;Variabel
Dim RTCMonth   as byte                                                           ;Variabel
Dim RTCDate    as byte                                                           ;Variabel
Dim RTCDay     as byte                                                           ;Variabel
Dim RTCHour    as byte                                                           ;Variabel
Dim RTCMinute  as byte                                                           ;Variabel
Dim RTCSecond  as byte                                                           ;Variabel
Dim RTCCtrl    as byte                                                           ;Variabel
Dim TheTime    as Word

RTCYear    = $07                                                                 ;Variabel instellen
RTCMonth   = $09                                                                  ;Variabel instellen
RTCDay     = $16                                                                  ;Variabel instellen
RTCHour    = $13                                                                 ;Variabel instellen
RTCMinute  = $30                                                                   ;Variabel instellen
RTCSecond  = $45                                                                   ;Variabel instellen
RTCCtrl    = $0                                                                  ;Variabel instellen
                                                                     
Gosub settime		                                                                 ;Subroutine settime
Goto start

settime:
 busOut SDA, 0, [RTCYear, RTCMonth, RTCDate, RTCDay, RTCHour, RTCMinute, RTCSecond, RTCCtrl]
return

gettime:
 busIn SDA, 0, [RTCYear, RTCMonth, RTCDate, RTCDay, RTCHour, RTCMinute, RTCSecond, RTCCtrl]
return

DelayMS 500                                                                      ;LCD Opstarten

start:
gosub gettime

	print $fe, 1, hex2 RTCMonth, "/", hex2 RTCDate, "/" , hex2 RTCYear,_
  "  ", hex2 RTCHour, ":", hex2 RTCMinute, ":", hex2 RTCSecond

	delayms 500

goto start

Dit geeft op het LCD weer: 08/08/08 08:08:08
Ik heb eigelijk geen flauw idee hoe ik precies de tijd/datum uit deze RTC kan trekken.

Wie kan mij een duwtje in de rug geven?

Groet, Boeip

Wie kan mij een duwtje in de rug geven?

Het wordt een heel harde zet. Kortgeleden heb ik toevallig code geschreven voor de 16F628 met een DS1307 ;-)


Device = 16F628A

Config INTRC_OSC_NOCLKOUT, CP_OFF, WDT_OFF, PWRTE_ON, LVP_OFF, MCLRE_OFF, BODEN_off 
DelayMS 200
ALL_DIGITAL=true
PORTB_PULLUPS=true

EData 0,0,0,"SUN","MON","TUE","WED","THU","FRI","SAT","JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"

Declare LCD_DTPIN PORTB.0
Declare LCD_RSPIN PORTA.0
Declare LCD_ENPIN PORTA.1
Declare LCD_LINES 2
Cls

Declare SCL_PIN PORTA.2
Declare SDA_PIN PORTA.3

Dim secs As Byte
Dim mins As Byte
Dim hrs As Byte
Dim weekday As Byte
Dim day As Byte
Dim month As Byte
Dim year As Byte
Dim dummy As Byte               ;dummy var for calculations
Dim q As Byte                   ;loopcounter
Dim tablepos As Byte            ; position of the eeprom lookup table
Dim mode As Byte                ;mode 0= normal operation, everything else= setting by hand
Clear                           ;reset all variables
TRISA=0                         ;all outputs. don't leave floating pins anywhere.

Symbol modekey=PORTB.4
Symbol upkey=PORTB.5
Symbol downkey=PORTB.6

;------------- initial start of RTC-oscillator
BusIn %11010001,0,[secs]             ;read seconds + bit7 (=osc. on/off)
DelayMS 10
Clear secs.7                         ; make sure osc. = on
BusOut %11010000,[secs]              ;write back to RTC
;-----------------------
BusIn %11010001,2,[hrs]             ;read hours
Clear hrs.6                         ;select 24 hr clock
BusOut %11010000,2,[hrs]            ;write back to rtc

;-----------------------------


main:

If mode=0 Then                      ;read RTC only if mode=0 (no change by hand)
    BusIn %11010001,0,[secs,mins,hrs,weekday,day,month,year]
    ;don't allow some values to be 0 when starting for the first time.
    If weekday=0 Then weekday=1
    If day=0 Then day=1
    If month=0 Then month=1


    ; because of the fact that each byte in the RTC is organised as a high nibble,
    ; containing the tens and a low nibble containing the singles, we have
    ; to manipulate the data a little. The tens are first masked, so we have
    ; nothing else than that.
    ; After that, we shift that value 4 bits to the righ, and we multiply it
    ; by 10 to get a good value. Then we add the singles to it.

    dummy=secs & %01110000                 ;bitwise AND
    GoSub calc_tens
    secs=secs & %00001111
    secs=secs+dummy
    dummy=mins & %11110000
    GoSub calc_tens
    mins=mins & %00001111
    mins=mins+dummy
    dummy=hrs & %00110000
    GoSub calc_tens
    hrs=hrs & %00001111
    hrs=hrs+dummy
    dummy=day & %11110000
    GoSub calc_tens
    day=day & %00001111
    day=day+dummy
    dummy=month & %11110000
    GoSub calc_tens
    month=month & %00001111
    month=month+dummy
    dummy=year & %11110000
    GoSub calc_tens
    year=year & %00001111
    year=year + dummy
End If
Print At 1,1,DEC2 hrs,":",DEC2 mins,":",DEC2 secs


Print $FE, $C0                      ;go to 2nd LCD-line
For q=0 To 2
    tablepos=weekday*3+q            ; mon, tue etc. have all 3 positons
    dummy=ERead tablepos            ;read 3 chars from eeprom table
    Print dummy
Next
Print At 2,5,DEC2 day," "
For q=0 To 2
    tablepos=month*3+21+q
    dummy=ERead tablepos
    Print dummy
Next
Print " 20",DEC2 year
If modekey=0 Then
    DelayMS 10
    Inc mode
    If mode>7 Then
        mode=0
        GoSub write_to_rtc          ;convert time/date values back to BCD and send to rtc
    End If
    Repeat                          ;wait until modekey is released
    Until modekey=1
End If
;---------- this part of the code is only executed when time/date must be set by hand.
If mode>0 Then                      
    DelayMS 300
    Select Case mode
        Case 1
            Print At 1,1,"  "
            DelayMS 300
            If upkey=0 Then
                DelayMS 10
                Inc hrs
                If hrs>23 Then
                    hrs=0
                End If
            End If
            If downkey=0 Then
                DelayMS 10
                Dec hrs
                If hrs>254 Then            ;in fact: <0
                    hrs=23
                End If
            End If
        Case 2
            Print At 1,4,"  "
            DelayMS 300
            If upkey=0 Then
                DelayMS 10
                Inc mins
                If mins>59 Then
                    mins=0
                End If
            End If
            If downkey=0 Then
                DelayMS 10
                Dec mins
                If mins>254 Then             ;<0
                    mins=59
                End If
            End If
        Case 3
            Print At 1,7,"  "
            DelayMS 300
            If upkey=0 Or downkey=0 Then
                DelayMS 10
                secs=0
            End If
        Case 4
            Print At 2,1,"   "
            DelayMS 300
            If upkey=0 Then
                DelayMS 10
                Inc weekday
                If weekday>7 Then
                    weekday=1
                End If
            End If
            If downkey=0 Then
                DelayMS 10
                Dec weekday
                If weekday<1 Then
                    weekday=7
                End If
            End If
        Case 5
            Print At 2,5,"  "
            DelayMS 300
            If upkey=0 Then
                DelayMS 10
                Inc day
                If day>31 Then
                    day=1
                End If
            End If
            If downkey=0 Then
                DelayMS 10
                Dec day
                If day<1 Then
                    day=31
                End If
            End If
        Case 6
            Print At 2,8,"   "
            DelayMS 300
            If upkey=0 Then
                DelayMS 10
                Inc month
                If month>12 Then
                    month=1
                End If
            End If
            If downkey=0 Then
                DelayMS 10
                Dec month
                If month<1 Then
                    month=12
                End If
            End If
        Case 7
            Print At 2,14,"  "
            DelayMS 300
            If upkey=0 Then
                DelayMS 10
                Inc year
                If year>99 Then
                    year=0
                End If
            End If
            If downkey=0 Then
                DelayMS 10
                Dec year
                If year<1 Then
                    year=99
                End If
            End If
    End Select
End If
;-------------------------------------------------------
GoTo main


;----------------------------------------------------------------------------------------
calc_tens:
dummy=(dummy/16)*10               ;shift value 4 positions to the right & multiply by 10.
Return
;-----------------------------------------------------------------------------------------

write_to_rtc:

;convert time/date values back to BCD and write it to RTC

dummy=secs/10                 ;we only want the 10s here
dummy=dummy*16                ;shift that value to the high nibble
secs=secs//10                 ;for the singles we want the remainder of that division.
secs=secs | dummy             ;Bitwise OR to combine both values in 1 byte

dummy=mins/10
dummy=dummy*16
mins=mins//10
mins=mins | dummy

dummy=hrs/10
dummy=dummy*16
hrs=hrs//10
hrs=hrs | dummy

dummy=day/10
dummy=dummy*16
day=day//10
day=day | dummy

dummy=month/10
dummy=dummy*16
month=month//10
month=month | dummy

dummy=year/10
dummy=dummy*16
year=year//10
year=year | dummy

BusOut %11010000,0,[secs,mins,hrs,weekday,day,month,year]  ;write it al back to RTC


Return