FLOWCODE 3 PWM

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

Moderators: Benj, Mods

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

FLOWCODE 3 PWM

Post by ALAN_26 »

Hello
I am currently working on ( building ) a grid tie inverter for my wind turbine , the output stage to the transformer must be
Driven by a variable PWM in order to create a pure sine wave at the output , now the problem is that the PWM resolution in FLOWCODE is only 8bit ( 255 steps ) this is making it impossible to inclement the output voltage by say 0.2v , plus other limitations .

I have FLOWCODE v3 for PIC and AVR .

IF someone can help me I need PWM to have at least 10bit of resolution ( 1023 steps ) .

Thanks in advance
Regards ALAN.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: FLOWCODE 3 PWM

Post by Sean »

Most PIC and AVR devices are capable of generating PWM signals with upto 16-bit resolution using their 16-bit timers.
Either option could be controlled from Flowcode, but would require a small amount of C code to provide direct access to the control registers.

One of the main difficulties with controlling PWM signals above 8-bit resolution, with any 8-bit micro, is that two 8-bit registers must be updated when the period or duty cycle is changed. This can cause glitches in the signal if the system accesses a register pair between the two individual register updates. Both options provide timing signals and update mechanisms to help avoid this situation.

The choice of option might also depend on the required PWM frequency and duty cycle stability.

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

Re: FLOWCODE 3 PWM

Post by Steve »

The PWM module within a PICmicro is 10-bit, but we have implemented just an 8-bit PWM within Flowcode to make it easier.

You should be able to look at the code for the PWM component and you may even be able to modify it to allow 10-bit resolution. We will be doing this change as part of the work for Flowcode v4, but this is not expected soon.

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

Re: FLOWCODE 3 PWM

Post by ALAN_26 »

HELLO STEVE

Thanks for Your Quick reply ,
My problem is that I don’t know C language so it’s not possible for me to modify PWM component
So I think I will need some help on that . but if this is not possible , you sad that the main difficulties with controlling PWM signals above 8-bit resolution, with any 8-bit micro, is that two 8-bit registers must be updated. ARM devices are 32bit micros so maybe they can solve my problem .
I have not both flowcode for ARM because I am waiting for 4v but if arm devices can do it than I will buy a copy ASAP .

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

Re: FLOWCODE 3 PWM

Post by Steve »

The 8-bit limitation is with Flowcode (for PICs and ARMs), so Flowcode for ARM will not solve your problem.

It may be fairly easy to alter the code behind the PWM component so that it utilises all 10-bits. The PIC overcomes the possible glitches by double-buffering access to the PWM timer registers, so you would not see a problem in practice.

I'll try to look into this more tomorrow, but I can't promise as I am really busy (as always!) and I'm off on holiday next week.

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

Re: FLOWCODE 3 PWM

Post by Steve »

Hi Alan,

I managed to knock together the 10-bit code for the PWM - it is attached. Put this new code into the "Components" directory of Flowcode for PICmicros.

It will now show an extra function called "SetDutyCycle10bit" which works in the same way as the normal "SetDutyCycle" function, but instead takes a value between 0 and 1023 for the nDuty parameter. This should give you 10-bit resolution on the duty cycle.

Note that this additional funtion will not simulate.
Attachments
PWM_Code.c
(7.78 KiB) Downloaded 1334 times

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

Re: FLOWCODE 3 PWM

Post by ALAN_26 »

THANKS STEVE !

I will try it and let you know
.

Thanks again !

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

Re: FLOWCODE 3 PWM

Post by ALAN_26 »

HI STEVE .

Well done steve tryed it and warks 100% whene downloaded to PIC18F452 !

freaks out on PROTEUS simulation but this is not important ( just for the information ).

THANKS VERY MUCH ...CHEERS !

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

Re: FLOWCODE 3 PWM

Post by ALAN_26 »

Hello
I’m comparing the old PWM_code.c with the new one in order to see the changes made and trying to learn something , my aim is to modify the PWM_code.c for AVR as these have 16bit PWM , I think this is the pace of code where the 10bit pwm is being produced.

void SetDutyCycle10bit(char nIdx, short nDuty)
{
/*Macro_SetDutyCycle10bit_Start*/

//error checking
#ifndef MX_PWM
#warning "This chip does not have PWM capability"
#endif

#ifdef MX_PWM
#if (MX_PWM_CNT >= 1)
if (nIdx == 1)
{
ccpr1l = (nDuty & 0x3FC) >> 2;
}
nDuty = (nDuty & 0x03) << 4;
ccp1con &= 0xCF;
ccp1con |= nDuty;
#endif

#if (MX_PWM_CNT >= 2)
if (nIdx == 2)
{
ccpr2l = (nDuty & 0x3FC) >> 2;
}
nDuty = (nDuty & 0x03) << 4;
ccp2con &= 0xCF;
ccp2con |= nDuty;
#endif
#endif

/*Macro_SetDutyCycle10bit_End*/
}


My problem is that registers ccp1con , ccp2con , ccpr1, ccpr2, are not the same for AVR and all doe I have searched in the AVR datasheet I cant find PWM registers ,

Thanks in advance for any help , while hopping this is the only problem to accomplish this task .

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: FLOWCODE 3 PWM

Post by Sean »

Hello Alan.

The PWM C code for the AVR devices is contained in the AVR version of the PWM component and is different to the PIC code.

The AVR devices are already capable of operating with 16-bit resolution, and are configured correctly in the TCCR1A register, so only minor modifications will be required.

The period value for both Flowcode PWM channels is held in the ICR1 register. This is a 16-bit register that can be set from 0 to 255 from Flowcode, but could be set upto 65535 from C.

The duty values for the 2 Flowcode channels are set in the OCR1A and OCR1B 16-bit registers. These can also recieve values from 0 to 255 from Flowcode, or 0 to 65535 from C.

The parameters passed to the controlling functions would need to be changed from 'char' to 'int' or 'unsigned int'

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

Re: FLOWCODE 3 PWM

Post by ALAN_26 »

Hello Sean

Thanks very much for your help and concern

I tried to make the modification to the file but I’ cant get it to work ,
If only minor modifications are required ,I will greatly appreciate if you can give us a working copy of the 16BIT PWM_code.c for AVR .

Thank in advance
Alan cilia

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: FLOWCODE 3 PWM

Post by Sean »

I have written a program that uses the original PWM component to initialize the PWM channels and set the clock prescaler. 16-bit control is then provided by 3 simple macros that can be exported, or copied into other programs.
PWM16bit.fcf_avr
(5.5 KiB) Downloaded 1162 times
The macros allow integer values (16-bit) to be set for the period and duty cycles. There is a minor difficulty when using full 16-bit resolution because Flowcode uses signed integer values. The maths can become confusing because values with the 16th bit set are treated as being negative (there are ways around these problems). Resolutions of 15-bit or below avoid all these problems.

The example program sets the period value for 12-bit resolution (0x0FFF), sets Duty cycle 1 to 0x0001, and sets duty cycle 2 to 0x0FFE. The prescaler is set to 1:1 in the PWM component - the clock is divided by 2 because of the PWM mode being used.

The program target is the ATmega324P running at 16MHz, but can be retargetted at most ATmega devices.

Post Reply