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

software PWM,

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







software PWM,
PostPosted: Mon May 22, 2006 1:44 pm     Reply with quote

HI

I'm pretty new to PIC programming and I'm trying to program software pwm (see code)


Code:


//-----
//interrupt routines
//----

unsigned int16 pwm_duty_cycle;
unsigned int1 bit_state=0;

#int_timer3 //
void timer3_isr() {
   switch(bit_state) {
   case 1:
      OUTPUT_FLOAT(PIN_BIRD1B);
      bit_state=0;
      set_timer3(0xFFFF-pwm_duty_cycle);   // pwm_duty_cycle
      break;
   case 0:
      OUTPUT_LOW(PIN_BIRD1B);
      bit_state=1;
      set_timer3(0xFFFF-(50-pwm_duty_cycle));   // period-pwm_duty_cycle
      break;
   }

}

//---------------------------------------------------------------------------------------------------------------
// Initialization
//---------------------------------------------------------------------------------------------------------------

void init()
{

   // Setup oscillator mode @ 32MHz
//   setup_oscillator(OSC_8MHZ|OSC_NORMAL|OSC_31250|OSC_PLL_ON);
   #asm
   MOVLW  0b01110000   // setup internal oscillator
   MOVWF  0xFD3
   MOVLW  0b11000000 // enable PLL
   MOVWF  0xF9B
   #endasm

   // Setup peripherals
   setup_adc_ports(AN0_TO_AN4);
   setup_adc(ADC_CLOCK_DIV_32);         // ADC clock must be set such to ensure TAD=1.6us minimum (TOSC = 0.05us @ 20MHz)


   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);

   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32); //
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);   /
   setup_timer_2(T2_DIV_BY_16,255,1);   
   setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);            
   setup_ccp1(CCP_PWM);                     
   setup_ccp2(CCP_PWM);            

   //setup_comparator(NC_NC_NC_NC);
   //setup_vref(FALSE);
   setup_low_volt_detect(FALSE);

   // Setup PIC I/O ports; 1 indicates the pin is input and 0 output.
   set_tris_a(0b00011110);   
  set_tris_b(0b11110011);
   set_tris_c(0b10000000);   
   set_tris_d(0b11111111);
   set_tris_e(0b100);

   delay_ms(100);



   // Setup interrupts.
   enable_interrupts(INT_TIMER1);   // Anemometer monitoring
   enable_interrupts(INT_TIMER3);   // BIRDS PWM
   enable_interrupts(global);

}

//---------------------------------------------------------------------------------------------------------------
// MAIN routine
//---------------------------------------------------------------------------------------------------------------


void main() {
   init();
   //while (true) {
led_on_led0();

   pwm_duty_cycle=20; // trying to set the duty cycle to control actuators
 
led_off_led0();   
   
}



It is a PIC18F4620 @32MHz, TImer3 is 16bit
so after my calculation
it overflows every 65.5ms
(4/32E6 * 65535 *8=65.5ms)

and if I want a frequency of 20kHz (50us period) of my PWM
.....(4/32E6 * x *8=50us) => x=50 counts in timer3

my problem is that I can not change the fraction of high to low states of the pwm...
Are my calculations correct? Is it very unelegant to do PWM this way/are there mistakes?
Did I enable the Interrupts correctly...?

I'm stuck on this topic for quite a while now...
Thank you very much!
(Youre saving my life)

best

mirko

Rolling Eyes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 22, 2006 1:57 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=20050
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