Altera_Forum
Honored Contributor
15 years agoSynchronization problem in PWM generation
I am using the DE2 board to generate a 20 kHz PWM signal. The duty ratio is constant 20%. The device is implemented by verilog. My method is to use DE2 board to generate a 20 kHz * 1000 = 20 MHz clock signal. The 20 kHz synchronization signal is an external signal from a master DSP board, 50% duty ratio square wave. We have to use external synchronization signal.
I set a variable a = 500. During the time when the 20 kHz synchronization signal is in its positive half cycle, let a = a-1 for each clock cycle (20 MHz). During the negative half cycle, a=a+1 for each clock cycle. Therefore, I have a triangle carrier signal and by comparing a with 100, I get the PWM output of 20 % duty ratio. I set the limits for a: the upper limit is 500 and the lower limit is 0. In normal case, during the 1st 40% of the 20kHz synchronization cycle, the PWM output is 0. The next 20% is 1 and the final 40% is 0. However, every tens of normal cycles, we can observe an abnormal cycle. The 1st half cycle PWM is 0 or 1, the next 10% is 1 and the last 40% is 0. Therefore, the PWM output is wrong during the half positive cycle of the synchronization signal and is correct in the negative half cycle. I can not figure out the reason for this problem. A possible reason is that the frequency of the 20MHz clk signal is not exactly 1000 times of that of 20 kHz signal. But how does this cause the abnormal cycle? How to solve the problem? Thank you very much!