real time clock


Closed Thread
Results 1 to 40 of 45

Thread: real time clock

Hybrid View

  1. #1
    maria's Avatar
    maria Guest

    Default real time clock

    Hi there,

    I'm using PIC BASIC with a PIC16F877. I'm wondering if there's anyone out there who could tell me how to connect a real time clock IC (the Dallas DS1302 is the one I have) to the PIC and get the PIC to display the time on an LCD? I have my LCD up and running so I wouldnt need too much detail on the LCD bit.

    Thanks!

    Best Wishes,
    Maria.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Have a look at the RTC thread in ths section (set your display to show posts from the last 30 days or more).

    PS. Posts do get read by folks regardless what section you post them in... posting in multiple sections is unnescessary.

  3. #3
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: real time clock

    I know this is old thread but have a need fro RTC and its using the ds1302

    i did have a quick question in relation to how the I/O port is set on the PIC for I/O the interface to the DS1302 , in that the sample code does not change TRIS bits on the PIC from an output to an input for when it doing a read ( shiftin command ) of the DS1302 -

    does issuing the shiftin command automatically change the tris bits for that port to allow input ???
    cheers

    Sheldon

  4. #4
    Join Date
    Apr 2012
    Location
    Ptolemaida
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: real time clock

    Hello longpole, i've been reading the thread today and some others about 1302 and i think shiftin and shiftout does all the work with tris and with clk. Just send the right registers for reading/writing and u should be fine.

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Maria,

    You might want to try this code. This is how I control this chip. If you need help with it just ask and I'll see if I can provide you some assistance.

    '-------------------------- Clock Variables ---------------------------------

    rst var portb.2 ' DS1302 Reset Pin
    clk var portb.1 ' DS1302 Clock Pin
    dq var portb.0 ' DS1302 Data Pin

    '----------------------- Write Commands For DS1302 --------------------------

    writectrl con $8E ' Control byte
    writeram con $80 ' Write to RAM
    writeprotect con $00 ' Write-protect DS1302
    writesec con $80 ' Write seconds
    writemin con $82 ' Write minutes
    writehour con $84 ' Write hour
    writedate con $86 ' Write date
    writemonth con $88 ' Write month
    writeyear con $8C ' Write year

    '------------------------- Read Commands For DS1302 -------------------------

    readsec con $81 ' Read seconds from DS1302
    readmin con $83 ' Read minutes from DS1302
    readhour con $85 ' Read hours from DS1302
    readdate con $87 ' Read date from DS1302
    readyear con $8D ' Read year from DS1302
    readmonth con $89 ' Read month from DS1302

    '------------------------------ Time Variables ------------------------------

    mem var byte ' Temporary data holder
    outbyte var byte ' Second byte to ds1302
    reg_adr var byte ' First byte to DS1302
    date var byte ' Date variable
    ss var byte ' Seconds variable
    mm var byte ' Minutes varibale
    hh var byte ' Hours variable
    mo var byte ' Month variable
    yr var byte ' Year variable

    '------------------------ Initial Settings For Ports ------------------------

    low rst ' Set reset pin low
    low clk ' Set clock pin low

    trisb = 0
    trisa = 0

    '----------------------- Set Initial Time Variables -------------------------

    if portb.4 = 1 then START ' Set inital clock start up if jumper is present
    reg_adr = writectrl ' Set to control byte
    outbyte = writeprotect ' Set turn off protection
    gosub w_out ' Send both bytes
    reg_adr = writesec ' Set to write seconds register
    outbyte = $00 ' Set to write 00 to seconds register
    gosub w_out
    reg_adr = writemin
    outbyte = $30
    gosub w_out
    reg_adr = writehour
    outbyte = $00
    gosub w_out
    reg_adr = writedate
    outbyte = $01
    gosub w_out
    reg_adr = writemonth
    outbyte = $01
    gosub w_out
    reg_adr = writeyear
    outbyte = $00
    gosub w_out
    reg_adr = writectrl
    outbyte = writeprotect
    gosub w_out

    start:
    reg_adr = readsec ' Read seconds
    gosub w_in
    ss = mem
    reg_adr = readmin ' Read minutes
    gosub w_in
    mm = mem
    reg_adr = readhour ' Read Hours
    gosub w_in
    hh = mem
    reg_adr = readyear ' Read Year
    gosub w_in
    yr = mem
    reg_adr = readdate ' Read Date
    gosub w_in
    date = mem
    reg_adr = readmonth ' Read Month
    gosub w_in
    mo = mem
    lcdout $fe,1,"TIME:",HEX2 hh,":",HEX2 mm,":",HEX2 ss
    pause 500
    goto start

    '----------------------- Time Commands Subroutines --------------------------

    w_in:
    mem = reg_adr ' Set mem variable to reg_adr contents
    high rst ' Activate the DS1302
    shiftout dq,clk,0, [mem] ' Send control byte
    shiftin dq,clk,1, [mem] ' Retrieve data in from the DS1302
    low rst ' Deactivate DS1302
    return

    w_out:
    mem = reg_adr ' Set mem variable to reg_adr contents
    high rst ' Activate the DS1302
    shiftout dq,clk,0, [mem] ' Send control byte
    mem = outbyte ' Set mem variable to outbyte contents
    shiftout dq,clk,0, [mem] ' Send data stored in mem variable to DS1302
    low rst ' Deactivate DS1302
    return

  6. #6


    Did you find this post helpful? Yes | No

    Default

    when i use this code, with a serout commando the compiler give a error on the code HEX2.

    And when i leave the HEX2 in the serout and replace them bij # the counting gives problems what does the hex2 does, and how can i display the time and date on a console pc with the serout command?

  7. #7
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    RTFM

    There is no HEX modifier with SEROUT, use SEROUT2 instead.

    see PBP Manual Section 5.73 & 5.74
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  8. #8


    Did you find this post helpful? Yes | No

    Default

    already fix the problem use the and and shift >>4 function

  9. #9
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    Hey CocaColaKid, thanx for the code it worked, however, I suggest an improvement as stated in the DS1302 datasheet U R supposed to read
    the seconds again at the end in case the second ticks over from 59 to
    00 while you were reading the mins, hrs, etc. this could result in erroneous
    Real Time reading.

    Suggest modification here:
    ss var byte 'seconds
    ss2 var byte 'seconds comparison byte

    start:
    reg_adr = readsec ' Read seconds
    gosub w_in
    ss = mem
    reg_adr = readmin ' Read minutes
    gosub w_in
    mm = mem
    reg_adr = readhour ' Read Hours
    gosub w_in
    hh = mem
    reg_adr = readyear ' Read Year
    gosub w_in
    yr = mem
    reg_adr = readdate ' Read Date
    gosub w_in
    date = mem
    reg_adr = readmonth ' Read Month
    gosub w_in
    mo = mem
    reg_adr = readsec ' Read seconds
    gosub w_in
    ss2 = mem
    IF ss <> ss2 THEN start 'COMPARE FIRST AND SECOND READINGS AND TRASH IF DIFFERENT.
    lcdout $fe,1,"TIME:",HEX2 hh,":",HEX2 mm,":",HEX2 ss
    pause 500
    goto start

    Cheers, Art.

  10. #10
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: real time clock

    yes it does change the input /output of the tris control , how ever it not documented in any of the manuals or online help files that i have found to date

  11. #11
    bahattinkor's Avatar
    bahattinkor Guest


    Did you find this post helpful? Yes | No

    Smile Problem with DS1302 and Proton+

    dear friends,

    I have a problem with the DS1302, PIC16f877 and PIC Proton+ combination. I designed a basic circuit in PROTEUS, and put your code in it. I worked fine. There was no problem. but when I made that circuit real, I met with problems. When I powered the PIC I can not see the time runing. The only thing that I see is " 00:00:80" and it always stays same. I used portd.0,d1 and d2 in the connection of PIc and DS1302. I used 32.768khz crystal. And there is no any mistake in connections. How can I fix this problem ?
    And one more small question. How can I reset LCD ? in PicBasic PRo I used to written "Flags=0". In Proton+ what should I write instead of this ?

  12. #12
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    For info on Proton... www.picbasic.org/forum

    I doubt PROTON have this option or it's hidden somewhere in the Help file, or somewhere on the forum.

    BUT try
    Code:
    Print $fe,$28 
    Print $fe,$0c 
    Print $fe,1
    This should work
    Last edited by mister_e; - 27th July 2005 at 03:38.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  13. #13
    bot402's Avatar
    bot402 Guest


    Did you find this post helpful? Yes | No

    Default

    You should know better than that Steve. The PROTON does everything the PBP does, and a whole lot more!

    To reset the LCD, simply use: -

    Dim BPF as Byte SYSTEM ' Bring the system variable into BASIC

    Clear BPF ' Force a reset on the LCD the next time it's used

    However, this is the wrong forum for the PROTON compiler. As long as your not using a pirated version of the compiler, you are welcome to use the dedicated PROTON forum

  14. #14
    Join Date
    Sep 2004
    Location
    Mentor, Ohio
    Posts
    352


    Did you find this post helpful? Yes | No

    Smile

    Hello bahattinkor,

    I had the same problem for 2 days using a DS1337. It displayed the default time and if I programmed a new starting time that would display but would continue from there. This was on a printed circuit board that was already working but had a little accident with a floating wire that went into the side of the power supply box and blew everything up! After replacing most of the parts the board worked for a few days then quit. I put new chips on the board again but to no gain. I put all of the same parts into a newer board and they worked fine. Went back to the old board and found no connection between one side of the crystal and the DS 1337.

    The moral of this story is triple check your connections and use a meter!

    BobK

  15. #15
    bahattinkor's Avatar
    bahattinkor Guest


    Did you find this post helpful? Yes | No

    Unhappy Still not working

    yes, I checked connections. voltages are normal but when I measured the cyrstal of Ds1302 with ossiloscope, I did not see any frequency. The ossilator does not ossilates. then I change DS1302 and 32.768Khz cyrstal with new ones but they are still not working. Can it be related with the Vcc that I used. I use 5V for Vcc for DS1302. Or Should I use any resistor or capacitor plus for the DS1302.

  16. #16
    engrehaf's Avatar
    engrehaf Guest


    Did you find this post helpful? Yes | No

    Default Re: real time clock

    hey guys i am a new member here
    i want some help in this topic if you can help

    HEX2 !! what it is ?
    because i use this sample code for 7 segment display, but i cannot deal with these variable like HEX2 hourx, HEX2 minutex and HEX2 seconds ???

    and thank you

  17. #17
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: real time clock

    From the manual
    BIN, DEC and HEX may also be followed by a number. Normally, these modifiers display exactly as many digits as are necessary, zero blanked (leading zeros are not sent). However, if a number follows the modifier, SEROUT2 will always send that number of digits, adding leading zeros as necessary. It will also trim of any extra high order digits. For example, BIN6 8 would send A001000" and BIN2 8 would send A00".
    Dave
    Always wear safety glasses while programming.

  18. #18
    Join Date
    Aug 2008
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: real time clock

    hello know the post and very old I'm in need of a little help on this clock project using the DS1302 with alarm it even works more Think it has error, If anyone can help me I thank here all the files + simulation
    Attached Files Attached Files

Similar Threads

  1. Real Time Clock
    By in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd June 2012, 04:52
  2. real time clock - PCF8583
    By maria in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 15th April 2010, 15:41
  3. Real time clock ICs
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 66
    Last Post: - 20th October 2008, 16:05
  4. Real Time Clock
    By savnik in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th December 2006, 02:02
  5. Real time clock... what a headache!
    By Eng4444 in forum mel PIC BASIC
    Replies: 2
    Last Post: - 8th June 2006, 21:56

Members who have read this thread : 4

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