Shop OBEX P1 Docs P2 Docs Learn Events
Problem with Arduino DS1302 sketch ported to Spin — Parallax Forums

Problem with Arduino DS1302 sketch ported to Spin

Hi again Community,

It's been a while since I was last here.
And I hadn't been able to visit the forums as much as I would have liked.
Mostly due the same things as everyone else, work, home, family, life.
But now I have some free time to get back to the P1.

And so I restarted with the DS1302 RTC.
I know there are newer RTC's and since I'm still only in the learning curve, its okay for my needs.

I started to code my own Rtc module using the datasheet.
But it seems like age is catching up with me.
Since my memory seems to be getting worse.

My first attempt was overblown and jumbled up (abysmal).
I've attached it as JD_Ds1302v1.spin

In the end I just scrapped it an tried to port an Arduino sketch to spin.
The original git can be found here:
https://github.com/Treboada/Ds1302/tree/master

I've completed the conversion from sketch to spin, but on compilation there is an error and I cannot figure out how to resolve it.

I'm kind of happy with it, as it is a quarter of the size of my first attempt.
The error is to do with operators and I'm not confident I understand them at times.
And I cannot seem to resolve the error.

Line: 313 in procedure SetHaltFlag(Stopped)

I've attached the new file New_DS1302.spin
In case someone can explain what is happening in the line of code.

Comments

  • JonnyMacJonnyMac Posts: 8,988
    edited 2024-07-13 20:10

    Is this the line giving you trouble?

        _RtcRegister[0] & (!= %10000000) 
    

    If your goal is to clear bit7 of _rtcregister[0] you can do it like this:

        _RtcRegister[0] &= !%10000000
    

    If you want to make your code very obvious you could swap out the hard coding for a couple general-purpose routines.

    pub set_bit(value, bit) : result
    
    '' Returns value with specified bit set
    '' -- bit must be 0..31
    
      if (bit => 0) and (bit =< 31)
        return value | (1 << bit)
      else
        return value
    
    
    pub clr_bit(value, bit) : result
    
    '' Returns value with specified bit cleared
    '' -- bit must be 0..31
    
      if (bit => 0) and (bit =< 31)
        return value & !(1 << bit)
      else
        return value
    

    In this case the line giving you trouble becomes

        _rtcregister[0] := clr_bit(_rtcregister[0], 7)
    

    Yeah, it adds a bit of overhead but makes the code more obvious to those with less experience. The next logical step would be to replace the magic number 7 with a named constant

        _rtcregister[0] := clr_bit(_rtcregister[0], CLOCK_HALT)
    
  • Thank You VERY Much JonnyMac,

    It was indeed that line.

    I never even realised it was clearing the bit lol...

    I've changed it as advised now, and all seems to be well and it compiles without errors.
    Cheers Once Again...

  • JonnyMacJonnyMac Posts: 8,988
    edited 2024-07-14 02:50

    No worries. I needed a break from work so I had lunch and downloaded the Schmarschmino code for the DS1302 that you reference. I did a near-direct translation; there are areas where the Px can be more efficient and we need to be given it's a interpreted (unless you run it through FlexSpin). Have gone through your code after translating mine, I can see we made many similar choices.

    I have to order a DS1302, but I've attached my primary WIP in case you want to see how I do things. I did a timing test and changed the clear bit value in set_halt_flag() to a simple constant.

      if (state)
        regs.byte[0] |= %10000000
      else
        regs.byte[0] &= %01111111
    

    Doing it the way I showed you above uses about 80% more ticks. You can fix that with the constant command -- like this

      if (state)
        regs.byte[0] |= %10000000
      else
        regs.byte[0] &= constant(!%10000000)
    

    I think the simple constant is cleaner and more obvious.

    Note, too, that I'm using a local array in that method. Locals are always defined as longs (in the P1), but you can still access them as bytes or words.

Sign In or Register to comment.