Shop OBEX P1 Docs P2 Docs Learn Events
BS2 Functions Library — Parallax Forums

BS2 Functions Library

Martin HebelMartin Hebel Posts: 1,239
edited 2006-03-23 02:36 in Propeller 1
This my completed (hopefully) library performing many of the functions of a BS2, plus some with my own 'spin'. hop.gif

It also includes a sample program running various functions in different cogs, and a watch-dog cog to ensure another cog doesn't get hung-up waiting forever for a PULSIN that never arrives.

-Martin

Post Edited (Martin Hebel) : 3/20/2006 11:40:55 PM GMT

Comments

  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-03-20 23:40
    Here's the list of functions in case you can't read the Spin properly.


    It's 100% spin making it easier to modify it as you like. My proudest achievments were using the cog counter for a COUNT and being able to accept an ASCII string, such as "abc123". This will really be helpful fhow those who need to get serial data from a device as a string, such as GPS units, to easily parse it. But, being in Spin, there wasn't time to perform loops for timeout, so it will wait forever, such as in PULSIN or SERIN, thus the use of the WatchDog Cog in the example.



    
    Object "BS2_Functions" Interface: 
    
    PUB  Start(Debug_rx, Debug_tx)  
    
    PUB  COUNT(Pin, Duration) : Value  
    
    PUB  DEBUG_CHAR(Char)
    PUB  DEBUG_BIN(value, Digits)
    PUB  DEBUG_DEC(Value)
    PUB  DEBUG_HEX(value, digits)
    PUB  DEBUG_STR(stringPtr)
    PUB  DEBUG_IBIN(value, digits)
    PUB  DEBUG_IHEX(Value, Digits)  
    
    PUB  DEBUGIN_CHAR : ByteVal
    PUB  DEBUGIN_DEC : Value
    PUB  DEBUGIN_STR(stringptr)  
    
    PUB  FREQOUT(Pin, Duration, Frequency)
    PUB  FREQOUT_SET(Pin, Frequency)   
    
    PUB  FREQIN(pin, duration) : Frequency   
    
    PUB  PAUSE(Duration)
    PUB  PAUSE_uS(Duration)  
    
    PUB  PULSOUT(Pin, Duration)
    PUB  PULSOUT_uS(Pin, Duration)  
    
    PUB  PULSIN(Pin, State) : Duration
    PUB  PULSIN_uS(Pin, State) : Duration
    PUB  PULSIN_Clk(Pin, State) : Duration  
    
    PUB  PWM(Pin, Duty, Duration)
    PUB  PWM_100(Pin, Duty, Duration)  
    
    PUB  RCTIME(Pin, State) : Duration  
    
    PUB  SERIN_CHAR(pin, Baud, Mode, Bits) : ByteVal
    PUB  SERIN_DEC(Pin, Baud, Mode, Bits) : value
    PUB  SERIN_STR(Pin, stringptr, Baud, Mode, Bits)  
    
    PUB  SEROUT_CHAR(Pin, char, Baud, Mode, Bits)
    PUB  SEROUT_DEC(Pin, Value, Baud, Mode, Bits)
    PUB  SEROUT_STR(Pin, stringptr, Baud, Mode, bits)  
    
    PUB  SHIFTIN(Dpin, Cpin, Mode, Bits) : Value
    PUB  SHIFTIN_SLV(Dpin, Cpin, Mode, Bits) : Value  
    
    PUB  SHIFTOUT(Dpin, Cpin, Value, Mode, Bits)
    PUB  SHIFTOUT_SLV(Dpin, Cpin, Value, Mode, Bits)  
    
    Program:     463 Longs
    Variable:     17 Longs
     
     
    
    


    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    Perform an Employer's Survey of Electronic Technologies Graduates· - Click here!
    Personal Links with plenty of BASIC Stamp info
    and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control

    Post Edited (Martin Hebel) : 3/21/2006 5:39:31 AM GMT
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-03-20 23:53
    And, finally, for those who can't see the Spin code in the beauty of the editor, here's pdf's of the code directly from the editor.

    -Martin
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-22 18:32
    Just a note on your RCTIME method (what we call them), Marty: it doesn't account for cnt register rollover where clkStop is smaller than clkStart. Should you not use the absolute operator in this line:

    · duration := ||(clkStop - clkStart) * 1000

    ?

    BTW, I think it's very cool that you've created this library and it should make porting BASIC Stamp projects a lot of fun.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-03-22 18:54
    Interesting Jon,
    I was thinking the math would take care of itself, but that probably only works for waitcnt. I'll try to force a rollover and see what happens. || is the absolute function? Glad you told me [noparse]:)[/noparse] There's several other functions/methods where this would be an issue.

    I hope it helps!

    Thanks,
    Martin
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-22 19:03
    Perhaps I'm over-complicating things.... I guess I need to do some experiments as well.· I'm working on a version of Beau's RCTIME method for my next N&V article, and the aspect of a negative value occurred to me then.

    Jon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-03-22 19:39
    I'll sit down with it, but when you look at a high count, it will be act as a signed negative value. When we subtract a positive low value.... hmmmmm. I'll try it out tonight.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    Perform an Employer's Survey of Electronic Technologies Graduates· - Click here!
    Personal Links with plenty of BASIC Stamp info
    and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-03-22 19:41
    No, a high count would be a small negative value, so subtracting a low positive value.... ok.. enough thinking. It hurts too much :P Later.

    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    Perform an Employer's Survey of Electronic Technologies Graduates· - Click here!
    Personal Links with plenty of BASIC Stamp info
    and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
  • Beau SchwabeBeau Schwabe Posts: 6,545
    edited 2006-03-22 19:57
    Thanks Jon,

    I had forgotten about the absolute operator.... RCTIME probably needs the absolute operator as well
    as the 32SERVO, although I did take some consideration to roll-over within the 32SERVO method.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-03-23 02:36
    Well, here's the results of my testing with a .1uF cap and 10K ohm resistor for rctime:

    Start: 777869
    Stop: 803805
    Dur in uS = 324

    Waiting for the counter to wind up:
    Start: -22997
    Stop: 2827
    Dur in uS = 322

    2827 -(-22997) = +25724 /80 =322

    Looks like with even counter wrap around, the math works out.

    Even high in the negatives, it works fine. 2's compliment at it best:
    Start: -1046988
    Stop : -1020988
    Dur in uS = 325

    Ok, but I will admit my RCTime code is hosed. I made it too convoluted... it was one of the earlier ones I tried. With higher RC values, I was getting negative numbers, but I think it was my after-math than counter issues. I'll just keep the duration in 1uS units.

    I'll repost with new code after a few other tweaks.

    PUB RCTIME (Pin,State):Duration | ClkStart, ClkStop
    {{
       Reads RCTime on Pin starting at State, returns discharge time, returns in 1uS units
         dira~~                 ' Set as output
         outa:=1                ' Set high
         BS2.Pause(10)             ' Allow to charge
         x := BS2.RCTime(5,1)          ' Measure RCTime
         BS2.DEBUG_DEC(x)          ' Display
    }}
    
       DIRA[noparse][[/noparse]Pin]~
       ClkStart := cnt                                         ' Save counter for start time
       waitpne(State << pin, |< Pin, 0)                        ' Wait for opposite state to end
       clkStop := cnt                                          ' Save stop time
       Duration := (clkStop - ClkStart)/uS                     ' calculate in 1us resolution
    
    



    Thanks for making me take a closer look Jon.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    Southern Illinois University Carbondale - Electronic Systems Technologies

    Personal Links with plenty of BASIC Stamp info
    StampPlot - Graphical Data Acquisition and Control

    Post Edited (Martin Hebel) : 3/23/2006 3:46:22 AM GMT
Sign In or Register to comment.