News:

;) This forum is the property of Proton software developers

Main Menu

DS1307

Started by henktimmerman47@gmail.com, Apr 29, 2023, 09:26 PM

Previous topic - Next topic

henktimmerman47@gmail.com

Forum question DS1307
The purpose of the enclosed circuit (Appendix 1) with PIC16F628A and DS1307 is to display the time and date data on an LCD display.
For this I use the DS1307.inc program (attachment 2) and the accompanying Proton program (attachment 3).
At the beginning of the program the DS1307 is set: oscillator enabled, set 24 hour display of control register (8.192 kHz).
The time and date data is then read out in an infinite loop and presented on an LCD screen. The number 20 continuously appears on the LCD screen for each variable, which is obviously not the intention.
Who can advise on this?
DS1307.jpg

$IFNDEF _DS1307_INC_
$DEFINE _DS1307_INC_
'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Routines to access a DS1307 Real Time Clock/Calendar via a software I2C bus
' Written by L.Johnson for the Positron8 Compiler.
'
' Declare the default pins used for I2Cin and I2Cout
'
$IFNDEF DS1307_SCL_Pin
    $DEFINE DS1307_SCL_Pin PORTB.3          ' SCL connected to this pin
    $SENDWARNING "The $define DS1307_SCL_Pin is missing from the main program. Defaulting to using PORTB.3"
$ENDIF
$IFNDEF DS1307_SDA_Pin
    $DEFINE DS1307_SDA_Pin PORTA.5          ' SDA connected to this pin
    $SENDWARNING "The $define DS1307_SDA_Pin is missing from the main program. Defaulting to using PORTA.5"
$ENDIF
'
' Global Time variables
'
    DIM RTC_Second AS BYTE                  ' Second (0..59)
    DIM RTC_Minute AS BYTE                  ' Minute (0..59)
    DIM RTC_Hour   AS BYTE                  ' Hour   (0..11 or 0..23)
'
' Global Date variables
'
    DIM RTC_Day       AS BYTE               ' Date  (0..31)
    DIM RTC_Month     AS BYTE               ' Month (1..12)
    DIM RTC_Year      AS BYTE               ' Year  (0..99)
    DIM RTC_DayOfWeek AS BYTE               ' Day of the week (1..7)
    ;Dim RTC_DayName As String * 9 Heap      ' Holds the name of the day
    ;Dim RTC_MonthName As String * 9 Heap    ' Holds the name of the month
'
' DS1307 register addresses
'
     $DEFINE cRTC_DEV_ADDR  $D0         ' DS1307 device Address
     $DEFINE cRTC_TIME      $00         ' Address for time
     $DEFINE cRTC_WDAY      $03         ' Address for day of week number
     $DEFINE cRTC_DATE      $04         ' Address for date
     $DEFINE cRTC_CONTROL   $07         ' Address for control byte
     $DEFINE cRTC_NVRAM     $08         ' NVRAM start
     $DEFINE cRTC_NVRAM_MAX $38         ' NVRAM address max (56 decimal)
'
' Values - used in calls to DS1307_ReadControl() and DS1307_WriteControl()...
'
     $DEFINE cRTC_SQWE       $10        ' Square Wave enable
     $DEFINE cRTC_SQWF_1HZ   $00        ' Square Wave frequency  1Hz
     $DEFINE cRTC_SQWF_4KHz  $01        ' Square Wave frequency  4.096KHz
     $DEFINE cRTC_SQWF_8KHz  $02        ' Square Wave frequency  8.192KHz
     $DEFINE cRTC_SQWF_32KHz $03        ' Square Wave frequency 32.768KHz
     $DEFINE cRTC_SQWOUT_H   $80        ' Set Square Wave output to high when disabled

$IFNDEF False
    $DEFINE False 0
$ENDIF
$IFNDEF True
    $DEFINE True 1
$ENDIF

'----------------------------------------------------------------------------
' Convert a packed BCD (Binary Coded Decimal) value to a decimal value
' Input     : pValue holds the BCD value to convert to decimal
' Output    : pResult holds the decimal value
' Notes     : None
'
PROC DS1307_BCDToDec(pValue AS BYTE), BYTE
    RESULT = (pValue >> 4) * 10
    RESULT = RESULT + (pValue & $0F)
ENDPROC

'----------------------------------------------------------------------------
' Convert a decimal value to packed BCD (Binary Coded Decimal) value
' Input     : pValue holds the decimal value to convert to BCD
' Output    : pResult holds the packed BCD value
' Notes     : None
'
PROC DS1307_DecToBCD(pValue AS BYTE), BYTE
    RESULT = ((pValue / 10) << 4)
    RESULT = RESULT | (pValue // 10)
ENDPROC

'----------------------------------------------------------------------------
' Read a DS1307 register ($00..$3F)
' Input     : pAddress holds the address of the register
' Output    : pResult holds the byte read from the register
' Notes     : None
'
PROC DS1307_ReadRegister(pAddress AS BYTE), BYTE
    I2CIN DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, pAddress, [RESULT]
ENDPROC

'----------------------------------------------------------------------------
' Write to a DS1307 register ($00..$3F)
' Input     : pAddress holds the address of the register
'           : pData holds the byte to write to the register
' Output    : None
' Notes     : None
'
PROC DS1307_WriteRegister(pAddress AS BYTE, pData AS BYTE)
    I2COUT DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, pAddress,[pData]
ENDPROC

'----------------------------------------------------------------------------
' Read the RTC control byte ($07)
' Input     : None
' Output    : pResult holds the byte read from the register
' Notes     : See the control constants cRTC_SQWXXXX above
'
$DEFINE DS1307_ReadControl() DS1307_ReadRegister(cRTC_CONTROL)

'----------------------------------------------------------------------------
' Write the RTC Control byte ($07)
' Input     : pData holds the byte to write to the register
' Output    : None
' Notes     : See the control constants cRTC_SQWXXXX above
'
$DEFINE DS1307_WriteControl(pData) I2COUT DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_CONTROL,[pData]

'----------------------------------------------------------------------------
' Write a byte to Non-Volatile RAM
' Input     : pAddress holds the Non-Volatile RAM address
'           : pData holds the byte to write to Non-Volatile RAM
' Output    : None
' Notes     : The RAM is physically located at
'           : $08..$3F - this routine uses address $00..$37 (56 bytes)
'
$DEFINE DS1307_WriteByte(pAddress, pData) '
    IF pAddress < cRTC_NVRAM_MAX THEN     '
        DS1307_WriteRegister(pAddress + cRTC_NVRAM, pData) '
    ENDIF

'----------------------------------------------------------------------------
' Read a byte from Non-Volatile RAM
' Input     : pAddress holds the Non-Volatile RAM address
' Output    : Returns holding the byte read from NV RAM
' Notes     : The RAM is physically located at
'           : $08..$3F - this routine uses address $00..$37 (56 bytes)
'
PROC DS1307_ReadByte(pAddress AS BYTE), BYTE
    RESULT = DS1307_ReadRegister(pAddress + cRTC_NVRAM)
ENDPROC

'----------------------------------------------------------------------------
' Read the Time
' Input     : None
' Output    : Variable RTC_Second holds the seconds value (in decimal)
'           : Variable RTC_Minute holds the minutes value (in decimal)
'           : Variable RTC_Hour holds the hours value (in decimal)
' Notes     : None
'
PROC DS1307_ReadTime()
    I2CIN DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_TIME, [RTC_Second, RTC_Minute, RTC_Hour]

    RTC_Second = DS1307_BCDToDec(RTC_Second)       ' Convert the Seconds value to decimal
    RTC_Minute = DS1307_BCDToDec(RTC_Minute)       ' Convert the Minutes value to decimal
    RTC_Hour   = DS1307_BCDToDec(RTC_Hour)         ' Convert the Hours value to decimal
ENDPROC

'----------------------------------------------------------------------------
' Read the Date
' Input     : None
' Output    : Variable RTC_DayOfWeek holds the week day value (in decimal)
'           : Variable RTC_Day holds the days value (in decimal)
'           : Variable RTC_Month holds the months value (in decimal)
'           : Variable RTC_Year holds the years value (in decimal)
' Notes     : None
'
PROC DS1307_ReadDate()
    I2CIN DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_WDAY, [RTC_DayOfWeek, RTC_Day, RTC_Month, RTC_Year]

    RTC_DayOfWeek = DS1307_BCDToDec(RTC_DayOfWeek)  ' Convert the WeekDay value to decimal
    RTC_Day       = DS1307_BCDToDec(RTC_Day)        ' Convert the Days value to decimal
    RTC_Month     = DS1307_BCDToDec(RTC_Month)      ' Convert the Months value to decimal
    RTC_Year      = DS1307_BCDToDec(RTC_Year)       ' Convert the Years value to decimal
ENDPROC

'----------------------------------------------------------------------------
' Set the Time
' Input     : pHour holds the hours value (in decimal)
'           : pMinute holds the minutes value (in decimal)
'           : pSecond holds the seconds value (in decimal)
' Output    : None
' Notes     : None
'
PROC DS1307_WriteTime(pHour AS RTC_Hour, pMinute AS RTC_Minute, pSecond AS RTC_Second)
    RTC_Second = DS1307_DecToBCD(pSecond)           ' Convert the Seconds value to decimal
    RTC_Minute = DS1307_DecToBCD(pMinute)           ' Convert the Minutes value to decimal
    RTC_Hour   = DS1307_DecToBCD(pHour)             ' Convert the Hours value to decimal

    I2COUT DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_TIME, [RTC_Second, RTC_Minute, RTC_Hour]
ENDPROC

'----------------------------------------------------------------------------
' Set the Date
' Input     : pDayOfWeek holds the week day value (in decimal)
'           : pDay holds the days value (in decimal)
'           : pMonth holds the months value (in decimal)
'           : pYear holds the years value (in decimal)
' Output    : None
' Notes     : None
'
PROC DS1307_WriteDate(pDayOfWeek AS RTC_DayOfWeek, pDay AS RTC_Day, pMonth AS RTC_Month, pYear AS RTC_Year)

    RTC_DayOfWeek = DS1307_DecToBCD(pDayOfWeek)     ' Convert the WeekDay value to decimal
    RTC_Day       = DS1307_DecToBCD(pDay)           ' Convert the Days value to decimal
    RTC_Month     = DS1307_DecToBCD(pMonth)         ' Convert the Months value to decimal
    RTC_Year      = DS1307_DecToBCD(pYear)          ' Convert the Years value to decimal

    I2COUT DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_WDAY, [RTC_DayOfWeek, RTC_Day, RTC_Month, RTC_Year]
ENDPROC

'----------------------------------------------------------------------------
' Return the name of the day
' Input     : pDayOfWeek holds the day value 1 to 7 (Sunday to Saturday)
' Output    : RTC_DayName holds a string containing the day's name
' Notes     : None
'
PROC DS1307_Get_DayName(pDayOfWeek AS BYTE), RTC_DayName
    SELECT pDayOfWeek
        CASE 1
            RESULT = "Sunday"
        CASE 2
            RESULT = "Monday"
        CASE 3
            RESULT = "Tuesday"
        CASE 4
            RESULT = "Wednesday"
        CASE 5
            RESULT = "Thursday"
        CASE 6
            RESULT = "Friday"
        CASE 7
            RESULT = "Saturday"
    ENDSELECT
ENDPROC

'----------------------------------------------------------------------------
' Return the name of the month
' Input     : pMonth holds the month value 1 to 12 (January to December)
' Output    : RTC_MonthName holds a string containing the month's name
' Notes     : None
'
PROC DS1307_Get_MonthName(pMonth AS BYTE), RTC_MonthName
    SELECT pMonth
        CASE 1
            RESULT = "January"
        CASE 2
            RESULT = "February"
        CASE 3
            RESULT = "March"
        CASE 4
            RESULT = "April"
        CASE 5
            RESULT = "May"
        CASE 6
            RESULT = "June"
        CASE 7
            RESULT = "July"
        CASE 8
            RESULT = "August"
        CASE 9
            RESULT = "September"
        CASE 10
            RESULT = "October"
        CASE 11
            RESULT = "November"
        CASE 12
            RESULT = "December"
    ENDSELECT
ENDPROC

'----------------------------------------------------------------------------
' Detect the DS1307
' Input     : None
' Output    : Returns holding 1 if the DS1307 is available
' Notes     : Uses the Ack return for confirmation
'           : This procedure assumes that there is only 1 I2C device attached to the bus
'
PROC DS1307_Present(), BIT
     I2CIN DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_TIME, [WREG]
     RESULT = ~SRbits_C
ENDPROC

'----------------------------------------------------------------------------
_DS1307_Main:

$ENDIF  ' _DS1307_INC_






'****************************************************************
'*  Name    : KlokDS1307.BAS                                    *
'*  Author  : Henk Timmerman                                    *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 18-01-2023                                        *
'*  Version : 1.0                                               *
'*  Notes   : PIC 16F628A met klok DS1307                       *
'*          : I2E verbinding, print geheugen H00 t/m H06        *
'****************************************************************
DEVICE  16F628A               ;We gebruiken een 16F628A type
CONFIG  HS_OSC, WDT_Off,PWRTE_ON, LVP_OFF, BODEN_OFF, MCLRE_OFF                 
DECLARE ALL_DIGITAL   = True  ;Allle ingangen digitaal
DECLARE XTAL          = 20    ;Kristal 20 MHz
DECLARE LCD_RSPIN = PORTA.2   ;RS pin van LCD verhuizen naar PortA.2
DECLARE LCD_ENPIN = PORTA.3   ;EN pin van LCD verhuizen naar PortA.3

INCLUDE "C:\PICprogrammas\WeerstationOpdrachtcodes.inc"
INCLUDE "C:\PICprogrammas\DS1307.inc"

;Logische constanten
$DEFINE DS1307_SCL_Pin PORTB.3
$DEFINE DS1307_SDA_Pin PORTA.5

SYMBOL LED        = PORTA.0

;Variabelen declareren
;Word array

;Word

;Byte
DIM Init AS BYTE
DIM Sec AS BYTE
DIM Min AS BYTE
DIM Uur AS BYTE
DIM Dag AS BYTE
DIM Datum AS BYTE
DIM Maand AS BYTE
DIM Jaar AS BYTE
DIM Control AS BYTE

;        76543210
PORTA = %00000001             ;Bij opstarten wordt LED geactiveerd
PORTB = %00000000
TRISA = %11111110             ;A0 uitgang voor LED
TRISB = %11111111             ;USART B2 Moet in rust hoogohmig zijn

DECLARE PORTB_PULLUPS ON
CLEAR
DELAYMS 500
LED = 0                                                                                           
Init = $00

'Register instellen
  DS1307_WriteRegister($00, $00); adres $00 bit7=0, osc. enabled
  DELAYMS 10
  DS1307_WriteRegister($02, $00); adres $02 bit6=0, 24 uur weergave
  DELAYMS 10
  DS1307_WriteRegister($07, $02); adres $07 bit1=1 en bit0=0, SQW=8.192 kHz
  DELAYMS 10
 
'Datum instellen
  DS1307_WriteDate(4, 12, 4, 23); 1-dag van de week (1..7), 2-dag (0..31), 3-maand (1..12), 4-jaar (0..99)
  DELAYMS 10
 
'Tijd instellen
  '
  DS1307_WriteTime(10, 10, 10); Uur (0..23), Minuut (0..59), Seconde (0..59)
  DELAYMS 10
 
WHILE 1=1
'Tijd lezen
  DS1307_ReadTime()           ;RTC_Second (0..59), RTC_Minute (0..59), RTC_Hour (0..23)
  DELAYMS 10
  Uur = RTC_Hour
  Min = RTC_Minute
  Sec = RTC_Second
'Datum lezen
  DS1307_ReadDate()           ;RTC_DayOfWeek (1..7), RTC_Day (0..31), RTC_Month (1..12), RTC_Year (0..99)
  DELAYMS 10
  Datum = RTC_Day
  Maand = RTC_Month
  Jaar = RTC_Year
 
  CLS
  PRINT AT 1,1,DEC2 Uur, " ", DEC2 Min, " ", DEC2 Sec, " "; resultaat 32?
  PRINT AT 2,1,DEC2 Datum, " ", DEC2 Maand, " ", DEC2 Jaar, " "; resultaat 32?
  DELAYMS 1000
WEND


henktimmerman47@gmail.com


'****************************************************************
'*  Name    : KlokDS1307.BAS                                    *
'*  Author  : Henk Timmerman                                    *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 18-01-2023                                        *
'*  Version : 1.0                                               *
'*  Notes   : PIC 16F628A met klok DS1307                       *
'*          : I2E verbinding, print geheugen H00 t/m H06        *
'****************************************************************
DEVICE  16F628A               ;We gebruiken een 16F628A type
CONFIG  HS_OSC, WDT_Off,PWRTE_ON, LVP_OFF, BODEN_OFF, MCLRE_OFF                 
DECLARE ALL_DIGITAL   = True  ;Allle ingangen digitaal
DECLARE XTAL          = 20    ;Kristal 20 MHz
DECLARE LCD_RSPIN = PORTA.2   ;RS pin van LCD verhuizen naar PortA.2
DECLARE LCD_ENPIN = PORTA.3   ;EN pin van LCD verhuizen naar PortA.3

INCLUDE "C:\PICprogrammas\WeerstationOpdrachtcodes.inc"
INCLUDE "C:\PICprogrammas\DS1307.inc"

;Logische constanten
$DEFINE DS1307_SCL_Pin PORTB.3
$DEFINE DS1307_SDA_Pin PORTA.5

SYMBOL LED        = PORTA.0

;Variabelen declareren
;Word array

;Word

;Byte
DIM Init AS BYTE
DIM Sec AS BYTE
DIM Min AS BYTE
DIM Uur AS BYTE
DIM Dag AS BYTE
DIM Datum AS BYTE
DIM Maand AS BYTE
DIM Jaar AS BYTE
DIM Control AS BYTE

;        76543210
PORTA = %00000001             ;Bij opstarten wordt LED geactiveerd
PORTB = %00000000
TRISA = %11111110             ;A0 uitgang voor LED
TRISB = %11111111             ;USART B2 Moet in rust hoogohmig zijn

DECLARE PORTB_PULLUPS ON
CLEAR
DELAYMS 500
LED = 0                                                                                           
Init = $00

'Register instellen
  DS1307_WriteRegister($00, $00); adres $00 bit7=0, osc. enabled
  DELAYMS 10
  DS1307_WriteRegister($02, $00); adres $02 bit6=0, 24 uur weergave
  DELAYMS 10
  DS1307_WriteRegister($07, $02); adres $07 bit1=1 en bit0=0, SQW=8.192 kHz
  DELAYMS 10
 
'Datum instellen
  DS1307_WriteDate(4, 12, 4, 23); 1-dag van de week (1..7), 2-dag (0..31), 3-maand (1..12), 4-jaar (0..99)
  DELAYMS 10
 
'Tijd instellen
  '
  DS1307_WriteTime(10, 10, 10); Uur (0..23), Minuut (0..59), Seconde (0..59)
  DELAYMS 10
 
WHILE 1=1
'Tijd lezen
  DS1307_ReadTime()           ;RTC_Second (0..59), RTC_Minute (0..59), RTC_Hour (0..23)
  DELAYMS 10
  Uur = RTC_Hour
  Min = RTC_Minute
  Sec = RTC_Second
'Datum lezen
  DS1307_ReadDate()           ;RTC_DayOfWeek (1..7), RTC_Day (0..31), RTC_Month (1..12), RTC_Year (0..99)
  DELAYMS 10
  Datum = RTC_Day
  Maand = RTC_Month
  Jaar = RTC_Year
 
  CLS
  PRINT AT 1,1,DEC2 Uur, " ", DEC2 Min, " ", DEC2 Sec, " "; resultaat 32?
  PRINT AT 2,1,DEC2 Datum, " ", DEC2 Maand, " ", DEC2 Jaar, " "; resultaat 32?
  DELAYMS 1000
WEND


RGV250

#2
Hi,
The link to DS1307.inc in your first post is invalid. Anyway, without that I notice you are using PortA.5, I would not use A.5 for anything unless I was desperate.
I would suggest looking at another pin on PortB and also make sure you have set all the fuses etc so any peripheral is not being used. This is not that bad on the device you are using. How about Port A0/A1 as they have the least peripherals and really only nee the analog registers set so they are not used.

It seems the forum adds the link to DS1307.inc as I didn't.

Bob

henktimmerman47@gmail.com

Hi Bob,

I saved the program DS1307 locally in C:\PICprograms\DS1307.inc
I have connected the SCL pin to port A0 and the SDA pin to port A1 (see listing). The result is not good. The presentations for the hours, minutes, seconds, date, month and year remain continuous 02. What is going wrong?

Regards Henk

My program
'****************************************************************
'*  Name    : KlokDS1307.BAS                                    *
'*  Author  : Henk Timmerman                                    *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 18-01-2023                                        *
'*  Version : 1.0                                               *
'*  Notes   : PIC 16F628A met klok DS1307                       *
'*          : I2E verbinding, print geheugen H00 t/m H06        *
'****************************************************************
DEVICE  16F628A               ;We gebruiken een 16F628A type
CONFIG  HS_OSC, WDT_Off,PWRTE_ON, LVP_OFF, BODEN_OFF, MCLRE_OFF                 
DECLARE ALL_DIGITAL   = True  ;Allle ingangen digitaal
DECLARE XTAL          = 20    ;Kristal 20 MHz
DECLARE LCD_RSPIN = PORTA.2   ;RS pin van LCD verhuizen naar PortA.2
DECLARE LCD_ENPIN = PORTA.3   ;EN pin van LCD verhuizen naar PortA.3

INCLUDE "C:\PICprogrammas\DS1307.inc"

;Logische constanten
$DEFINE DS1307_SCL_Pin PORTA.0 ;clock signaal voor DS1307
$DEFINE DS1307_SDA_Pin PORTA.1 ;data signaal voor DS1307

SYMBOL LED        = PORTB.3   ;led licht na opstarten 0,5 sec op

;Variabelen declareren
;Word array

;Word

;Byte
DIM Sec AS BYTE
DIM Min AS BYTE
DIM Uur AS BYTE
DIM Dag AS BYTE
DIM Datum AS BYTE
DIM Maand AS BYTE
DIM Jaar AS BYTE
DIM Control AS BYTE

;        76543210
PORTA = %00000000             
PORTB = %00001000             ;Bij opstarten wordt LED geactiveerd
TRISA = %11111111             
TRISB = %11110111             ;B3 is output voor LED

DECLARE PORTB_PULLUPS ON
CLEAR
DELAYMS 500
LED = 0                                                                                           

'Register instellen
  DS1307_WriteRegister($00, %10000000); adres $00 bit7=0, osc. enabled
  DELAYMS 10
  DS1307_WriteRegister($02, %00000000); adres $02 bit6=0, 24 uur weergave
  DELAYMS 10
  DS1307_WriteRegister($07, %00000010); adres $07 bit1=1 en bit0=0, SQW=8.192 kHz
  DELAYMS 10
 
'Datum instellen
  DS1307_WriteDate(4, 12, 4, 23); 1-dag van de week (1..7), 2-dag (0..31), 3-maand (1..12), 4-jaar (0..99)
  DELAYMS 10
 
'Tijd instellen
  '
  DS1307_WriteTime(10, 10, 10); Uur (0..23), Minuut (0..59), Seconde (0..59)
  DELAYMS 10
 
WHILE 1=1
'Tijd lezen
  DS1307_ReadTime()           ;RTC_Second (0..59), RTC_Minute (0..59), RTC_Hour (0..23)
  DELAYMS 10
  Uur = RTC_Hour
  Min = RTC_Minute
  Sec = RTC_Second
'Datum lezen
  DS1307_ReadDate()           ;RTC_DayOfWeek (1..7), RTC_Day (0..31), RTC_Month (1..12), RTC_Year (0..99)
  DELAYMS 10
  Datum = RTC_Day
  Maand = RTC_Month
  Jaar = RTC_Year
 
  CLS
  PRINT AT 1,1,DEC2 Uur, " ", DEC2 Min, " ", DEC2 Sec, " "; resultaat 32?
  PRINT AT 2,1,DEC2 Datum, " ", DEC2 Maand, " ", DEC2 Jaar, " "; resultaat 32?
  DELAYMS 1000
WEND

AlbertoFS

#4
Hi Henk,
I take a look few minutes only...
The first line is bad because you stop the clok of the DS1307.
 DS1307_WriteRegister($00, %10000000); adres $00 bit7=0, osc. disabled
Bit7 = 0 => always clock ON.
 DS1307_WriteRegister($00, %00000000); adres $00 bit7=0, osc. enabled
It is not necessary to write it.

The second line is not necessary, I suppose because the calendar runs always at 24H with 0.
I don't know what the library you are using. Do this library uses the 2 formats?

The third line is bad because the oscillator out is OFF.
DS1307_WriteRegister($07, %00000010); adres $07 bit1=1 en bit0=0, SQW=8.192 kHz
Must to be:
DS1307_WriteRegister($07, %10010010); adres $07 bit1=1 en bit0=0, SQW=8.192 kHz   OK

Try to use my new library I posted.
I just realized that I have not taken into account the 2 12/24H formats. I'll see if I can modify it.
My library use the 24H format only.




73's de EA3AGV

Pepe

#5
I attach a demonstration in proteus of the watch

AlbertoFS

#6
Hi Henk,
I tried to run your code with Proteus, but I have it for PIC18.
I can see:
The LCD definitions are not fully declared.
The addresses of the SDA/SCL pins when using $Define must be written before the Include "DS1307.inc"
Thank you Pepe for the files.
I can try it with this library.
To enable and config the osc the config must be included the osc ON.
DS1307_WriteRegister($07, %10010010) ' For 8KHz

There is an error in the declare constants in the library to configure the oscillator out.
Deleting  the 2 first lines and corrected the declare pins, your code is running in Proteus.
Alberto
73's de EA3AGV

AlbertoFS

Hi Henk,
I have just updated my DS1307 library for 12H/24H formats, incorporating 3 different new libraries. I encourage you to consult them.

DS1307
73's de EA3AGV

henktimmerman47@gmail.com

Hi Alfredo,

I saved the program DS1307 locally in C:PICprogramsDS1307.inc
I have incorporated your comments into the program below. I also modified the hardware (see attachment). The presentations for the hours, minutes, seconds, date, month and year remain continuous 02. What is going wrong?

Regards Henk

My program
'****************************************************************
'*  Name    : KlokDS1307.BAS                                    *
'*  Author  : Henk Timmerman                                    *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                              *
'*  Date    : 18-01-2023                                        *
'*  Version : 1.0                                              *
'*  Notes  : PIC 16F628A met klok DS1307 (Alberto)            *
'*          : I2E verbinding, print geheugen H00 t/m H06        *
'****************************************************************
DEVICE  16F628A              ;We gebruiken een 16F628A type
CONFIG  HS_OSC, WDT_Off,PWRTE_ON, LVP_OFF, BODEN_OFF, MCLRE_OFF               
DECLARE ALL_DIGITAL  = True  ;Allle ingangen digitaal
DECLARE XTAL          = 20    ;Kristal 20 MHz

    DECLARE LCD_DTPIN = PORTB.4        ' LCD's Data lines (D4 to D7)
    DECLARE LCD_ENPIN = PORTA.3        ' LCD's EN line
    DECLARE LCD_RSPIN = PORTA.2        ' LCD's RS line
    DECLARE LCD_INTERFACE = 4          ' 4-bit interface to LCD
    DECLARE LCD_LINES = 2              ' LCD contains 2 lines
    DECLARE LCD_DATAUS = 50            ' Time to wait after print a data To LCD
    DECLARE LCD_COMMANDUS = 2000        ' Time to wait after print a command to LCD
    DECLARE LCD_TYPE = ALPHANUMERIC    ' LCD type is alphanumeric

'/-------------------------------------------------------------------------
;Logische constanten
$DEFINE DS1307_SCL_Pin PORTA.0 ;clock signaal voor DS1307
$DEFINE DS1307_SDA_Pin PORTA.1 ;data signaal voor DS1307

INCLUDE "C:PICprogrammasDS1307.inc"


SYMBOL LED = PORTB.3  ;led licht na opstarten 0,5 sec op

;Variabelen declareren
;Word array

;Word

;Byte
DIM Sec AS BYTE
DIM Min AS BYTE
DIM Uur AS BYTE
DIM Dag AS BYTE
DIM Datum AS BYTE
DIM Maand AS BYTE
DIM Jaar AS BYTE
DIM Control AS BYTE

;        76543210
PORTA = %00000000           
PORTB = %00001000            ;Bij opstarten wordt LED geactiveerd
TRISA = %11111111           
TRISB = %11110111            ;B3 is output voor LED

DECLARE PORTB_PULLUPS ON
CLEAR
DELAYMS 500
LED = 0                                                                                         

'Register instellen
  DS1307_WriteRegister($07, %10010010); adres $07 bit1=1 en bit0=0, SQW=8.192 kHz  OK
  DELAYMS 10



'Datum instellen
  DS1307_WriteDate(4, 12, 4, 23); 1-dag van de week (1..7), 2-dag (0..31), 3-maand (1..12), 4-jaar (0..99)
  DELAYMS 10

'Tijd instellen
  '
  DS1307_WriteTime(10, 10, 10); Uur (0..23), Minuut (0..59), Seconde (0..59)
  DELAYMS 10

WHILE 1=1
'Tijd lezen
  DS1307_ReadTime()          ;RTC_Second (0..59), RTC_Minute (0..59), RTC_Hour (0..23)
  DELAYMS 10
  Uur = RTC_Hour
  Min = RTC_Minute
  Sec = RTC_Second
'Datum lezen
  DS1307_ReadDate()          ;RTC_DayOfWeek (1..7), RTC_Day (0..31), RTC_Month (1..12), RTC_Year (0..99)
  DELAYMS 10
  Datum = RTC_Day
  Maand = RTC_Month
  Jaar = RTC_Year

  CLS
  PRINT AT 1,1,DEC2 Uur, " ", DEC2 Min, " ", DEC2 Sec, " "; resultaat 32?
  PRINT AT 2,1,DEC2 Datum, " ", DEC2 Maand, " ", DEC2 Jaar, " "; resultaat 32?
  DELAYMS 1000
WEND

C:PICprogramsDS1307.inc


$IFNDEF _DS1307_INC_
$DEFINE _DS1307_INC_
'
'  /\\\\
'  /\///////\
'  /\    /\                                                /\          /\
'  /\\\\\/        /\\    /\\\\\    /\\\\  /\\\\\  /\\\\\  /\\\\
'    /\//////\      /\///\  /\//////    /\/////\ ////\////  ////\////  ////////\
'    /\    //\    /\  //\ /\\\\\  /\\\\\    /\        /\        /\\\\\
'      /\    //\  //\  /\  ////////\ //\///////      /\ /\    /\ /\  /\/////\
'      /\      //\  ///\\/    /\\\\\  //\\\\\    //\\      //\\  //\\\\/\
'        ///        ///    /////    //////////    //////////      /////        /////    //////////
'                                  Let's find out together what makes a PIC Tick!
'
' Routines to access a DS1307 Real Time Clock/Calendar via a software I2C bus
' Written by L.Johnson for the Positron8 Compiler.
'
' Declare the default pins used for I2Cin and I2Cout
'
$IFNDEF DS1307_SCL_Pin
    $DEFINE DS1307_SCL_Pin PORTA.0          ' SCL connected to this pin
    $SENDWARNING "The $define DS1307_SCL_Pin is missing from the main program. Defaulting to using PORTB.3"
$ENDIF
$IFNDEF DS1307_SDA_Pin
    $DEFINE DS1307_SDA_Pin PORTA.1          ' SDA connected to this pin
    $SENDWARNING "The $define DS1307_SDA_Pin is missing from the main program. Defaulting to using PORTA.5"
$ENDIF
'
' Global Time variables
'
    DIM RTC_Second AS BYTE                  ' Second (0..59)
    DIM RTC_Minute AS BYTE                  ' Minute (0..59)
    DIM RTC_Hour  AS BYTE                  ' Hour  (0..11 or 0..23)
'
' Global Date variables
'
    DIM RTC_Day      AS BYTE              ' Date  (0..31)
    DIM RTC_Month    AS BYTE              ' Month (1..12)
    DIM RTC_Year      AS BYTE              ' Year  (0..99)
    DIM RTC_DayOfWeek AS BYTE              ' Day of the week (1..7)
    ;Dim RTC_DayName As String * 9 Heap      ' Holds the name of the day
    ;Dim RTC_MonthName As String * 9 Heap    ' Holds the name of the month
'
' DS1307 register addresses
'
    $DEFINE cRTC_DEV_ADDR  $D0        ' DS1307 device Address
    $DEFINE cRTC_TIME      $00        ' Address for time
    $DEFINE cRTC_WDAY      $03        ' Address for day of week number
    $DEFINE cRTC_DATE      $04        ' Address for date
    $DEFINE cRTC_CONTROL  $07        ' Address for control byte
    $DEFINE cRTC_NVRAM    $08        ' NVRAM start
    $DEFINE cRTC_NVRAM_MAX $38        ' NVRAM address max (56 decimal)
'
' Values - used in calls to DS1307_ReadControl() and DS1307_WriteControl()...
'
    $DEFINE cRTC_SQWE      $10        ' Square Wave enable
    $DEFINE cRTC_SQWF_1HZ  $00        ' Square Wave frequency  1Hz
    $DEFINE cRTC_SQWF_4KHz  $01        ' Square Wave frequency  4.096KHz
    $DEFINE cRTC_SQWF_8KHz  $02        ' Square Wave frequency  8.192KHz
    $DEFINE cRTC_SQWF_32KHz $03        ' Square Wave frequency 32.768KHz
    $DEFINE cRTC_SQWOUT_H  $80        ' Set Square Wave output to high when disabled

$IFNDEF False
    $DEFINE False 0
$ENDIF
$IFNDEF True
    $DEFINE True 1
$ENDIF

'----------------------------------------------------------------------------
' Convert a packed BCD (Binary Coded Decimal) value to a decimal value
' Input    : pValue holds the BCD value to convert to decimal
' Output    : pResult holds the decimal value
' Notes    : None
'
PROC DS1307_BCDToDec(pValue AS BYTE), BYTE
    RESULT = (pValue >> 4) * 10
    RESULT = RESULT + (pValue & $0F)
ENDPROC

'----------------------------------------------------------------------------
' Convert a decimal value to packed BCD (Binary Coded Decimal) value
' Input    : pValue holds the decimal value to convert to BCD
' Output    : pResult holds the packed BCD value
' Notes    : None
'
PROC DS1307_DecToBCD(pValue AS BYTE), BYTE
    RESULT = ((pValue / 10) << 4)
    RESULT = RESULT | (pValue // 10)
ENDPROC

'----------------------------------------------------------------------------
' Read a DS1307 register ($00..$3F)
' Input    : pAddress holds the address of the register
' Output    : pResult holds the byte read from the register
' Notes    : None
'
PROC DS1307_ReadRegister(pAddress AS BYTE), BYTE
    I2CIN DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, pAddress, [RESULT]
ENDPROC

'----------------------------------------------------------------------------
' Write to a DS1307 register ($00..$3F)
' Input    : pAddress holds the address of the register
'          : pData holds the byte to write to the register
' Output    : None
' Notes    : None
'
PROC DS1307_WriteRegister(pAddress AS BYTE, pData AS BYTE)
    I2COUT DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, pAddress,[pData]
ENDPROC

'----------------------------------------------------------------------------
' Read the RTC control byte ($07)
' Input    : None
' Output    : pResult holds the byte read from the register
' Notes    : See the control constants cRTC_SQWXXXX above
'
$DEFINE DS1307_ReadControl() DS1307_ReadRegister(cRTC_CONTROL)

'----------------------------------------------------------------------------
' Write the RTC Control byte ($07)
' Input    : pData holds the byte to write to the register
' Output    : None
' Notes    : See the control constants cRTC_SQWXXXX above
'
$DEFINE DS1307_WriteControl(pData) I2COUT DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_CONTROL,[pData]

'----------------------------------------------------------------------------
' Write a byte to Non-Volatile RAM
' Input    : pAddress holds the Non-Volatile RAM address
'          : pData holds the byte to write to Non-Volatile RAM
' Output    : None
' Notes    : The RAM is physically located at
'          : $08..$3F - this routine uses address $00..$37 (56 bytes)
'
$DEFINE DS1307_WriteByte(pAddress, pData) '
    IF pAddress < cRTC_NVRAM_MAX THEN    '
        DS1307_WriteRegister(pAddress + cRTC_NVRAM, pData) '
    ENDIF

'----------------------------------------------------------------------------
' Read a byte from Non-Volatile RAM
' Input    : pAddress holds the Non-Volatile RAM address
' Output    : Returns holding the byte read from NV RAM
' Notes    : The RAM is physically located at
'          : $08..$3F - this routine uses address $00..$37 (56 bytes)
'
PROC DS1307_ReadByte(pAddress AS BYTE), BYTE
    RESULT = DS1307_ReadRegister(pAddress + cRTC_NVRAM)
ENDPROC

'----------------------------------------------------------------------------
' Read the Time
' Input    : None
' Output    : Variable RTC_Second holds the seconds value (in decimal)
'          : Variable RTC_Minute holds the minutes value (in decimal)
'          : Variable RTC_Hour holds the hours value (in decimal)
' Notes    : None
'
PROC DS1307_ReadTime()
    I2CIN DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_TIME, [RTC_Second, RTC_Minute, RTC_Hour]

    RTC_Second = DS1307_BCDToDec(RTC_Second)      ' Convert the Seconds value to decimal
    RTC_Minute = DS1307_BCDToDec(RTC_Minute)      ' Convert the Minutes value to decimal
    RTC_Hour  = DS1307_BCDToDec(RTC_Hour)        ' Convert the Hours value to decimal
ENDPROC

'----------------------------------------------------------------------------
' Read the Date
' Input    : None
' Output    : Variable RTC_DayOfWeek holds the week day value (in decimal)
'          : Variable RTC_Day holds the days value (in decimal)
'          : Variable RTC_Month holds the months value (in decimal)
'          : Variable RTC_Year holds the years value (in decimal)
' Notes    : None
'
PROC DS1307_ReadDate()
    I2CIN DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_WDAY, [RTC_DayOfWeek, RTC_Day, RTC_Month, RTC_Year]

    RTC_DayOfWeek = DS1307_BCDToDec(RTC_DayOfWeek)  ' Convert the WeekDay value to decimal
    RTC_Day      = DS1307_BCDToDec(RTC_Day)        ' Convert the Days value to decimal
    RTC_Month    = DS1307_BCDToDec(RTC_Month)      ' Convert the Months value to decimal
    RTC_Year      = DS1307_BCDToDec(RTC_Year)      ' Convert the Years value to decimal
ENDPROC

'----------------------------------------------------------------------------
' Set the Time
' Input    : pHour holds the hours value (in decimal)
'          : pMinute holds the minutes value (in decimal)
'          : pSecond holds the seconds value (in decimal)
' Output    : None
' Notes    : None
'
PROC DS1307_WriteTime(pHour AS RTC_Hour, pMinute AS RTC_Minute, pSecond AS RTC_Second)
    RTC_Second = DS1307_DecToBCD(pSecond)          ' Convert the Seconds value to decimal
    RTC_Minute = DS1307_DecToBCD(pMinute)          ' Convert the Minutes value to decimal
    RTC_Hour  = DS1307_DecToBCD(pHour)            ' Convert the Hours value to decimal

    I2COUT DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_TIME, [RTC_Second, RTC_Minute, RTC_Hour]
ENDPROC

'----------------------------------------------------------------------------
' Set the Date
' Input    : pDayOfWeek holds the week day value (in decimal)
'          : pDay holds the days value (in decimal)
'          : pMonth holds the months value (in decimal)
'          : pYear holds the years value (in decimal)
' Output    : None
' Notes    : None
'
PROC DS1307_WriteDate(pDayOfWeek AS RTC_DayOfWeek, pDay AS RTC_Day, pMonth AS RTC_Month, pYear AS RTC_Year)

    RTC_DayOfWeek = DS1307_DecToBCD(pDayOfWeek)    ' Convert the WeekDay value to decimal
    RTC_Day      = DS1307_DecToBCD(pDay)          ' Convert the Days value to decimal
    RTC_Month    = DS1307_DecToBCD(pMonth)        ' Convert the Months value to decimal
    RTC_Year      = DS1307_DecToBCD(pYear)          ' Convert the Years value to decimal

    I2COUT DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_WDAY, [RTC_DayOfWeek, RTC_Day, RTC_Month, RTC_Year]
ENDPROC

'----------------------------------------------------------------------------
' Return the name of the day
' Input    : pDayOfWeek holds the day value 1 to 7 (Sunday to Saturday)
' Output    : RTC_DayName holds a string containing the day's name
' Notes    : None
'
PROC DS1307_Get_DayName(pDayOfWeek AS BYTE), RTC_DayName
    SELECT pDayOfWeek
        CASE 1
            RESULT = "Sunday"
        CASE 2
            RESULT = "Monday"
        CASE 3
            RESULT = "Tuesday"
        CASE 4
            RESULT = "Wednesday"
        CASE 5
            RESULT = "Thursday"
        CASE 6
            RESULT = "Friday"
        CASE 7
            RESULT = "Saturday"
    ENDSELECT
ENDPROC

'----------------------------------------------------------------------------
' Return the name of the month
' Input    : pMonth holds the month value 1 to 12 (January to December)
' Output    : RTC_MonthName holds a string containing the month's name
' Notes    : None
'
PROC DS1307_Get_MonthName(pMonth AS BYTE), RTC_MonthName
    SELECT pMonth
        CASE 1
            RESULT = "January"
        CASE 2
            RESULT = "February"
        CASE 3
            RESULT = "March"
        CASE 4
            RESULT = "April"
        CASE 5
            RESULT = "May"
        CASE 6
            RESULT = "June"
        CASE 7
            RESULT = "July"
        CASE 8
            RESULT = "August"
        CASE 9
            RESULT = "September"
        CASE 10
            RESULT = "October"
        CASE 11
            RESULT = "November"
        CASE 12
            RESULT = "December"
    ENDSELECT
ENDPROC

'----------------------------------------------------------------------------
' Detect the DS1307
' Input    : None
' Output    : Returns holding 1 if the DS1307 is available
' Notes    : Uses the Ack return for confirmation
'          : This procedure assumes that there is only 1 I2C device attached to the bus
'
PROC DS1307_Present(), BIT
    I2CIN DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_TIME, [WREG]
    RESULT = ~SRbits_C
ENDPROC

'----------------------------------------------------------------------------
_DS1307_Main:

$ENDIF  ' _DS1307_INC_



AlbertoFS

Hi Henk,
I am sorry that I didn't see the fault last time. I tested your code deleting some innecessary lines that cause the problem.
Basically your code deleted the configuration of the LCD.
I put some comments and modifications in your programm to ameliorate your code.

Do not use the Procedure: DS1307_Present(), there is problem with the ACK flag.
Alberto
73's de EA3AGV

henktimmerman47@gmail.com

Hi Alberto,
Thanks for the tips. I have implemented your changes (see attached listing). Unfortunately, the result is not good (all data remains constant and does not change). It seems that the DS1307 is not configured properly. Do you have any suggestions?

Greetings Henk

My program
'****************************************************************
'*  Name    : KlokDS1307.BAS                                    *
'*  Author  : Henk Timmerman                                    *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 18-01-2023                                        *
'*  Version : 1.0                                               *
'*  Notes   : PIC 16F628A met klok DS1307 (Alberto2)            *
'*          : I2E verbinding, print geheugen H00 t/m H06        *
'****************************************************************
DEVICE  16F628A               ;We gebruiken een 16F628A type
CONFIG  HS_OSC, WDT_Off,PWRTE_ON, LVP_OFF, BODEN_OFF, MCLRE_OFF                 
DECLARE ALL_DIGITAL   = True  ;Allle ingangen digitaal
DECLARE XTAL          = 20    ;Kristal 20 MHz

    DECLARE LCD_DTPIN = PORTB.4         ' LCD's Data lines (D4 to D7)
    DECLARE LCD_ENPIN = PORTA.3         ' LCD's EN line
    DECLARE LCD_RSPIN = PORTA.2         ' LCD's RS line
    DECLARE LCD_INTERFACE = 4           ' 4-bit interface to LCD
    DECLARE LCD_LINES = 2               ' LCD contains 2 lines
    DECLARE LCD_DATAUS = 50             ' Time to wait after print a data To LCD
    DECLARE LCD_COMMANDUS = 2000        ' Time to wait after print a command to LCD
    DECLARE LCD_TYPE = ALPHANUMERIC     ' LCD type is alphanumeric

'/-------------------------------------------------------------------------
DECLARE PORTB_PULLUPS ON

;Logische constanten
$DEFINE DS1307_SCL_Pin PORTA.0 ;clock signaal voor DS1307
$DEFINE DS1307_SDA_Pin PORTA.1 ;data signaal voor DS1307

INCLUDE "C:\PICprogrammas\DS1307.inc"

SYMBOL LED = PORTB.3   ;led licht na opstarten 0,5 sec op
'***************************************************************************
CLS

;Variabelen declareren
;Byte
DIM Sec AS BYTE
DIM Min AS BYTE
DIM Uur AS BYTE
DIM Dag AS BYTE
DIM Datum AS BYTE
DIM Maand AS BYTE
DIM Jaar AS BYTE

OUTPUT PORTB.3

DELAYMS 500
LED = 0                                                                                           

'Register instellen
  DS1307_WriteRegister($07, %00010010); adres $07 bit1=1 en bit0=0, SQW=8.192 kHz   OK
  DELAYUS 50
 
'Datum instellen
  DS1307_WriteDate(6, 6, 5, 23); 1-dag van de week (1..7), 2-dag (0..31), 3-maand (1..12), 4-jaar (0..99)
  DELAYUS 50
 
'Tijd instellen
  DS1307_WriteTime(10, 15, 10); Uur (0..23), Minuut (0..59), Seconde (0..59)
  DELAYUS 50
 
WHILE 1=1
'Tijd lezen
  DS1307_ReadTime()           ;RTC_Second (0..59), RTC_Minute (0..59), RTC_Hour (0..23)
  Uur = RTC_Hour
  Min = RTC_Minute
  Sec = RTC_Second
'Datum lezen
  DS1307_ReadDate()           ;RTC_DayOfWeek (1..7), RTC_Day (0..31), RTC_Month (1..12), RTC_Year (0..99)
  Datum = RTC_Day
  Maand = RTC_Month
  Jaar = RTC_Year
 
  PRINT AT 1,1,"TIME: ",DEC2 Uur, ":", DEC2 Min, ":", DEC2 Sec; resultaat 00?
  PRINT AT 2,1,"DATE: ",DEC2 Datum, ":", DEC2 Maand, ":", DEC2 Jaar; resultaat 00?
WEND

AlbertoFS

#11
Hi Henk,
The problem you have is with the LCD. Check all the connections as declared. Have you inverted the 4 PinBus?
There can be no other mistake. Here everything works (with another PIC).
The 20 Mhz oscillator runs well?
I delete another line of your code.

After having verified everything, there is one last possibility; is that the PIC clock is too high for this library. In this case, download the new ones that are available: HERE and you can choose the software library whose speed is adjustable.
Regards
Alberto
73's de EA3AGV

henktimmerman47@gmail.com

Hi Alberto,

Thanks for your comment. I copied your listing. The only thing I have changed/added is
• Include "C:\PICprograms\DS1307.inc"
• LED = 1 (see accompanying listing)
I also added the schedule and the result of the LCD console (continuous 00).
Do I need to do something with the SQW/OUT pin of DS1307? Changing the data bus pins of the LCD console gives no improvement. I do not understand it any more. Do you have any suggestions?

Greetings Henk

RGV250

#13
Hi Henk,
You dont need to do anything with the SQW/OUT pin, leave it unconnected.

Here is a very old program that might help to try, it is around 12 years ago and was running on 18F452 with a 4mhz xtal so you will need to alter the configs. LCD was on PortD so that will be easy to change.

There is an issue with the hours showing 40 added on but if it works for you I will look into why that is.

Device = 18F452
    Xtal = 4

Config_Start
   OSC = HS ; HS
   OSCS = OFF ; Disabled
   PWRT = OFF ; Disabled
   BOR = OFF ; Disabled
   BORV = 45 ; 4.5V
   WDT = OFF ; Disabled
   WDTPS = 1 ; 1:1
   CCP2MUX = OFF ; Disable (RB3)
   STVR = On ; Enabled
   LVP = OFF ; Disabled
   Debug = OFF ; Disabled
   Cp0 = OFF ; Disabled
   CP1 = OFF ; Disabled
   CP2 = OFF ; Disabled
   CP3 = OFF ; Disabled
   CPB = OFF ; Disabled
   CPD = OFF ; Disabled
   WRT0 = OFF ; Disabled
   WRT1 = OFF ; Disabled
   WRT2 = OFF ; Disabled
   WRT3 = OFF ; Disabled
   WRTB = OFF ; Disabled
   WRTC = OFF ; Disabled
   WRTD = OFF ; Disabled
   EBTR0 = OFF ; Disabled
   EBTR1 = OFF ; Disabled
   EBTR2 = OFF ; Disabled
   EBTR3 = OFF ; Disabled
   EBTRB = OFF ; Disabled
Config_End

'Device = 16F876
'Config CP_OFF, DEBUG_OFF, WRT_ENABLE_OFF, CPD_OFF, LVP_OFF, BODEN_ON, PWRTE_ON, WDT_OFF, XT_OSC
'Xtal = 4
All_Digital = True
''''declare PortB_Pullups = True
 
' Setup the LCD
Declare LCD_DTPin = PORTD.4
Declare LCD_RSPin = PORTE.0
Declare LCD_ENPin = PORTE.1
Declare LCD_Interface = 4
Declare LCD_Lines = 2
Declare LCD_Type = 0
 
' Define I2C bus ports  4.7k pull ups
Declare SDA_Pin = PORTA.1 'DS1307 SDA pin
Declare SCL_Pin =PORTA.0 'DS1307 SCL pin
 
Dim Temp1 As Byte
Dim Temp2 As Byte
Dim TempVal As Byte
 
Dim Secs As Byte
Dim Mins As Byte
Dim Hrs As Byte
Dim day As Byte
Dim Date As Byte
Dim Month As Byte
Dim Year As Byte
Dim Ctrl As Byte
 
Dim Secs_last As Byte

'Initialize LCD
DelayMS 100
Cls

Secs = 00 ' Set seconds
Mins = 00 ' Set minutes
Hrs = 00 ' Set hours
day = 0 ' Set day of week value
Date = 05' Day of month value
Month = 05 ' Month value
Year = 23 ' Year value
Ctrl = %10010000


' The DS1307 works with data in BCD format, so convert BIN to BCD
TempVal=Secs
GoSub BIN_TO_BCD
Secs=TempVal
 
TempVal=Mins
GoSub BIN_TO_BCD
Mins=TempVal
 
TempVal=Hrs
GoSub BIN_TO_BCD
Hrs=TempVal
 
TempVal=day
GoSub BIN_TO_BCD
day=TempVal
 
TempVal=Date
GoSub BIN_TO_BCD
Date=TempVal
 
TempVal=Month
GoSub BIN_TO_BCD
Month=TempVal
 
TempVal=Year
GoSub BIN_TO_BCD
Year=TempVal
 
BStart
 
' The datasheet specifies the first byte is 1101000x where x is read(1) or write(0).
' The second byte tells the DS 1307 where to start reading, 0 is at the start.
' The Ctrl byte contains advanced features, read the datasheet for more info
BusOut 11010000, 0, [Secs, Mins, Hrs, day, Date, Month, Year, Ctrl]
 
'Write initial values for time / Date
BStop
 
DelayMS 20


BStart
DelayMS 10
BusIn 11010001, 2, [Hrs]
Hrs = Hrs | 01000000
BusOut 11010000, 2, [Hrs]
BStop
 
Main:
 
BStart
' The datasheet specifies the first byte is 1101000x where x is read(1) or write(0).
' The second byte tells the DS 1307 where to start reading, 0 is at the start.
BusIn 11010001, 0, [Secs, Mins, Hrs, day, Date, Month, Year, Ctrl]
 
BStop
 
' The DS1307 sends it data in BCD, therefore it must be changed to
' BIN so that it can be easily used (eg, print onto an LCD)
TempVal=Secs
GoSub BCD_TO_BIN
Secs=TempVal
 
TempVal=Mins
GoSub BCD_TO_BIN
Mins=TempVal

' I think there is an error here where the hours has 40 added to it.
TempVal=Hrs
''TempVal = TempVal & 00011111  ' not sure what i was trying to do.

GoSub BCD_TO_BIN
Hrs=TempVal
 
TempVal=Date
GoSub BCD_TO_BIN
Date=TempVal
 
TempVal=Month
GoSub BCD_TO_BIN
Month=TempVal
 
TempVal=Year
GoSub BCD_TO_BIN
Year=TempVal
 
'If there is update in Secs, display time and Date
If Secs - Secs_last = 0 Then GoTo Main
 
' The Dec2 modifier makes sure that each value will have 2 characters, eg 1 becomes 01
Print At 1,1,"Time: ",Dec2 Hrs, ":", Dec2 Mins,":", Dec2 Secs
If Hrs.5 = 0 Then Print At 1,15," A"
If Hrs.5 = 1 Then Print At 1,15," P"
Secs_last = Secs
 
GoTo Main
 
BCD_TO_BIN: ' Convert the BCD values into BIN
Temp1 = $0F & TempVal   ' Clear off the top four bits
Temp1 = Dig Temp1, 0
Temp2 = TempVal >> 4 ' Shift down four to read 2 BCD value
Temp2 = Dig Temp2, 0
TempVal = Temp2 * 10 + Temp1
 
Return
 
BIN_TO_BCD:
 
Temp1 = Dig TempVal, 0 ' GET THE DEC DIGIT FOR THE FIRST NIBBLE
Temp2 = Dig TempVal, 1 ' GET THE DEC DIGIT FOR THE FIRST NIBBLE
Temp2 = Temp2 << 4 ' MOVE NUMBER OVER TO 2ND NIBBLE
' XOR THEM TOGTHER TO MAKE THE WHOLE BCD NUMBER
TempVal = Temp1 ^ Temp2
 
Return

RGV250

Hi,
If you comment this out
'BStart
'DelayMS 10
'BusIn 11010001, 2, [Hrs]
'Hrs = Hrs | 01000000
'BusOut 11010000, 2, [Hrs]
'BStop

and
'If Hrs.5 = 0 Then Print At 1,15," A"
'If Hrs.5 = 1 Then Print At 1,15," P"

It should be OK as 24 hour clock, I think the issue was when I was trying to get a 12 hour display.

Bob

AlbertoFS

Hi Henk,
It really is a difficult case.
Is your LCD a standard model?
Did you try any other readable messages on the LCD? Try the code that I sent you.
In your schematic I don't see the LCD connections. I send you a drawing to confirm it.

Make sure the RW pin is connected to 0V.
Does the LED light up?
Alberto
73's de EA3AGV

henktimmerman47@gmail.com

Hi Alberto,
One last try. Hereby 3 appendices: the schematic, the listing and the specification of the LCD.
I think I connected the LCD correctly:
1. VSS – 5V
2. VDD – 0
3. V0 - potentiometer
4.RS-A2
5. R/W - 0
6. E - A3
11. DB4 - B4
12. DB5 - B5
13. DB6 - B6
14. DB7 - B7
The result is still not good. What am I doing wrong?

Greetings Henk

RGV250

#17
Hi,
The right hand image shows the display controller is KS0066 which i do not think Positron supports.  Do you have one that is Hitachi HD44780 or equivalent.
Just done a bit of googling and apparently they are compatible?

You could also prove the clock is working by sending out to the serial port and display the information in serial monitor. That will split the problem.

Bob

top204

Just to make sure everything is as it should be in the compiler for the standard 14-bit core devices, I created a library for the DS1307 RTC for use with a standard 14-bit core device and tested it with a demo program in a simulator and on a real board.

The demo program is listed below:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate writing and reading a DS1307 Real Time Clock/Calendar using the DS1307 library
'
' Written by Les Johnson for the Positron8 BASIC compiler
'
    Device = 16F628A                                                ' Tell the compiler what device to compile for
    Declare Xtal = 20                                               ' Tell the compiler what frequency the device is operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                                     ' Set the Baud rate for USART1
    Declare HRsout1_Pin  = PORTB.2                                  ' Set the pin to use for USART1 TX
'
' Setup the Alphanumeric LCD
'
    Declare LCD_DTPin = PORTB.3                                     ' The LCD's Dt4 to Dt7 lines connect to PORTB.3 to PORTB.7
    Declare LCD_ENPin = PORTA.3                                     ' Connects to the LCD's EN line
    Declare LCD_RSPin = PORTA.2                                     ' Connects to the LCD's RS line
    Declare LCD_Interface = 4                                       ' The LCD is going to use a 4-pin interface
    Declare LCD_Lines = 2                                           ' The LCD has 2 lines
    Declare LCD_Type = Alphanumeric                                 ' The LCD is an Hitachi alphanumeric type
'
' Set the pins to use for the I2C interface to the DS1307 device
'
$define DS1307_SDA_Pin PORTA.1                                      ' I2C SDA for the DS1307 connected to this pin
$define DS1307_SCL_Pin PORTA.0                                      ' I2C SCL for the DS1307 connected to this pin

    Include "DS1307_14.inc"                                         ' Load the DS1307 library routines for a standard 14-bit core device into the program
'
' Create a variable for the demo
'
    Dim bPrevSecond As Byte                                         ' Holds the previous seconds value

'----------------------------------------------------------------------------
' The main program starts here
' Read the time and date from a DS1307 device and display them on a serial terminal and an alphanumeric LCD
'
Main:
    bPrevSecond = 255                                               ' Set a previous seconds value
    Cls                                                             ' Clear the LCD's display

    DS1307_WriteDate(3, 17, 05, 23)                                 ' Write an initial date to the DS1307
    DS1307_WriteTime(09, 46, 0)                                     ' Write an initial time to the DS1307

    Do                                                              ' Create a loop
        DS1307_ReadTime()                                           ' Read the time from the DS1307
        If RTC_bSecond <> bPrevSecond Then                          ' Has the seconds value changed?
            DS1307_ReadDate()                                       ' Yes. So read the date from the DS1307
            '
            ' Transmit the ASCII date and time to a serial terminal
            '
            HRSOutLn "Time = ", Dec2 RTC_bHour, ":",_               ' \
                                Dec2 RTC_bMinute, ":",_             ' | Serially transmit the time as ASCII
                                Dec2 RTC_bSecond                    ' /

            HRSOutLn "Date = ", Dec2 RTC_bDate, ":",_               ' \
                     Dec2 RTC_bMonth, ":", _                        ' | Serally transmit the date as ASCII
                     "20", Dec2 RTC_bYear                           ' /
            '
            ' Display the date and time on the LCD
            '
            Print at 1, 5, Dec2 RTC_bHour, ":",_                    ' \
                           Dec2 RTC_bMinute, ":",_                  ' | Display the time on line 1 of the LCD
                           Dec2 RTC_bSecond                         ' /

            Print at 2, 4, Dec2 RTC_bDate, ":",_                    ' \
                           Dec2 RTC_bMonth, ":", _                  ' | Display the date on line 2 of the LCD
                           "20", Dec2 RTC_bYear                     ' /
        EndIf
        bPrevSecond = RTC_bSecond                                   ' Save this seconds value for later comparison
    Loop                                                            ' Do it forever

The DS1307 library's code listing is below. Name it "DS1307_14.inc" and place it in the same folder as the demo program using it, or in the "Includes" folder for all programs to see (C:\Users\User Name\PDS\Includes\):

$ifndef _DS1307_14_INC_
$define _DS1307_14_INC_
'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Routines to access a DS1307 Real Time Clock/Calendar via a software I2C bus and suitable for standard 14-bit core devices.
'
' Written by Les Johnson for the Positron8 Compiler.
'
' Declare the default pins used for I2Cin and I2Cout to interface to the DS1307 device
'
$ifndef DS1307_SCL_Pin
    $define DS1307_SCL_Pin PORTA.0      ' DS1307 SCL connected to this pin
    $SendWarning "The $define DS1307_SCL_Pin is missing from the main program. Defaulting to using PORTA.0"
$endif
$ifndef DS1307_SDA_Pin
    $define DS1307_SDA_Pin PORTA.1      ' DS1307 SDA connected to this pin
    $SendWarning "The $define DS1307_SDA_Pin is missing from the main program. Defaulting to using PORTA.1"
$endif
'
' DS1307 register addresses
'
     $define cRTC_DEV_ADDR  $D0         ' DS1307 device Address
     $define cRTC_TIME      $00         ' Time register
     $define cRTC_WDAY      $03         ' Day of week number register
     $define cRTC_DATE      $04         ' Date register
     $define cRTC_CONTROL   $07         ' Control byte register
     $define cRTC_NVRAM     $08         ' NVRAM start
     $define cRTC_NVRAM_MAX $38         ' NVRAM address max (56 decimal)
'
' Values - used in calls to DS1307_ReadControl() and DS1307_WriteControl()...
'
     $define cRTC_SQWE       $10        ' Square Wave enable
     $define cRTC_SQWF_1HZ   $00        ' Square Wave frequency  1Hz
     $define cRTC_SQWF_4KHz  $01        ' Square Wave frequency  4.096KHz
     $define cRTC_SQWF_8KHz  $02        ' Square Wave frequency  8.192KHz
     $define cRTC_SQWF_32KHz $03        ' Square Wave frequency 32.768KHz
     $define cRTC_SQWOUT_H   $80        ' Set Square Wave output to high when disabled

$ifndef False
    $define False 0
$endif
$ifndef True
    $define True 1
$endif
'
' Create some Time and Date variables
'
    Dim RTC_bSecond As Byte                 ' Second (0..59)
    Dim RTC_bMinute As Byte                 ' Minute (0..59)
    Dim RTC_bHour   As Byte                 ' Hour   (0..11 or 0..23)
    Dim RTC_bDate   As Byte                 ' Date  (0..31)
    Dim RTC_bMonth  As Byte                 ' Month (1..12)
    Dim RTC_bYear   As Byte                 ' Year  (0..99)
    Dim RTC_bDay    As Byte                 ' Day of the week (1..7)
'
' Create a shared parameter variable
'
    Dim DS1307_bParam1 As Byte

'----------------------------------------------------------------------------
' Convert a packed BCD (Binary Coded Decimal) value to a decimal value
' Input     : pValue holds the BCD value to convert to decimal
' Output    : pResult holds the decimal value
' Notes     : None
'
Proc DS1307_BCDToDec(pValue As DS1307_bParam1), Byte
    Result = (pValue >> 4) * 10
    Result = Result + (pValue & $0F)
EndProc

'----------------------------------------------------------------------------
' Convert a decimal value to packed BCD (Binary Coded Decimal) value
' Input     : pValue holds the decimal value to convert to BCD
' Output    : pResult holds the packed BCD value
' Notes     : None
'
Proc DS1307_DecToBCD(pValue As DS1307_bParam1), Byte
    Result = (pValue / 10) << 4
    Result = Result | (pValue // 10)
EndProc

'----------------------------------------------------------------------------
' Read a DS1307 register ($00..$3F)
' Input     : pAddress holds the address of the register
' Output    : pResult holds the byte read from the register
' Notes     : None
'
Proc DS1307_ReadRegister(pAddress), Byte
    I2CIn DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, pAddress, [Result]
EndProc

'----------------------------------------------------------------------------
' Write to a DS1307 register ($00..$3F)
' Input     : pAddress holds the address of the register
'           : pData holds the byte to write to the register
' Output    : None
' Notes     : None
'
Proc DS1307_WriteRegister(pAddress As Byte, pData As Byte)
    I2COut DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, pAddress,[pData]
EndProc

'----------------------------------------------------------------------------
' Read the RTC control byte ($07)
' Input     : None
' Output    : pResult holds the byte read from the register
' Notes     : See the control constants cRTC_SQWXXXX above
'
$define DS1307_ReadControl() DS1307_ReadRegister(cRTC_CONTROL)

'----------------------------------------------------------------------------
' Write the RTC Control byte ($07)
' Input     : pData holds the byte to write to the register
' Output    : None
' Notes     : See the control constants cRTC_SQWXXXX above
'
$define DS1307_WriteControl(pData) I2COut DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_CONTROL,[pData]

'----------------------------------------------------------------------------
' Write a byte to Non-Volatile RAM
' Input     : pAddress holds the Non-Volatile RAM address
'           : pData holds the byte to write to Non-Volatile RAM
' Output    : None
' Notes     : The RAM is physically located at
'           : $08..$3F - this routine uses address $00..$37 (56 bytes)
'
$define DS1307_WriteByte(pAddress, pData) '
    If pAddress < cRTC_NVRAM_MAX Then     '
        DS1307_WriteRegister(pAddress + cRTC_NVRAM, pData) '
    EndIf

'----------------------------------------------------------------------------
' Read a byte from Non-Volatile RAM
' Input     : pAddress holds the Non-Volatile RAM address
' Output    : Returns holding the byte read from NV RAM
' Notes     : The RAM is physically located at
'           : $08..$3F - this routine uses address $00..$37 (56 bytes)
'
Proc DS1307_ReadByte(pAddress As Byte), Byte
    Result = DS1307_ReadRegister(pAddress + cRTC_NVRAM)
EndProc

'----------------------------------------------------------------------------
' Read the Time
' Input     : None
' Output    : Variable RTC_bSecond holds the seconds value (in decimal)
'           : Variable RTC_bMinute holds the minutes value (in decimal)
'           : Variable RTC_bHour holds the hours value (in decimal)
' Notes     : None
'
Proc DS1307_ReadTime()
    I2CIn DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_TIME, [RTC_bSecond,
                                                                     RTC_bMinute,
                                                                     RTC_bHour]
    RTC_bSecond = DS1307_BCDToDec(RTC_bSecond)      ' Convert the Seconds value to decimal
    RTC_bMinute = DS1307_BCDToDec(RTC_bMinute)      ' Convert the Minutes value to decimal
    RTC_bHour   = DS1307_BCDToDec(RTC_bHour)        ' Convert the Hours value to decimal
EndProc

'----------------------------------------------------------------------------
' Read the Date
' Input     : None
' Output    : Variable RTC_bDay holds the week day value (in decimal)
'           : Variable RTC_bDate holds the days value (in decimal)
'           : Variable RTC_bMonth holds the months value (in decimal)
'           : Variable RTC_bYear holds the years value (in decimal)
' Notes     : None
'
Proc DS1307_ReadDate()
    I2CIn DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_WDAY, [RTC_bDay,
                                                                     RTC_bDate,
                                                                     RTC_bMonth,
                                                                     RTC_bYear]

    RTC_bDay   = DS1307_BCDToDec(RTC_bDay)          ' Convert the WeekDay value to decimal
    RTC_bDate  = DS1307_BCDToDec(RTC_bDate)         ' Convert the Days value to decimal
    RTC_bMonth = DS1307_BCDToDec(RTC_bMonth)        ' Convert the Months value to decimal
    RTC_bYear  = DS1307_BCDToDec(RTC_bYear)         ' Convert the Years value to decimal
EndProc

'----------------------------------------------------------------------------
' Set the Time
' Input     : pHour holds the hours value (in decimal)
'           : pMinute holds the minutes value (in decimal)
'           : pSecond holds the seconds value (in decimal)
' Output    : None
' Notes     : None
'
Proc DS1307_WriteTime(pHour As RTC_bHour, pMinute As RTC_bMinute, pSecond As RTC_bSecond)
    RTC_bSecond = DS1307_DecToBCD(pSecond)          ' Convert the Seconds value to decimal
    RTC_bMinute = DS1307_DecToBCD(pMinute)          ' Convert the Minutes value to decimal
    RTC_bHour   = DS1307_DecToBCD(pHour)            ' Convert the Hours value to decimal

    I2COut DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_TIME, [RTC_bSecond,
                                                                      RTC_bMinute,
                                                                      RTC_bHour]
EndProc

'----------------------------------------------------------------------------
' Set the Date
' Input     : pDayOfWeek holds the week day value (in decimal)
'           : pDay holds the days value (in decimal)
'           : pMonth holds the months value (in decimal)
'           : pYear holds the years value (in decimal)
' Output    : None
' Notes     : None
'
Proc DS1307_WriteDate(pDayOfWeek As RTC_bDay, pDay As RTC_bDate, pMonth As RTC_bMonth, pYear As RTC_bYear)

    RTC_bDay   = DS1307_DecToBCD(pDayOfWeek)        ' Convert the WeekDay value to decimal
    RTC_bDate  = DS1307_DecToBCD(pDay)              ' Convert the Days value to decimal
    RTC_bMonth = DS1307_DecToBCD(pMonth)            ' Convert the Months value to decimal
    RTC_bYear  = DS1307_DecToBCD(pYear)             ' Convert the Years value to decimal

    I2COut DS1307_SDA_Pin, DS1307_SCL_Pin, cRTC_DEV_ADDR, cRTC_WDAY, [RTC_bDay,
                                                                      RTC_bDate,
                                                                      RTC_bMonth,
                                                                      RTC_bYear]
EndProc

'----------------------------------------------------------------------------
_DS1307_Main:

$endif  ' _DS1307_14_INC_

A screenshot of the demonstration program listed above, working in the Proteus simulator is shown below:

DS1307 Controlled by a PIC16F628A.jpg

Craig

Thank you Les that looks excellent