Menu

DS3231

Help
Rox
2020-04-28
2020-04-28
  • Rox

    Rox - 2020-04-28

    Hi, Im trying to use the DS3231 library into my 4 digits 7 segments LEDS, but results in lot of error, i only need to Read/write the Hour and minutes. on my code upon program starts i want to read the year, if it's not set then I'll write to the RTC registers. can someone kindly point what im doing wrong, here's the code :
    Thank you

    #chip 16F18345, 16
    #option explicit
    #include <millis.h>
    #include <DS3231.H>
        '
        'Template comment at the start of the config file
        '
        #startup InitPPS, 85
        #define PPSToolPart 16f18345
    
        Sub InitPPS
    
                'Module: MSSP1
                SSP1DATPPS = 0x0014    'RC4 > SDA1
                RC4PPS = 0x0019    'SDA1 > RC4 (bi-directional)
                SSP1CLKPPS = 0x0015    'RC5 > SCK1
                RC5PPS = 0x0018    'SCK1 > RC5 (bi-directional)
    
        End Sub
        'Template comment at the end of the config file
        '
        ' Variables
     dim bHour  as Byte
     dim bSegment as Byte
     dim mS_CV, mS_LV as Word
     dim wCntr as word
     dim  Multiplex as Byte
     dim fFlash as bit
     dim hour, min, sec, year as byte
      dim oldsec as byte
      dim date, month, year, DOW, century as byte
    
    ' Defines
     #define SEG_A Portb.7
     #define SEG_B Portb.6
     #define SEG_C Portc.7
     #define SEG_D Portc.1
     #define SEG_E Portc.2
     #define SEG_F Porta.0
     #define SEG_G Portb.5
     #define SEG_DP Portc.0
     '
     #define DIG_1 PORTC.6
     #define DIG_2 PORTA.2
     #define DIG_3 PORTA.1
     #DEFINE DIG_4 PORTB.4
     ' Port Direction
     Dir SEG_A out
     Dir SEG_B out
     Dir SEG_C out
     dir SEG_D out
     dir SEG_E out
     Dir SEG_F out
     dir SEG_G out
     dir SEG_DP out
     '
     Dir DIG_1 out
     Dir DIG_2 out
     dir DIG_3 out
     Dir DIG_4 out
     '
     #Define Match_Val PR2 'PR2 is the timer 2 match register
      Match_Val = 100       'Interrupt afer 100 uS
      On interrupt timer2Match call IntT2  'Interrupt on match
      Inittimer2 PS2_1, 1 '
      Starttimer 2
    
    '
    ; Define Hardware I2C settings for SSP module
      #define HI2C_BAUD_RATE 400
      #define HI2C_DATA PORTC.4
      #define HI2C_CLOCK PORTC.5
      ;I2C pins need to be input for SSP module
      Dir HI2C_DATA in
      Dir HI2C_CLOCK in
      ;MASTER I2C Device for SSP module
      HI2CMode Master
      '
      'read year,if year >=20 then RTC is initialized
      HI2CReStart                          ; is DS3231 present?
      HI2CSend(DS_AddrRead)
      HI2CStop
      HI2CReceive(year, NACK)
     ' DS3231_ReadDate(year)
      if year >=20 then
        UPDATE  'READ TIME FROM rtc
      else
        SEC = 0
        MIN = 30
        HOUR = 18
        DOW = 2
        DATE = 28
        MONTH = 4
        YEAR = 20
        DS3231_ControlRegister = 0
        DS3231_ControlStatus = 0
        '
        HI2CReStart                          ; is DS3231 present?
        HI2CSend(DS_AddrWrite)
        HI2CStop
        DS3231_SetClock(SEC,MIN,HOUR,DOW,DATE,MONTH,YEAR)
        DS3231_SetClock(DS3231_ControlRegister, DS3231_ControlStatus)
      END IF
    
    
    
    
    
    
     '
    ' First scan
    bHour = 0
    
    
     do
    '
        mS_CV = millis()
       if mS_CV - mS_LV >= 500 then  ' required Time has Elapsed
          fflash = not fflash
          mS_LV = mS_CV                  '
        end if
    
        if wcntr  >= 20000 then
          wCntr = 0
           bhour = bHour + 1
    
          if bHour > 9 then bHour = 0
           Encode_Segment
        end if
    
    
     loop
    
     'subroutines
    
     Sub IntT2
          wCntr = wCntr + 1
          Multiplex = Multiplex + 1
          if Multiplex > 4 then Multiplex = 1
    
          Select Case Multiplex
            Case 1
              set DIG_1 on
              set DIG_2 off
              set DIG_3 off
              set DIG_4 off
            case 2
              set DIG_1 off
              set DIG_2 on
              set DIG_3 off
              set DIG_4 off
            case 3
              set DIG_1 off
              set DIG_2 off
              set DIG_3 on
              set DIG_4 off
            case 4
              set DIG_1 off
              set DIG_2 off
              set DIG_3 off
              set DIG_4 on
          End Select
    
    
      End Sub
    
     sub Encode_Segment 'common cathode
    
      Select Case bHour
        Case 0
          bSegment = 0b00000011
        Case 1
          bSegment = 0b10011111
        Case 2
          bSegment = 0b00100101
        Case 3
          bSegment = 0b00001101
        Case 4
          bSegment = 0b10011001
        Case 5
          bSegment = 0b01001001
        Case 6
          bSegment = 0b01000001
        Case 7
          bSegment = 0b00011111
        Case 8
          bSegment = 0b00000001
        Case 9
          bSegment = 0b00001001
    '    case 10
    '      bsegment = 0b11111110 'decimal point
      End Select
      if fFlash then bsegment = bsegment - 1
      '
      SEG_A = bSegment.7
      SEG_B = bSegment.6
      SEG_C = bSegment.5
      SEG_D = bSegment.4
      SEG_E = bSegment.3
      SEG_F = bSegment.2
      SEG_G = bSegment.1
      SEG_DP = bSegment.0
    
     End Sub
    
     Sub UPDATE
    
    
     End Sub
    
     

    Last edit: Anobium 2020-04-28
  • Anobium

    Anobium - 2020-04-28

    DS3231_ControlRegister = 0
    DS3231_ControlStatus = 0

    The two are constants are they are define already as follows, so, delete.
    

    ~~~

    define DS3231_ControlRegister 0x0E 'Control register

    define DS3231_ControlStatus 0x0F 'Control Status register

    re...
    

    DS3231_SetClock(DS3231_ControlRegister, DS3231_ControlStatus)
    ~~~
    I am not sure what this call in meant to do. You have set the set the clock on the previous line.

    Surely you need to read the time? Use DS3231_ReadTime(out DS_Hour, out DS_Min, out DS_Sec, out DS_A_P)or DS3231_ReadClock(out DS_Hour, out DS_Min, out DS_Sec, out DS_A_P, out DS_DOW, out DS_Date, out DS_Month, out DS_Year)

     
  • Rox

    Rox - 2020-04-28

    "re...
    DS3231_SetClock(DS3231_ControlRegister, DS3231_ControlStatus)
    ~~~
    I am not sure what this call in meant to do. You have set the set the clock on the previous line."
    I thought i still need to write the values to the RTC.
    Ill go back to the board, ill revert back on result.

    appreciate your help, Thanks.

     
  • Rox

    Rox - 2020-04-28

    i still get errors, but it's ok for now. i just converted my old Proton code to GCB not using the library, here how it goes.

    Define Hardware I2C settings for SSP module
    #define HI2C_BAUD_RATE 400
    #define HI2C_DATA PORTC.4
    #define HI2C_CLOCK PORTC.5
    ;I2C pins need to be input for SSP module
    Dir HI2C_DATA in
    Dir HI2C_CLOCK in
    ;MASTER I2C Device for SSP module
    HI2CMode Master
    '
    #define DS_AddrRead 0b11010001
    #define DS_AddrWrite 0b11010000
    '
    'read year,if year >=20 then RTC is initialized
    HI2CReStart ; is DS3231 present?
    HI2CSend(DS_AddrRead)
    HI2CReceive(ClockBuffer(6), NACK)
    HI2CStop
    '
    if clockbuffer(6) >=20 then
    UPDATE 'READ TIME FROM rtc
    else
    clockbuffer(0) = 0
    clockbuffer(1) = 30
    clockbuffer(2) = 18
    clockbuffer(3) = 2
    clockbuffer(4) = 28
    clockbuffer(5) = 4
    clockbuffer(6) = 20
    clockbuffer(7)= 0 'Alarm1 Secs
    clockbuffer(8)= 0x15 'alarm1 mins
    clockbuffer(9)= 0x18 'alarm1 hours
    clockbuffer(10)= 0x80 'a1am4 -alarm 1 when hours, mins, secs matched
    clockbuffer(11) = 0x30 'alarm2 minutes
    clockbuffer(12)= 5 'alarm2 hours
    clockbuffer(13)= 0x80 'a2m4 alarm2 when hours and minutes matched
    clockbuffer(14) = 0 'control
    clockbuffer(15)= 0 'status
    '
    for i = 0 to 15
    HI2CReStart
    HI2CSend(DS_AddrWrite)
    HI2CSend (i)
    HI2CSend clockbuffer(i)
    HI2CStop
    next

    END I

    '
    ' First scan
    bHour = 0

    do
    '
    mS_CV = millis()
    if mS_CV - mS_LV >= 500 then ' required Time has Elapsed
    fflash = not fflash

     
  • jackjames

    jackjames - 2020-04-28

    I have used the DS3231 many times and have never found problems, except for a defective one that had the incorrect 32.768KHz output.
    Before using it, I advise you to initialize it as I did:

    '       ========================================================================
    '       Inizializza il DS3231
    '       ------------------------------------------------------------------------
    Sub DS3231_Init
            DS3231_EnableOscillator (1)  ' Abilita oscillatore
            DS3231_EnableSQW             ' Uscita 1Hz
            DS3231_SetHourMode (24)      ' 24 ore
            DS3231_SetSQW (0)            ' Abilita uscita 1Hz
    End Sub
    

    Check it and then let me know.

     
  • mkstevo

    mkstevo - 2020-04-28

    As with @JackJames, I've used the DS3231 in a number of projects. I simply copied and pasted the code from one of the demo files and, it, in an Apple style, "Just worked".

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.