PWM component in v3.2

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

PWM component in v3.2

Post by echase »

Thanks for the new PWM component. However the help file on this is not very comprehensive so I am baffled as to how to use it. For instance under the parameters box it is expecting a 2 parameter entry with rather funny names for these 2 (sorry not near a copy of Flowcde at moment so can't look up these names). No idea what this is all about, although one is presumably the mark/space ratio to set. Are the funny names variables I need to define? Can anyone explain?

I am also not clear generally on outputs like this (and also straight on/off digital outputs) what variable to set. Why does an output need a variable? Is it because it sets the output to 1 if the variable =1 and 0 if =0? So what happens if variable is say 207 on a single bit output?

Any further source of help files for this?

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

I'll answer your second question first...

Setting a single-bit output with a variable (or a number) works like this: if it is zero, the output is low; if it is not zero, the output is high.

On to the PWM...

Yes - I'm aware that the help file is a little thin. The important bits in the property page are the "period register" and the "prescaler". These both set the period of the PWM signal - neither sets the mark/space ratio. When you change either of these properties, you will see the period of the PWM displayed.

To set the mark/space ratio, you need to call the "SetDutyCycle" macro of the component. Setting the <nDuty> parameter to zero means the PWM output is always low (i.e. 0%). Higher values will increase the percentage of on-time. 100% duty cycle will be achieved when the value of nDuty is larger than the value of the "period register". 50% will be when nDuty is half of (periodReg+1).

Having a big value for the period register will give you greater control of the mark/space ratio (because all values of nDuty above the period register all give 100%).

More info can be found in the datasheet for the particular PICmicro.

I hope this helps a little...

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

What is the parameter nldb (byte)?

Is it the same as PWM channel (char), as sort of implied in the Help file? If not how do you specify which of the 2 PWM outputs the commands go to?

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

nIdx is the PWM channel and takes the value of 1 or 2.

Cybergent
Posts: 3
Joined: Sun Jul 01, 2007 6:22 am
Contact:

PWM Resolution?

Post by Cybergent »

At PWM Properties the Periode Register has a range of 0 - 255.

If I understand this right, the duty cycle resolution is limited to 8 bits.
But when I check the Microchip manuals of the microcontrollers they state a 10 bits resolution for PWM.

So is it not possible to use the full 10 bits resolution with PWM?
If it is only 8 bit it would be completely useless to me.

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

We took the decision to limit it to 8-bit because this was thought more than enough for the majority of Flowcode users.

It is relatively easy to write C code for the PWM module, so any competent C programmer can write code to allow 10-bit resolution.

Cybergent
Posts: 3
Joined: Sun Jul 01, 2007 6:22 am
Contact:

Post by Cybergent »

Thanks for the reply, but if I could write C-code or Assembler I wouldnΒ΄t be using Flowcode, but the original development system of Microchip.

The reason I use Flowcode is, that I do not want to always have to work with programmers when I have a small microchip application.

Even if 8 bits is enough for most users I stilll do not see a reason why to limit that as all the chips I found can do 10 bits. If anyone only needs 256 steps they can limit it by themselves.

As from the advertising I thought Flowcode is a professional solution for those, who work with electronics, but are no programmers. But if I cannot use the chips capabilities it looks more like a studentΒ΄s tool...

So what should I do now?

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Limiting numbers to 8-bits does make things less complicated because these numbers will work with all number types - this is the reason it was limited to 8-bits. If the macro needed a 10-bit number as a parameter, then you would need to use an "INT" data type and variables set as a "BYTE" would be near-useless. I hope this explains the reason for our decision.

To allow 10-bit access, you will need to set the 2 lowest bits yourself in a 'C' icon. These are bits 4 and 5 of the CCP1CON (and CCP2CON) registers. The best place to do this is immediately after the "SetDutyCycle" macro call.

I'll suggest 3 methods for this...

Method 1
The 'C' icon should contain one of these lines:

Code: Select all

ccp1con = (ccp1con & 0xCF);         // lsb's = 0
ccp1con = (ccp1con & 0xCF) | 0x10;  // lsb's = 1
ccp1con = (ccp1con & 0xCF) | 0x20;  // lsb's = 2
ccp1con = (ccp1con & 0xCF) | 0x30;  // lsb's = 3

Method2
You could perform this calculation automatically if you wish. The following code segment should go into a "calculation" icon. It separates a 10-bit "PWM_VAL" into 2 parts - "PWM_MSB" is the 8-bit value to be passed to the SetDutyCycle macro and "PWM_LSB" is the value to go into the ccp1con register:

Code: Select all

PWM_MSB = (PWM_VAL & 0x3FC) >> 2
PWM_LSB = ((PWM_VAL & 0x03) << 4) | 0x0F
To set the ccp1con value, put this is a 'C' icon:

Code: Select all

ccp1con = FCV_PWM_LSB;

Method 3
You can edit the code for the PWM component yourself to add a function that accept a 10-bit value instead of an 8-bit value for the duty cycle. I have done this here and it seems to work. If you want the file, please email me and I'll send it to you.

I hope you find this information useful.

And I'm sorry you find Flowcode inadequate for your needs . But because it can accept standard C, it does mean that almost every limitation of Flowcode can be overcome quite easily (without necessarily impacting on its ease-of-use).

We realised a long time ago that you can never please everyone! I regularly get emails asking for additional functionality, and in the same week I will get one asking for reduced functionality!!! It's a difficult balance to maintain, but the majority of our customers seem happy with Flowcode and with their help and feedback we will continue to make it better and better.

Cybergent
Posts: 3
Joined: Sun Jul 01, 2007 6:22 am
Contact:

Post by Cybergent »

steve wrote:Method 3
You can edit the code for the PWM component yourself to add a function that accept a 10-bit value instead of an 8-bit value for the duty cycle. I have done this here and it seems to work. If you want the file, please email me and I'll send it to you.
Thanks for all your help.
IΒ΄ve sent you an email to get the file.

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Post by ALAN_26 »

Hi Steve I really would like to congratulate for the great work done in V3.2 !

Having said that ,

I think the one asking for reduced functionality is your software competitor ! :?

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: PWM component

Post by echase »

Far as I can see on my 2520 PIC the slowest the PWM can go with clock speed of 500kHz is several Hz. I want about 0.1Hz or even slower to drive a buzzer on and off (like the brief slow beeps you get out of a smoke alarm when the battery is giving out).

Would going to 10 bit slow it further or is there any other way to do it? Or is it a fundamental limitation of the PIC rather than the software?

I could use an interrupt routine instead of a PWM to drive a slow on/off but as am having trouble with interrupts already am reluctant to add complexity to them.

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 component in v3.2

Post by Benj »

Hello

Sorry but the 10-bit PWMs run at the same speed as the 8-bit versions. You just have more control over the mark / space ratios.

If you want to add another interrupt then I can show you how to add a timer2 interrupt to your FCD file. This will allow you to add the interrupt without having to change your previous interrupt routines. Otherwise you will need to work this operation into your main program loop somewhere.

Post Reply