PWM on 12F675 using TMR0

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
MarkW
Flowcode v5 User
Posts: 118
Joined: Thu Sep 17, 2009 1:30 pm
Has thanked: 3 times
Been thanked: 11 times
Contact:

PWM on 12F675 using TMR0

Post by MarkW »

Hi Guys

Been trying to pwm a dc brushed motor using a 12F675, pwm freq to be in
khz range. Processor is running internal osc = 4mhz. Read pot on ADC
to adjust pwm duty cycle. Since 12F675 has no pwm module i opted
for using tmr0 as freq generator....but i have tried all sorts of interrupt
algorithms etc no luck. either get strange signal on scope or it crashes etc.
Any ideas what will work with tmr0? pwm is sent out on portA, bit5
Thanx in advance!

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 on 12F675 using TMR0

Post by Benj »

Hello Mark,

Any chance you can post up your Flowcode file and I will have a look and see if I can spot what is going wrong for you.

MarkW
Flowcode v5 User
Posts: 118
Joined: Thu Sep 17, 2009 1:30 pm
Has thanked: 3 times
Been thanked: 11 times
Contact:

Re: PWM on 12F675 using TMR0

Post by MarkW »

Hi Ben

Thanx for getting back, i have attached a simple form of the FC file that illustrates
my exercise. Looking on the scope at the output pin, as i adjust the pot (1K centre
tap to ADC input, ends to 5V / GND), i see the period shifting all over the place.
I see at certain point of rotation range the pwm happens. Outside of that narrow range
the period/duty cycle is all over the place. Is the interrupt routine loop causing a timing
error with the tmr0?
Attachments
12F675_FC4_PWM.fcf
(5.5 KiB) Downloaded 334 times

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM on 12F675 using TMR0

Post by medelec35 »

Hi Mark.

You could give attached flowchart a try.

I have not been able to test it on real hardware as it's a bit late for me, so I can't guarantee it will work.

With PWM you need to set a pin high at exactly the same time period e.g every 1ms. its the off time that is variable depending on the duty required.

If get a bit of time spare friday, I will also test on hardware.

Martin
Attachments
12F675 PWM1.fcf
(7 KiB) Downloaded 330 times
Martin

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM on 12F675 using TMR0

Post by medelec35 »

Unless I can work out a different method, I believe I created a similar flowchart for controlling the brightness of an LED not a motor.

I thought at the time that using timer0 to create PWM, the frequency will not be that high at all.

If I remember correctly it was designed to run at about 96Hz using 19.6MHz xtal.

So using 4MHz internal osc, your probably only going to get 20 or so Hz.

If using a pre-set value for timer0, if too high then hardware will probably hang, and you won't get any time to run main code.


So the flowchart on above post may not be any use to you at all?

Martin
Martin

MarkW
Flowcode v5 User
Posts: 118
Joined: Thu Sep 17, 2009 1:30 pm
Has thanked: 3 times
Been thanked: 11 times
Contact:

Re: PWM on 12F675 using TMR0

Post by MarkW »

Hi Martin,

Thanx for reply. Your chart is pretty neat, but i figured a method that must allow the duty cycle
to adjust without crashing...if one reads the ADC value it must be scaled to a range that such that
the interrupt routine can finish looping before the next tmr0 interrupt. But there are still issues,
will post a chart later. Funny thing is, doing a simple on/off single loop in the interrupt
i get a 2.5khz period even though the timer is set 1:1 prescale, which should be 3.9khz?
Strange....thanx for your input!

Mark

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM on 12F675 using TMR0

Post by medelec35 »

The problem with using timer0 for pwm, is suppose set prescale to 1:1 ,as you say should be 3.9khz.
Then the higher the resolution of duty then the lower the pwm frequency will be.
E.g there there are 50 settings of duty value for a 0 to 100 duty. So count would be set to 50.
Then PWM frequency = 3.9KHz / 50 = 78Hz

The other factors to take in account is all timer0 is running all the time.
So if still inside timer0 interrupt macro (at this point interrupts are disabled) when timer0 rolls over then the timer0 macro is exited, then you will have missed the interrupt from being triggered at the correct time.
This will cause the the pwm period to be incorrect.
You also cant use a delay in both timer0 interrupt and in main, or your get a corruption issue since there is a call to delay macro from both interrupt and main.

I may have thought of a way round this so you can use timer0 for pwn at a high frequency (the 3KHz range). Buts its only theory, so may not even work.
I will let you know.
Have you though of using a 12F615. Its a similar chip ie 8pins, but it has built in PWM, so you can use it with pwm component.
Aslo has a 1% 4MHz or 8MHz selectable internal osc. That will solve your pwm problems in an instance.
MarkW wrote: i get a 2.5khz period even though the timer is set 1:1 prescale, which should be 3.9khz?
Since osc is running at 4MHz. if prescaller is set to 1:1 then timer0 register increments every 4x10E6/4 = 1MHz (or 1 uS). Roll over occurs after every 256 counts from 0 = 1MHz/256 = 3906.25 (or 256uS)
So it could be a combination of:
Your internal osc, and measuring equipment are 100% accurate, they do have some error tolerances.
Although saying that 36% error is lager than I would of expected TBH.
Did you want to post your flowchart so I can take a look?

Martin
Martin

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM on 12F675 using TMR0

Post by medelec35 »

Try this.
Using Timer0 and 4MHz int osc
PWM frequency is about 2KHz (not quiet the 3KHZ range but don't want to reduce steps to much).

Duty from about 1 to 90% in 50 steps

Not tried on real hardware, only tried on a simulator

If this does work, you was very close with your flowchart!
MarkW wrote:i see the period shifting all over the place.
I see at certain point of rotation range the pwm happens. Outside of that narrow range
the period/duty cycle is all over the place. Is the interrupt routine loop causing a timing
error with the tmr0?
Yes I believe that's what is happening.

The main difference is you was allowing the time within the interrupt to exceed the duration of from one roll over to the next, since each POT = POT-1 takes about 11uS to carry out.
You only had a maximum time of a bit less than 256uS
if POT was 255 this would take 11 x 255 = 2805uS + time for A5 on and off etc.
That's why your frequency was all over the place when you adjusted the pot.

Martin
Attachments
12F675 PWM2.fcf
(7 KiB) Downloaded 390 times
Martin

MarkW
Flowcode v5 User
Posts: 118
Joined: Thu Sep 17, 2009 1:30 pm
Has thanked: 3 times
Been thanked: 11 times
Contact:

Re: PWM on 12F675 using TMR0

Post by MarkW »

Hi Martin

Thanx for all your input etc. You are correct in all you said about this bitbang
method (tmr0) for pwm.....and you are correct that using a chip with pwm
is much better. I did not have the 12F chip you recommended at the time, but
i did dig out a spare 16F818 that has ADC/ PWM etc. using the PWM module
is a breeze and i can get very precise control at the 5khz or greater frequency.
I personally would not recommend the bit bang method....its too fiddly and the highest
frequency obtainable is not in the range that dc motors like too much.
It was an interesting exercise :)

I consider this issue to be SOLVED. Thanx to Martin.

Mark

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM on 12F675 using TMR0

Post by medelec35 »

Hi Mark,
You’re welcome.
I agree with the comments you made regarding creating PWM with timer0 at higher frequencies.

Martin
Martin

Post Reply