This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

PWM Resolution

Other Parts Discussed in Thread: HALCOGEN

Hi, i'm using the NHET for PWM, with the instructions PWCNT (duty cycle) and DJZ (period). I chose those instructions, because HalCoGen also generated those instructions for PWM.

Assuming 100MHz(10 ns) VCLK2 and 64 instructions, I have a Loop Resolution of 640 ns (hr = 1).

Lets say, i want to make a PWM with 19.9kHz period frequency. 19.9kHz means 50251,256ns for one period.

--> CNT(DJZ) = (50251,256 / 640) -1 = 78,52 -1=77,52. --> CNT = 78.

And here is my problem. We got a resolution of something about 0,6%, which is not very good. But it gets much when we look at the resolution of the Duty Cycle.

Maybe we want a DutyCycle of 10%. --> CNT(PWCNT) = 7,752. --> CNT = 8

As you can see, the resolution for the Dutycycle can easily become higher than 5%.

This bad accuracy results from the bad loop resolution. Is there a way to work with High Resolution?

  • R. K.,

    Actually, the easiest method for generating a PWM is to use CNT and ECMP instructions. You can set the max count value of CNT for the period. and the data field for ECMP for pulse width. The ECMP instruction is set to produce PWM. If you enable hr for ECMP instruction, the pin action will be delayed for the time defined in the HR portion in the data field. In other words, you are adjusting duty cycle with HR resolution. The limitation of this approach is that the period can only be adjusted in LR resolution. I would suggest you try this method first. There is a method which allows period adjustment in HR resolution. But it is much more involved and debugging is not as straightforward.

    Please let me if the above answers your question.

    Thanks and regards,

    Zhaohong

  • Zhaohong,

    why is HalCoGen then using those other 2 instructions? I knew you could do this with the normal CNT instruction and ECMP, but is there an advantage?

    I just saw PWCNT can also enable hr.

    You mentioned, that there is a method to adjust HR resolution for the period. Where can I read something about that? Or would this method include that I don't get HR in DutyCycle, but then ONLY for my period? Then I wouldn't need it anyway ;-)

  • R. K.,

    The other method is to use ECMP to set or clear each edge of the pulse with HR delay. It requires updating the control (for set or clear) and data field for each edge. Since the debugging is involved, we normally suggest people to use the simple method in my earlier reply. With the simple method, you can improve the accuracy in two ways.

    (1) Reducing LR time. If you can reduce the LR cycle from 64 to 16, you can achieve period accuracy to about 0.1%. I think it more than enough in most applications considering the waveform distortion (ringing, etc) in the external circuit.

    (2) Tuning the VCLK2 frequency. You can slightly modify the PLL frequency to reduce the "quantization" error.

    Thanks and regards,

    Zhaohong

  • R.K.,

    Do you need more information on this topic? Would you please select the "verified" button if you consider that this question is answered?

    Thanks and regards,

    Zhaohong