Proton BASIC Compiler - DS1307 Real Time Clock(2)

  • PicŪ Basic

  • DS1307 Real Time Clock(2)

    This library is based on the Les's article which can be seen HERE.
    It is a simple RTC but always works well.

    For a future project, I added a few extra features.
    - ACK control that can be disabled.
    - Protection against interrupts (option).
    - The days/months can be printed in several languages.
    - The I2C pins are automatically placed in digital mode (pins ANSELx = 0).
    - 3 types of commands with Hardware of Software I2C (HBusOut/HBusIn, BusOut/BusIn or I2COut/I2Cin)
    - New DS1307_Present() command that lets you know the device is properly connected and working.
    - A Command to compress the RTC data to write it to an external eeprom.

    To see the basic commands please see Les's article.

    RTC variables:
    The declaration of RTC variables are made different.
    ' Time variables BCD & Decimal values
    Dim DS1307_Array[8] As Byte
    Dim RTC_Second    As DS1307_Array#0     ' Second (0..59)
    Dim RTC_Minute    As DS1307_Array#1     ' Minute (0..59)
    Dim RTC_Hour      As DS1307_Array#2     ' Hour   (0..11 or 0..23)
    Dim RTC_DayOfWeek As DS1307_Array#3     ' Day of the week (1..7)
    Dim RTC_Day       As DS1307_Array#4     ' Date   (0..31)
    Dim RTC_Month     As DS1307_Array#5     ' Month  (1..12)
    Dim RTC_Year      As DS1307_Array#6     ' Year   (0..99)
    New commands:

    This command interrogates the device to know if it works correctly.
    If the control acknowledge is enabled then it is very easy just checking the return acknowledge.
    If the control acknowledge is disabled by the user then a predetermined value is written to the DS1307 NVRAM. If reading this value is correct then a compatible acknowledge will be sent.

    DS1307_ZipPage8(Humidity, Temperature)
    This command allows to organize the RTC data to save two free positions in the array to obtain an 8 byte page that will be writable to the EEPROM. The two byte variables are entered directly with the command and placed in the correct position in the array. (Humidity, Temperature)

    As an example the RTC data can be written to an EEPROM in this way.
    DS1307_ZipPage8(Humidity, OrgTemperature)
    WritePage_E2P8(DeviceNumber, E2PArray, E2P_Page)
    DS1307_UnZipPage8(Humidity, Temperature)
    This command allows to decompress the data from the array and to rebuild the original position of the data and extracting the 2 bytes saved.

    As an example the RTC data can be restored in this way.
    ReadPage_E2P8(DeviceNumber, E2PArray, E2P_Page)
    DS1307_UnZipPage8(Humidity, Temperature)
    Some options could be enabled/disabled by the user.
    $define _French_
    $define _EnableGIE_Save_
    $define _DisableNACK_
    Include ""
    TC74 Temperature sensor.
    By placing several devices on the same I2C bus, the TC74 sensor gives some trouble again. See the article about TC74 temperature sensor. Thanks to the double reading and autocorrect of the TC74_ReadSeCTemp() command, it works well and I did not realize the issue. The TC74 sensor is put into standby mode when seeing an I2C header that is not yours. In this way each time the temperature is read the sensor must be activated to Normal Mode first.
    No problem already planned!

    3 Files are available:

    Include "" using the hardware I2C of the PIC (HBusOut/HBusIn commands) with acknowledge control.
    Include "" using the software I2C (BusOut/BusIn commands) with acknowledge control.
    Include "" using the software I2C (I2COut/I2CIn commands) without acknowledge control.

    The file "Test1_DS1307.bas" allows to test the DS1307 commands.
    The files "Test2_DS1307.bas" and "Test3_DS1307.bas" are a mini DataLogger to see the possibilities to use different libraries that can be found in the WIKI.

    12/04/2017 Update DS1307 library
    Enjoy the datalogger.

    Download the code from the Files Server.
  • Recent Activity


    TM1637 code

    Thread Starter: RGV250

    Hi, I have written some code for TM1637 modules, it does not do the DP/Colon or key scan as I only wanted it for a revcounter for a lathe. They are...

    See_Mos Yesterday, 19:53 Go to last post

    Is it possible to alias the SerOut pin

    Thread Starter: RGV250

    Hi, As the title says, is it possible to alias the pin used for "SerOut"?, I would like to send a string out on each pin of a port so PortB.0 first...

    RGV250 Yesterday, 16:22 Go to last post