• PicŪ Basic


  • Re: Setting PCF8583 RTC with PC

    I have been doing some playing with your PC timesetter and what and excellent little program it is.
    I have got it working for the PCF8563 and the DS1307 RTC by just highlighting a couple of lines in the code with added temperature measurement.
    I've tested both RTC'S over a week for each one and everyting seems to be working well, Well apart from one little bug which is do to with the temperature reading when it first starts up for the first minute it reads high on display but after that is works as it should, I'm looking into this should be easy to fix.


    So I thought I'd share the code with everyone and give a something back to the community.

    Code:
    '#########################################################################################################
    '# SET THE CLOCK FOR RTC (PCF8563 & DS1307) VIA THE SERIAL PORT FROM A COMPUTER TIME  (CHUCKIEBOY)
    '# THE PC SOFTWARE CAN BE DOWNLOADED FROM HERE
    '# http://www.protonbasic.co.uk/content...rs-with-PC-pic
    '# THIS WAS WROTE BY ADLIN SYSTEMS SO ALL CRDEIT GOES TO HIM 
    '# I JUST PLAYED AND MODIFIED THE SAMPLE CODE THAT CAME WITH IT AND GOT IT TO WORK WITH THE TIME SETTER
    '# I'VE ALSO ADDED TEMPERATURE READOUT IT SAMPLES THE TEMPERATURE EVERY 10 SECONDS (BUT CAN BE ALTERED)
    '# I'VE USED AND 18F45K80 AND EASYPIC 7 BOARD BUT YOU CAN USE WHAT YOU LIKE
    '##########################################################################################################
    Device = 18F45K80
    'SET UP THE CONFIGS 
      Config_Start
     RETEN = OFF ;ULTRA LOW-POWER REGULATOR IS DISABLED (CONTROLLED BY REGSLP BIT)
    INTOSCSEL = High ;LF-INTOSC IN HIGH-POWER MODE DURING SLEEP
     SOSCSEL = Dig ;DIGITAL (SCLKI) MODE
     XINST = OFF ;DISABLED
     FOSC = HS2  ;INTERNAL RC OSCILLATOR
     PLLCFG = OFF ;ENABLED
     FCMEN = OFF ;DISABLED
      IESO = OFF ;DISABLED
      PWRTEN = OFF ;DISABLED
      BOREN = On'SBORDIS ;ENABLED IN HARDWARE, SBOREN DISABLED
      BORV = 0 ;1.8V
      BORPWR = Low'ZPBORMV ;ZPBORMV INSTEAD OF BORMV IS SELECTED
      WDTEN = On ;WDT ENABLED IN HARDWARE; SWDTEN BIT DISABLED
      WDTPS = 1048576 ;1:1048576
    CANMX = PORTB ;ECAN TX AND RX PINS ARE LOCATED ON RB2 AND RB3, RESPECTIVELY
      MSSPMSK = MSK7 ;7 BIT ADDRESS MASKING MODE
     MCLRE = On ;MCLR ENABLED, RG5 DISABLED
     STVREN = On ;ENABLED
      BBSIZ = BB2K ;2K WORD BOOT BLOCK SIZE
     CP0 = OFF ;DISABLED
     CP1 = OFF ;DISABLEDFF
     CP2 = OFF ;DISABLED
     CP3 = OFF ;DISABLED
     CPB = OFF ;DISABLED
    CPD = OFF ;DISABLED
    WRT0 = OFF ;DISABLED
    WRT1 = OFF ;DISABLED
    WRT2 = OFF ;DISABLED
    WRT3 = OFF ;DISABLED
     WRTC = OFF ;DISABLED
     WRTB = OFF ;DISABLED
     WRTD = OFF ;DISABLED
     EBTR0 = OFF ;DISABLED
     EBTR1 = OFF ;DISABLED
     EBTR2 = OFF ;DISABLED
     EBTR3 = OFF ;DISABLED
     EBTRB = OFF ;DISABLED
    Config_End
    'SET UP THE SERIAL PORTS,DISPLAY & CLOCK SPEED
    Declare Xtal = 4   'SET CRYTSAL
    '
    Declare Hserial_Baud = 2400 ' SET BAUD RATE TO 9600
    Declare Hserial_RCSTA = 010000 ' ENABLE CONTINUOUS RECEIVE
    Declare Hserial_Clear = On ' CLEAR THE BUFFER BEFORE RECEIVING
    Declare Rsin_Timeout = 200
    Declare Rsin_Pin = PORTC.7  'SET THE RSIN PIN
    Declare Rsin_Mode = 0       'RS MODE
    Declare LCD_DTPort = PORTD
    Declare LCD_RSPin = PORTB.2
    Declare LCD_ENPin = PORTB.4
    Declare LCD_RWPin = PORTB.3
    Declare LCD_CS2Pin = PORTB.1
    Declare LCD_CS1Pin = PORTB.0
    Declare LCD_Type = Samsung
    Declare Internal_Font = On
    Declare Font_Addr = 0
    Declare GLCD_STROBE_DELAY = 20
    '############################
    '# SET UP THE VARIABLES
    '############################
        Dim TRUE As 1
        Dim FALSE As 0
        Dim LOGBYTE As Byte
        Dim RTCYEAR  As Byte
        Dim RTCDAY   As Byte
        Dim RTCMONTH As Byte
        Dim RTCDATE  As Byte
        Dim RTCHR    As Byte
        Dim RTCMIN   As Byte
        Dim RTCSEC   As Byte
        Dim BCD2BIN_VAL As Byte
        Dim BIN2BCD_VAL As Byte
        Dim FORCE_SET_TIME As Bit                   ' USED TO FORCE A TIME UPDATE MAINLY AFTER A DOGGY CLOCK READ
        Dim TEMP12 As Byte
        Dim TEMP21 As Byte
        Dim TDOW As Word
        Dim TIMER1REG As TMR1L.Word
        Dim INT_CNT_RELOAD As 50    ' RELOAD VALUE SO YOU CAN RUN CODE EVERY X X 100MS 50 = 1/2 SECOND
        Dim GIE As INTCON.7
        Dim LED1 As PORTC.5         ' THIS LED WILL SHOW THAT THE INTERRUPT IS WORKING
        Dim SEC As Byte     ' VARIABLE FOR SECONDS
        Dim TEMP5 As Byte
        Dim TEMP6 As Byte
        Dim VAR12 As Byte
        Dim TEMP As Float
     Dim ZWISCHEN As Float
     Dim NEG_POS As Byte 
     Dim DUM As Byte
        Dim MINUTE As Byte  ' VARIABLE FOR MINUTES
        Dim HOUR As Byte    ' VARIABLE FOR HOUR
        Dim GCT As Byte     ' VARIABLE FOR 100HZ COUNTER
        Dim TSF As Byte     ' THIS IS A FLAG (TIME SAVE FLAG) FOR WRITING TO EEPROM     
        Dim TEMPERATURE As Word
      Dim TIMER_FLAGS As Byte                    ' TIMER FLAGS 2  
        Dim ONE_SEC As TIMER_FLAGS.0                ' ONE SECOND FLAG
        Dim ONE_MIN As TIMER_FLAGS.1                ' ONE MINUTE FLAG
        
        Symbol DQ =PORTE.2             'DS1820 PORT
        Symbol DQ2 = PORTE.2           'DS1820 PORT
        Symbol SET_TIME = PORTC.1      'SET TO 5V TO SET TIME VIA JUMPER/SWITCH
        Symbol FUDGE_FACTOR = 7
        Symbol TMR1_VAL =((65536)-(XTAL*2500))+FUDGE_FACTOR ' CALCULATE OSC OFFSET VALUES FOR 100HZ INTERRUPT 
    '************SET UP THE A/D CONVERTER******************
       Declare All_Digital = TRUE   
    Declare    SCL_Pin = PORTC.3
    Declare    SDA_Pin = PORTC.4     
      
    '******************SET UP THE PORTS********************
       Cls
       DelayMS 150
       
       On_Interrupt TMR_INTERRUPT
       GoTo START  'JUMP TO THE SETUP MENU
    TMR_INTERRUPT:
    Context Save
    Clear T1CON.0 ' STOP THE TIMER
    TIMER1REG = TIMER1REG + TMR1_VAL ' LOAD TMR1
    Set T1CON.0
      Inc GCT                                   ' INCREASE GENERAL COUNTER
    If GCT = 100 Then Inc SEC : GCT = 0       ' WHEN 100 (HZ) REACHED INCREASE SECONDS AND CLEAR GCT
    If SEC = 60 Then Inc MINUTE : SEC = 0     ' WHEN 60 SECONDS REACHED, INCREASE MINUTE AND CLEAR SECONDS
    If MINUTE = 60 Then Inc HOUR : MINUTE = 0 ' WHEN 60 MINUTES REACHED, INCREASE HOUR
    Clear PIR1.0 ; CLEAR TMR1 INTERRUPT  FLAG
    Context Restore
    INITIALISE_INTERRUPTS:
    TIMER1REG = TMR1_VAL
    T1CON = 000000 'SET UP TMR1 TO  HAVE 1:1 PRESCALER AND ACT AS A TIMER
    PIR1.0 = 0 'CLEAR TMR1  INTERRUPT FLAG
    INTCON = 000000 'GLOBAL AND  PERIPHERAL INTERRUPTS ON
    PIE1.0 = 1 'ENABLE TMR1 AS PERIPHERAL INTERRUPT SOURCE
    T1CON.0 = 1
    Return
     TSF =0
     SEC=0
     START:
    GoSub  INITIALISE_INTERRUPTS
     TSF =0
     SEC=5
        OWrite DQ2, 1, [$CC, $44]  'INSTRUCT SENSOR 2 TO CHECK THE TEMPERATURE
      OWrite DQ, 1, [$CC, $44]   'INSTRUCT SENSOR 1 TO CHECK THE TEMPERATURE
            While ORead DQ, 4 = 0 : Wend  'WAIT UNTIL SENSOR 1 IS FINISHED CHECKING THE TEMPERATURE 
     OWrite DQ, 1, [$CC, $BE] 'INSTRUCT SENSOR 1 TO REPORT THE CURRENT TEMPERATURE
      ORead DQ, 0, [TEMPERATURE.LowByte, TEMPERATURE.HighByte, DUM,DUM,DUM,DUM, DUM]  'READ TEMPERATURE FROM SENSOR 1
              NEG_POS=" "   'SET THE POSITIVE/NEGATIVE SYMBOL TO "+"
     If TEMPERATURE.8=1 Then  'CHECK RECEIVED DATA TO SEE IF THE TEMP IS ABOVE OR BELOW 0
         NEG_POS="-"   'TEMPERATURE IS BELOW 0, CALCULATE THE TEMPERATURE AND SET THE SYMBOL
         TEMPERATURE = TEMPERATURE.LowByte^111111 'CALCULATE THE TEMPERATURE
         TEMPERATURE = TEMPERATURE.LowByte >> 1         'CALCULATE THE TEMPERATURE
          TEMP = TEMPERATURE - 0.25                   'CALCULATE THE TEMPERATURE
         ZWISCHEN = (16 - DUM) / 16                   'CALCULATE THE TEMPERATURE
          TEMP = TEMP + 1.5 - ZWISCHEN                'CALCULATE THE TEMPERATURE
      Else    'TEMP IS ABOVE 0 CALCULATE TEMP
          TEMP = ((TEMPERATURE >> 1) - 0.25)          'CALCULATE THE TEMPERATURE
          ZWISCHEN = (16 - DUM) / 16                  'CALCULATE THE TEMPERATURE
          TEMP = TEMP + ZWISCHEN                       'CALCULATE THE TEMPERATURE
      EndIf 
    If SET_TIME = 1 Then          'JUMPER USED TO SET TIME ON FIRST POWER UP
    GoTo LPLP1
    ElseIf SET_TIME =0 Then       'IF NOT HIGH THEN GOTO MAIN PROGRAM TIME SETTING NOT NEEDED
    GoTo MAIN
    EndIf
    GoTo START
    LPLP1:
          Print At 1,3,"SETTING THE TIME"
          Print At 2,1,"PRESS SYNC BUTTON"
          Print At 3,1,"ON TIMESETTER "
         HRSIn {HSCONT1},LOGBYTE 
         If LOGBYTE="J" Then GoTo SUB_PC_SET
    HSCONT1:
    Cls
    Print At 2,1,"INCORRECT DATA/"
    Print At 3,1,"NO DATA TRY"
    Print At 4,1,"AGAIN"
    DelayMS 1000
    Cls
         GoTo LPLP1 
     MAIN:
     GoSub GETTIME    'GET THE TIME FROM THE RTC
     If SEC = TSF  Then   ' SAMPLES THE TEMPERATURE READING EVRY 10 SECONDS SAVES THE READINGS GOING SILLY
           OWrite DQ2, 1, [$CC, $44]  'INSTRUCT SENSOR 2 TO CHECK THE TEMPERATURE
      OWrite DQ, 1, [$CC, $44]   'INSTRUCT SENSOR 1 TO CHECK THE TEMPERATURE
            While ORead DQ, 4 = 0 : Wend  'WAIT UNTIL SENSOR 1 IS FINISHED CHECKING THE TEMPERATURE 
     OWrite DQ, 1, [$CC, $BE] 'INSTRUCT SENSOR 1 TO REPORT THE CURRENT TEMPERATURE
      ORead DQ, 0, [TEMPERATURE.LowByte, TEMPERATURE.HighByte, DUM,DUM,DUM,DUM, DUM]  'READ TEMPERATURE FROM SENSOR 1
              NEG_POS=" "   'SET THE POSITIVE/NEGATIVE SYMBOL TO "+"
     If TEMPERATURE.8=1 Then  'CHECK RECEIVED DATA TO SEE IF THE TEMP IS ABOVE OR BELOW 0
         NEG_POS="-"   'TEMPERATURE IS BELOW 0, CALCULATE THE TEMPERATURE AND SET THE SYMBOL
         TEMPERATURE = TEMPERATURE.LowByte^111111 'CALCULATE THE TEMPERATURE
         TEMPERATURE = TEMPERATURE.LowByte >> 1         'CALCULATE THE TEMPERATURE
          TEMP = TEMPERATURE - 0.25                   'CALCULATE THE TEMPERATURE
         ZWISCHEN = (16 - DUM) / 16                   'CALCULATE THE TEMPERATURE
          TEMP = TEMP + 1.5 - ZWISCHEN                'CALCULATE THE TEMPERATURE
      Else    'TEMP IS ABOVE 0 CALCULATE TEMP
          TEMP = ((TEMPERATURE >> 1) - 0.25)          'CALCULATE THE TEMPERATURE
          ZWISCHEN = (16 - DUM) / 16                  'CALCULATE THE TEMPERATURE
          TEMP = TEMP + ZWISCHEN                       'CALCULATE THE TEMPERATURE
      EndIf 
         
     TSF = TSF + 5  ' INCREASE TSF 
      
    If TSF = 60 Then TSF = 0    'ROLL OVER MINUTE INTO HOURS 
            EndIf
        Print At 0,1,"CLOCK & TEMPERATURE"
      Print At 3,1, Dec2 RTCHR,":",Dec2 RTCMIN,":",Dec2 RTCSEC," "     ' PRINT TIME        
        Print At 4,1,Dec2 RTCDATE,"/",Dec2 RTCMONTH,"/20",Dec2 RTCYEAR
        Print At 5,1,"sec ",Dec SEC,";TFS ",Dec TSF,"  "
        Print At 7, 1,"TEMP:",NEG_POS,Dec1 TEMP,"C "
         GoTo MAIN
     
    
    GETTIME:
    'WHICH EVER RTC YOU WANT TO USE JUST HIGHLIGHT OR UN-HIGHLIGHT OUT ONE OF THESE LINES
    '####################### USED FOR DS1307 RTC #########################################
     '  HBusIn $D1,0,[RTCSEC,RTCMIN,RTCHR,RTCDAY,RTCDATE,RTCMONTH,RTCYEAR] ' READ THE TIME FROM RTC
    '#####################################################################################
    '######################### READS THE PCF8563 #########################################
     HBusIn $A2,2,[RTCSEC,RTCMIN,RTCHR,RTCDATE,RTCDAY,RTCMONTH,RTCYEAR] ' READ IN THE TIME 
    '#######################################################################################  
            RTCMIN.7 = 0                           ' MASK OFF ALL UNWANTED BITS
            RTCHR.7 = 0
            RTCHR.6 = 0
            RTCDATE = RTCDATE & 111111
            RTCMONTH = RTCMONTH & 011111
            
            BCD2BIN_VAL = RTCYEAR                   ' CONVERT THE YEARS
            GoSub BCD_TO_BIN1
            RTCYEAR = BCD2BIN_VAL
            '---------------------
            BCD2BIN_VAL = RTCMONTH                  ' CONVERT THE MONTHS
            GoSub BCD_TO_BIN1
            RTCMONTH = BCD2BIN_VAL
            '---------------------
            BCD2BIN_VAL = RTCDATE                   ' CONVERT THE DAYS
            GoSub BCD_TO_BIN1
            RTCDATE = BCD2BIN_VAL
            '---------------------
            BCD2BIN_VAL = RTCHR                     ' CONVERT THE HOURS
            GoSub BCD_TO_BIN1
            RTCHR = BCD2BIN_VAL
            '---------------------
            BCD2BIN_VAL = RTCMIN                    ' CONVERT THE MINS
            GoSub BCD_TO_BIN1
            RTCMIN = BCD2BIN_VAL
            '---------------------
            BCD2BIN_VAL = RTCSEC                    ' CONVERT THE SECONDS
            GoSub BCD_TO_BIN1
            RTCSEC = BCD2BIN_VAL
            
            If RTCYEAR = 0 Then 
                RTCYEAR = 1                         ' WE CANNOT HAVE THE YEAR AS 00 (SCREWS THINGS UP)
                FORCE_SET_TIME = TRUE
            EndIf
            
            If RTCMONTH = 0  Then                    ' THESE CHECKS HAVE BEEN ADDED TO ENSURE THE SYSTEM WORKS PROPERLY
                RTCMONTH = 1                         ' BY NOT LETTING ANY OF THE DATES DAY ETC = 0
                FORCE_SET_TIME = TRUE  
            EndIf                    
            
           If FORCE_SET_TIME = TRUE Then 
                FORCE_SET_TIME = FALSE
               GoSub SETTIME                        ' RESET TIME AGAIN       
            EndIf
            Return
           
            SETTIME:
        BIN2BCD_VAL = RTCMIN
        GoSub BIN_TO_BCD1                           ' CONVERT THE MINS TO COMPACT BCD
        RTCMIN = BIN2BCD_VAL
        
        BIN2BCD_VAL = RTCHR 
        GoSub BIN_TO_BCD1                           ' CONVERT THE HOURS TO COMPACT BCD
        RTCHR = BIN2BCD_VAL
        
       BIN2BCD_VAL = RTCDATE 
        GoSub BIN_TO_BCD1                           ' CONVERT THE DATE TO COMPACT BCD
        RTCDATE  = BIN2BCD_VAL
        
        BIN2BCD_VAL = RTCMONTH 
        GoSub BIN_TO_BCD1                           ' CONVERT THE MONTH TO COMPACT BCD
        RTCMONTH = BIN2BCD_VAL
        
        BIN2BCD_VAL = RTCYEAR 
        GoSub BIN_TO_BCD1                           ' CONVERT THE YEAR TO COMPACT BCD
        RTCYEAR = BIN2BCD_VAL
    'WHICH EVER RTC YOU WANT TO USE JUST HIGHLIGHT OR UN-HIGHLIGHT OUT ONE OF THESE LINES
    '####################### USED FOR DS1307 RTC #########################################
       '  HBusOut $D0,0,[RTCSEC,RTCMIN,RTCHR,RTCDAY,RTCDATE,RTCMONTH,RTCYEAR] ' NOW WRITE OUT THE TIME
    '#####################################################################################
       
    '######################### READS THE PCF8563 #########################################
       HBusOut $A2,2,[RTCSEC,RTCMIN,RTCHR,RTCDATE,RTCDAY,RTCMONTH,RTCYEAR] ' NOW WRITE OUT THE TIME
    '#######################################################################################  
          DelayMS 50                                  ' SHORT DELAY JUST INCASE
          Return
       
    
    '-----------------------------------------------
    ' BCD CONVERSION ROUTINES
    ' 
    '-----------------------------------------------      
            
    BCD_TO_BIN1:
        TEMP5 = BCD2BIN_VAL & $F                    ' CONVERT THE VALUES FROM BCD TO BIN
        TEMP6 = BCD2BIN_VAL & $F0                   ' MASK OF EITHER SIDE
        TEMP6 = TEMP6 >>4                           ' DIVIDE BY 16
        TEMP6 = TEMP6 * 10                          ' X BY 10
        BCD2BIN_VAL = TEMP5 + TEMP6                 ' NOW ADD THE FIRST NUMBER YOU THOUGHT OF
        Return
        
    BIN_TO_BCD1:
        TEMP5 = Dig BIN2BCD_VAL, 0                  ' GET THE DEC DIGIT FOR THE FIRST NIBBLE
        TEMP6 = Dig BIN2BCD_VAL, 1                  ' GET THE DEC DIGIT FOR THE FIRST NIBBLE
        TEMP6 = TEMP6 << 4                          ' MOVE NUMBER OVER TO 2ND NIBBLE
        BIN2BCD_VAL = TEMP5 ^ TEMP6                 ' XOR THEM TOGTHER TO MAKE THE WHOLE BCD NUMBER 
        Return
     
    SUB_PC_SET:
             HRSIn VAR12                               'WAIT FOR SERIAL PORT
             TEMP12 =Dig VAR12,0                          'BCD CONVERSION
             TEMP21 = Dig VAR12,1                          'BCD CONVERSION
             RTCHR = (TEMP21*16)+TEMP12                   'BCD CONVERSION
            HRSIn VAR12                               'WAIT FOR SERIAL PORT
             TEMP12 = Dig VAR12,0                          'BCD CONVERSION
             TEMP21 = Dig VAR12,1                          'BCD CONVERSION 
             RTCMIN = (TEMP21*16)+TEMP12                 'BCD CONVERSION
           HRSIn VAR12                               'WAIT FOR SERIAL PORT
             TEMP12 = Dig VAR12,0                          'BCD CONVERSION
             TEMP21 = Dig VAR12, 1                          'BCD CONVERSION
             RTCSEC = (TEMP21*16)+TEMP12                 'BCD CONVERSION
            HRSIn VAR12                               'WAIT FOR SERIAL PORT
             TEMP12 = Dig VAR12, 0                          'BCD CONVERSION
             TEMP21 = Dig VAR12,1                          'BCD CONVERSION
             RTCDAY = (TEMP21*16)+TEMP12                    'BCD CONVERSION
           HRSIn VAR12                               'WAIT FOR SERIAL PORT
             TEMP12 = Dig VAR12, 0                          'BCD CONVERSION
             TEMP21 = Dig VAR12,1                          'BCD CONVERSION
             RTCMONTH = (TEMP21*16)+TEMP12                  'BCD CONVERSION
            HRSIn VAR12                               'WAIT FOR SERIAL PORT
             RTCYEAR = VAR12                               'BCD CONVERSION
           HRSIn VAR12                               'WAIT FOR SERIAL PORT
             TDOW = VAR12*32
              VAR12=TDOW+RTCMONTH
    '####################### USED FOR DS1307 RTC #########################################
       '  HBusOut $D0,0,[RTCSEC,RTCMIN,RTCHR,RTCDAY,RTCDATE,RTCMONTH,RTCYEAR] ' NOW WRITE OUT THE TIME
    '#####################################################################################
       
    '######################### READS THE PCF8563 #########################################
       HBusOut $A2,2,[RTCSEC,RTCMIN,RTCHR,RTCDATE,RTCDAY,RTCMONTH,RTCYEAR] ' NOW WRITE OUT THE TIME
    '#######################################################################################  
          
         Cls                                 'DELAY
            GoTo MAIN
          Include "FONT.INC"
    This article was originally published in forum thread: Setting PCF8583 RTC with PC started by ADLIN SYSTEMS View original post