Real time clock... what a headache!


Closed Thread
Results 1 to 3 of 3

Hybrid View

  1. #1
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Talking No answer...

    i'm still waiting for some answers... thread is not closed yet :P

  2. #2
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Simple code for use with DS1307 and 18F8722

    -------------- RTC -----------------

    RTC CON %11010000 ' RTC device address (byte addressing)
    ScReg CON $00 ' seconds address (00 - 59) MSBit of SecReg must be set to a 0 to enable RTC
    ContReg CON $07 ' control register
    RAMstart CON $08
    RTCflag CON 0 ' RTC flag (location 0 of internal EEPROM)
    ClockControlByte CON %00010000 ' sets the SQW/OUT to 1Hz pulse, logic level low


    ' Clock Variables


    Sc VAR BYTE
    Mn VAR BYTE
    Hr VAR BYTE
    Da VAR BYTE
    Dt VAR BYTE
    Mo vAR BYTE
    YR var BYTE


    '---------------- END RTC assingment ---------------


    Set up Seconds in TimBuff[5], TimBuff[6]
    Minutes in TimBuff[3], TimBuff[4]
    Hours in TimBuff[1],TimBuff[2]


    Etc... ONE digit in each buffer element

    Then GOSUB SetTime


    To READ:

    Gosub ShowTime
    Current time is shown in the TimBuff[X] registers.







    SetTime:

    Number1 = TimBuff[5]
    Number2 = TimBuff[6]

    Gosub HexAscii2BCD

    Sc = BCD

    Number1 = TimBuff[3]
    Number2 = TimBuff[4]


    Gosub HexAscii2BCD

    Mn = BCD

    Number1 = TimBuff[1]
    Number2 = TimBuff[2]

    Gosub HexAscii2BCD

    Hr = BCD

    Number1 = TimBuff[7]
    Number2 = TimBuff[8]

    Gosub HexAscii2BCD

    Mo = BCD

    Number1 = TimBuff[9]
    Number2 = TimBuff[10]

    Gosub HexAscii2BCD

    Da = 0 ' We don't care what day of the week it is
    Dt = BCD

    Number1 = TimBuff[11]
    Number2 = TimBuff[12]

    Gosub HexAscii2BCD

    YR = BCD

    Cntrl = 0 ' Must be zero to make clk run - NOT same as control reg!
    Gosub ClockWrite
    GOSUB ClockControl
    Spare1 = 55 ' Doesn't have to be BCD
    GOSUB ClockRamWrite
    RETURN


    ShowTime:

    Gosub ClockRead
    BCD = sc
    GOSUB BCD2HexAscii

    TimBuff[5] = Number1
    TimBuff[6] = Number2

    BCD = mn
    GOSUB BCD2HexAscii
    TimBuff[3] = Number1
    TimBuff[4] = Number2

    BCD = hr
    GOSUB BCD2HexAscii
    TimBuff[1] = Number1
    TimBuff[2] = Number2

    BCD = mo
    GOSUB BCD2HexAscii
    TimBuff[7] = Number1
    TimBuff[8] = Number2

    BCD = dt
    GOSUB BCD2HexAscii
    TimBuff[9] = Number1
    TimBuff[10] = Number2

    BCD = yr
    GOSUB BCD2HexAscii
    TimBuff[11] = Number1
    TimBuff[12] = Number2



    RETURN


    HexASCII2BCD:
    BCD = (Number1 - 48)<<4 + (Number2 - 48)
    RETURN

    BCD2HexASCII:
    Number1 = (BCD >> 4) + 48
    Number2 = (BCD & %00001111) + 48
    RETURN





    ClockWrite:
    I2CWrite SDA,SCL,RTC,ScReg,[sc,mn,hr,da,dt,mo,yr,Cntrl]
    Return

    ClockControl:
    I2CWRITE SDA,SCL,RTC,ContReg,[ClockControlByte]
    RETURN

    ClockRAMRead:
    I2CRead SDA,SCL,RTC,RAMstart,[Spare1,Spare2,Spare3,Spare4,Spare5,Spare6]
    RETURN
    ClockRAMWrite:
    I2CWRITE SDA,SCL,RTC,RAMstart,[Spare1,Spare2,Spare3,Spare4,Spare5,Spare6]
    RETURN

    ClockRead:
    I2CRead SDA,SCL,RTC,ScReg,[sc,mn,hr,da,dt,mo,yr]
    RETURN
    Charles Linquist

Similar Threads

  1. real time clock
    By maria in forum Code Examples
    Replies: 44
    Last Post: - 1st March 2022, 12:13
  2. Real Time Clock
    By in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd June 2012, 04:52
  3. real time clock - PCF8583
    By maria in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 15th April 2010, 15:41
  4. Real time clock ICs
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 66
    Last Post: - 20th October 2008, 16:05
  5. Real Time Clock
    By savnik in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th December 2006, 02:02

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts