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

Need a suggestion on PWM application

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



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

Need a suggestion on PWM application
PostPosted: Sat Jul 10, 2004 8:30 am     Reply with quote

Hi,

I need a suggestions and/or comments on my PWM application.

I have setup a PWM mode on my PIC18F452 and the code is listed below:

Code:

#include <18F452.H>
#device  *=16

#fuses HS,NOPROTECT,NOWDT,NOLVP,PUT
#use delay(clock=20000000)


void init_mcu()
{
   delay_ms(3000);

   set_tris_c(0b11111011);             // set PIN_C2 as output
   setup_adc_ports(NO_ANALOGS);        // set PortA to digital I/O
   setup_ccp1(CCP_PWM);                // set CCP1 to PWM mode
   
   setup_timer_2(T2_DIV_BY_1,39,1);    // (1/20000000)*4*1*40
                                       // t2=8uSec. or f2=125KHz
   set_pwm1_duty(20);                  // 50% duty cycle at 125KHz
}


void main()
{
   init_mcu();                         // initialize MCU
   while (TRUE);
}



the code above will provide a 125KHz square wave at 50% duty cycle on PIN_C2.

The catch here I want to turn-on my PWM for 100ms then turn-off for 100ms... this cycle will repaet periodically...

what would be a good approach in doing this? Is it good practice to use setup_ccp1(CCP_OFF) such that my PWM is turn-off for 100ms then turn-on again.

Do I need to use two(2) timer interrupt to handle this turn-on or turn-off thing. I prefer to use interrupt on this....

I need your comments and suggestions... a simple code snippet will provide me help.

thank u.
Ttelmah
Guest







Re: Need a suggestion on PWM application
PostPosted: Sat Jul 10, 2004 11:47 am     Reply with quote

ritchie wrote:
Hi,

I need a suggestions and/or comments on my PWM application.

I have setup a PWM mode on my PIC18F452 and the code is listed below:

Code:

#include <18F452.H>
#device  *=16

#fuses HS,NOPROTECT,NOWDT,NOLVP,PUT
#use delay(clock=20000000)


void init_mcu()
{
   delay_ms(3000);

   set_tris_c(0b11111011);             // set PIN_C2 as output
   setup_adc_ports(NO_ANALOGS);        // set PortA to digital I/O
   setup_ccp1(CCP_PWM);                // set CCP1 to PWM mode
   
   setup_timer_2(T2_DIV_BY_1,39,1);    // (1/20000000)*4*1*40
                                       // t2=8uSec. or f2=125KHz
   set_pwm1_duty(20);                  // 50% duty cycle at 125KHz
}


void main()
{
   init_mcu();                         // initialize MCU
   while (TRUE);
}



the code above will provide a 125KHz square wave at 50% duty cycle on PIN_C2.

The catch here I want to turn-on my PWM for 100ms then turn-off for 100ms... this cycle will repaet periodically...

what would be a good approach in doing this? Is it good practice to use setup_ccp1(CCP_OFF) such that my PWM is turn-off for 100ms then turn-on again.

Do I need to use two(2) timer interrupt to handle this turn-on or turn-off thing. I prefer to use interrupt on this....

I need your comments and suggestions... a simple code snippet will provide me help.

thank u.

The last number in the Timer2 setup, is the number of counts between it's interrupt. If (for instance), you set this to '10', then you will get an interrupt every 1/12,500th second. In this, implement a counter, and when it gets to zero, set the duty cycle to '0'. You can turn the PWM off, but assuming that for your 'off', just wants a logic '0', setting the duty cycle does a similar thing, with less operations involved.
The cycle will update on the next PWM cycle.
The same will be true if you set the pwm cycle back to 20, after the same time interval.
So you can use the same timer for both jobs, with just an extra counter, and a 'flag', to say which way to change the counter.

Best Wishes
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Sat Jul 10, 2004 7:44 pm     Reply with quote

I don't think you can service an interupt every 40 instruction cycles. I would sugest using timer4 to time the on and off intervals.

Code:
#int_TIMER4                                                 
void TIMER4_isr()
{  Miliseconds++;
   if(Miliseconds>99)
   {  Miliseconds=0;
      if(On_Cycle)
      {  set_pwm1_duty(20);
         On_Cycle=0;
      }
      else
      {  set_pwm1_duty(0);
         On_Cycle=1;
      }
   }
}

Code:
   setup_timer_4(T4_DIV_BY_4,249,5);                     // Set 1mS period on a 20Mhz xtl
   enable_interrupts(INT_TIMER4);
ritchie



Joined: 13 Sep 2003
Posts: 87

View user's profile Send private message

PostPosted: Sat Jul 10, 2004 8:14 pm     Reply with quote

Neutone wrote:
I don't think you can service an interupt every 40 instruction cycles. I would sugest using timer4 to time the on and off intervals.

Code:
#int_TIMER4                                                 
void TIMER4_isr()
{  Miliseconds++;
   if(Miliseconds>99)
   {  Miliseconds=0;
      if(On_Cycle)
      {  set_pwm1_duty(20);
         On_Cycle=0;
      }
      else
      {  set_pwm1_duty(0);
         On_Cycle=1;
      }
   }
}

Code:
   setup_timer_4(T4_DIV_BY_4,249,5);                     // Set 1mS period on a 20Mhz xtl
   enable_interrupts(INT_TIMER4);


Thank you Ttelmah and Neutone for such a great idea.... btw, I don't think a PIC18F452 has a timer4? Anyway, I will use timer3 for this ON-OFF thing..

once again... tengkyu
Ttelmah
Guest







PostPosted: Mon Jul 12, 2004 3:24 am     Reply with quote

Neutone wrote:
I don't think you can service an interupt every 40 instruction cycles. I would sugest using timer4 to time the on and off intervals.

Code:
#int_TIMER4                                                 
void TIMER4_isr()
{  Miliseconds++;
   if(Miliseconds>99)
   {  Miliseconds=0;
      if(On_Cycle)
      {  set_pwm1_duty(20);
         On_Cycle=0;
      }
      else
      {  set_pwm1_duty(0);
         On_Cycle=1;
      }
   }
}

Code:
   setup_timer_4(T4_DIV_BY_4,249,5);                     // Set 1mS period on a 20Mhz xtl
   enable_interrupts(INT_TIMER4);

If you use the 'counter' for the interrupts on Timer2, then set to 10, the interrupt will be every 400 instructions, which is achievable. The maximum allowed is 16, which gives one interrupt every 640 instructions, which is fine. It does save a timer, if this is important.

Best Wishes
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Mon Jul 12, 2004 7:47 am     Reply with quote

ritchie posted a PWM period of 40 instruction cycles. It's my understanding that the interupt period of the timer and the PWM period are the same. Does the overflow counter not figure into the PWM period?
Ttelmah
Guest







PostPosted: Mon Jul 12, 2004 2:19 pm     Reply with quote

Neutone wrote:
ritchie posted a PWM period of 40 instruction cycles. It's my understanding that the interupt period of the timer and the PWM period are the same. Does the overflow counter not figure into the PWM period?

Timer2, has a post divider, between the timer, and the interrupt. This is the last number in the timer definition, and can be 1 to 16. Hence with a 40 instruction pwm, you can have interrupts at 40 instructions (with it set to 1), 80 instructions (set to 2), up to 640 instructions (with it at 16). It is a very useful feature, when you want to do something 'synchronous' to a fast PWM waveform, but need a longer interval.
I use this on a PWM system at 80KHz (with a 40MHz processor clock), to synchronise the servo update loop driving the PWM, to 8 cycles of the PWM.
Using this, avoids the need for another timer, and would work at a 'reasonable' frequency.

Best Wishes
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