Shop OBEX P1 Docs P2 Docs Learn Events
How to convert this duty to P1 — Parallax Forums

How to convert this duty to P1

T ChapT Chap Posts: 4,259
edited 2025-12-05 01:37 in General Discussion

I want to see if there is a way to convert this to P1 so I can get a value ie 20% as the small one looks or 95% on the big one. Any ideas ?

Comments

  • T ChapT Chap Posts: 4,259
    edited 2025-12-05 03:19

    Better said, I want to see if I can send this signal direct to the propeller and convert it to a value 0-100. Counter?

    throttlePin  = 0  ' whatever pin you wire it to
    
    PUB MeasureDuty : dutyPercent
      dira[throttlePin]~        ' input
    
      ' Wait for a clean rising edge
      waitpne( |< throttlePin, |< throttlePin )  ' wait until low
      waitpeq( |< throttlePin, |< throttlePin )  ' wait until high
    
      t0 := cnt                   ' start of period
      waitpne( |< throttlePin, |< throttlePin )  ' wait for falling edge
      tHigh := cnt - t0
    
      waitpeq( |< throttlePin, |< throttlePin )  ' wait for next rising edge
      tPeriod := cnt - t0
    
      dutyPercent := (tHigh * 100) / tPeriod
    
  • JonnyMacJonnyMac Posts: 9,539

    The recipe for fixed frequency PWM on the P1 is pretty easy, but it does require a cog. I've used the attached strategy to drive motors and LEDs and other devices that need fixed-frequency PWM.

    Here's the outputs captured on my LA; frequency is set to 24kHz with ch0 set to 20% and ch1 to 95%.

  • T ChapT Chap Posts: 4,259

    Thanks Jon, I’m sure I may have not been clear. I’m trying to connect something to the propeller that is outputting that signal on the scope so that I can read it into the propeller and convert it into a value inside the propeller.

  • JonnyMacJonnyMac Posts: 9,539
    edited 2025-12-05 19:24

    Sorry, I misunderstood. No problem, there is a counter mode that can be used -- let me look it up.

    A few minutes later....

    Found it. I created a dual pwm-read object for a TV prop that was controlled via wireless DMX receiver; my object could read the PWM signal from the receiver and convert it to a value between 0..255. For this demo I added a conversion in the main code to get 0..100. The reader object should work -- if not, it will give you direction in how to use the counters to measure pulses.

  • T ChapT Chap Posts: 4,259

    Awesome! I will hook this up tomorrow and test it out thanks a bunch for posting this.

  • JonnyMacJonnyMac Posts: 9,539

    My demo uses the pwm output from the first code I posted -- just disable that and connect your external signal to one of your PWM input pins.

  • This is an excellent document from Parallax:
    https://obex.parallax.com/obex/propeller-p8x32a-counters-an001/

Sign In or Register to comment.