PWM and TMR2

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
User avatar
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

Post by Jan Lichtenbelt »

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

User avatar
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

Post by Benj »

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?

User avatar
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

Post by Jan Lichtenbelt »

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?

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

Post by Mikat »

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

User avatar
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

Post by Jan Lichtenbelt »

TRM1 interrupt frequency is maximum 75 Hz
TRM1.jpg
TRM1.jpg (36.39 KiB) Viewed 7567 times
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

Post by Mikat »

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

User avatar
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

Post by Jan Lichtenbelt »

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
Attachments
Alternative_PWMs_V2.fcf
(8 KiB) Downloaded 325 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

Post by Spanish_dude »

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

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

Post by Mikat »

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
Was that for my suggestion?
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

Post by jgu1 »

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

Thank you in advance.

greeting

Jorgen

User avatar
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

Post by Jan Lichtenbelt »

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
RC-Integrator.jpg
RC-Integrator.jpg (7.06 KiB) Viewed 7536 times

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

Post by Spanish_dude »

@ Mikat : I actually said that for Jan, I should have mentioned it, my bad :).

User avatar
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

Post by Jan Lichtenbelt »

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

Post by jgu1 »

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.

Post Reply