PWM function incorrectly timing

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
mikikimi
Posts: 13
Joined: 25 Nov 2020 17:29

PWM function incorrectly timing

#1 Post by mikikimi » 04 Feb 2022 09:36

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.
3.jpg
3.jpg (41.67 KiB) Viewed 944 times
2.jpg
2.jpg (41.72 KiB) Viewed 944 times
1.jpg
1.jpg (40.34 KiB) Viewed 944 times

paulfjujo
Posts: 1550
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: PWM function incorrectly timing

#2 Post by paulfjujo » 04 Feb 2022 14:50

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...

mikikimi
Posts: 13
Joined: 25 Nov 2020 17:29

Re: PWM function incorrectly timing

#3 Post by mikikimi » 05 Feb 2022 09:48

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?
timer1 sinhronizacija.PNG
timer1 sinhronizacija.PNG (52.95 KiB) Viewed 920 times

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: PWM function incorrectly timing

#4 Post by hexreader » 05 Feb 2022 11:27

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.
Attachments
forum.zip
(20.24 KiB) Downloaded 41 times
Start every day with a smile...... (get it over with) :)

mikikimi
Posts: 13
Joined: 25 Nov 2020 17:29

Re: PWM function incorrectly timing

#5 Post by mikikimi » 05 Feb 2022 12:03

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.
1.jpg
1.jpg (47.62 KiB) Viewed 909 times
2.jpg
2.jpg (51.49 KiB) Viewed 909 times

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: PWM function incorrectly timing

#6 Post by hexreader » 05 Feb 2022 12:15

TIMER2=0; Is this necessary?
Try commenting out these two lines - you will see that pulses wander (roll).

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
NewFile1.png (45.82 KiB) Viewed 908 times
Start every day with a smile...... (get it over with) :)

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: PWM function incorrectly timing

#7 Post by hexreader » 05 Feb 2022 12:32

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....
Start every day with a smile...... (get it over with) :)

Post Reply

Return to “mikroC PRO for PIC General”