Shop OBEX P1 Docs P2 Docs Learn Events
Writing to Ds1302 — Parallax Forums

Writing to Ds1302

SassySassy Posts: 21
edited 2007-03-27 23:55 in BASIC Stamp
I have been looking for an example propgram that I saw on this Forum for writing to the Ds1302?
Any one know where?

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-25 23:47
    Sassy,

    Would this be the code in question? Take care.

    http://forums.parallax.com/showthread.php?p=531080

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • SassySassy Posts: 21
    edited 2007-03-26 23:48
    Chis,

    I am new and this program workrd great with debugin and out.....But I tried all day to get it to read and write with out the degugger!

    trying to write a three digit number and read it back and have it backed up with the clocks battery
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-27 01:16
    Sassy,

    What exactly is it you’re trying to do?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • SassySassy Posts: 21
    edited 2007-03-27 10:12
    Chris,

    I am trying to·write a number, Lets say 123 into the DS1302 Ram. This number will be backed up with a battery so if the program crashes and restarts this number will be in memory. This number can·and will change on certain conditions and I just need to be able to simply write it and read and display it.

    I realaize you can write and read to the stamp using:

    write 0,123 with the address as 0, but I just can not figure out how you did it in your program to write and read...I tried all day yesterday to get it to work!!!!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-27 15:06
    Sassy,

    The code I linked above explains in detail how to write to the DS1302 RAM. In the code the comments explain exactly what is necessary and there is even a template for creating your own program without all the demo overhead. Are you having issues with that code?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-27 15:09
    Sassy,

    Almost forgot…what is the range of the numbers? A number such as 123 only takes on byte to write, but if the number will ever be greater than 255 then it would take two bytes to store it. Using the subroutines in the demo code it’s really not too complex. You only need an address and value to write. To read you need an address and it puts the value into the variable. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • SassySassy Posts: 21
    edited 2007-03-27 20:15
    Chris thanks for your help!
    Yes I am new and don't really understand the code. Attaches is what I am playing with. I do want to learn how to put larger numbers in to the ram, but right now I am only dealing with a three digit number....example 123
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-27 20:16
    Sassy,

    Does the number ever go above 255?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-03-27 20:17
    Hi Chris Savage

    I have tried to use this before my self like Sassy·has
    When i tried this before I might not have understood·what to ENTER
    Can you give an example with the xxxxx filled in
    and explane what you can do with it

    Thanks for your help in this matter and your time

    ·RAM_Mode:
    · DO
    ··· DEBUG CLS, "Enter an address from 0-30 (>31 Exits):", CR

    What would you Enter_________? Example Please
    ·
    ··· DEBUGIN DEC2 index················· ' Address in DS1302 RAM
    ··· IF index > 30 THEN EXIT············ ' Exit if out of range

    ··· DEBUG CRSRXY, 0, 1, "Enter # from 0-255 to store at ", DEC index, CR

    ·
    What would you Enter_________? Example Please

    ··· DEBUGIN DEC3 ioByte················ ' Value to store in DS1302 RAM
    ··· DEBUG CRSRXY, 0, 2, "Storing ", DEC ioByte, " at ", DEC index, CR

    What would you Enter_________? Example Please


    · ' The address to store data in the DS1302 RAM is obtained by



    ·What dose this mean.......................................OR-ing
    · ' the command byte
    with the address shifted left one bit.

    ·


    · 'This is
    · ' because the command byte uses BIT0, BIT6 and BIT7.· The address
    · ' occupies BIT1 through BIT5.· You are effectively putting the address
    · ' in BIT1-BIT5 of reg.

    ·What would you Enter_________? Example Please
    ·' Write RAM command = 11xxxxx0 (Uses 3 bits)

    Could you put a time in lets say 13:00
    ·

    What would you Enter_________? Example Please

    What would you put in·for the XXXXX and what would that do for you

    · ' Read· RAM command = 11xxxxx1(Uses 3 bits)
    · ' xxxxx = address (Uses 5 bits)


    What would you Enter_________? Example Please

    ··· reg = WrRam | (index << 1)········· ' RAM Write Mode + Address
    ··· GOSUB RTC_Out······················ ' Send reg + ioByte

    ··· ioByte = 0························· ' Clear Data Variable
    ··· reg = RdRam | (index << 1)········· ' RAM Read Mode + Address
    ··· GOSUB RTC_In······················· ' Get Data From Address

    ··· DEBUG CRSRXY, 0, 3, "Address ", DEC index, " contains ", DEC ioByte, CR
    ··· DEBUG CRSRXY, 0, 4, "Press ENTER.", CR
    ··· DEBUGIN work
    · LOOP
    · RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 3/27/2007 8:22:47 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-27 20:37
    Sassy,
    ·
    ·· I see part of your problem…In my code I show how to store a value to the DS1302 RAM as follows…

      ' The address to store data in the DS1302 RAM is obtained by OR-ing
      ' the command byte with the address shifted left one bit.  This is
      ' because the command byte uses BIT0, BIT6 and BIT7.  The address
      ' occupies BIT1 through BIT5.  You are effectively putting the address
      ' in BIT1-BIT5 of reg.
      ' Write RAM command = 11xxxxx0 (Uses 3 bits)
      ' Read  RAM command = 11xxxxx1 (Uses 3 bits)
      ' xxxxx = address (Uses 5 bits)
        reg = WrRam | (index << 1)          ' RAM Write Mode + Address
        GOSUB RTC_Out                       ' Send reg + ioByte
        ioByte = 0                          ' Clear Data Variable
        reg = RdRam | (index << 1)          ' RAM Read Mode + Address
        GOSUB RTC_In                        ' Get Data From Address
    
    

    So as you can see when you call the routine the variable index (byte only) needs to contain the address in the RAM you want to write to.· This will be encoded into the variable reg.· ioByte contains the data (byte only) to be written.· Once the RTC_Out subroutine is called that value is stored at the location specified in the variable index.· The second part of the routine clears ioByte before calling the subroutine to retrieve the value from RAM.· Again, index contains the address (0-31) and that is encoded into the variable reg and the RTC_In subroutine is called, which populates ioByte with the stored value.· In your code you are doing the following…

    DS:
        ioByte = WPr0                       ' Clear Write Protect
        reg = WrRam |0123                   ' RAM Write Mode + Address
        GOSUB RTC_Out                       ' Send reg + ioByte
        ioByte = 0                          ' Clear Data Variable
        reg = RdRam | 0                     ' RAM Read Mode + Address
        GOSUB RTC_In                        ' Get Data From Address
     RETURN
    
    

    This essentially means that reg is never getting a valid address and you are attempting to store the constant WPr0, which probably doesn’t get stored anyway.· You shouldn’t have to change any of the lines of code for the read and write sections.· Instead you change the ioByte and index variables when calling them.· The rest of the work is done for you.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-27 20:47
    Sam,

    Enter an address is asking for the address within the DS1302 RAM. A value to put there is the byte value you want to store in the address. So if you want to store a value of 65 at address 4 then you would’ve entered 4 in the first question and 65 at the second question. The command byte is the constant that tells the DS1302 that you are reading or writing a byte to RAM. This is ORed with the address (for example the 4 from above) that you enter. Left shifted 1 bit is just that…the address needs to occupy bits 1 through 5 while the control byte uses bits 0, 6 and 7. The two are being merged unaligned. You were asking about putting a time in there. The time uses two bytes so each one would have to be stored separately.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • SassySassy Posts: 21
    edited 2007-03-27 21:24
    reg = WrRam |·0························· ' RAM Write Mode + Address······· '·Making this Address 0 in·DS1302·Ram
    ··· GOSUB RTC_Out······················ ' Send reg + ioByte··· ioByte =·123····························' Clear Data Variable········· ······ ·' writing variable 123
    ··· reg = RdRam |·0············ ········· ' RAM Read Mode + Address······· ·'·Reading Address 0 in·DS1302·Ram

    ··· GOSUB RTC_In······················· ' Get Data From Address

    Is this correct or am I still not understanding it?
  • SassySassy Posts: 21
    edited 2007-03-27 23:27
    Sam,
    Did you get your question answered?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-27 23:55
    Sassy,

    You’re still changing the code in the routine. When you put that 0 after the | (OR) it no longer forms a coherent command with the address. As I explained above you need to set the address in the variable ‘index’ and call the write routine. Do not change the lines you are changing…They no longer work that way.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.