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 support@ccsinfo.com

pwm phase shift

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ledman1



Joined: 25 Apr 2017
Posts: 6

View user's profile Send private message

pwm phase shift
PostPosted: Tue Apr 25, 2017 2:21 am     Reply with quote

Hi..I'm a begginer in c and ccs. How to phase shift pwm wave ? I'm using pic16f877a.
Thank you
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 4:38 am     Reply with quote

your question is too vague.

PWM is easy.....

'phase shift'... of you mean the output (PWM signal) is to be some time from the original start signal, simply
...
'look for ' start signl'
delay 'sometime'
send PWM to I/O pin
...

BTW the 877 is almost obsolete, so you should choose a more modern PIC. You'll find lots of them cheaper with MORE features !

Jay
ledman1



Joined: 25 Apr 2017
Posts: 6

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 5:31 am     Reply with quote

temtronic wrote:
your question is too vague.

PWM is easy.....

'phase shift'... of you mean the output (PWM signal) is to be some time from the original start signal, simply
...
'look for ' start signl'
delay 'sometime'
send PWM to I/O pin
...

BTW the 877 is almost obsolete, so you should choose a more modern PIC. You'll find lots of them cheaper with MORE features !

Jay


i have 20 khz pwm signal and i want use this signal on other pin with 120 degree phase shift
ledman1



Joined: 25 Apr 2017
Posts: 6

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 6:26 am     Reply with quote

temtronic wrote:
your question is too vague.

BTW the 877 is almost obsolete, so you should choose a more modern PIC. You'll find lots of them cheaper with MORE features !

Jay


whats your offer?
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 7:37 am     Reply with quote

rough math...
20KHz = 50us
1/3=16.6us
... idea.
read input pin level ( 20KHz signal)
delay 16us
write 1 to output pin
...
the idea is the outpit pin 'follows' the input pin ,though delayed.

with some real code you'll have to account for actual code times(depends on PIC and speed..)


there is another hardware trick (really old school) where you start with 6x the clock signal, run into counter and get 3 phases exactly 120* apart.


as more which PIC.. the 16F887 I believe is an upgrade, currently I use the 18F46K22 as my 'goto' PIC. 40 pins, 2 HW UARTS, lots of mem,etc.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 11:43 am     Reply with quote

You would normally never phase shift the PWM. What you would phase shift is the waveform being synthesised _by_ the PWM.

So you have perhaps an xxHz, sinusoid (or another waveform, but a sinusoid is the commonest), synthesised using modulated PWM, and you then generate another waveform at a phase shift from this.

Look on the Microchip site. They give a lot of examples of this. Most are fairly easy to port.

You just use a lookup table for the values to feed to the PWM controller, with values for a cycle, and output the value at a pointer to PWM#1, and at 1/3rd the way further through the table for PWM#2.
ledman1



Joined: 25 Apr 2017
Posts: 6

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 1:45 pm     Reply with quote

temtronic wrote:
rough math...

the idea is the outpit pin 'follows' the input pin ,though delayed.


Jay


Is there any way except capture pwm pin there?
ledman1



Joined: 25 Apr 2017
Posts: 6

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 1:52 pm     Reply with quote

Ttelmah wrote:


So you have perhaps an xxHz, sinusoid (or another waveform, but a sinusoid is the commonest), synthesised using modulated PWM, and you then generate another waveform at a phase shift from this.


You just use a lookup table for the values to feed to the PWM controller, with values for a cycle, and output the value at a pointer to PWM#1, and at 1/3rd the way further through the table for PWM#2.


I create this table:
Code:


#define Step_delay 1000 //micro second

const int pwmSin[] = {127, 138, 149, 160, 170, 181, 191, 200, 209, 217, 224, 231, 237, 242, 246, 250, 252, 254, 254, 254, 252, 250, 246, 242, 237, 231, 224, 217, 209, 200, 191, 181, 170, 160, 149, 138, 127, 116, 105, 94, 84, 73, 64, 54, 45, 37, 30, 23, 17, 12, 8, 4, 2, 0, 0, 0, 2, 4, 8, 12, 17, 23, 30, 37, 45, 54, 64, 73, 84, 94, 105, 116 };


and create sine wave with this method:

Code:

       set_pwm_duty(1,pwmsin[PWM]);
       
       PWM++;
       delay_us(Step_delay);
       if (pwm>71){pwm=0;}
       


and its work very well...now my focus is on phase shift method.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Apr 26, 2017 12:29 am     Reply with quote

OK.
Beware of your 'mixed case'. If PWM is upper case, it should always be upper case. This will catch you later if you are not careful....
There is also a 'standard' in C to reserve declarations in all upper case, for macros etc.. It makes debugging easier.

Code:

#define STEP_DELAY 1000 //micro second

        //obviously the second PWM will need setting up somewhere.

       int8 PWMvalue2; //in your declarations
       int8 PWMvalue1; //using this instead of just PWM
 
       //then for the loop:
       PWMvalue2=PWMvalue1+24;
       if (PWMvalue2>71)
           PWMvalue2-=72;
       
       set_pwm_duty(1,pwmsin[PWMvalue1]);
       set_pwm_duty(2,pwmsin[(PWMvalue2]);
 
       PWMvalue1++;
       if (PWMvalue1>71)
           PWMvalue1=0;

       delay_us(STEP_DELAY);


To give a really accurate time, you might want to consider using a timer, instead of just the delay. This then means the timings won't be upset by time used for the maths and array lookup. Depends how accurate your frequency needs to be, and what else the code must do.
ledman1



Joined: 25 Apr 2017
Posts: 6

View user's profile Send private message

PostPosted: Wed Apr 26, 2017 6:21 am     Reply with quote

Ttelmah wrote:
OK.
Beware of your 'mixed case'. If PWM is upper case, it should always be upper case. This will catch you later if you are not careful....
There is also a 'standard' in C to reserve declarations in all upper case, for macros etc.. It makes debugging easier.

Code:

#define STEP_DELAY 1000 //micro second

        //obviously the second PWM will need setting up somewhere.

       int8 PWMvalue2; //in your declarations
       int8 PWMvalue1; //using this instead of just PWM
 
       //then for the loop:
       PWMvalue2=PWMvalue1+24;
       if (PWMvalue2>71)
           PWMvalue2-=72;
       
       set_pwm_duty(1,pwmsin[PWMvalue1]);
       set_pwm_duty(2,pwmsin[(PWMvalue2]);
 
       PWMvalue1++;
       if (PWMvalue1>71)
           PWMvalue1=0;

       delay_us(STEP_DELAY);


To give a really accurate time, you might want to consider using a timer, instead of just the delay. This then means the timings won't be upset by time used for the maths and array lookup. Depends how accurate your frequency needs to be, and what else the code must do.


I did not understand, but I try to get noticed...thank you
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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