Hello, i have question about PWM function. I set my PWM function on 30KHz:
PWM2_Init(30000);
PWM2_Set_Duty(127);
And this is part of my code which doesn't work well.
When i write this:
PWM2_Start();
delay_us(500);
PWM2_Stop();
delay_us(500);
PWM2_Start();
delay_us(500);
PWM2_Stop();
On my ossciloscope i see that my PWM function and delay doesn't working in this timing.
Before first delay my PWM are working 500-600us then delay is about 650 and second PWM is about 600-700us. Why?
Thanks.
PWM function incorrectly timing
-
- Posts: 1550
- Joined: 24 Jun 2007 19:27
- Location: 01800 St Maurice de Gourdans France
- Contact:
Re: PWM function incorrectly timing
hello,
Wich timer is associated to your PWM ?
Dely_ms normally doesn't use a timer .. just count somme loops.
30 Khz means T=33µS
you Start and Stop PWM without being synchronised with Delay
so maybe + - 33µS minima shift..
and
When you Stop PWM , does PRx (on Timer x used by PWM ) just roll over ? or not reach his normal value ...
somme PIC have ehanced function with PWM like 18F27K42
and Synchro facilities...
Wich timer is associated to your PWM ?
Dely_ms normally doesn't use a timer .. just count somme loops.
30 Khz means T=33µS
you Start and Stop PWM without being synchronised with Delay
so maybe + - 33µS minima shift..
and
When you Stop PWM , does PRx (on Timer x used by PWM ) just roll over ? or not reach his normal value ...
somme PIC have ehanced function with PWM like 18F27K42
and Synchro facilities...
Re: PWM function incorrectly timing
Hello, thank you for reply.
I am using pic16f1825 , so PWM2 function use TIMER1.
I will create some Delay function based on timer0 for better precision. ( thank you for idea).
Do i need some extra definition for TIMER1 to be synchronised with PWM2?
I am using pic16f1825 , so PWM2 function use TIMER1.
I will create some Delay function based on timer0 for better precision. ( thank you for idea).
Do i need some extra definition for TIMER1 to be synchronised with PWM2?
Re: PWM function incorrectly timing
Attached is my attempt at example code to do what I think you want to do.
I noticed the following strange features shown in your scope traces:
1) A 500us burst of 30kHz should show 14 to 15 cycles of 30kHz. Your pictures only show 3 or 4 cycles
2) The third picture (1.jpg) shows you measuring only 2.5 cycles, not 3 cycles. A cycle of a square wave consists of 50% high AND 50% low.
3) Your scope shows something closer to triangle waves than square waves - no idea why that might be. My scope shows square waves.
4) I find that a 10k Ohm pull-down resistor gives better wave shape between bursts. Don't know the reason, but PWM2_Stop(); leaves RC3 as input.
I noticed the following strange features shown in your scope traces:
1) A 500us burst of 30kHz should show 14 to 15 cycles of 30kHz. Your pictures only show 3 or 4 cycles
2) The third picture (1.jpg) shows you measuring only 2.5 cycles, not 3 cycles. A cycle of a square wave consists of 50% high AND 50% low.
3) Your scope shows something closer to triangle waves than square waves - no idea why that might be. My scope shows square waves.
4) I find that a 10k Ohm pull-down resistor gives better wave shape between bursts. Don't know the reason, but PWM2_Stop(); leaves RC3 as input.
- Attachments
-
- forum.zip
- (20.24 KiB) Downloaded 41 times
Start every day with a smile...... (get it over with)
Re: PWM function incorrectly timing
Thank you for reply.
I did wrong scope trigger. I will show you now the same signal with better trigger. I saw your code, and is similar like mine. Could you show me pictures of your scope?
And why you wrote this line : TIMER2=0; Is this necessary?
Thing that bothering me is why i have longer cycles about 17-18 when my delay is 500us, but i saw in last reply ,that delay doesn't use timer, so that might be the problem.
I did wrong scope trigger. I will show you now the same signal with better trigger. I saw your code, and is similar like mine. Could you show me pictures of your scope?
And why you wrote this line : TIMER2=0; Is this necessary?
Thing that bothering me is why i have longer cycles about 17-18 when my delay is 500us, but i saw in last reply ,that delay doesn't use timer, so that might be the problem.
Re: PWM function incorrectly timing
Try commenting out these two lines - you will see that pulses wander (roll).TIMER2=0; Is this necessary?
For most applications this "roll" will not matter. If you need exact timing and total predictability, then synchronise the pulses to the Start_PWM().
There may be other ways to synchronise, but TIMER2=0 seemed an easy way for me.
Scope trace attached
Note that 500us is not an exact number of cycles.
- Attachments
-
- NewFile1.png (45.82 KiB) Viewed 908 times
Start every day with a smile...... (get it over with)
Re: PWM function incorrectly timing
By the way....
If I wanted accurate, predictable timing, I would bit-bang the pulses.
The down side of bit-banging is that code is blocking, and the code will be no good if interrupts are enabled.
All depends on the application....
If I wanted accurate, predictable timing, I would bit-bang the pulses.
The down side of bit-banging is that code is blocking, and the code will be no good if interrupts are enabled.
All depends on the application....
Start every day with a smile...... (get it over with)