Need help for PWM control

Any general or miscellaneous queries that do not fit into the other forum catagories

Moderators: Benj, Mods

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Need help for PWM control

Postby mhuang » Mon Apr 18, 2011 1:24 am

Hi

This is my first time to post here,and also a fresh student to use the PWM in my project.

For aim of my project, it needs build the circuit to generate the different voltage output, so I choose to use the Multi-channel PWM control by the PIC 16F88, in order to I am a little experience to program, use the flow code as programming software. I have built the circuit and simulated the signal, but the problem is that I don't know how to program the Muti-channel PWM output for different duty cycle simultaneously, so I have saw some example of flowcode, but I am not sure which one is really ralated to my project, and I think that it need add the C code to achieve, so please give me some suggestion how to design the flowcode from the single PWM output to the Multi-channel output, and also which example or tutoral can help me to understand it ( give me some website link as well) . Many thanks.


Huang Min

User avatar
Benj
Matrix Staff
Posts: 14157
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

Re: Need help for PWM control

Postby Benj » Mon Apr 18, 2011 10:36 am

Hello,

The 16F88 only has one PWM peripheral so you can only use the one hardware channel.

Here is a method of using a timer interrupt to generate more software controlled PWM channels.
http://www.matrixmultimedia.com/article.php?a=52

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Mon Apr 18, 2011 3:11 pm

Hi Ben:

Thanks your reply, Can you give me a link or send me the Flowcode file by V3? In my computer, only have Version V3.
By the way, I just use the Low pass filter to generete the output depend on the duty cycle, so I can build the multi-channel in the PIC 16F88, many thanks.

Huang Min

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Tue Apr 19, 2011 2:11 pm

Hi
I have download the flowcode v4 and run the example, but when it connects to oscilicope, the waveform always change in the same period time, otherwise, can you explain foe me how to control the duty cycle, thanks!

Huang min

User avatar
Benj
Matrix Staff
Posts: 14157
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

Re: Need help for PWM control

Postby Benj » Tue Apr 19, 2011 2:42 pm

Hello Huang,

Do you need to be able to control the period or do you just need a fixed period with changeable duty?

By default the period is fixed but can be changed via the ChangePeriod macro.

The component help file should aid you in understanding the component macros available for the PWM.

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Tue Apr 19, 2011 3:57 pm

Hi Ben

I need control the period the duty cycle, and I need the define the timer and a frequency counter to count the period of time, the similar project for me, Can you give me your eamil address,because my pdf file is so big that don't upload the webpage, after you recieve the file please help me how to design the flowchart. Many thanks.

also other c code explain the performence,

MAX_COUNT = 2^N;MASK = 2^N - 1;PWM_output = 1;rising_edge = 0; // Rising edge counterfalling_edge1 = MAX_COUNT - duty_cycle1; // Falling edge counter #1falling_edge2 = MAX_COUNT - duty_cycle2; // Falling edge counter #2falling_edge3 = MAX_COUNT - duty_cycle3; // Falling edge counter #3 // Loop forever: while(1) { pwm_temp = 0; // If the rising_edge counter rolls over, make the output high rising_edge = ++rising_edge & MASK;if(!rising_edge)pwm_temp = 0xff; // all outputs high. // If the falling_edge counter rolls over, make the output low falling_edge1 = ++falling_edge1 & MASK;if(!falling_edge1)pwm_temp &= ~1; falling_edge2 = ++falling_edge2 & MASK;if(!falling_edge2)pwm_temp &= ~(1<<1); falling_edge3 = ++falling_edge3 & MASK;if(!falling_edge3)pwm_temp &= ~(1<<2); PWM_OUTPUT = pwm_temp; } This is a 3 PWMS controlled by 3 falling_edges.


Huang Min

User avatar
Benj
Matrix Staff
Posts: 14157
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

Re: Need help for PWM control

Postby Benj » Tue Apr 19, 2011 4:42 pm

Hello Huang,

after you recieve the file please help me how to design the flowchart. Many thanks.


Sorry but we do not generally offer help with customers programs only with bugs and problems with Flowcode. Maybe another forum user could provide you with help or you could pay one of the freelance engineers to sort a program out for you.

The software approach may be the easiest way to do things. Here is a v3 example of bit banging a PWM output.
http://www.matrixmultimedia.com/Downloa ... .php?id=38

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Tue Apr 19, 2011 7:23 pm

Hi

I just want to show you my idea, define the timer to counte the period time, the file also shows my design specificated. I think I can not use the flowchart to define the timer, it should be embeded with c code, for the flowcode v3, it is quite limited function for program, but it is only version to provide from my university, that is why I need your support how to use the c code to define the timer which is quite complex for me, e.g. the setting of interrupt.
Thanks.


Huang Min

User avatar
Benj
Matrix Staff
Posts: 14157
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

Re: Need help for PWM control

Postby Benj » Wed Apr 20, 2011 10:07 am

Hello Huang,

Flowcode v3 does support timers and you do not have to use C code.

Please see these examples.
http://www.matrixmultimedia.com/Downloa ... .php?id=42
http://www.matrixmultimedia.com/Downloa ... .php?id=41
http://www.matrixmultimedia.com/Downloa ... .php?id=40

If you need more control over your timer then you can disable and reconfigure using the interrupt icons in Flowcode. You may also get directly to the timer registers by using C code if you need to tweak the values at all.

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Wed Apr 20, 2011 3:20 pm

Hi

Thanks your reply, the I hav download website link files, whether it can modify the my enclosed c code ?

I think some files need add the c code icon, and the frequency is so slow.


Huang Min

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8335
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2382 times
Been thanked: 3393 times
Contact:

Re: Need help for PWM control

Postby medelec35 » Wed Apr 20, 2011 5:35 pm

mhuang wrote:the frequency is so slow.

If you double click on the timer interrupt, and then select properties.
You can then change prescaller rate to increase frequency.
If the frequency you want is different from the interrupt frequency, you have a couple of options.

1) You could add a C box, with either tmr0 += x; or tmr0l += x;
where x = a number from 0 to 255
see:
viewtopic.php?f=29&t=7921&p=21462

2) Select a frequency slightly higher than you need (but remember the lower the frequency the better, so don't go too high if possible.)
Then in the timer interrupt macro, use Count = count+1. If count >= x then count = 0 : update flag =1.

If you post your flowchart, and state frequency you are after, we can then help you further. Please don't forget to change osc speed to correct settings, as that will have a overall effect on the interrupt frequency required.

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Thu Apr 21, 2011 12:33 pm

Hi

I still don't know how to control the duty cycle only by the software solution, not CCP, the flow code should follow the C code which explain my idea, Please see my flowcode, tell me where the problem is, many thanks.

Huang Min


Code: Select all

> >> MAX_COUNT = 2^N;
> >> MASK = 2^N - 1;
> >> PWM_output = 1;
> >> rising_edge = 0; // Rising edge counter
> >> falling_edge1 = MAX_COUNT - duty_cycle1; // Falling edge counter #1
> >> falling_edge2 = MAX_COUNT - duty_cycle2; // Falling edge counter #2
> >> falling_edge3 = MAX_COUNT - duty_cycle3; // Falling edge counter #3
> >>
> >> // Loop forever:
> >>
> >> while(1) {
> >>
> >> pwm_temp = 0;
> >>
> >> // If the rising_edge counter rolls over, make the output high
> >>
> >> rising_edge = ++rising_edge & MASK;
> >> if(!rising_edge)
> >> pwm_temp = 0xff; // all outputs high.
> >>
> >> // If the falling_edge counter rolls over, make the output low
> >>
> >> falling_edge1 = ++falling_edge1 & MASK;
> >> if(!falling_edge1)
> >> pwm_temp &= ~1;
> >>
> >> falling_edge2 = ++falling_edge2 & MASK;
> >> if(!falling_edge2)
> >> pwm_temp &= ~(1<<1);
> >>
> >> falling_edge3 = ++falling_edge3 & MASK;
> >> if(!falling_edge3)
> >> pwm_temp &= ~(1<<2);
> >>
> >> PWM! _OUTPUT = pwm_temp;
> >>
> >> }
> >>
> >> This is a 3 PWMS controlled by 3 falling_edges
Attachments
timer.fcf
(10 KiB) Downloaded 270 times

User avatar
Benj
Matrix Staff
Posts: 14157
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

Re: Need help for PWM control

Postby Benj » Thu Apr 21, 2011 12:51 pm

Hello,

In the software PWM example "PWM_LED_Example" there is currently only one channel that is being used. The duty cycle is controlled using the LED_Brightness variable. You could scale this up to make 3 PWM channels by using 3 control variables and 2 additional decision icons inside the Timer_Int macro to switch the output pin on and off.
These users thanked the author Benj for the post:
mhuang (Wed Apr 27, 2011 6:10 pm)
Rating: 5.56%
 

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Wed Apr 27, 2011 5:30 pm

Hi

Thanks for your help. I have programed following what you said, but it alson doesn't sort my problem, because the current task need wait to previous task complete, so the OFF can not control depend on how long for the previous task running. The file is enclosed below, please check.

Huang Min
Attachments
PWM_LED_Example.fcf
(5 KiB) Downloaded 306 times

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Wed Apr 27, 2011 6:31 pm

Hi

For example, the duty cycle of B0 is 20%, the B1 is 60%, so how is setting the LED_brighten_variable.Many thanks.


Huang Min

User avatar
Benj
Matrix Staff
Posts: 14157
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

Re: Need help for PWM control

Postby Benj » Thu Apr 28, 2011 11:32 am

Hello Huang,

You need more control variables.

Here is a better example.
Attachments
Multi_Channel_SW_PWM.fcf
(8 KiB) Downloaded 394 times

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Thu Apr 28, 2011 4:31 pm

Hi

The file is created by the V4 or other further version, but I only have the V3 in my computer, so please attach the file for V3.
In addtion, for this new program, how to control the duty cycle? Please also explain for me in the reply, Many thanks.


Huang Min

mhuang
Posts: 13
Joined: Fri Apr 15, 2011 11:20 am
Has thanked: 1 time
Contact:

Re: Need help for PWM control

Postby mhuang » Thu Apr 28, 2011 10:00 pm

Hi Benj

Thanks for your example, I have donwload new verison and run the file. when I change the delay time to 1s, it is not stable, because the simulation shows the B0 is brighten and B1 is off after running 50 second. Please verify what I am saying for the program, many thanks.

Furthermore, for the better example, how can I control the duct cycle.

The previouly attached C code may give some tips. :)

Many thanks.

Huang Min