Shop OBEX P1 Docs P2 Docs Learn Events
SOLVED: Converting DS1302 driver from P1 to P2 question — Parallax Forums

SOLVED: Converting DS1302 driver from P1 to P2 question

dgatelydgately Posts: 1,621
edited 2021-01-02 06:07 in Propeller 2
Excuse my simplistic code conversion...

I'm sure there's a much simpler solution in Spin2, but I'm trying to start with a literal translation from P1 Spin. I'm trying to send out the command byte to the DS1302, one bit at a time, in the following excerpt:
pub main()
  setup()
  wait_for_terminal(true)
  write_a_byte(%10000101)			'' $85 DS1302 read hours command

PRI write_a_byte( cmd ) | i  
'' P1 dira & outa version
{{
  dira[io]~~              ' set to output 
  repeat i from 0 to 7    
    outa[io] := cmd   ' send the LSB bit to the IO pin ???
    outa[clk]~~          
    cmd >>= 1
    outa[clk]~           
}}
'' P2 Spin2 methods
  repeat i from 0 to 7
    if (cmd & %00000001 == 1)       ' REPLACE: outa[io] := cmd for P2 ???
      pinhigh(io)                               ' the masked bit is 1
    else
      pinlow(io)                                ' the masked bit was 0
    pinhigh(clk)
    cmd >>= 1
    pinlow(clk)
The Spin2 version does not yet work (timing issue or just bad coding?)

dgately

Comments

  • JonnyMacJonnyMac Posts: 8,923
    edited 2021-01-01 22:28
    You can simplify a little bit. This is how I would translate that P1 code:
    pri wr_byte(cmd)
    
      repeat 8
        pinw(io, cmd)
        pinh(clk)
        cmd >>= 1
        pinl(clk)
    
    Since io is a single pin, the pinw() function will only write bit0 of cmd.
  • Thanks Jon!

    Changed my code, still not getting good results, but at least I think I understand how that piece of code works. Now, on to the rest of the code.
  • dgatelydgately Posts: 1,621
    edited 2021-01-02 03:19
    I'm down to one last function that should be a simple P1 to P2 conversion... I've actually been using the P1 version of the code, which flexprop happily translates for P2, but I would rather use code that will compile on PropTool, eventually. I'm unsure if the P1 "datar |= |< i" code works as P2 code. The global var, datar stays empty with the P2 code. All of the functions outside of this private function are working. When I comment the P2 variant and uncomment the P1 code, it works!

    Here's a code snippet with commented-out working P1 code (compiles with flexprop):
    VAR
      byte datar                 ' a GLOBAL var 
    PRI readByte|i
    '' REMOVE P1 dira & outa
    {{
      datar~                           
      repeat i from 0 to 7          
        if ina[io] == 1
          datar |= |< i     ' set bit
        outa[clk]~~          
        outa[clk]~           
    }}
    '' P2 REWRITE
      datar := 0                    ' set datar to zero  
      repeat i from 0 to 7
        if (pinr(io) == 1)          ' is the pin high?
          datar |= |< i             '  yes, set the corresponding bit to 1
        pinh(clk)                   ' strobe the clock
        pinl(clk)
    

  • ozpropdevozpropdev Posts: 2,791
    edited 2021-01-02 03:45
    Change this line
    datar |= decod i             '  yes, set the corresponding bit to 1
    
  • dgatelydgately Posts: 1,621
    edited 2021-01-02 05:43
    ozpropdev wrote: »
    Change this line
    datar |= decod i             '  yes, set the corresponding bit to 1
    

    OK... that's the correct P2 replacement, but it still just returns with datar as 0. Is there any other difference from the P1 code?

    One more try and it works:
    PRI readByte|i
    '' REMOVE P1 dira & outa
    {{  
      datar~                          
      repeat i from 0 to 7
        if ina[io] == 1
          datar |= |< i     ' set bit	
        outa[clk]~~          
        outa[clk]~    
    }}
    
      datar := 0                    ' set datar to zero  
      repeat i from 0 to 7
        if (pinr(io) == 1)          ' is the pin high?
          datar |= decod i          '  yes, set the corresponding bit to 1
        pinh(clk)                   ' strobe the clock
        pinl(clk)
    



  • JonnyMacJonnyMac Posts: 8,923
    edited 2021-01-02 06:01
    Here's another option.
    pri read_byte() : result | i
    
      pinf(io)                                              ' set to input
    
      repeat i from 0 to 7
        result.[i] := pinr(io)
        pinh(clk)
        pinl(clk)
    
  • Keeps getting better! I just need to test all the functions and there will be a DS1302 driver for Spin2!

    Thx
Sign In or Register to comment.