Shop OBEX P1 Docs P2 Docs Learn Events
Algorithm Adivce — Parallax Forums

Algorithm Adivce

Jim KuenemanJim Kueneman Posts: 9
edited 2007-11-20 06:45 in General Discussion
I have been struggling to come up with a plan of attack to code the SX28. Here are the requirements:

1) PWM: Constant Period, User definable Period with max period >25kHz (40us), user definable 127 or 255 step resolution

2) While the PWM is running need to search for an asynchronous pulse width encoded input data stream. A "1" is defined as a pulse 52us to 64us wide and a "0" is defined as a pulse 90us to 1000us wide.

3) once a command is detected in 2) the execution time the received command is not critical (though it does have a requirement to be ready to accept another command with a specific time frame the SX runs fast enough this should not be an issue)

Any suggestion on how to attack this problem?

Thanks,
Jim

Comments

  • PJMontyPJMonty Posts: 983
    edited 2007-11-19 19:16
    Jim,

    Not sure if you program in assembly or not as you didn't specify it in your post. I program assembly so that the answer I'll provide. Others may have an SX/B solution.

    1 - Set up a a nice, fast interrupt. I suggest minimum of 250 kHz. This runs constantly in the BG.

    2 - Inside this interrupt, implement the PWM.

    3 - Also inside this interrupt, have code that monitors your input that expects the asynchronous pulse. When the interrupt sees the input go high, start a counter that is incremented on every subsequent interrupt.

    4 - When the input goes low, set a flag that tells your FG code that a new pulse width was determined and that it copy it.

    5 - Have your FG code spin and look for the flag to be set. When it sees it, copy the pulse width into a variable it can use without worrying about the interrupt writing over it.

    6 - The FG code can now leisurely determine if the length of the count makes it qualify as a 1 or a zero. A 250 kHz interrupt take 4 us to complete. At this rate, your "1" bit is 52/4 to 64/4, or 13 to 16 counts long. The "0" length is calculated the same way.

    There are many way to accomplish this goal. Someone else will probably have a simpler solution. I recommend you start coding using an approach that makes sense to you. See how it goes. If you get into trouble, come back here and post for more help. The act of actually coding a solution will usually present answers as you go.

    Thanks,
    PeterM
  • Jim KuenemanJim Kueneman Posts: 9
    edited 2007-11-20 02:20
    Thanks, I have started this a couple of times (yes I do assembler). I don't see how you can get a 255 step 25kHz PWM with this approach.

    Jim
  • PJMontyPJMonty Posts: 983
    edited 2007-11-20 06:45
    Jim,

    I missed your 25 kHz spec.

    How to do it? Simple. You can't. At least not according to my math.

    256 (yes, to do 0-255 you need an 8 bit accumulator, hence the 256 multiplier) times 25 kHz gives you 6.4 Mhz as your interrupt rate. With the 50 MHz SX, that gives you 7.8 clock cycles to enter the interrupt, execute an interrupt, exit, and process FG code. Not going to happen. Even at 75 MHz you've only got 11.7 clock cycles.

    Even if you skip the interrupt and code it all as inline, you basically have enough speed to get in your own way.

    Thanks,
    PeterM
Sign In or Register to comment.