Shop OBEX P1 Docs P2 Docs Learn Events
Question about jm_io.spin — Parallax Forums

Question about jm_io.spin

Hi,

I am having some trouble getting a pulse to emulate an NPN output from a PLC (0=active)

I was happy to see such a function available in jm_io, and that it seems to be able to handle pulses of either polarity.

In running the attached code on a flip, I am able to get a positive pulse, but if I uncomment line 22, or add a pullup to P26, I cannot get a negative pulse.

Also, the output seem to remain asserted after the pulse is done. (Even with line 22 commented out.)
this shows as being unable to pull up with the resistor after a successful positive pulse.

I am using ctra for the function this code should test, but it seems this should not be a problem.

Am I doing something wrong?

Thanks!

Comments

  • Do you have a pull-up on the IO pin? My experience is that the active-low output of a PLC-type device is typically and open-collector or open-drain circuit; this allows the output to work with different voltages since it's only pulling the pin to ground. Put a 4.7K resistor between 3.3v and the IO pin you're connecting to the PLC and see if that helps.

    If I may, long term you'll be happier with tidier code formatting. As programs become longer, subtle errors can be hidden in untidy code. At the very least, use a bit more whitespace as this will make your code easier to read and has no negative impact on the compiled code size.

    This is how I would reformat the code you posted.

    con
    
    ' Standard clock mode * crystal frequency = 80 MHz    
    
      _clkmode = xtal1 + pll16x  
      _xinfreq = 5_000_000
    
    
    con { io pins }
    
      PLC_SIM_IN = 26
    
    
    var
    
      long  minPulse
      long  ms
    
    
    obj
    
      pst : "parallax serial terminal"
      io  : "jm_io"
    
    
    pub main | plcMsg , length
    
      setup
    
      ms := clkfreq / 1000
      minPulse :=  ms * 50
    
    ' Set up ctra as timerfor PLC pulse interpretation. Used by PlcPulse
    ' Set up CTRA to measure the time of the PLC pulse.   
    
      ctra := %01100 << 26 | PLC_SIM_IN   
      frqa := 1
    
    ' Begin main loop of test
    
      repeat  
        pst.str(string("Length?"))
        length := pst.DecIn * 1000
        pst.dec(length)
        pst.NewLine
        io.pulse_out(PLC_SIM_IN, length)
    
    
    pub setup
    
      pst.start(115200)
      pst.clear
    
  • Thanks for the code tips!. This is a little messy due to being separated from the larger application for debug.

    My test setup has nothing connected to any pin except for a pull-up on P26. This code is to simulate the PLC pulse, the code to be tested will use P26 as input.

    Running your code from above:

    With pull-up(1k).. no change of output state. entry to terminal was 1000
    Without pull-up (And a reset.. otherwise still driven on) 1 second pulse. Entry to terminal was 1000

  • JonnyMacJonnyMac Posts: 8,999
    edited 2023-10-23 20:27

    This is a little messy due to...

    If you get into the "quick and dirty" trap you'll find it's never quick, it's just dirty.

    I didn't rewrite you code, I simply cleaned up the formatting.

    In your first post you said:

    I am having some trouble getting a pulse to emulate an NPN output from a PLC (0=active)

    Well, it only took a few minutes for me to knock together a program that simulates an open-collector output from a PLC. The great thing about the multi-core Px micros is that the IO pins are OR'd together (that is, all the P0s from every cog are OR'd together, all the P1s, etc.). As you can see, I start a background program that causes an active-low pulse (20ms) every 500ms. The IO pin requires a pull-up (I used 3.3K). In the foreground cog I used my jm_io.spin object to measure the pulse on the same pin. Magically, it works, and all the code is neatly formatted! :D

    One final tip: Don't assume other forum members have the objects you're using -- it's best to archive the project so that you can provide them everything they need to assist.

  • Another thought for your final project: Make sure that your PLC ground and your Propeller ground are common.

  • Thanks Jon!

    That wend above and beyond for sure. Lots of handy stuff there.

Sign In or Register to comment.