12f615 Pwm

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
CubyteJ
Flowcode V4 User
Posts: 2
Joined: Mon May 02, 2011 2:11 pm

12f615 Pwm

Post by CubyteJ »

I'm new to flowcode and need a little help
i used to use proton ide to program a 12f615 with 2 pwm output's (same frequency but 180 deg's out of phase) using pins (5&7)
how do i this with flowcode?

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: 12f615 Pwm

Post by Benj »

Hello,

To do this you simply create a program using the PWM component if the device supports 2 hardware PWM channels or using a software method as shown here.

http://www.matrixmultimedia.com/article.php?a=52

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

Re: 12f615 Pwm

Post by medelec35 »

Normally when using the pwm, even if pwm 1 and pwm2 are set at different duties (if chip has at least 2 pwm channels). Then both ports connected to PWM (CCP1 and CCP2) go high in value when internal register TMR2 rolls over from 255 to 0. when TMR2 equals internal CCPR1L (assuming 8 bit pwm is used) then CCP1 pin goes low. or in the case of 2 pwms when TMR2 equals internal CCPR2L (again assuming 8 bit pwm is used) then CCP2 pin goes low. The only way these can be inverted is by using a chip with half bridge function
Using 12F615, since there is not a PWM half bridge arrangement, there will be two ways round this.

1) Use some clever custom code embedded within the PWM component (although that's a pure guess of course). You cant read the port register since that is not controllng the pin for PWM. Thats a pity since it would make this project a lot eaiser :P )

2) Use Bit bang PWM. can be done and there are numerous example on this. but more difficult to control.
A good bit bang software control PWM can be found here:
http://www.matrixmultimedia.com/mmforum ... 891#p22891
Then its a simple case of bit for PortA0 =! bit for PortA5 which means if A0=1 then A5=0 or if A0=0 then A5=1 Etc

I Have decided to give option 1 a try. Although I can't do the clever custom part. I have used a bit of C code embedded in timer 0 interrupt.
The idea is the timer interrupt would activate much faster than PWM, so would keep checking the internal registers that control PWM o/p.
Since the flowchart usies the PWM component, you should be able to alter its duty or frequency much easier than bit bang method.

You have not stated the OSC or pwm frequency, so the for the former i have just used the internal 8MHz osc.
As for the frequency I have just randomly thought of a number and used that.
The duty is variable from 0 to 100%.
The o/p from GP0 should be inverted to GP5.
If you have a OSC frequency, and type eg. internal/external etc., PWM frequcey and duty in mind then im sure I could adjust the flowchart to suit your needs.
That's if works the way you have intended of course.

Note: Because I'm using a bit of C, the inverse port function will not simulate. Therefore I have attached a screen shot from a simulator that runs form hex generated files.:
PWM & 180.jpg
PWM & 180.jpg (93.4 KiB) Viewed 5965 times
Martin
Attachments
PWM & 180 inv.fcf
(7 KiB) Downloaded 397 times
Martin

CubyteJ
Flowcode V4 User
Posts: 2
Joined: Mon May 02, 2011 2:11 pm

Re: 12f615 Pwm

Post by CubyteJ »

thanks for the example it's a great help :D

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

Re: 12f615 Pwm

Post by medelec35 »

CubyteJ wrote:thanks for the example it's a great help :D
Your welcome. So long as it did help (and works on hardware of course )that's the main thing.
There will be a point when it does not work so well as you increase PWM frequency above a certain value(do not know the maximum frequency of PWM will be)
Timer0 frequency can be increased more to compensate but there will be limits.
So it worked on your hardware the way you wanted it to?

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

Re: 12f615 Pwm

Post by medelec35 »

The way I achieved the desired effect as stated earlier is to check if tmr2>ccpr1l etc.
PWM + PWM180.jpg
PWM + PWM180.jpg (68.47 KiB) Viewed 5906 times
I checked with a scope and this does work well if lower PWM frequencies are used.
If frequency is about 4KHz then is a small time delay of about 20uS
Between CCP1 going high and GP0 going low etc.
Even with tmr0=+220;
Is there a better method?
My idea is to use custom code and as soon as flag for port CCP1 changes, then use GP0=!ccp1.
Does anyone know how this custom code can be placed.

I believe since tmr2 is auto running all the time due to osc, then where the code is placed should be where the actual comparing takes place.
Placed anywhere else like timer0 or main etc., then there will be some delay.

Thanks

Martin
Martin

Post Reply