Shop OBEX P1 Docs P2 Docs Learn Events
Storing and retrieving DS1302 register data in an attempt to store only a part of a long numeric var — Parallax Forums

Storing and retrieving DS1302 register data in an attempt to store only a part of a long numeric var

I may be having a total stupid attack here - but I am having a problem trying to take the lower order three bytes of a long numeric in SPIN and storing each of the three bytes in one of the registers on the DS1302 time keeper chip. I am successfully accessing the chip for time setting and retrieval, and believe that I am also accessing it correctly for register read and write. The reason I am doing this is that I have a limited amount of storage capacity in the DS1302 so I am trying to save only the significant digits (project perspective not mathematically significant digits - I am grabbing the low order bits) - basically the low order three bytes of each of 9 longs... so it all fits into 30 one byte registers... (with room to spare)

My Process:
My current process (which apparently doesn't work)
- Upon first execution I am initializing all the target longs to zeros.
- I am then grabbing the low order three bytes of each variable and writing them to registers sequentially (after building the correct cmd byte).
(e.g., rtc.writeN(cmd,@num_var[1],3)
- On reboot/reset - I initialize all the longs to zeros then retrieve the three one byte register values (after building the correct cmd byte) and copy them into the corresponding address for the long (e.g., rtc.readN(cmd,@num_var[1],3)

I thought I was grabbing simply the low order 24 bits (8 bits at a time) and mapping them into place from the register as literals (not numerics)

The Problem:
When I start with all zeros and then reset, the long value is suddenly = 1 - not zero (this is after writing 0s and then rebooting and then retrieving the registers into the long vars).

The Ask:
Is this caused by not dealing with the numeric sign? or am I having some other kind of stupid attack here?

Any help will be most appreciated.

If it would be helpful I can also post the code fragments ...

Mostly Useless Background Info:
I am building a water monitoring system for my residence to try to reduce overall water use by measuring and controlling water consumption on various subsystems (like irrigation of the stupid lawn) - I am using the multi-cog capacity of the Prop to pulse count and manage accumulation (native pulse counting was too sensitive for sloppy water switches) - The reason for writing the data to the DS1302 registers is that I just don't want to lose the daily, monthly and annual counts in the event the system is restarted... the numbers I am generating on an annual basis are only large enough to pass the size limit of a word variable (two bytes) and not big enough to occupy the full size of a long (four bytes)

Comments

  • Usually with problems like this, the error is in the assumptions you've made or something you think you're doing correctly, but you're not. Post your code, not just the little snippets. Make an archive of your program and the objects it references and attach it to a post ... don't cut and paste as part of a post ... the formatting gets lost.
  • I think it should be
    rtc.writeN(cmd,@num_var,3) and
    rtc.readN(cmd,@num_var,3) since in Spin the representation of say 255 in a long has the first byte at @num_var at 255 and the following 3 bytes are zero.

    A value of 256 will be represented by a 1 at address @numvar and a 1 at @numvar+1 and two following zeros on the next two bytes.

    A value of 257 will be represented by a 2 at address @numvar and a 1 at @numvar+1 and two following zeros on the next two bytes.

    the sign-bit is in the forth byte aka @numvar+3.

    Enjoy!

    Mike
Sign In or Register to comment.