• PicŪ Basic


  • How to Display and adjust an RTC

    In response to a Request on the forum I rewrote this bit of a larger lump of code to Display the time. This uses the DS1307 (I2C) which I am told some people have problems getting to work, so I have left some lines in I use to get the RTC setup and going, after which I remove them. I have used this code as a snippet in many programs with some recent updates thanks to TIM and has worked reliably .

    The Button comands I am still not happy with but works OK.

    Example Code

    Device 16F876 ' We're using a 16F876 device with DS1307 RTC
    ' and 2*32 LCD default connection

    REMARKS on ' Enable remarks in the source
    Declare XTAL 4
    Declare SDA_PIN PortC.4 ' I2C
    Declare SCL_PIN PortC.3

    Adcon1 = 7

    Symbol SOUT=PortB.0 ' Assign the Speaker PortB.0

    dim INDX as Byte
    dim LDAT as Byte
    dim SEC as byte
    dim MN as byte
    dim HOUR as byte 'Time Var
    dim DAY as byte
    dim DATE as byte
    dim MONTH as byte
    dim YEAR as byte
    dim VR as byte

    dim BIN_RVAL as byte
    dim BCD_RVAL as byte
    dim BIN2BCD_VAL as byte
    dim BCD2BIN_VAL as byte 'BCD/BIN Conver Var
    dim TEMP1 as Byte
    dim TEMP2 as byte

    dim Key as byte
    dim KeyD as byte
    dim BTNVAR1 as byte 'Button Var
    dim BTNVAR2 as byte
    dim BTNVAR3 as byte

    Data "SunMonTueWedThuFriSatJanFebMarAprMayJunJlyAugSepO ctNovDec"

    Delayms 50 'powerup delay
    gosub HI 'Start up Beep
    'Busout $D0,$00,[$00] 'Start the clock
    'Busout $D0,$07,[$10]

    'Busout $D0,$02,[$18] '2lines to set the time
    'Busout $D0,$01,[$27]
    'Busout $D0,$03,[$05] 'Day
    'Busout $D0,$04,[$29] 'Date
    'Busout $D0,$05,[$04] 'Month
    'Busout $D0,$06,[$04] 'Year

    'Busout $D0,$07,[$11] 'LED on
    'Busout $D0,$07,[$10] 'LED Flash Output from DS 1307
    'Busout $D0,$07,[$00] 'LED off

    Start:
    Cls
    gosub ZROBTNVAR
    RunDisp: 'Main Prog
    Gosub Disp_TIM
    Key = 0
    KeyD = 255
    gosub Keys
    If Key = 1 Then gosub ZROBTNVAR : goto MinAdj
    goto RunDisp
    MinAdj:
    Cursor 2,2
    Print "Adj Min "
    Gosub TimAdj
    If Key = 1 then gosub ZROBTNVAR : goto HourAdj
    If Key = 2 then gosub MinP
    If Key = 3 then goto Start
    goto MinAdj
    HourAdj:
    Cursor 2,2
    Print "Adj Hour"
    Gosub TimAdj
    If Key = 1 then gosub ZROBTNVAR : goto DayAdj
    If Key = 2 then gosub HourP
    If Key = 3 then goto Start
    goto HourAdj
    DayAdj:
    Cursor 2,2
    Print "Adj Day/Date"
    Gosub TimAdj
    If Key = 1 then gosub ZROBTNVAR : goto MonthAdj
    If Key = 2 then gosub DayP
    If Key = 3 then goto Start
    goto DayAdj
    MonthAdj:
    Cursor 2,2
    Print "Adj Month/Year"
    Gosub TimAdj
    If Key = 1 then gosub ZROBTNVAR : goto MinAdj
    If Key = 2 then gosub monthP
    If Key = 3 then goto Start
    goto monthAdj
    TimAdj:
    Gosub PrtPN
    KeyD = 15
    Gosub Disp_Tim
    Key = 0
    gosub Keys
    Return
    MinP:
    BCD2BIN_VAL = MN
    gosub BCD2BIN
    BIN_RVAL = BIN_RVAL + 1
    If BIN_RVAL > 59 Then BIN_RVAL = 0
    BIN2BCD_VAL = BIN_RVAL
    gosub BIN_TO_BCD
    Busout $D0,$01 ,[BCD_RVAL] 'Save Mins
    return
    HourP:
    BCD2BIN_VAL = Hour
    gosub BCD2BIN
    BIN_RVAL = BIN_RVAL + 1
    If BIN_RVAL > 23 Then BIN_RVAL = 0
    BIN2BCD_VAL = BIN_RVAL
    gosub BIN_TO_BCD
    Busout $D0,$02,[BCD_RVAL] 'Save Hours
    return
    DayP:
    Day = Day + 1
    If Day > 7 Then Gosub DateP
    If Day > 7 Then Day = 1
    Busout $D0,$03,[Day] 'Save Day
    return
    DateP:
    BCD2BIN_VAL = Date
    gosub BCD2BIN
    BIN_RVAL = BIN_RVAL + 1
    If BIN_RVAL > 31 Then BIN_RVAL = 1
    BIN2BCD_VAL = BIN_RVAL
    gosub BIN_TO_BCD
    Busout $D0,$04,[BCD_RVAL] 'Save Date
    return
    MonthP:
    Month = month + 1
    If Month > 12 Then Gosub YearP
    If month > 12 Then Month = 1
    Busout $D0,$05,[month] 'Save Month
    return
    YearP:
    BCD2BIN_VAL = Year
    gosub BCD2BIN
    BIN_RVAL = BIN_RVAL + 1
    If BIN_RVAL > 20 Then BIN_RVAL = 0
    BIN2BCD_VAL = BIN_RVAL
    gosub BIN_TO_BCD
    Busout $D0,$06,[BCD_RVAL] 'Save Years
    return
    Disp_TIM:
    Busin $D1,$3F,[sec,sec,mn,hour,day,date,month,year]
    Cursor 1,4 'Set Display to beginning
    Vr=(day*3)-3 ' Display Day/Date
    Restore Vr
    For Indx=0 to 2
    read Ldat
    Print Ldat
    Next
    Cursor 1,8
    BCD2BIN_VAL=date
    gosub BCD2BIN
    Gosub DispBin
    Cursor 1,11
    Vr = (((month & $f0)>>4)*10)+(month & $0f)
    Vr=(Vr*3)+18
    Restore Vr
    For Indx=0 to 2
    read Ldat
    Print Ldat
    Next
    Print " 20"
    BCD2BIN_VAL=year
    gosub BCD2BIN
    Gosub DispBin
    Cursor 1,22
    BCD2BIN_VAL=hour ' Display Hours/Mins
    gosub BCD2BIN
    Gosub DispBin
    Print ":"
    BCD2BIN_VAL=mn
    gosub BCD2BIN
    Gosub DispBin
    Print ":"
    BCD2BIN_VAL=sec
    gosub BCD2BIN
    Gosub DispBin
    Return
    BIN_TO_BCD:
    TEMP1 = DIG BIN2BCD_VAL, 0
    TEMP2 = DIG BIN2BCD_VAL, 1
    TEMP2 = TEMP2 << 4 ' MOVE NUMBER OVER TO 2ND NIBBLE
    BCD_RVAL = TEMP1 ^ TEMP2
    Return
    BCD2BIN:
    TEMP1 = BCD2BIN_VAL & $F ' CONVERT THE VALUES FROM BCD TO BIN
    TEMP2 = BCD2BIN_VAL & $F0 ' MASK OF EITHER SIDE
    TEMP2 = TEMP2 >>4 ' DIVIDE BY 16
    TEMP2 = TEMP2 * 10 ' X BY 10
    BIN_RVAL = TEMP1 + TEMP2 ' NOW ADD THE FIRST NUMBER YOU THOUGHT OF
    Return
    DispBin:
    Print Dec2 BIN_RVAL
    Return
    PrtPN:
    Cursor 2,25
    Print "Adj +"
    Return
    ZROBTNVAR:
    Gosub Click
    BTNVAR1 = 0 : BTNVAR2 = 0 : BTNVAR3 = 0
    Return
    Keys:
    Gosub ConDly
    Button PortA.3,1,KeyD,1,BTNVAR1,1,Key1
    Button PortA.4,1,KeyD,1,BTNVAR2,1,Key2
    Button PortA.5,1,KeyD,1,BTNVAR3,1,Key3
    Return
    Key1:
    Key = 1
    Return
    Key2:
    Key = 2
    Return
    Key3:
    Key = 3
    Return
    ConDly:
    Delayms 30
    Return
    Click:
    Sound SOUT,[120,10]
    return
    HI:
    Sound SOUT,[70,25]
    Sound SOUT,[100,15]
    Return

    End
    contributed by Andy C.