Shop OBEX P1 Docs P2 Docs Learn Events
Is this valid or invalid in Spin — Parallax Forums

Is this valid or invalid in Spin

idbruceidbruce Posts: 6,197
edited 2011-01-15 10:49 in Propeller 1
lllllllllllllllllllll

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-01-14 19:32
    Yes, that's okay and will give the result you expect.

    -Phil
  • idbruceidbruce Posts: 6,197
    edited 2011-01-14 20:01
    Thanks Phil

    I was not sure if it would work or not, but I thought that it should because a constant is a constant. That was actually pretty lazy of me, I guess I could have just done a debug with it. But thanks anyhow.

    Bruce
  • SapiehaSapieha Posts: 2,964
    edited 2011-01-15 00:52
    Hi idbruce.

    With respect to You.
    If You have only debugged it That will be only info for You.
    As You posted it NOW even Beginners have good insight how it can be done.

    idbruce wrote: »
    Thanks Phil

    I was not sure if it would work or not, but I thought that it should because a constant is a constant. That was actually pretty lazy of me, I guess I could have just done a debug with it. But thanks anyhow.

    Bruce
  • idbruceidbruce Posts: 6,197
    edited 2011-01-15 00:54
    Sapieha

    I agree 100%!

    Bruce
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-01-15 08:03
    You can even do calculations in the constant definition. The advantage of doing this is, that the calculation is done during compile-time and not during run-time. For example:
    CON
      INPUT_MASK = |<1
    PUB main
      dira := INPUT_MASK
      outa |= INPUT_MASK
      waitcnt( clkfreq + cnt )
      outa &= ! |< 1
    

    Another way of doing pre-calculations during compile-time is using constant()
    outa &= constant( ! |<1 )
  • idbruceidbruce Posts: 6,197
    edited 2011-01-15 09:52
    MagIO2

    That was a very good tip! I liked that. I have done calculations in the constant definition but
    The advantage of doing this is, that the calculation is done during compile-time and not during run-time.

    I never thought about the calcuations continuing on through the program.

    Bruce
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-15 10:16
    You can also force the compiler to precompile inline constants like this:
    volts := (5_000 * adc[7] / 4_095) * constant(14_700 + 1_620) / 1_620
    

    This is actual code from a program that reads the voltage from the tap of a divider that uses a 14.7K and 1.62K resistor. The MCP3208 is running at 5v (5_000) and has 12 bits (4_095) of resolution. The divider allows the circuit to know what the input voltage is which could be 12 or 24 volts, and to measure it specifically in case the motors it's running start demanding more current than the supply can handle.

    Note that this is a test program and I would suggest using named constants; I will ultimately change this code to use named constants -- something like this:
    con
    
      VIN_SUM = 14_700 + 1_620
      VIN_TAP = 1_620
    
    volts := (5_000 * adc[7] / 4_095) * VIN_SUM / VIN_TAP
    
  • idbruceidbruce Posts: 6,197
    edited 2011-01-15 10:21
    Jon

    I just looked at the MCP3208 datasheet, at it says
    MCP3208 features 100k samples/second
    That sounds very impressive. How does that compare to other A/D converters?

    And thanks for the tip Jon

    Bruce
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-15 10:33
    That sounds very impressive. How does that compare to other A/D converters?

    Honestly, I don't know -- you'd have to look at the specs for other ADCs under consideration. I wrote a PASM driver for the above project which lets me pass the address of an array of my choice to it and the array is updated every millisecond which is more than fast enough for my application. My PASM object allows me to set the update rate so I could go faster since the chip and PASM would support it.

    The MCP32xx is an easy chip to use and you can find more than one file in ObEx (including an very easy-to-use Spin version) from me. I also used that family in this project for Nuts & Volts.

    http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp8.pdf
  • idbruceidbruce Posts: 6,197
    edited 2011-01-15 10:46
    Jon
    I also used that family in this project for Nuts & Volts.

    Okay, I can be gullible, I'll bite, is Jon Williams your pen name? Are you that Jon?

    Bruce
  • idbruceidbruce Posts: 6,197
    edited 2011-01-15 10:49
    Jon

    Never mind. I found the answer to that question. I definitely have a whole lot more respect for you now. Very very impressive. I have referred to your work on countless occasions.

    Bruce
Sign In or Register to comment.