example code for DS1337


Results 1 to 26 of 26

Threaded View

  1. #18
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Thumbs down Made changes...new code still doesn't work w/ DS1337

    Quote Originally Posted by BobK View Post
    I only have a few minutes before I need to go to work but I use the DS1337 quite a bit and this is all I use to set the time information and to display it. Nothing else and it works great. I will check back tonight when I get home.
    Thanks, BobK. I studied your code you posted and made some changes in my code to reflect what I saw. I have listed my latest code below. Also added a DEFINE OSC 48 statement that was pointed out to me by someone else as needed since I am using 48 MHz CPU clock derived from the 96 MHz clock for a USB interface I will also be adding to this code at later time.
    I also notice an anomally in my breadboard setup of the DS1337 circuit when I connect my oscilloscope to the SQW/INTB output:
    1) When power is applied to the DS1337 circuit but the code is erased from my controlling 18F4550 MCU, I see a 32.7787kHz clock signal on my oscilloscope at the SQW output of the DS1337.
    2) When I then program the 18F4550 the clock signal on SQW output disappears (flat line at 0 volts).
    This appears to be opposite to what should be happening. The clock should appear at SQW when the MCU is programmed, time has been set and DS1337 is clocking....this isn't happening.
    I also checked the INTA output to see if the Alarm1 was happening 30 seconds after programming and setting the DS1337 time, and it isn't happening either.
    Any ideas what may be causing all this?
    Code:
    Include "Modedefs.Bas"
    INCLUDE "ALLDIGITAL.pbp"    ' Sets all registers for digital ops.                   
    DEFINE OSC 48
    
    ;--- if you un-comment these, you must comment the ones in the .inc file--
    ASM ; 18F2550/4550, 8mhz crystal
       __CONFIG    _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
       __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H
       __CONFIG    _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
       __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
       __CONFIG    _CONFIG3H, _PBADEN_OFF_3H ; PortB resets as digital
       __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
        
    '**--SETUP FOR USING 2x16 LCD THAT IS INSTALLED IN EASYPIC6---------- 
        '( commented out during testing of DS1337 operations)
    '********************************************************************   
      ' LCD DEFINES FOR USE WIH 2x16 LCD..commented out during testing
           
      ' DEFINE LCD Control Constants ** 
    '====================================
        'I       CON 254           ' Control Byte 
        'Clr     CON 1             ' Clear the display 
        'Line1   CON 128           ' Point to beginning of line 1 
        'Line2   CON 192           ' Point to beginning of line 2 
    
    '**-------------SETUP FOR USING DS1337 Real Time Clock------------------ 
    ' Setup Hardware for uart
    ' ==================
        DEFINE HSER_BAUD 115200
        DEFINE HSER_RCSTA 90h
        DEFINE HSER_TXSTA 24h
        DEFINE HSER_CLROERR 1
    
    ' Aliased Variables for CLock 
    ' ====================
        Alarm1      VAR PORTB.4     ' Output of Alarm1 signal
        Alarm2      VAR PORTB.5     ' Output of Alarm2 signal
        DS_SCL      VAR PORTB.1     ' I2C clock pin 
        DS_SDA      VAR PORTB.0     ' I2C data pin 
        RTC         CON %11010000   ' RTC device write address(byte addressing)
           'contrl     CON %00011001    ' Sets RS1 & RS2,
                                    ' clears INTCN to enable Alarm1 & 3.2768kHz
                                    ' outputs separately. 
    
    ' -------------- RTC Address definitions
        SecReg      CON $00   ' seconds address (00 - 59) 
                              ' MSB of SecReg must be set to a 0 to enable RTC 
        MinReg      CON $01   ' minutes address (00 - 59) 
        HourReg     CON $02   ' hours address (01 - 12) or (00 - 23) 
        DayReg      CON $03   ' day address (1 - 7) 
        DateReg     CON $04   ' date address (01 - 28/29, 30, 31) 
        MonthReg    CON $05   ' month address (01 - 12) 
        YearReg     CON $06   ' year address (00 - 99) 
    
    'Alarm 1 Address definitions 
    '=================
        Alm1sec     CON $07   ' Alarm 1 seconds address (00 - 59) 
        Alm1min     CON $08   ' Alarm 1 minutes address (00 - 59)
        Alm1hr      CON $09   ' Alarm 1 hours address (01 - 12) or (00 - 23) 
        Alm1Day     CON $0A   ' Alarm 1 day address (1 - 7)
    
    'Alarm 2 Address definitions 
    '==================
        Alm2min     CON $0B   ' Alarm 2 minutes address (00 - 59)
        Alm2hr      CON $0C   ' Alarm 2 hours address (01 - 12) or (00 - 23) 
        Alm2Day     CON $0D   ' Alarm 2 day address (1 - 7)
    
    ' Alias of Clock register addresses
    '========================
       ContReg     CON $0E         ' CONTROL register address 
       StatusReg   CON $0F         ' STATUS register address
     
    ' Clock Variables
    '=========
        sec         VAR BYTE  ' seconds 
        MINs        VAR BYTE  ' minutes 
        hr          VAR BYTE  ' hours 
        day         VAR BYTE  ' day 
        date        VAR BYTE  ' date 
        mon         VAR BYTE  ' month 
        yr          VAR BYTE  ' year 
    
    'ALARM1 VARIABLES
    '==============
        A1sec       VAR BYTE  ' seconds 
        A1MINs      VAR BYTE  ' minutes 
        A1hr        VAR BYTE  ' hours 
        A1day       VAR BYTE  ' day 
        
    'ALARM2 VARIABLES
    '==============
        A2MINs      VAR BYTE  ' minutes
        A2hr        VAR BYTE  ' hours
        A2day       VAR BYTE  ' day
    
    ' Initialize Hardware 
    ' ------------------- 
        'Set registers
          TRISA =%00000000 
          TRISB =%00000000 
          TRISC =%00000000 
          TRISD =%00000000 
          TRISE.0 = 0
          TRISE.1 = 0
          TRISE.2 = 0
          INTCON2.7 = 0
                              
        ' Initialize Display    ' commented out during DS1337 testing
          'Pause 1000                         ' Delay to let LCD start up 
          'LCDOut I,CLR:Pause 50              ' Clear Display
          'LCDOut I,Line1+2," RTC TEST "      ' Display "RTC TEST" on 1st line
          'Pause 500
          'LCDOut I,Line2+2,"..Power On.. !!" ' Display "Power on" on 2nd line
          'PAUSE 1000
        
        GoSub SetTimeAndDate 
    
    ' Main Program Loop 
    ' ----------------- 
    Main:
        ' Read the current time and ALARM settings from the DS1337 
            I2CRead DS_SDA, DS_SCL, RTC, SecReg,[sec,MINs,hr,day,date,mon,yr]
            Pause 20
            I2CREAD DS_SDA, DS_SCL, RTC, Alm1sec,[A1sec,A1MINs,A1hr,A1day]
            Pause 20
            'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day] ' No Alarm2 during test
            'PAUSE 20 
    
        ' Display Time and ALARM2 settings on the LCD display 'No LCD during test 
            
        GoTo Main 
    End     ' Insure program stops if reaches here 
    
    ' START LIST OF SUBROUTINES
    '***********************
    'DisplayTest:       'Commented out during test of DS1337 code
    '============
        'LCDOut I,CLR:Pause 50               ' Clear Display 
        'LCDOut I,Line1+2," TEST "           ' Display "TEST" on 1st line
        'PAUSE 500
        'LCDOut I,Line2+2,"..Power On.. !!"  ' Display "Power on" on 2nd line
        'Pause 1000
        'RETURN
       
    SetTimeAndDate: 
    '============
    ' Initialize clock variables to 0 
        yr=0:date =0:mon =0:day=0:hr=0:MINs=0:sec=0 
        A1sec=0:A1MINs=0:A1hr=0:A1Day=0 
        A2MINs=0:A2hr=0:A2day=0 
    ' The BCD constants below set the RTC to: 19:20:30 on 06-02-2008 
        yr=$08 
        mon=$06 
        date=$02 
        day=$01
        hr=$19           
        MINs=$20
        sec=$30 
    'Define ALARM1 FOR 19:21:00 & to alarm when hours, minutes, & secs match.
        ' This requires AIM1=AIM2=AIM3=0 and AIM4=1
        ' (A1xxx + $80 or A1xxx + 128d sets bit 7 in AIM1,A1M2,A1M3,A1M4)
        A1hr   = $19        ' for AIM1 = 0
                            'Bit6 must be 0 for 24 hour clock
        A1MINs = $21       'for AIM2 = 0
        A1sec  = $00        'for AIM3 = 0
        A1day  = $01 + $80  '$01 + $80 = $81 = 129d = $10000001 for AIM4 = 1
                            'DY/_DT Bit6 = 0 for using Day of month alarm
    'Define ALARM2 FOR 19:22:00 'Commented out during DS1337 testing of 1Hz
        'A2hr=$19 
        'A2MINs=$22
        'A2day=$01          'DY/_DT Bit6 = 0 for using Day of month alarm
    'Set the main Time
        I2CWrite DS_SDA, DS_SCL, RTC, SecReg,[sec,MINs,hr,day,date,mon,yr]
        Pause 20     
    'Set the Alarm1 Time
        I2CWrite DS_SDA, DS_SCL, RTC,Alm1sec,[A1sec,A1MINs,A1hr,A1day]
        Pause 20
    'Set the Alarm2 Time   ' Alarm2 Commented out during testing
        'I2CWrite DS_SDA, DS_SCL, RTC,Alm2min,[A2MINs,A2hr,A2day,contrl] 
        'Pause 20
        'I2CRead DS_SDA, DS_SCL, RTC, Alm2min,[A2MINs,A2hr,A2day]
    ' Clear STATUS and set CONTROl registers 
        I2CWrite DS_SDA,DS_SCL,RTC,StatusReg,[$00] ' Clear STATUS register
        PAUSE 20 
        I2CWrite DS_SDA, DS_SCL, RTC, ContReg,[%00011001]' Starts the oscillator, sets the SQW/OUT, enables INTA 
        Pause 20 
    Return
    Last edited by jellis00; - 1st December 2009 at 22:16. Reason: Typos

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

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