Hi Meng,
The period and duty registers for each channel are double buffered so that if they change while the channel is enabled, the change will NOT take effect until one of the following occurs:
• The effective period ends
• The counter is written (counter resets to $00)
• The channel is disabled
In this way, the output of the PWM will always be either the old waveform or the new waveform, not some variation in between. If the channel is not enabled, then writes to the period register will go directly to the latches as well as the buffer.
So, period and duty cycle should be automatically updated according to the last value before the end of the active period. You may try to write into PWMCNTx counter for reset counter to zero and force to load new values. The command PWME_PWME1 = 1; should not have any influence on output and you could remove it.
Unfortunately, this description didn’t fit exactly to your measurement results and I suppose that there must be some other issue.
I have three additional doubts related to your code:
1. The command ECT_TFLG1_C0F = 1; is not the correct way how to clear a flag – it is read/modify/write command and it will clear all currently pending flags. Please use rather ECT_TFLG1 = 0x01; or ECT_TFLG1_C0F = ECT_TFLG1_C0F_MASK; for clearing such flag. It should not cause any problem in your code, but it is still not correct. For more details, please look at application note AN2554 Clearing and Disabling Interrupt Flags http://www.nxp.com/files/microcontrollers/doc/app_note/AN2554.pdf
2. You didn’t implement overflow interrupt for input capture. Are you sure that time2-time1 is always smaller than full timer counter period?
3. The type of variables delaytime, aa, bb, cc and signalf is unsigned int. Are you sure that you didn’t lose any precision during calculations like signalf=delaytime*0.001*4/MUL_FREQ;?
0.001 is float number.
Please check whether any overflow or underflow cannot cause an issue.
Shouldn’t be better to calculate with unsigned long variables?
Please ensure that your formula is correct (signalf=delaytime/2500).
For example:
aa=delaytime*0.001; //16000*0.001=16
bb=aa*4; //16*4=64
cc=bb/MUL_FREQ; //64/10=6
I hope it helps you.
Have a great day,
Radek
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------