PWM and TMR2
Moderator: Benj
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
PWM and TMR2
Hi All,
I try to change the duty time of the PWM output by measuring the time of an input pulse. This last will be done by means of TRM2 interrupt.
But now I get wrong PWM outputs with eg. only 100% duty, or a total wrong frequency. That seems like the registers of PWM are re-written. And that made me look more in detail in the PIC16F877A datasheet. This shows that PWM uses the TRM2 timer. And that means during PWM TRM2 can not be used for other applications.
Is this correct? Is there a solution?
With kind regards
Jan Lichtenbelt
I try to change the duty time of the PWM output by measuring the time of an input pulse. This last will be done by means of TRM2 interrupt.
But now I get wrong PWM outputs with eg. only 100% duty, or a total wrong frequency. That seems like the registers of PWM are re-written. And that made me look more in detail in the PIC16F877A datasheet. This shows that PWM uses the TRM2 timer. And that means during PWM TRM2 can not be used for other applications.
Is this correct? Is there a solution?
With kind regards
Jan Lichtenbelt
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PWM and TMR2
Hello Jan,
Yes the PWM peripheral uses the Timer 2 capture compare registers so you can only use one or the other in your program. Could you use timer 0 or timer 1 for your interrupt instead?
Yes the PWM peripheral uses the Timer 2 capture compare registers so you can only use one or the other in your program. Could you use timer 0 or timer 1 for your interrupt instead?
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: PWM and TMR2
I'm afraid TRM0 and TRM1 are to slow to measure a 1 msec pulse with some accuracy. What to do?
Can I read the TRM2 counter of PWM?
Can I read the TRM2 counter of PWM?
-
Mikat
- Posts: 258
- Joined: Wed Nov 29, 2006 6:32 pm
- Location: Finland
- Has thanked: 7 times
- Been thanked: 36 times
Re: PWM and TMR2
Hi.
Correct me if I am wrong, but the timer 1 should run at Fosc/4, so it is definitely fast and accurate enough.. At 4MHz chip clock, the timer 1 runs at 1µs increments, so the accuracy at 1ms pulse is 0.1%.
Flowcode just doesn't support reading the timer direct, so you have to do it by c-code like FCV_VARIABLE_H = tmr1h and FCV_VARIABLE_L = tmr1L, and the combine those two bytes to int..
Mika
Correct me if I am wrong, but the timer 1 should run at Fosc/4, so it is definitely fast and accurate enough.. At 4MHz chip clock, the timer 1 runs at 1µs increments, so the accuracy at 1ms pulse is 0.1%.
Flowcode just doesn't support reading the timer direct, so you have to do it by c-code like FCV_VARIABLE_H = tmr1h and FCV_VARIABLE_L = tmr1L, and the combine those two bytes to int..
Mika
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: PWM and TMR2
TRM1 interrupt frequency is maximum 75 Hz
But I found a solution: Using the ADC I can measure the input pulse duty time.
In the meanwhile I found out that the RC-circuit needed for ADC makes recording change in pulse duty times to slow.
This returns me to the original question: Is there a solution?
Thanks for help.
Kind regards
Jan
But I found a solution: Using the ADC I can measure the input pulse duty time.
In the meanwhile I found out that the RC-circuit needed for ADC makes recording change in pulse duty times to slow.
This returns me to the original question: Is there a solution?
Thanks for help.
Kind regards
Jan
-
Mikat
- Posts: 258
- Joined: Wed Nov 29, 2006 6:32 pm
- Location: Finland
- Has thanked: 7 times
- Been thanked: 36 times
Re: PWM and TMR2
Hi.
i don't know exactly what are you doing, but if I had to measure pulse width, this is the way I maybe do it.
Connect the incoming pwm pulse both b0 and b1 rb0 int for rising edge and rb1 int for falling edge. When rb0 int occurs, write the tmr1 to 0 (c-code tmr1h = 0; tmr1l =0;) and when the rb1 int occurs read the timer 1 as i mentioned above... Then just little calculation depending the clock speed and timer1 confs (4MHz device clock and 1:1 prescaler gives the time direct in µs) gives the pulse time.
but I don't know how this suits in your application....
Mika
i don't know exactly what are you doing, but if I had to measure pulse width, this is the way I maybe do it.
Connect the incoming pwm pulse both b0 and b1 rb0 int for rising edge and rb1 int for falling edge. When rb0 int occurs, write the tmr1 to 0 (c-code tmr1h = 0; tmr1l =0;) and when the rb1 int occurs read the timer 1 as i mentioned above... Then just little calculation depending the clock speed and timer1 confs (4MHz device clock and 1:1 prescaler gives the time direct in µs) gives the pulse time.
but I don't know how this suits in your application....
Mika
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: PWM and TMR2
Hi All,
I found a solution! With Flowcode you can produce easy PWM yourself with a very simple interrupt maco on TRM0 instead of TRM2.
Disadvantage:
Of coarse TRM0 is slower compared to TRM2, but some applications the high speed is not needed. The maximum speed of TRM0 is 19.2 kHz and with 100 counts a repetition time of 5.2 msec of the PWM is possible. For 255 counts this will be 13.3 msec.
Advantage:
As many as PWM you can make as you want, up to all outputs!!!
With kind regards
Jan Lichtenbelt
PS. Use the second version V2
I found a solution! With Flowcode you can produce easy PWM yourself with a very simple interrupt maco on TRM0 instead of TRM2.
Disadvantage:
Of coarse TRM0 is slower compared to TRM2, but some applications the high speed is not needed. The maximum speed of TRM0 is 19.2 kHz and with 100 counts a repetition time of 5.2 msec of the PWM is possible. For 255 counts this will be 13.3 msec.
Advantage:
As many as PWM you can make as you want, up to all outputs!!!
With kind regards
Jan Lichtenbelt
PS. Use the second version V2
- Attachments
-
- Alternative_PWMs_V2.fcf
- (8 KiB) Downloaded 324 times
-
- Alternative_PWMs.fcf
- (7.5 KiB) Downloaded 351 times
Last edited by Jan Lichtenbelt on Fri Sep 16, 2011 8:36 pm, edited 2 times in total.
-
Spanish_dude
- Posts: 594
- Joined: Thu Sep 17, 2009 7:52 am
- Location: Belgium
- Has thanked: 63 times
- Been thanked: 102 times
Re: PWM and TMR2
Flowcode's timer interrupt parameters supposes you do not set the timer register. (I don't know if there is a TMR1 register I never used that interrupt)
Nicolas
Nicolas
-
Mikat
- Posts: 258
- Joined: Wed Nov 29, 2006 6:32 pm
- Location: Finland
- Has thanked: 7 times
- Been thanked: 36 times
Re: PWM and TMR2
Was that for my suggestion?Spanish_dude wrote:Flowcode's timer interrupt parameters supposes you do not set the timer register. (I don't know if there is a TMR1 register I never used that interrupt)
Nicolas
If it was I know that, but the thing was that measuring the pulse width, using directly timer1 counter, not the interrupt... Btw, at some point the flowcode doesn't set the timer1 control register correctly, at least I need to manually configure it (yes timer1 have control register t1con).
The software pwm could be done via other timers too, if you use direct access to the timer, and interrupt, the pwm frequency could be quite high, over kHz, but there must be carefully that the software has time to get out the interrupt macro before next interrupt occurs (the pwm is so fast that in big duty cycles, the next int triggers before the software gets out of the pin dowm macro) .. That subject was topic somewhere, if somebody is interested at sw pwm...
Mika
-
jgu1
- Posts: 1333
- Joined: Tue Oct 06, 2009 9:39 am
- Has thanked: 1135 times
- Been thanked: 299 times
Re: PWM and TMR2
Hi Jan!
Very interested project. I've looked at it and have not quite figured out yet. Would you please give an example where you adjust the pulse width in via analog input.
Thank you in advance.
greeting
Jorgen
Very interested project. I've looked at it and have not quite figured out yet. Would you please give an example where you adjust the pulse width in via analog input.
Thank you in advance.
greeting
Jorgen
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: PWM and TMR2
Dear Jorgen,
An other way of measuring the on duty time is by integration the input signal. And then digitalise the input by means of ADC.
Integration is very easy. Just a RC circuit. However, the disadvantage of this method is that the RC-time must be about 10 times the period time of the input signal. If that is not a problem, than you can use this method.
Succes,
Jan Lichtenbelt
An other way of measuring the on duty time is by integration the input signal. And then digitalise the input by means of ADC.
Integration is very easy. Just a RC circuit. However, the disadvantage of this method is that the RC-time must be about 10 times the period time of the input signal. If that is not a problem, than you can use this method.
Succes,
Jan Lichtenbelt
-
Spanish_dude
- Posts: 594
- Joined: Thu Sep 17, 2009 7:52 am
- Location: Belgium
- Has thanked: 63 times
- Been thanked: 102 times
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: PWM and TMR2
Please find an improved macro PWM version3, as example to produce as much as PWM's you like. The PWM are produced by means of TRM0 instead of TRM2. TRM2 will be still available of other fast processes.
Succes
Jan
Succes
Jan
- Attachments
-
- Alternative_PWMs_V3.fcf
- (7.5 KiB) Downloaded 268 times
-
jgu1
- Posts: 1333
- Joined: Tue Oct 06, 2009 9:39 am
- Has thanked: 1135 times
- Been thanked: 299 times
Re: PWM and TMR2
Hi Jan!
Thanks for your reply.
I think you misunderstood. I will not measure the PWM input. But I want to adjust the PWM output, by analog input by a potm. in YOUR examble. I know about the hardware.
Best regard
Jørgen.
Thanks for your reply.
I think you misunderstood. I will not measure the PWM input. But I want to adjust the PWM output, by analog input by a potm. in YOUR examble. I know about the hardware.
Best regard
Jørgen.