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

PWM1 and Timer1 problems

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



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PWM1 and Timer1 problems
PostPosted: Thu Aug 06, 2009 1:19 pm     Reply with quote

All,

I was wondering if I could get some input from the group. The following is a small part of some PWM code. The PWM users PWM1 and Timer 2.
Code:

// Set up PWM
set_pwm1_duty(0);              // This sets the on time
setup_ccp1(CCP_PWM);           // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1, 127, 1);    // Set Parms

I am also using Timer 1 to determine the time a switch is pressed.

Problem is; when I enable the interrupts (enable_interrupts(GLOBAL);) the PWM for loop runs the first time and then stops. Is it valid to use the PWM1 and Timer1 for two different functions?

I am using a 18f252 and compiler 4.073.

Thanks, Ray

The following is some of the code I have written.
Code:

void main (void)
{
// Set up int for t1 for switch timer
// T1 timer used for key switch management
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);  // T1 clk = 20 MHz
set_timer1(TIMER1_PRESET);
   

//
// Main system loop
//

while(1)
   {
   if(!input(sw_left))                 // look for Left Switch low
   {
   Miliseconds = 0;
   clear_interrupt(INT_TIMER1);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);

      delay_ms(10);          // debounce
      if(!input(sw_left))    // look for Left Switch low
      {
      while(1)
      {
      if(input(sw_left))     // look for Left Switch high
      {
      move_back();          // move carirer to home
      break;
      }
      if(Miliseconds > 500)
      {
      move_back();          // move carirer to home
      while(!input(sw_left)){}
      hard_stop();           // Stop now
      break;
      }
      }
      }
     }
  }
}

void move_back(void)
   {
   carrier_stat=1;            // 0 = stop, 1 = moving home, 2 maving DR
   // Set direction = back to home
   output_bit(direction, back);     // Set Direction = Back)
   ramp_up();
   }
   
void move_forward(void)
   {
   carrier_stat=1;                             //
   // Set direction = back to home 
   output_bit(direction, forward);     // Set Direction = Back)
   ramp_up();
   }


 void ramp_up()
{
// Set up PWM
set_pwm1_duty(0);            // This sets the on time
setup_ccp1(CCP_PWM);              // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1, 127, 1);    // Set Parms
     
ml=0;                              // reset ml counter
     
motor_speed=50;                   // motor speed start
     
for (ml=51;ml<=254;++ml)          // loop for ramp up
  {
    motor_speed=((motor_speed*sp_factor));   // Set motor to x%
    set_pwm1_duty(motor_speed);              // This sets the on time
    delay_ms(5);                             // loop timer 5 ms
    motor_speed=motor_speed+1;               // inc the loop 1
  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 06, 2009 2:57 pm     Reply with quote

Quote:
clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

You don't have (or didn't post) a #int_timer1 interrupt routine.
You need one.
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Thu Aug 06, 2009 3:14 pm     Reply with quote

PCM Programmer;

I forgot to include it with the post. Here it is:

#define TIMER1_PRESET (65536 - 5000) // Interrupt every 5000 counts

// t1 timer used for key switch management
#int_timer1
void timer1_isr(void)
{
set_timer1(TIMER1_PRESET);

{
miliseconds++;
}
}

I am using it to count milliseconds. In my code I need to know if a button is pressed and released or held down. If the count is > 500, it is assumed the button is held. There is a different action taken if it is pressed and released or held.

The PWM is controlled by a for loop. (If there is a better way to do this, please let me know) However, the for loop works great until I turn on the enable_interrupts(GLOBAL); After that the for loop only starts one pass and then stops. Control returns to my button code.

Any thoughts you have would be a big help.

Ray


void ramp_up()
{
// Set up PWM
set_pwm1_duty(0); // This sets the on time
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1, 127, 1); // Set Parms

ml=0; // reset ml counter

motor_speed=50; // motor speed start

for (ml=51;ml<=254;++ml) // loop for ramp up
{
motor_speed=((motor_speed*sp_factor)); // Set motor to x%
set_pwm1_duty(motor_speed); // This sets the on time
delay_ms(5); // loop timer 5 ms
motor_speed=motor_speed+1; // inc the loop 1
}
}
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