PWM pic12f675 time period?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
Drakkor
Posts: 41
Joined: Sun Dec 30, 2012 9:34 pm
Has thanked: 5 times
Been thanked: 4 times
Contact:

PWM pic12f675 time period?

Post by Drakkor »

Hello again. While I have been using pic 12f's for awhile now with good results, I am unsure of what controls each part of PWM in code (Not C code though) as I have been doing OK without it so far. This time (Project) instead of reading PWM I am generating it. I just cannot seem to find out what controls the time period? I am using this for RC planes to control servos, etc, and it's best to have a 20ms period and it needs to be 50Hz (I have the 50Hz already ) I am getting close but still not there. I can post my code when I get off work but you may not need that to help me? I am using tmr0 interrupt. The reason I am using pic12 is the size and I want to learn it well before I move on. So........If anyone can give me a clue, that would be great : ) If you make an example chart, please keep it as simple as possible as I believe I am kinda slow : ( Thanks again and Merry Christmas to everybody : )

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 pic12f675 time period?

Post by Benj »

Hello,

The easiest option is for you to use the servo motor component. For this to work correctly you will need two PWM capture compare (CCP) peripherals on board your device. If the chip has these then you can use the servo component to generate the 50Hz servo motor signal.

Looking at the 12F675 it doesn't have any PWM hardware available. therefore to use this device you will probably have to use a timer interrupt and bit bang the pwm out yourself.

Drakkor
Posts: 41
Joined: Sun Dec 30, 2012 9:34 pm
Has thanked: 5 times
Been thanked: 4 times
Contact:

Re: PWM pic12f675 time period?

Post by Drakkor »

Benj, Hi. I am able to generate pwm with this chip but the only thing I don't know is how to calculate, and then control the time period, which needs to be (Or is best) at 20ms. I guess in rc servos they call it frame rate?

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

Re: PWM pic12f675 time period?

Post by medelec35 »

Martin

Drakkor
Posts: 41
Joined: Sun Dec 30, 2012 9:34 pm
Has thanked: 5 times
Been thanked: 4 times
Contact:

Re: PWM pic12f675 time period?

Post by Drakkor »

Hi Medelec35. Thanks for the links but they were doing more than I was looking for and in French language? I was really lost. I have it running at 50 Hz. I just need to see how to get the 20ms frame/time period? I can't see the relationship between clock speed/pre-scaler/ etc. I'm one of those slow people that can only learn in little steps : ( I notice in the charts that in the main, the tmr0 C code was not there? Only in the interrupt macro? I have been doing all of mine without any C code and that may be part of the problem. I will put my fcf here but try not to laugh too hard. I am getting pwm, just not doing something right. It's named bomb drop because it is going to release toy bombs from rc planes : )
Attachments
BombDrop2.fcf
(14.5 KiB) Downloaded 420 times

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

Re: PWM pic12f675 time period?

Post by medelec35 »

Hi Drakkor,
One problem with your flowchart is you can't set internal oscillator to 3276800Hz
Unlike other newer devices (even then osc can only be set to multiples of 4) , The built in oscillator can only be set at 4HMz
There is a osctune register which will adjust the frequency up and down by a few percent but I doubt if you can get that near to 3276800Hz

The only option if you want to use internal osc is to change target device e.g 12F1840 so internal oscillator can be set at 32MHz
Then you should have more control over duty.
If you can let me know how long you want the output to stay on for (every 20ms) for say an i/p on high then for an input on low.

I will then see what I can do for you.

Can I assume with your servo the on-time will be between 1 or 2ms every 20ms?
E.g hold on = 1ms and drop = 2ms?

Martin
Martin

Drakkor
Posts: 41
Joined: Sun Dec 30, 2012 9:34 pm
Has thanked: 5 times
Been thanked: 4 times
Contact:

Re: PWM pic12f675 time period?

Post by Drakkor »

Medelec, Hi. Thanks for all the help. I just happen to have some of those 1840 chips. I remember I bought them for some reason but can't remember why? In my code, I thought that the prescaler made the pulses (Not oscillator speed) run at 32xxx, I may have put an extra digit in there by mistake? As far as the bomb drop goes, yes I was going to have it go full left to full right. I would have to adjust ho long I want it to stay right or left be mechanical limitations of the actual drop mechanism. I was also wondering....... Do I need that much speed to do what I am wanting to do? The 675 works great for reading pwm and ramping led's. I am still missing what controls the frame rate and the relationship of frequency to time period ( 20ms).

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

Re: PWM pic12f675 time period?

Post by medelec35 »

The problem isn't producing the frame rate of a timer interrupt.
the problem is down to producing a 1ms to 2ms on time within the 20ms frame rate.
Its all down to the resolution of the on time.
If you just want a resolution of 1ms then osc frequency can be much lower then if if you want say a resolution 0.1ms
Also the osc frequency and timer settings determines the accuracy of the frame rate.
So we will need to keep increasing timer settings until desired frequency be found (assuming you do not wish to use C code)
If osc frequency is kept low then you may need to increase timer settings so much to get good frame rate accuracy, then there will not be much time left for the routines outside of interrupts.

I will post something in a while for you to look at.
Drakkor wrote:I am still missing what controls the frame rate and the relationship of frequency to time period ( 20ms).
Suppose osc freq is 32MHz you choose a timer interrupt (numbers after decimal point rounded as sloe to 0 as possible) of 1:256
Interrupt period = 122.070Hz = 1/122.070=8.2ms
since 8.2ms is greater than 1ms, that means you can't have the o/p on for 1ms then back off again.

That's the relation ship betwwen the two.

Although it can be done by palcing a small 1ms delay in the timmer interrupt and work, placing time delays within interrupts are not recommended.

Since 1ms = 1/0.001 = 1000Hz then 1000Hz is the minimum timer interrupt frequency.
Then the resolution is 1ms, so you can have on period on for multiples of 1ms
So if you want a resolution of .1ms then timer interrupt frequency must be 1/0.0001= 10KHz

That is why you need the faster oscillator speed.

Going back the the lower resolution 1ms for an example.
The o/p on period can start when count = 0
since count increases by 1 every 1ms then when count =1 output has to go off
then 20ms = count of 20.
So you will need if count = 20 then count = 0

Example if hold = 1ms and drop = 2ms:

Just place at the start of the timer interrupt:
Count = Count +1
Then within the timer interrupt place a decision branch:
If Count = 20 : Count =0: Output = high
(If count =1) && (Drop = 0) then o/p = low
(If count =2) && (Drop = 1) then o/p = low

in reality without C code you will not get and interrupt that triggers exactly every 1ms, so the above was just an example.

Hope this helps

Martin
Martin

Drakkor
Posts: 41
Joined: Sun Dec 30, 2012 9:34 pm
Has thanked: 5 times
Been thanked: 4 times
Contact:

Re: PWM pic12f675 time period?

Post by Drakkor »

Martin, Now I finally see what I was missing : ) Thank you for all the time you took to help me, I appreciate it very much : )
This information is like a Christmas gift to me : ) I'll let you know how I do, although I am working 12 hour shifts right now so it may be a couple weeks. Thanks again and if I don't talk to you again before Christmas, Have a Merry Christmas. If you don't celebrate Christmas, then happy holidays. Take care.

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

Re: PWM pic12f675 time period?

Post by medelec35 »

Drakkor wrote:This information is like a Christmas gift to me : )
Glad you found information useful :)
Drakkor wrote:although I am working 12 hour shifts right now
Ouch!
Drakkor wrote: Have a Merry Christmas
Thank you.
Same to you and everyone reading this.
Merry Christmas :)

Martin
Martin

Drakkor
Posts: 41
Joined: Sun Dec 30, 2012 9:34 pm
Has thanked: 5 times
Been thanked: 4 times
Contact:

Re: PWM pic12f675 time period?

Post by Drakkor »

Hello again. I found my 12f1840 chips, updated my pickit2 device list but I get the same error. HHHMMmmmmmmmmmm?
C:\Program Files\Flowcode\v5\Tools\PICkit2\pk2cmd.exe -BC:\Program Files\Flowcode\V5\Tools\PICkit2\ -PPIC12F1840 -FTest1840.hex -M -A5 -R -T
PK2DeviceFile.dat device file requires an update of pk2cmd. (I have the latest version!)
C:\Program Files\Flowcode\v5\Tools\PICkit2\pk2cmd.exe -BC:\Program Files\Flowcode\V5\Tools\PICkit2\ -PPIC12F1840 -FBombDrop2.hex -M -A5 -R -T
Could not find device PIC12F1840.
Return code = 36
Flowcode was unable to transfer the flowchart to the microcontroller. Check the programmer options and physical connections.

Also, If I open Pickit2, it can read the 1840 no problem............

I can go to any other chip (675, 509, etc and no problems at all.
I am so hooked on this stuff that i am getting fat because I can't break away from it : )


OK. I FINALLY found the problem and it took so long to find as it seems only one person posted some info on the web. If you use the updated device list for pickit2, it will NOT work! It will tell you, that to use the updated .dat file, you need updated PK2cmd. which is needed for the pic12f1840. Here's how to deal with it.....Download the updated device.dat file and ONLY put it in the same directory as the microchip pickit2 -DOT- exe. Leave the old version on your Flowcode V5/tools/pickit2. Then, when you are working with chips only supported with the updated device list, compile to Hex, and open Pickit2 stand alone and IMPORT the Hex file and burn it there. I am still able to configure the chips (It seems) from Flowcode. Bottom line is that the updated device file is ONLY for the stand alone Pickit2, and not the PK2cmd.

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

Re: PWM pic12f675 time period?

Post by medelec35 »

Hi Drakkor
I successfully programmed a 12F1840 directly with Flowcode V5:
Program 12F1840.png
(64.93 KiB) Downloaded 5514 times
To do that I had to place the attached files into:
C:\Program Files (x86)\Flowcode\v5\Tools\PICkit2
Since I have 64 bit windows
I you have 32 bit windows then the path is:
C:\Program Files\Flowcode\v5\Tools\PICkit2

IMPORTANT:
You should not use connect VPP directly from VPP of programmer to the target device!
VPP output from programmer is 12.5V
VPP of 12F1840 is 9V max!

What I did was to connect a 10K resistor in series from VPP of programmer to VPP of 12F1840.

If not already done so, you may also need to update the operating system of you PICkit2 programmer.
See:
http://www.matrixmultimedia.com/mmforum ... 17&#p15617

I have also found that:
If using the stand alone Pickit2 programmer and it states no device connected, If you place the attached Dat file in the programmers directory, then the device will be detected.

Martin
Attachments
pk2cmd.zip
(82.23 KiB) Downloaded 412 times
Martin

Post Reply