Page 1 of 1
Duo PWM with PIC12F683 I think I can?
Posted: Thu Dec 08, 2011 3:22 am
by Werner
Hi Guys, I have been having fun playing around with PWM on a PIC12F683 and am trying to figure out how I can get two PWM channels out of this one pic? I currently am using Pin5 ccp1 for the one channel and works great! Is it possible to create another PWM channel that could be used to dim another LED using interrupt? I am able to blink an LED using Martins example 12F675PWM2.fcf but can seem to get it to work as I would like similar to channel 1. The first file is the one I am working with the second one is the great example Martin posted.
Thanks for the help! Have a great day!
Werner
Re: Duo PWM with PIC12F683 I think I can?
Posted: Thu Dec 08, 2011 11:56 am
by medelec35
Hi Werner, Try this Flowchart.
It should control two separate PWM channels OK.
Since one is using CCP and the other is bit banged using timer0
With Timer0 the PWM is running at 78Hz which should alter brightness of LED from nearly off to full brightness (4% to 100% duty) with no flickering.
I have not go a 12F683, but it’s working as it should on a 12F617 I had laying around.
The Default internal oscillator setting for 12F683 is 4MHz.
If you require a different speed for example running at 8MHz then you will need a C code block with osccon=0x70;
(osccon not required for 4MHz)
Martin
Re: Duo PWM with PIC12F683 I think I can?
Posted: Thu Dec 08, 2011 1:33 pm
by medelec35
This should be a much better version.
Now there are two independent PWM channels both have a variable duty from 0% to approx. 100%
However the clever thing is they both run at the same frequency of CCP1 = 244 Hz.
How this works is:
Timer0 interrupt is running faster than Timer2 interrupt (used for controlling built-in PWM)
Each time Timer0 is triggered, then the contents of timer2 register is compared with the value of Brightness variable (set by AN1).
If contents of timer2 register is less Brightness variable then GP5 = 5V else GP5 = 0V
Don’t know why I didn’t think of this before!
Again confirmed working on 12F617.
Note If you want to adjust PWM frequency there is an upper PWM frequency limit since Timer0 must always run faster then timer2
Martin.
Re: Duo PWM with PIC12F683 I think I can?
Posted: Thu Dec 08, 2011 5:29 pm
by Werner
VERY VERY clever Martin!!

I want you to know I tried to work this out, thinking OK this should be easy. After a million versions of my own I started thinking yeah no so easy….

And then I see how you did it and think, that is simple,

Yeah for Flowcode genius!! I had something similar to your Timer_Tick Macro however I kept thinking that I needed to decision statements like I found in one of your other examples. Would it have not been possible without the C Code that you put in? It looks like your just making TMR2_VAL = tmr2 before the decision? I know I am missing something? Is it that tmr2 is internal to the chip that we have no other way to access it in Flowcode?
Thanks a lot for the help Professor!
Werner
Re: Duo PWM with PIC12F683 I think I can?
Posted: Thu Dec 08, 2011 5:59 pm
by medelec35
Your welcome Werner.
Werner wrote: Is it that tmr2 is internal to the chip that we have no other way to access it in Flowcode?
Well spotted!
FCV_TMR2_VAL = tmr2;
Just means read value of tmr2 (timer 2 interrupt register) that’s used for internally generating PWM signal. and assign to the Flowcode variable called TMR2_VAL
In C if name is in lower case then it’s an internal register
FCV_ part is how you assign a Flowcode variable (one you added yourself) with the contents of register.
Note: When preceding Flowcode variable with FCV_ then you must use the variable name in upper case. E.g a Flowcode variable is called: My Flowcode variable,
then you will use
FCV_MY FLOWCODE VARIABLE;
As far as I'm aware, there is no other way to access chip register without using C code.
Hope I have explained it Ok.
Martin.
Re: Duo PWM with PIC12F683 I think I can?
Posted: Thu Dec 08, 2011 7:30 pm
by Werner
Thanks Martin, C code box is easy enough! You got me thinking, can I do the same thing with a third channel like we did the second one?
Have a great day Buddy!
Werner
Re: Duo PWM with PIC12F683 I think I can?
Posted: Thu Dec 08, 2011 8:31 pm
by medelec35
Werner wrote:Thanks Martin, C code box is easy enough! You got me thinking, can I do the same thing with a third channel like we did the second one?
Have a great day Buddy!
Werner
Just had a look and unfortunately not.
This is because GP3 is a non Analogue input only, so there is no way of controlling the last available output.
Pitty.
Note to anyone who downloads DimmerTest3.fcf.
It is designed to work on hardware at 4MHz with a CCP PWM channel. It will not simulate as it uses a bit of C code which does not simulate.
Hope you have a great day too!
Martin