|
|
View previous topic :: View next topic |
Author |
Message |
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
shift PWM |
Posted: Wed Oct 02, 2013 3:41 pm |
|
|
Hi I'm still having trouble adjusting this PWM
how can I shift one of the PWM 200us
Code: | #include <18F4431.h>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=20M)
#define POWER_PWM_PERIOD 2000 // 1 KHz pwm freq with 8 MHz osc.
void main()
{
int8 value_adc;
float new_duty_fil;
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(0);
//setup_power_pwm_pins(PWM_ODD_ON, PWM_ODD_ON, PWM_ODD_ON,PWM_ODD_ON);
setup_power_pwm_pins(PWM_COMPLEMENTARY,PWM_COMPLEMENTARY,PWM_OFF,PWM_OFF);
setup_power_pwm(PWM_FREE_RUN | PWM_CLOCK_DIV_4, 1, 1, POWER_PWM_PERIOD-1, 0, 1, 0);
while(1)
{
value_adc = read_adc(); //gives us an 8 bit value
new_duty_fil = value_adc * 0.005;
set_power_pwm0_duty((int16)(POWER_PWM_PERIOD * new_duty_fil));
set_power_pwm2_duty((int16)(POWER_PWM_PERIOD * new_duty_fil));
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Oct 03, 2013 12:30 am |
|
|
You can't.....
Different chip needed.
You could possibly get close, using the complementary outputs from one generator, and then adjusting the dead-time to move the second pulse where you want it to be, but all the PWM's, on this generator, run from a a single timer, so for the separate channels, the rising edges align (or the centres if you switch to up/down counting).
You are using slightly the wrong tool for the job...
Any standard chip with two PWM's could generate what you want. Just program them to use the different timers (90+% of chips allow two timers to be used), Start one timer, wait 200uSec, and start the other. Then adjust the pulse widths as required.
You have got the two PWM's to do this, using the CCP1/CCP2 pins, but this chip doesn't allow a second timer to be used.
To do it with dead-band, you'd need to set the set the dead-band on the first channel to zero, then set this channel's pulse width, and set the dead-band to 2000-this width, then set the second channel's width.
As a comment, slow down the update rate a little, and see if you can avoid fp maths. You also potentially overflow the pulse width (adc = 0 to 255)/200 *2000.... Remember integer multiplications/divisions by multiples of two are quick, so 'scale'. Your current code gives 0 to 2550, for much less work, just use adc*8, in int16, which will take perhaps 1/10th the code space, and only overflow right at the top of the adc...
Best Wishes |
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Thu Oct 03, 2013 9:49 am |
|
|
Quote: | but all the PWM's, on this generator, run from a a single timer |
Thank you for the information, but how can I use the other timers according to the datasheet on page 13
http://ww1.microchip.com/downloads/en/DeviceDoc/39616d.pdf
This chip it has 4 timers, would be possible to start the timers at different time?
or what they mean by 4 timers?
I just need the PWM to be offset and when I adjust them not to overlap each other, but not to be inverted either, I hope it make sense. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Thu Oct 03, 2013 10:49 am |
|
|
It may have 4 timers, but the PWM module only works with timer 2. Look at the PWM section of the datasheet you linked. |
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Thu Oct 03, 2013 11:06 am |
|
|
jeremiah wrote: | It may have 4 timers, but the PWM module only works with timer 2. Look at the PWM section of the datasheet you linked. |
ok so is not 1 timer, any idea how can I run both timer separate from each other? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9227 Location: Greensville,Ontario
|
|
Posted: Thu Oct 03, 2013 11:20 am |
|
|
One possible solution is to just have a second I/O pin follow the logic level of the PWM output pin, but delayed by 200us.
Easily done in software, since as previously noted the PIC you're using doesn't have the hardware you need for a 'hardware' solution.
Depending on what else the PIC has to do, this can work.
hth
jay |
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Thu Oct 03, 2013 11:29 am |
|
|
temtronic wrote: | as previously noted the PIC you're using doesn't have the hardware you need for a 'hardware' solution. |
sorry I still don't know what you mean by that, as jeremiah mention it has 2 timers, wouldn't that mean they can run individual from each other? |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu Oct 03, 2013 11:32 am |
|
|
Not 2 timers, but Timer2.
Regards |
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Fri Oct 04, 2013 8:43 am |
|
|
so is there code to talk to a pic hardware to accomplish what I'm trying to do? is there any pic that actually has 2 timers? according to the datasheet I though I would be able to talk to each timer individually, but your guys are saying this chip even if it says it has 4 timers is only 1 timer? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Fri Oct 04, 2013 2:09 pm |
|
|
It is not a question of 'having two timers'. Most PIC's have at least four. It is a question of having two selectable by the PWM.
You happen to have chosen a chip with the power control PWM, which is specifically designed to give multiple high resolution PWM channels. Generally the actual timing of PWM pulses does not matter at all for most applications, it is the timing of the modulation that matters, hence the PWM's are synchronised to reduce the number of actual timers needed.
However a very large number of PIC's support using two different timers for the PWM. Usually chips with several ECCP modules, and more timers, allow the ECCP modules to select different timers Examples are the PIC45K22 family, that allow Timers 2, 4, and 6 to be selected for the ECCP channels. The key is to read the PWM section of the data sheet and look for a table listing what timers can be used. Of the first half dozen data sheets I looked at, more than half support more than one timer for the PWM.
Best Wishes |
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Mon Oct 07, 2013 9:00 am |
|
|
thank you, so i'm back where i started
how can i control each timer separate so they would start at different time? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9227 Location: Greensville,Ontario
|
|
Posted: Mon Oct 07, 2013 9:21 am |
|
|
simple answer...you can't with that PIC.
from the datasheet, timers, diagrams
It appears all timers are 'synced' with FOsc/4, or an external clock
The PWM module has it's own 'clock', so ALL 8 PWMs are synced to it..
As I've said before, one easy solution, is to have one 'master' PWM that does what you want, then have code to read that pin,delay 200us, then set a second pin to whatever the first one is.Depending on what else is going on a simple inline delay_us(200) could work or get fancy an do it inside an ISR.
hth
jay |
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Mon Oct 07, 2013 9:42 am |
|
|
OK Jay, so all 4 timer on datasheet is just for advertising, when in reality is only 1 timer?
what would be an example code with CCS with a pic with 2 timers and which pic microcontroller would be? |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Mon Oct 07, 2013 11:23 am |
|
|
No, there are 4 timers, but only 1 can be mapped to the PWM circuitry. Look at a Motor control chip, some of them have independant timers for the PWM's or a PHASE register which will do exactly what you want. I use dsPIC's for that, but I am sure you will get an PIC18 or PIC24 as well.
Regards |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9227 Location: Greensville,Ontario
|
|
Posted: Mon Oct 07, 2013 1:11 pm |
|
|
You should be able to use a PIC with ECCP modules using PWM(Enhanced mode) like the PIC18F46K22. Download the datasheet and read section 14.4( page 190+).Using half-bridge mode and pins PxA,PxB and setting the 'delay' correctly may get you what you want.
The PIC18F46K22 is my 'Swiss Army Knife' goto PIC. Lots on memory and internals.Though I haven't used the enhanced mode it seems simple enough to setup.
Just start off with the '1Hz LED ' test program to confirm the PIC's Ok, then configure the ECCP as required.A 2 channel scope on the output pins will confirm operation.
good luck
Jay |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|