Shop OBEX P1 Docs P2 Docs Learn Events
Timers again! — Parallax Forums

Timers again!

TappermanTapperman Posts: 319
edited 2010-09-22 14:12 in Propeller 1
I know there is probably a thread already that covers this question, but I can't find it! Maybe someone can point me to the answer?

goal: I wan't to use a timer for PWM signal at a programmable carrier freq. But it doesn't appear to me that the duty mode is going to work?

The output of the duty mode is the 'carry' bit of the timer. Is there another mode, or combination of modes that would achieve my goal?

... Tim

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-09-18 11:34
    It's not possible for a timer to do PWM without assistance from a running PASM program. Consult the AppNote AN001 for an example.

    -Phil
  • T ChapT Chap Posts: 4,223
    edited 2010-09-18 12:25
    This will do what you want. For a test, connect an LED to a pin and set the range of values from 0 - 4.2B and see the result. Or, ramp the values from 0 - 4B or 4B - 0 and see the change. This works great for no oversight set and forget PWM - ish needs, not true PWM but does average out to do nice work for some things.
    PUB  PWMish
            SetBuzzVol(4_220_000_000)
           ' SetBuzzVol(0)
    
    PUB Setbuzzvol(freq)  
        ctra := %00110 << 26 + pin
        frqa := freq
    
    
  • TappermanTapperman Posts: 319
    edited 2010-09-18 14:05
    It's not possible for a timer to do PWM without assistance from a running PASM program. Consult the AppNote AN001 for an example.

    -Phil

    Thankyou Phil ... direct and to the point!
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2010-09-18 19:34
    Not so fast! This topic of independently variable frequency and percent duty does come up from time to time, for example, a couple of weeks ago in this thread:
    http://forums.parallax.com/showthread.php?p=931378#post931378
    It is possible to do this by overlapping the outputs of two cog counters running in NCO mode. (no pasm intervention) They have the same frequency but offset phase. Cog counters obey the Propeller i/o control rule: output trumps input, high trumps low. So without a little outside help the PWM is variable from 50% to 100% by overlap of the high output phases at any chosen frequency. My solution to that was to add an external 2-input XOR gate, with the 50%-100% PWM going to the first input and a second propeller pin controlling the second input as a conditional inverter. That allows the full 0% to 100% PWM coverage with independently variable frequency and duty. That is the object PWM_2ctrx.spin and demo attached.

    Now, to carry the story further. Kuroneko (the riddler!) wrote to say that the external XOR gate is irksome, and that it should be possible to use a third cog counter in POSDET mode as an inverter. A challenge! So the third object below does that. PWM_3ctrx.spin. A third counter is set up as an inverter, and its apin looks at the PWM output of the first two counters and inverts whatever it sees there at the next clock cycle, and puts that out on a second pin (its bpin). When the duty percentage crosses the 50% mark, the pins need to be swapped. So you get PWM 0% to 100% on one pin and its complement on the second pin, automomous (no spin or pasm intervention once running at a desired frequency and duty) .
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-09-18 21:00
    Ookaaay. Yes. And it's very clever, too, I have to admit! despite requiring two cogs and three counters But I still stand behind my original statement, "It's not possible for a timer [i.e. counter] to do PWM without assistance from a running PASM program." :)

    -Phil
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2010-09-19 00:41
    Phil, what you have stated is singularly true! But in the multiplicity of Propeller resources, the counters may be freebies that do not count as requiring any cogs at all. By freeloading the PWM onto existing cogs not currently using their counters, the program can excel by upping its sacred MIPS quotient!

    -- Tracy
  • Heater.Heater. Posts: 21,230
    edited 2010-09-19 01:07
    I agree, Tracy Allen.

    Once again I wake up in the morning to find the Prop doing yet another impossible thing.
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-09-19 02:16
    Neat. Often the counters are unused in a number of cogs, so this is a great addition to the many things that were impossible, but not any more. LOL
  • TappermanTapperman Posts: 319
    edited 2010-09-20 11:59
    Not so fast! This topic of independently variable frequency and percent duty does come up from time to time, for example, a couple of weeks ago in this thread:
    http://forums.parallax.com/showthread.php?p=931378#post931378
    It is possible to do this by overlapping the outputs of two cog counters running in NCO mode. (no pasm intervention) They have the same frequency but offset phase.


    Thanks so much for all your help ... but, I was just making sure I wasn't overlooking any resources the chip might have had? As it turns out ... there is no direct way for a single timer to achieve my goal ... and at present, little would be gained to pursue this direction. Since I use only 1 cog to achieve PWM at any freq (below 10 KHz) on 3 pins at the same time. I would gain little, and loose much in the way of resources on the chip, if I pursued this direction. However, this is probably one of the cleverist uses of the timers I've seen yet!

    ... Tim
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2010-09-22 01:06
    There's no way a single cog counter can do this, but one great thing about the Prop is that there are many different ways to approach the same problem, to optimize for number of channels, speed, or what have you. My solution that uses 2 or 3 cog counter solution might be fine if you only need one PWM or servo channel, and it makes it easy to do at relatively high speed from Spin. My own application for this has been to pulse laser diodes.

    One of my favorite clever counter contraptions is Phil's radio receiver, and also the understanding behind his PWM generator using the video hardware, but it was still fun to come up with a solution that Phil said was "not possible":idea:, even if that hinged around the semantics of "a" timer!

    Here is another little program along the same line. For illustration purposes. It directs two counters at the same pin, but this time at slightly different frequencies, so... it generates PWM at the beat frequency.
  • TappermanTapperman Posts: 319
    edited 2010-09-22 14:12
    One of my favorite clever counter contraptions is Phil's radio receiver

    Was that the AM receiver? I really liked reading that one ... had special interest to me, because I had encountered 'noise' winding my own loops, and detecting the center freq of the tank when married with a cap.

    I had thought about that in the back of my mind as a side project, and here someone already had the 'spark' in thier mind and did something with it!

    ... Tim

    PS - thanks for the attach ... will study this evening (I love food for thought). This forum is GREAT!
Sign In or Register to comment.