1 2 Previous Next 25 Replies Latest reply on Aug 28, 2013 8:33 PM by fezhc_293666

# A PWM Algorithm Realized With Only One Add Instruction

A PDM Algorithm Based On Addition

This algorithm is described in my Chinese blog “A PWM Algorithm For Software And Hardware Realized With Only One Add Instruction”. And an example implemented by hardware (UDB datapath) and software respectively on PSoC 4 pioneer kit is also included - ZhuPDM8(PSoC Creator 2.2 SP1).rar .

The blog is written in Chinese , but the programming language is common.

http://files.chinaaet.com/files/blog/2013/07/21/ZhuPDM8(PSoC%20Creator%202.2%20SP1).rar

• ###### 1. Re: A PWM Algorithm Realized With Only One Add Instruction
A clever approach. However, with N =2 and 1 =1, the output duty cycle would not be 50% as there is more CPU cycle in one half of the operation. The slower the CPU the higher the error.
• ###### 2. Re: A PWM Algorithm Realized With Only One Add Instruction

What's wrong?

MATLAB shows the duty cycle is 50% for N=2 and H=1:

>>

npulse = 8;

N = 2;

H = 1;

CNT = 0;

disp( '     k  Pout for init. CNT=0' )

for k = 1:npulse

CNT = CNT + H;

if CNT >= N

CNT=mod(CNT,N) ;

Pout = 1 ;

else

Pout = 0 ;

end

disp( [k Pout] )

end

k  Pout for init. CNT=0

1     0

2     1

3     0

4     1

5     0

6     1

7     0

8     1

>> ...

k  Pout for init. CNT=1

1     1

2     0

3     1

4     0

5     1

6     0

7     1

8     0

• ###### 3. Re: A PWM Algorithm Realized With Only One Add Instruction

The difference between the two condition is one with the % instruction,  and the other does not.

My answer is runing it from a 'CPU'. The CPU needs to process the % instruction.

You can compile the code and check the difference of instructions.

• ###### 4. Re: A PWM Algorithm Realized With Only One Add Instruction

Hi H L,

I don't think there is any difference between the mod() and the %-operator.

The debug result (see attached screen copy) of following programm runing in a real CPU with the %-operator

shows the same duty cycle of 50%.

void main()

{

uint16 N = 2;

uint16 H = 1;

uint16 CNT = 1;

uint8 Pout;

uint16 k=0;

do {

CNT += H;

if (CNT >= N)

{ CNT %= N ; Pout[k] = 1 ; }

else Pout[k] = 0 ;

k++;

}while(k<8);

for(;;) {    }

}

May the defferent compiler cause the defferent result of %-operator ?

http://files.chinaaet.com/images/blog/2013/07/24/442064405399.jpg

• ###### 5. Re: A PWM Algorithm Realized With Only One Add Instruction
the problem is having to do the mod function( % or MOD). try write down the operation needs to be done, consider you are the computer. you can see that the no. of operation is different from 1 to o and from o to 1 is different.
• ###### 6. Re: A PWM Algorithm Realized With Only One Add Instruction

Assume there is a function delayInSeconds().

And added the call to the function in RED as in the following.

***********

do {

CNT += H;

if (CNT >= N)

{ CNT %= N ;  delayInSeconds(99); Pout[k] = 1 ; }

else ( delayInSeconds(1) ;  Pout[k] = 0 ; }

k++;

}while(k<8);

***********

you have a delay of 100 seconds in one condition and 1 second in other condition. The duty cycle is around 1%, but the array Pout[] didn't show that because it doesn't capature the time information.

• ###### 7. Re: A PWM Algorithm Realized With Only One Add Instruction

Sorry, a typo.

you have a delay of 99 seconds in one condition and 1 second in other condition. The duty cycle is around 1%, but the array Pout[] didn't show that because it doesn't capature the time information.

• ###### 8. Re: A PWM Algorithm Realized With Only One Add Instruction

Is OK for following sentanses?

CNT += H;

if (CNT >= N)

{ Pout = 1 ;  CNT %= N ;}

else Pout = 0 ;

or

CNT += H;

if (CNT >= N) Pout = 1 ;

else Pout = 0 ;

CNT %= N ;

But maybe some one says : the if-branch and the else-branch use different numbers of CPU instructions.

And even in hardware imlemention with datapath there still has the difference of a few nano-seconds

caused by the difference between the pulse rise time and the pulse fall time. The duty cycle is not 50% in absolutely accurate meaning.

In my blog above, i just give a principle of the algorithm in style of C. In the real projects, I usualy use assembly language for software realization, and always pay attantion to minimazing the number of instructions and the balance of instruction cycles between branches.

As i declared in my chinese blog, for the convinience of reading i do not use the assembly language to describe the algorithm.

Thank you for your precision of CPU instruction number level.

Any more difference between us ?

• ###### 9. Re: A PWM Algorithm Realized With Only One Add Instruction

The first one

********

CNT += H;

if (CNT >= N)

{ Pout = 1 ;  CNT %= N ;}

else Pout = 0 ;

*************

Still has the same issue.

The second one

*********

CNT += H;

if (CNT >= N) Pout = 1 ;

else Pout = 0 ;

CNT %= N ;

****

doesn't has that issue.

My intention is not for the timing of different type of instruction as It would be different with CPU with branck prediction or newer and faster CPU with tricks to work faster. And I am not trying to argue about the ns different in rising or falling edge of the pulse.

My intention is that your orignal method of prove missing important information about the out come. For a fast CPU the different is very small. The bigger the number is N, the smaller is the error as well.

But as I mention before, it is a clever way to doing it.

• ###### 10. Re: A PWM Algorithm Realized With Only One Add Instruction

And as you mentioned. Using assembly language would give more accuray timming of the pulse.

A word of caution: The code is good as a concept. But in real life that with ISR or the PWM routine is part of a main loop, the timming would be complicate/inpossible  to calculate.

• ###### 11. Re: A PWM Algorithm Realized With Only One Add Instruction

A PWM should at best be realized in hardware because that implementation is most safe and precise and can be independent of CPU or CPU load.

Nevertheless, when short of hardware or UDBs which can easy be the case in a PSoC4 a software-solution could help.

Depending on the desired frequency and granulation there are different solutions possible which will result in a different CPU load.

Since most systems run interrupt driven the precision can be affected by those, but if you take into account to run the software-PWM interrupt driven as well which limits the frequency by the time needed to satisfy the interrupt.

Minimizing the CPU-overhad would include to reduce the code needed, that would include thinking over how to eliminate a division-operation (the modulo %), so there are still points of optimization.

Having a good software solution for a PWM will help many of us engineers to relax when running short of resources.

Happy coding

Bob

• ###### 12. Re: A PWM Algorithm Realized With Only One Add Instruction

One more thing about this approach is that for ratio of x/y where x and y are relatively prime,  x is not 1 and y is larger than x, the output with have jitter because the toggling point changes between cycles.

• ###### 13. Re: A PWM Algorithm Realized With Only One Add Instruction

All discussions above are helpful for me.

I found that my poor English is not enough to express my opinion correctly.

To avoid misunderstanding I will finish this topic on Cypress Forum.

Before finishing I’ll give a summary.

• ###### 14. Re: A PWM Algorithm Realized With Only One Add Instruction

Summary

1. History

My software-PDM was first used for DC-motor speed control in MCS-51 system since 1990 year and even earlier.

Some codes for 3 PDM-outputs were simply as following:

Timer_ISR:

...

;1 Timer for 3 PWM.

;Operation interval is determined by timer with higher priority.

;Default N=256, no mod-operation is needed.

;Each PDM-operation takes only a few CPU instruction cycles.

MOV  A,CNT1

MOV  CNT1,A

MOV  Px.1,C

;

MOV  A,CNT2

MOV  CNT2,A

MOV  Px.2,C

;

MOV  A,CNT3

MOV  CNT3,A

MOV  Px.3,C

...

RETI

Why not hardware PWM?

No any hardware PWM could be used in that years, and the MCS-51 MCU had even no Timer2 at that time.

1 2 Previous Next