Page 1 of 1

PIC18F4685 2 PWM?

Posted: Fri Nov 02, 2012 10:06 pm
by Speedy2k
I, i would like to know if it is possible with flowcode to have two pwm output, the frequency can be the same for both but the duty cycle must be independant. I know there is 1 CCP1 and 1 ECCP1 port, can they be used both for pwm and if yes how? Since flowcode doesn't let me put 2 different pwm?

Thanx a lot!

Re: PIC18F4685 2 PWM?

Posted: Fri Nov 02, 2012 10:57 pm
by medelec35
I created a Flowchart to run two pwm at the same frequency for a 16F615.
It uses one CCP and the other pwm channel uses C to monitor tmr2 channnel.
See:
http://www.matrixmultimedia.com/mmforum ... 01&#p30330

Alternatively the other way is to use bit bang pwm method.
There are a few posts on this.

If you get stuck I can help you further.

Martin

Re: PIC18F4685 2 PWM?

Posted: Fri Nov 02, 2012 11:13 pm
by Speedy2k
But this chip should already have 2 pwm channel no? Maybe i understand it wrong?

Re: PIC18F4685 2 PWM?

Posted: Fri Nov 02, 2012 11:38 pm
by medelec35
Hi Speedy2k,
You will need to determine how many CCP channels there are and not ECCP
Easiest way is to load data sheet and search for ccp2.

If only one CCP is found then you can only have 1 pwm, since ECCP is not currently supported by Flowcode.

I just had a look at the datasheet, and it looks like only 1 PWM is available since PWM2, PWM3 and PWM4 are ECCP, so I don't believe they count.

Martin

Re: PIC18F4685 2 PWM?

Posted: Fri Nov 02, 2012 11:41 pm
by Speedy2k
Oh i see!! this is really not nice that ECCP is not supported!! I really need 2 independant PWM channel with the same frequency but independant duty cycle!

Re: PIC18F4685 2 PWM?

Posted: Sat Nov 03, 2012 12:12 am
by Speedy2k
Here is my flowchart if maybe there is something to do with it! I know there is plenty of strange things in it, but i am still testing with it!! What i want to do with it, is to control with a PID 2 separate 12 volts motors by PWM, both motor must turn the same speed!

Re: PIC18F4685 2 PWM?

Posted: Sat Nov 03, 2012 4:53 am
by ionize
Here is a chart with 2 independent PWM channels based on brandonb's, Martin's, Enamul's and others various charts (thanks for sharing your knowledge!). It works well and does what I want but It needs a little refinement and maybe one of these Flowcode masters can help us both by doing that. For instance, I'm going to remove the fast/slow button since I don't need it. I believe the 16F690 has only one CCP. Now I'm just a newby/novice.....
Hope this helps.

Re: PIC18F4685 2 PWM?

Posted: Sat Nov 03, 2012 10:39 am
by Enamul
I believe the 16F690 has only one CCP.
Yes you are right. It has only one CCP module. You can't use two hardware CCP in this chip. As Martin said PIC18f4685 has one CCP as well.

Re: PIC18F4685 2 PWM?

Posted: Sat Nov 03, 2012 2:51 pm
by Speedy2k
And is there any way to activate and controll the ECCP PWM with C code ?

Re: PIC18F4685 2 PWM?

Posted: Mon Nov 05, 2012 12:24 am
by Speedy2k
I have tried what you hace said by enabling a second pwm based on timer2, it seems pretty good but there is some problem
Look at this video:
http://www.youtube.com/watch?v=PNxiTqCmfE0

On the scope, the purple is the ccp pwm and the green one is the software one.
You can see the hickup in the green signal from time to time, when connected to my motor with the pid, it create inconsistency. On this video, the motor is connected to the ccp pwm and it is able to keep the speed pretty well. I will upload a video with the motor connected to the software pwm later and the flowchart for your review. And on this video both pwm should be set on the same duty cycle and they are off a little bit.

Re: PIC18F4685 2 PWM?

Posted: Mon Nov 05, 2012 2:15 am
by Speedy2k
Ok here is the Flowchart! I don't know if there is any faster way to do that?

Re: PIC18F4685 2 PWM?

Posted: Mon Nov 05, 2012 2:23 pm
by medelec35
If there is an issue by using PWM component + reading timer2 register, then there are two options I can think of.

1) Use a target device with 2 CCP channels or

2) Use software bit bang method for both PWM o/p’s

I believe ECCP is used for bridge type controlling?
Therefore the o/p’s will be a certain phase with respect to CCP1.
So C code using ECCP may not work as expected?
I could be wrong of course.

Martin

Re: PIC18F4685 2 PWM?

Posted: Mon Nov 05, 2012 4:02 pm
by Mikat
The Eccp can be used either normal pwm, or a bridge driver...
Here is the code for eccp1 to use normal pwm via C code.

Code: Select all

//init
pr2 = 99; //period to 99
t2con = 0x05; //tmr2 on prescaler to 4
//enable pwms
ccp1con = 0x0C; // enable ccp1
 eccp1con = 0x0C; //Enable eccp1
//Set the duty cycles
ccpr1l = FCV_DUTY_1; //Set the cpp1 duty to FCV_DUTY_1
eccpr1l = FCV_DUTY_2; // set eccp1 duty cycle to FCV_DUTY_2
That should work, at least I've have used it, and it seems to work..
I am not sure is this needed, but in my code, the RC2 and RD4 pins are set to outputs before that init code (RC2 is ccp1 ans RD4 is eccp1)..

Mika

Re: PIC18F4685 2 PWM?

Posted: Mon Nov 05, 2012 4:42 pm
by medelec35
Mikat wrote:The Eccp can be used either normal pwm, or a bridge driver...
Here is the code for eccp1 to use normal pwm via C code.
Thanks Mika,
That is very useful information!

I was not familiar with ECCP (as you can tell) :lol:

I will also have a play so I can learn about it myself :)

Martin

Re: PIC18F4685 2 PWM?

Posted: Mon Nov 05, 2012 10:36 pm
by Speedy2k
Mikat wrote:The Eccp can be used either normal pwm, or a bridge driver...
Here is the code for eccp1 to use normal pwm via C code.

Code: Select all

//init
pr2 = 99; //period to 99
t2con = 0x05; //tmr2 on prescaler to 4
//enable pwms
ccp1con = 0x0C; // enable ccp1
 eccp1con = 0x0C; //Enable eccp1
//Set the duty cycles
ccpr1l = FCV_DUTY_1; //Set the cpp1 duty to FCV_DUTY_1
eccpr1l = FCV_DUTY_2; // set eccp1 duty cycle to FCV_DUTY_2
That should work, at least I've have used it, and it seems to work..
I am not sure is this needed, but in my code, the RC2 and RD4 pins are set to outputs before that init code (RC2 is ccp1 ans RD4 is eccp1)..

Mika
Wow thanx a lot!!! IT is working perfect!!!!!