CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Operating CCP1 and CCP2 of PIC 16F877A using C programming
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
safwat



Joined: 08 Feb 2015
Posts: 20

View user's profile Send private message

PostPosted: Sun Feb 22, 2015 11:06 am     Reply with quote

i thought you said i have to use a power pwm control module to produce shifting signal in pic18f4431. thats why i am trying 1st to find out how the power control pwm works and how to generate them. then when i generate them i will try to shift. i am just trying to go step by step.
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Sun Feb 22, 2015 11:34 am     Reply with quote

Visualise:

With one channel of the power PWM, you can produce two signals that are the complement of one another.
You can invert the second signal, so you then get two signals that are the same from one PWM.
You can separately program the 'dead time', giving you the ability to delay the rising edge of the second signal from the first, and the falling edge from the first.
This then potentially gives from one power PWM channel, some range of shifted signals.

You are ignoring Temtronic's request to actually tell us what the relationship of the signals wants to be?.

Without some details, we are guessing. If you want to learn of the capabilities of the power PWM, as already said, look at the data sheet, example etc., and see what happens if you select to invert the second waveform, and change the deadtime settings.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 22, 2015 11:59 am     Reply with quote

Quote:
i want to generate a power PWM signal in pic18f4431.

Power PWM examples for the 18F4431:

Example with two Complementary signals:
http://www.ccsinfo.com/forum/viewtopic.php?t=48851&start=1

Example with Center-aligned pwm
http://www.ccsinfo.com/forum/viewtopic.php?t=50501&start=3

Example of ordinary pwm (using power pwm module):
http://www.ccsinfo.com/forum/viewtopic.php?t=37206&start=2

Also, see the CCS example file, ex_power_pwm.c, for two
complementary pwm signals, in this directory on your PC:
Quote:
c:\program files\picc\examples\ex_power_pwm.c
safwat



Joined: 08 Feb 2015
Posts: 20

View user's profile Send private message

PostPosted: Mon Feb 23, 2015 9:28 am     Reply with quote

here is the answer to temtronics question (as much as i know until now)

1)as you know i want to generate two PWM signals with phase shift among them.
2) i want to use a 20 MHz crystal generating 20 kHz PWM, duty can be kept constant.
3)the phase shift will vary. i will vary the phase shift with adc. for example if i want a 25us phase shift then from outside by the use of a button i am gonna change the shift to 25us.

As the PIC has other things to do such as adc a normal delay/counter tactics wont work. So i need the PWM modules, and as Ttelmah said phase shift is impossible using PWM modules of pic 16f877a because both use same timer, i changed to PIC 18f4431.

this signals will later be used in the switch for a dc to dc converter.

i hope this gives you some idea, if not please ask specific questions.
thank you
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Mon Feb 23, 2015 10:23 am     Reply with quote

No guarantees on this, but you need something like:
Code:

#include <18F2431.h>
#device ADC=10
#use delay(crystal=20000000)

#WORD PTMR=getenv("SFR:PTMRL")

void set_time_between(int8 delay)
{
   //routine to set power_pwm to generate it's waveform 'delay' uSec after
   //the CCP
   //First check if the value is possible
   if (delay>50)
      return;
   clear_interrupt(INT_TIMER2);
   while (!interrupt_active(INT_TIMER2)) ; //wait for CCP to update
   if (delay>0)
      delay_us(delay);
   PTMR=0; //reset power PWM
}

void main()
{
   int8 ctr;
   setup_timer_2(T2_DIV_BY_1,249,1);      //50.0 us overflow, 50.0 us interrupt
   setup_power_pwm(PWM_CLOCK_DIV_4 | PWM_FREE_RUN | PWM_UPDATE_ENABLE, 1, 0, 249, 0, 1, 0);
   //same frequency for power PWM - interrupt at 0 count
   setup_power_pwm_pins(PWM_ODD_ON,PWM_PINS_DISABLED,PWM_PINS_DISABLED,PWM_PINS_DISABLED);
   //just enable one channel
   set_power_pwm0_duty((int16)498);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty((int16)498);

   while(TRUE)
   {
      //Now time shift is done by offsetting the timer2 counter from the counter
      //in the power_pwm.
      for (ctr=0;ctr<50;ctr++)
      {
         set_time_between(ctr);
         //program power_pwm to be ctr uSec after the CCP
         delay_ms(1000);
         //one second at this phase
      } //repeat for all the values
   }
}

This uses CCP1, and the first output on the power PWM. Giving 50 delay values from 0 to 50uSec between the two waveforms. As a demo, it is designed to step through the possible delay values between the waveforms.

Have not checked it at all..... Smile
safwat



Joined: 08 Feb 2015
Posts: 20

View user's profile Send private message

PostPosted: Sun Mar 01, 2015 11:58 am     Reply with quote

Hey Ttelmah,
Thanks for your code. I am currently trying to decipher your code. Some of the things are going way over my head for a second year student like me. So i am trying understand the datasheet.

Anyways, I want to buy some new controllers. Can you suggest me some micro controller which does this phase shifting quite easily (I mean easier than pic18f4431).
Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Sun Mar 01, 2015 1:30 pm     Reply with quote

The basis of what I posted is pretty simple. The two PWM's run off two oscillators. To update the shift, I wait for one oscillator to reset to zero, wait for the required delay, and then reset the other oscillator. They then keep running with the required relationship.

The first peripheral that does potentially allow this is the COG. This is designed to produce two complementary signals, from one signal. This has programmable rising edge delays, and falling edge delays, from the source signal. However the second one that is really designed to do this is the PSMC module. This allows a single PWM, to have multiple outputs, with separately programmed start and end points. Change these, and you shift the output waveforms relative to one another.

Look for a chip with this The PIC16LF1784 for example. You change the 16bit phase register, to shift the position of the start event.

I have to ask what you actually want to 'do' with these phase shifted signals?. It is such an unusual 'need', that I wonder. Most people in the past who start wanting phase shifted signals really don't need them.
It's worth realising that normally phase shift implies working with something like analog or simulated analog signals. For example BLDC control that requires 3*60 degree phase shifted signals, is normally done by using three standard 'in phase' PWM signals at a higher frequency, and using these to synthesise the required output waveforms by just modulating the mark/space ratio of the three generators.
I must admit that if I wanted signals with a programmable relationship between them, I'd probably just use something like the AD9501 to delay the second signal.
safwat



Joined: 08 Feb 2015
Posts: 20

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 11:22 am     Reply with quote

this is the formula to calculate power PWM period in Datasheet for 4431

TPWM = [(PTPER - 1) * PTMRPS] / (Fosc / 4)

i suppose PTPER is 249 in your code?

and i searched through data sheet for PTMRPS but the only place i found is n the formula.

what is PTMRPS? what is its value in your code?

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 11:40 am     Reply with quote

How about Google ? Google it:
Quote:
what is PTMRPS ?
safwat



Joined: 08 Feb 2015
Posts: 20

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 11:43 am     Reply with quote

answer to Ttelmah
i need to pass this two signals into the gates of a full bridge DC to DC converter. i have read a paper about this which says phase shifted signal into the gates of a DC to DC converter is much more efficient. thats why i want to check it out


Last edited by safwat on Mon Mar 02, 2015 11:56 am; edited 1 time in total
safwat



Joined: 08 Feb 2015
Posts: 20

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 11:55 am     Reply with quote

okay i think i found it, PTMRPS is the post scale value
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 12:09 pm     Reply with quote

OK. PSFB.
Very different from what you have perhaps been talking about. You were saying 'duty could be constant', but for this duty _must_ be constant.
You actually need two pairs of 50% duty 180 degree signals, and then to adjust the overlap of these pairs. The overlap determines the energy transferred. This is what the CWG/COG peripheral is designed to generate. Look for a PIC with this. If it has an ECCP, then program this to generate one pair at 50% duty, and then program the COG to adjust the rising edge and falling edge delays together to provide the overlap adjustment.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 02, 2015 2:38 pm     Reply with quote

Quote:
okay i think i found it, PTMRPS is the post scale value

Wrong, unfortunately. It's the Pre-scaler.

The Pre-scaler is used in the formula to calculate the PWM period.
It optionally increases the period.

The Post-scaler is used to optionally reduce the #INT_PWMTB interrupt rate.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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