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

Problem with interrupt

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



Joined: 12 Jul 2007
Posts: 32

View user's profile Send private message

Problem with interrupt
PostPosted: Wed Dec 12, 2007 5:05 am     Reply with quote

Hi,
I have a little problem with interrupts.
I use a stepping motor which should rotate right and after 168 steps rotate left. This works fine.
But now the motor should pause for 7 minutes and then starts again.
My Problem is, that the timer starts counting after the motor's rotation which takes approximately 15 seconds.
How can I handl this?

Thanks!
My code below:
Code:

#include <16f818.h>
#use delay(clock=8000000)
#fuses NOWDT,HS,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP

int8 switchstate;
int16 seconds;     
int8 int_count;   

#define INTS_PER_SECOND 61                   
#int_rtcc                                   
void clock_isr() {                                                                 
    if(--int_count==0) {                   
      ++seconds;
      int_count=INTS_PER_SECOND;
    }
}

#BYTE port_b = 6
#define FOUR_PHASE TRUE
#ifdef FOUR_PHASE

BYTE const POSITIONS[4] = {0b0101,
                           0b1001,
                           0b1010,
                           0b0110};
#else
BYTE const POSITIONS[8] = {0b0101,
                           0b0001,
                           0b1001,
                           0b1000,
                           0b1010,
                           0b0010,
                           0b0110,
                           0b0100};
#endif

drive_stepper(BYTE speed, char dir, BYTE steps)
{
   static BYTE stepper_state = 0;
   BYTE i;

   for(i=0; i<steps; ++i) {
     delay_ms(speed);
     set_tris_b(0xf0);
     port_b = POSITIONS[ stepper_state ];
     if(dir!='R')
       stepper_state=(stepper_state+1)&(sizeof(POSITIONS)-1);
     else
       stepper_state=(stepper_state-1)&(sizeof(POSITIONS)-1);
   }
}

main()
{
   BYTE speed, steps;
   char dir;

   int_count=INTS_PER_SECOND;
   set_timer0(0);
   setup_counters( RTCC_INTERNAL, RTCC_DIV_128 | RTCC_8_BIT);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);

   while(TRUE)
      {

        if(seconds == 420)
         {
          if(switchstate==0)             
            {                             
               switchstate=1;
                 
               speed = 40;
               steps = 168;
               dir='F';
               drive_stepper(speed,dir,steps);

               delay_ms(1000);

               speed = 40;
               steps = 168;
               dir='R';
               drive_stepper(speed,dir,steps);
            }
         else
            {
               switchstate=0;
            }
         seconds = 0;
         }
      }
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Dec 12, 2007 7:08 am     Reply with quote

Quote:
My Problem is, that the timer starts counting after the motor's rotation which takes approximately 15 seconds.
How can I handl this?
The answer is already present in your question. Do not reset the seconds timer after you have activated the motor but do it before, i.e. direct after the 7 minutes test.

Note that your current code waits 7 minutes. Activates the motor. Waits again 7 minutes before repeating the sequence. There is a total pause of 14 minutes between the motor activations, is that as intended?
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Dec 12, 2007 9:32 am     Reply with quote

One way is to place an if() statement in your ISR that will enable/disable your counting routine. You could have this variable set by your drive_stepper() routine when it reaches it's destination. It could be cleared by the ISR when your counter reaches 0.

One thing to note here, it is not a good idea to have key words as part of your variable names. int_count is an example. Bad things can happen when the mind sees this and is coasting in neutral.

Ronald
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