View previous topic :: View next topic |
Author |
Message |
jhasan
Joined: 03 Jun 2006 Posts: 14
|
PWM Dimming |
Posted: Mon Jul 11, 2011 11:28 pm |
|
|
I am using PIC18F4431 to do PWM dimming for a two string LED. The two PWM signals for dimming the LEDs have to be phase shifted. Also, I am using a voltage on the ADC pin so that I can change the dimming signals so that I can obtain various colors in the LED strings.
I can generate PWM signals using PIC18F4431 and then change the duty cycle using the ADC but I don't have any idea how to make two PWM dimming signals be phase shifted.
I tried using the OVDCOND and OVDCONS registers in the PIC18F4431 but did not get anywhere.
The problem is that my duty cycle can be changed using the ADC. It is not a set duty cycle.
Does anyone have any idea how I can do this?
I want to do this using the PWM module not using I/O pins.Can this be done this way?
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Jul 12, 2011 2:30 am |
|
|
It is not the PWM that you phase shift. It is your _modulation_ of it.
So (crude, and lacks the configuration stuff):
Code: |
#define OFFSET (0x40L) //offset between the modulations
int8 sig1=0,sig2;
int1 up=TRUE;
//running the pwm using 8bit only
do {
if (up) {
if (sig1<255) ++sig1;
else {
up=FALSE;
--sig1;
}
}
else {
if (sig1>0) --sig1;
else {
up=TRUE;
++sig1;
}
}
//Count 0....255....0 etc. Sawtooth output.
sig2=(sig1+OFFSET)&0xFF; //8bit value offset by 'OFFSET'
set_pwm1_duty(sig1);
set_pwm1_duty(sig2);
delay_ms(10); //Will then take about seconds/cycle - reduce for faster
} while (TRUE);
|
On each loop, 'sig1', is fed to PWM1, while 'sig2' is fed to PWM2. Sig1, counts 0...255, then ramps down to 0. Sig2, starts at 'OFFSET', with the waveform 'leading' sig1, by this offset value.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Jul 12, 2011 5:09 am |
|
|
Phase shifting is a delay between signals, in your case the 2 LED strings.
You don't say what the delay is to be between the two outputs.
I don't use the PIC you've chosen so I haven't read the datasheet to see if it allows for a programmable delay between the hardware PWMs. Others who use it (or one with same PWM hardware) may know.
Depending on the required shifting it can be done either in software or additional hardware. |
|
|
jhasan
Joined: 03 Jun 2006 Posts: 14
|
|
Posted: Tue Jul 12, 2011 11:29 am |
|
|
Hello,
I still did not get the PWM signal to be shifted.
This is my code:
Code: |
while(1)
{
set_adc_channel(0);
delay_us(20);
Vsense_adc_read_1 = read_adc();
delay_us(20);
Vsense_dimm_1 = (int32)(Vsense_adc_read_1*iADCgain_n16);
PWM_Duty1 = (signed int16)(Vsense_dimm_1);
PWM_Duty2 = (signed int16)((PWM_Duty1+400));
PDC0L = make8(PWM_Duty1,0);
PDC0H = make8(PWM_Duty1,1);
PDC1L = make8(PWM_Duty2,0);
PDC1H = make8(PWM_Duty2,1);
}
}
|
For PWM_Duty2 I have added a OFFSET but still no luck on shifting the original PWM signal.
I read the value from ADC and then move that value into PWM duty cycle registers.So that I can change the duty cycle using the ADC.
But is there anyway I can phase shift my original signal that PWM_Duty1.
For my case let us say there is delay of 10us between the original and phase shifted signal.
Thanks |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
Dimming PWM |
Posted: Tue Jul 12, 2011 2:03 pm |
|
|
Hi,
I'm not certain what you want.
Draw a diagram of the 'scope traces you expect for your two PWM signals.
Then there is less opportunity for confusion.
Mike Walne |
|
|
jhasan
Joined: 03 Jun 2006 Posts: 14
|
|
Posted: Tue Jul 12, 2011 3:13 pm |
|
|
For some reason I cannot download a image here.
If you could bare with me, and go to google and search for the following paper "LED Driver With Self-Adaptive Drive Voltage". On page 4 of that journal they implement phase shifted PWM dimming.
That is what I am trying to accomplish using PIC18F4431 where the PWM signals are phase shifted.
Thanks |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Jul 13, 2011 10:01 am |
|
|
Since the second output will be the same as the first output but phase shifted, simply have a routine to read the status of the first output pin ( high or low), delay whatever phase shift time you want, then set or reset the second PIC I/O pin to follow the first.
Simple and easy.....
..and it does work. |
|
|
|