Need help for PWM control

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

Moderators: Benj, Mods

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

Need help for PWM control

Post by mhuang »

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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Need help for PWM control

Post by Benj »

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

Post by mhuang »

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

Post by mhuang »

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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Need help for PWM control

Post by Benj »

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

Post by mhuang »

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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Need help for PWM control

Post by Benj »

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

Post by mhuang »

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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Need help for PWM control

Post by Benj »

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

Post by mhuang »

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

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: Need help for PWM control

Post by medelec35 »

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:
http://www.matrixmultimedia.com/mmforum ... 21&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

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

Re: Need help for PWM control

Post by mhuang »

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 481 times

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: Need help for PWM control

Post by Benj »

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.

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

Re: Need help for PWM control

Post by mhuang »

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 481 times

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

Re: Need help for PWM control

Post by mhuang »

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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Need help for PWM control

Post by Benj »

Hello Huang,

You need more control variables.

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

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

Re: Need help for PWM control

Post by mhuang »

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

Post by mhuang »

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

Post Reply