DS1307 RTC example


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    malc-c's Avatar
    malc-c Guest

    Default DS1307 RTC example

    I have an EasyPIC5 development board and one of the Microelectonica RTC2 add on modules which has been breadboarded and needed to be tested as part of an ongoing project. The following example simply shows todays date (14/2/10) and starts running from 19:00 hrs.

    Although this example was simply used to test the comms between PIC and Module, it should be a simple matter to add a few tweaks to allow the date and time to be set.

    Code:
    '****************************************************************
    '*  Name    : RTCtest877A.BAS                                   *
    '*  Author  : Malcolm Crabbe                                    *
    '*  Notice  : Copyright (c) 2010 M.Crabbe                       *
    '*          : All Rights Reserved                               *
    '*  Date    : 14/02/2010                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : Test of breadboarded RTC2 Module to prove         *
    '*          : hardware                                          *
    '*  Target  : 16F877A
    '****************************************************************
    
    @   __config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF
    
    DEFINE  OSC 20
    CLEAR
    
    ;----[LCD definitions]------------------------------------------------------
    DEFINE LCD_DREG  PORTB              ' LCD Data port
    DEFINE LCD_DBIT  0                  ' starting Data bit (0 or 4)
    DEFINE LCD_EREG  PORTB              ' LCD Enable port
    DEFINE LCD_EBIT  5                  ' Enable bit  (on EasyPIC 5 LCD)
    DEFINE LCD_RSREG PORTB              ' LCD Register Select port
    DEFINE LCD_RSBIT 4                  ' Register Select bit   (on EasyPIC 5 LCD)
    DEFINE LCD_BITS  4                  ' LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES 2                  ' number of lines on LCD
    DEFINE LCD_COMMANDUS 2000           ' Command delay time in us 
    DEFINE LCD_DATAUS 50                ' Data delay time in us 
    
    SDA var PORTC.1                     ' RTC data 
    SCL var PORTC.0                     ' RTC clock
    
    TRISC= %11111111
    DB0 var byte[8]
    CMCON = %00000111                   ' Comparators = off
    gosub write_1307
    read_1307:                          ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
    I2CREAD SDA,SCL,$D1,$00,[STR DB0\8] ' Read 8 bytes from DS1307
    
    lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1],":",hex2 DB0[0]    'bit 0=sec, bit 1=min, bit 2=hrs
    lcdout $fe,$c0,"Date=",hex2 DB0[4],":",hex2 DB0[5],":",hex2 db0[6]  'bit 4=day, bit 5=month, bit 6=year
    goto read_1307
    end
    
    Write_1307:                          ' Set time & date to 19:00:00  14th Feb 201
    I2CWRITE SDA,SCL,$D0,$00,[$00,$00,$19,$7,$14,$2,$10,$90] ' Write to DS1307
    pause 10
    RETURN

  2. #2
    Join Date
    Feb 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Unhappy Canīt read from it.

    Hi Malcolm.

    I'm trying this code but can't make it work. It only displays:

    Time= 00:00:00
    Date= 00:00:00

    but does not show the date and time that was set or advance.

    Tried grounding DS1307's Vbat pin and using a lithium battery between this pin and ground. Tried 1k, 4.7K and 10k resistors to Vcc from SCL & SDA.

    I know I can write to the DS1307 and I know it's ticking since I can make the SQWE pin stop or go 1 Hz by changing the last byte while writing, I just don't seem to be able to read from it.

    I'm using a 16F887 at 4 mhz, pin PortB.1 is SCL, pin PortB.2 is SDA.

    Any ideas?

    Thanks!

    Rogerio


    Code:
    DEFINE  OSC 4
    CLEAR
    
    ;----[LCD definitions]------------------------------------------------------
    DEFINE LCD_DREG  PORTC              ' LCD Data port
    DEFINE LCD_DBIT  0                  ' starting Data bit (0 or 4)
    DEFINE LCD_EREG  PORTD              ' LCD Enable port
    DEFINE LCD_EBIT  0                  ' Enable bit  (on EasyPIC 5 LCD)
    DEFINE LCD_RSREG PORTD              ' LCD Register Select port
    DEFINE LCD_RSBIT 1                  ' Register Select bit   (on EasyPIC 5 LCD)
    'DEFINE LCD_BITS  4                  ' LCD bus size (4 or 8 bits)
    'DEFINE LCD_LINES 2                  ' number of lines on LCD
    DEFINE LCD_COMMANDUS 2000           ' Command delay time in us 
    DEFINE LCD_DATAUS 50                ' Data delay time in us 
    SDA var PORTB.2                     ' RTC data 
    SCL var PORTB.1                     ' RTC clock
    DB0 var byte[8]
    
    TRISB= %11111111
    gosub write_1307
    read_1307:                          ' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
    I2CREAD SDA,SCL,$D1,$00,[STR DB0\8] ' Read 8 bytes from DS1307
    lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1],":",hex2 DB0[0]    'bit 0=sec, bit 1=min, bit 2=hrs
    lcdout $fe,$c0,"Date=",hex2 DB0[4],":",hex2 DB0[5],":",hex2 db0[6]  'bit 4=day, bit 5=month, bit 6=year
    
    goto read_1307
    end
    
    Write_1307:                          ' Set time & date to 19:00:00  14th Feb 201
    pause 1000
    I2CWRITE SDA,SCL,$D0,$00,[$00,$00,$19,$7,$14,$2,$10,$80] ' Write to DS1307
    pause 10
    RETURN

  3. #3
    Join Date
    Feb 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Problem solved!

    Ok, so I had forgoten to clear Anselh register, ha.

    Working great now,

    Thanks for the code malcolm!

    Rogerio

  4. #4
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Glad to hear you've got it working.

    It's fairly basic in functionality, I just used it as a test to confirm that the hardware is working (hence no fancy set buttons or alarms etc) - there are other examples on this forum and the net which cover the setting of these things to make a fully functional alarm clock, just use the search function or google

  5. #5
    Join Date
    Jul 2007
    Posts
    65


    Did you find this post helpful? Yes | No

    Default

    this is great!

    I'm not too sure to understand how you get the Decimal values when reading as STR and then displaying them as HEX2?

    also, a bit out of topic but I get a weird error:
    symbol not previously defined (ANSELH)
    when trying to set ANSEL = 0, to use other ports.
    On a 16F876a...
    Ever seen that?

  6. #6
    Join Date
    Feb 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flipper_md View Post

    also, a bit out of topic but I get a weird error:
    symbol not previously defined (ANSELH)
    when trying to set ANSEL = 0, to use other ports.
    On a 16F876a...
    Ever seen that?
    Does the 16F876A have an ANSEL or ANSELH register? Some devices use different registers to acomplish similar tasks.

Similar Threads

  1. 16F628 and an RTC DS1307
    By aurbo in forum General
    Replies: 2
    Last Post: - 7th March 2011, 22:06
  2. Random seed using the ds1307 RTC
    By rookie11 in forum General
    Replies: 7
    Last Post: - 25th November 2007, 20:31
  3. PIC 16F877 I2C communications
    By sigmoideng in forum General
    Replies: 7
    Last Post: - 13th July 2007, 10:28
  4. DS1307 on fire
    By brid0030 in forum General
    Replies: 6
    Last Post: - 25th November 2006, 02:44
  5. X1226 RTC - Help
    By charudatt in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th August 2006, 17:54

Members who have read this thread : 2

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