View previous topic :: View next topic |
Author |
Message |
annj
Joined: 03 Jun 2013 Posts: 4
|
Help for PWM and Different phase |
Posted: Tue Jun 11, 2013 10:41 pm |
|
|
Heya guys,
I hope this is not a redundant post since I could not find the one that match my problem.
I'm using PIC18F4550, CCS compiler V4.084
I wish to output a pwm from the PIC to the IC chip constant current which will feed the high power LEDs.
For starting, I only use two regular LEDs and try to see the output from the PIC using oscilloscope. I manage to lit up both leds using these codes:
Code: |
#include <18F4550.h>
#fuses INTRC_IO,PUT,NOWDT,NOPROTECT,NOLVP,BROWNOUT,MCLR
#use delay(clock=8M)
void main(void)
{
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM @ pin C2
setup_timer_2(T2_DIV_BY_16,124,1); //1ms 0r 1kHz@8Mhz clock
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM @ pin C1
setup_timer_2(T2_DIV_BY_16,124,1); //1ms 0r 1kHz@8Mhz clock
while(1){
long pwm1, pwm2;
pwm1=100; //D.C=20%
pwm2=100;//D.C=20%
set_pwm1_duty(pwm1);
set_pwm2_duty(pwm2);
}
}
|
However, my aim is to start the second pwm (pwm2) with a delay of 0.2ms or 90degrees out of phase. I already try to put delay_us() but to no avail.
What should I do to make it work?
Any suggestions are most welcome.
Thank you. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19383
|
|
Posted: Wed Jun 12, 2013 12:45 am |
|
|
OK.
Key thing is that to give a phase delay between the PWM's, they would need to run off different timers, and the timers are what would have to start with the delay between. Now on your PIC, this can't be done. The PWM only operates off timer2. So when you start the PWM's doesn't change the phase relationship, both PWM's would trigger at the same point, since they both come off one timer.....
Now, if you could use a lower frequency PWM, you could potentially give a result with phase shifting, by using a timer based 'software' PWM.
However if you need a 1KHz PWM, then you need to shift to a chip where the PWM can be operated off different timers. Chips like the 46J50, with ECCP modules rather than CCP modules, allow Timer2, or Timer4 to be selected by the PWM channels, or chips with power control PWM's again allow phase shifting between the channels.
Step back, and describe exactly, what phase timings you want to achieve, at what frequency (is a lower frequency than 1KHz acceptable), and at what resolution (if a low number of PWM 'steps' could be accepted, it simplifies things), and we may be able to tell you what chips can do this.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
annj
Joined: 03 Jun 2013 Posts: 4
|
|
Posted: Wed Jun 12, 2013 1:31 am |
|
|
Dear Ttelmah,
thank you for your quick response.
From your comments, I understand that the PIC that I currently use cannot output two PWm with different phase shift since they are sharing the same timer (timer2). I look at the datasheet, it also provides the ECCP module and can produce up to 4PWM, but again, they are sharing the same timer2.
Basically, in my project:
a. 4 PWMs are required to drive 4 LEDS.
b. I need the same frequency for all pwms and it can be less than 1k, for instance 725Hz and also the same D.C = 20%
c. If let say i use f=1kHz, i want my first PWM starts at t=0s, then the second PWM will start basically at t=0.2ms, third pwm at t=0.4ms and fourth pwm at t=0.6ms
d. No led will be lit up during 0.8ms<t<1ms and the loops continue.
I don't really understand the term 'resolution' that you have mentioned. Is it the duty cycle?
Hope to hear your feedback.
annj |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Jun 12, 2013 2:04 am |
|
|
Quote: | I don't really understand the term 'resolution' that you have mentioned. Is it the duty cycle? |
He wants to know how many different brightness levels do you want (need) from your LEDs?
Mike |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Jun 12, 2013 2:12 am |
|
|
I want to know (amongst other things):-
1) What is your real minimum PWM frequency?
2) What determines your minimum PWM frequency?
3) Will all the LEDs be running at the same duty ratio?
4) Will the duty ratios be larger than 20%?
5) What are you actually trying to do?
6) Have you done any prior testing?
Mike |
|
![](templates/subSilver/images/spacer.gif) |
annj
Joined: 03 Jun 2013 Posts: 4
|
|
Posted: Wed Jun 12, 2013 2:45 am |
|
|
Hi Mike,
My idea is to lit up each of the LEDs sequentially; similar to pulse oximetry applications.
To answer your questions:
1. I don't really set any minimum PWM frequency but it can be less than 1kHz. But think bout it again; probably not less than 500Hz.
2. Again my point is to light up the LEDs sequentially. If i use frequency less than 1kHz, i have to calculate what my duty cycle will be and "arrange the sequence of the LEDs to be lit up without overlapping". So this point will determine my minimum PWM freq.
3. Yes, same duty cycle for all LEDs.
4. I may not say it clearly in my last reply to Ttelmah; for the D.C =20%, I set it for f=1kHz. So, again, if i use less than 1kHz, i need to make sure that the LEDs lit up sequentially without overlapping.
5. The application is similar to pulse oximetry.
6. What kind of testing do you mean? I try to run the codes in my first post, but the PWMs for both LEDs start simultaneously. I want them to have a delay similar to the point c in my second post.
Hope my answers clear.
annj |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Jun 12, 2013 3:41 am |
|
|
Quote: | Hope my answers clear. | No. Your answers are somewhat vague.
Quote: | 1. I don't really set any minimum PWM frequency but it can be less than 1kHz. But think bout it again; probably not less than 500Hz. | We need to know EXACTLY what's required, so that we can better help.
Quote: | 2. Again my point is to lit up the LEds sequentially. If i use frequency less than 1kHz, i have to calculate what my duty cycle will be and "arrange the sequence of the LEDs to be lit up without overlapping". So this point will determine my minimum PWM freq. | I'm just confused by what you are saying here. As I understand it:-
a) You have 5 phases.
b) One phase for each of four LEDs and a blank.
c) If the duty ratio for all phases is just less than 20% there will be no overlap.
d) The frequency is irrelevant from an overlap point of view.
Quote: | 3. Yes, same duty cycle for all LEDs | Does this also mean you are NOT going to vary the duty ratio, and something just less than 20% will suffice?
Quote: | 5. The application is similar to pulse oximetry | Again, you're not REALLY telling me what you are trying to achieve.
Quote: | 6. What kind of testing do you mean? I try to run the codes in my first post, but the PWMs for both LEDs start simultaneously. I want them to have a delay similar to the point c in my second post. | Have you done any testing with real hardware/pulse-generators etc so that you KNOW the answers to the other questions?
Mike |
|
![](templates/subSilver/images/spacer.gif) |
annj
Joined: 03 Jun 2013 Posts: 4
|
PWM and Delay |
Posted: Wed Jun 12, 2013 7:57 pm |
|
|
Hi Mike,
Alright. I am not good in explaining things. My bad.
Ok, let's simplify the problem. I wish to use f=1kHz. 4 PWMs are required to be fed into the IC chip constant current and each of the PWM will start at different time and I want D.C=20% for all PWMs.
1. What is the suitable PIC chip that I can use?
2. How can I set different start time for PWM in the pic programming? Is this possible to do ? Please advice.
annj |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Jun 13, 2013 6:00 am |
|
|
For an application this complex maybe you should forget about hardware PWM and just drive the LEDs with software. If necessary use one PIC to drive the LEDs and another PIC to do whatever else is needed for the product. Small PICs are really cheap and separate processors can make the software much simpler. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
gaugeguy
Joined: 05 Apr 2011 Posts: 298
|
|
Posted: Thu Jun 13, 2013 6:40 am |
|
|
Set up an interrupt at five times the frequency and use a state machine with five states. |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Jun 13, 2013 10:00 am |
|
|
The M in PWM means modulation.
What you are now asking for is simply a four/five phase generator with a fixed output period on all phases.
i.e. no modulation, so you don't really need the built in PWM module.
Gaugeguy is suggesting:-
1) You create a 4kHz / 5kHz tick to drive an interrupt.
2) In the interrupt you turn off the old phase and turn on a new one.
3) You should be able to do this with almost any PIC.
4) A jitter of no more than 2 machine cycles should achievable.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19383
|
|
Posted: Thu Jun 13, 2013 10:13 am |
|
|
This is down to the 'resolution' question I asked earlier. The hardware PWM, has 1024 steps. 10bit resolution. You only want 5 steps. Under 3bits of resolution.
Mike and Gaugeguy have described what is needed.
Do a search here on 'state machine', and on the web as a whole. It is the key to doing this easily.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|