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

RTCC interrupt won't stop??

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







RTCC interrupt won't stop??
PostPosted: Tue Oct 09, 2007 9:10 pm     Reply with quote

Hi all, i have problems with interrupt again. i'm using PIC16F877, ccs 4.049, using interrupt to send pulse to stepper. My goal is to make the motor rotate 3 revolutions and stop, but what happen now is, the motor keeps rotating. I think it should be my coding problem, I need some guides, thanks a lot.

Code:
#include <16F877>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#define PWM_PIN  PIN_B1 

int8 i;
short rtcc_flag;    //declare global flag variable

#INT_RTCC
void rtcc_isr(void);
 
int8 iCounter=0;

//---------------------------
void main()
{

rtcc_flag = FALSE;
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

while(1)
   {
      if(iCounter<3)                   //counter for 3 revolutions
      {
         if(rtcc_flag == TRUE)
         {
            for(i=0; i<1000; i++)      //1000 steps = 1 revolution
            {
               output_high(PIN_B1);
               delay_us(450);
               output_low(PIN_B1);
               delay_us(450);
               rtcc_flag = FALSE;
            }
         }
      }
      else
      {
         output_low(PIN_B1);
      }

   }

}

//====================================
#INT_RTCC
void rtcc_isr(void)
{
rtcc_flag = TRUE;
iCounter++;
}
Ttelmah
Guest







PostPosted: Wed Oct 10, 2007 2:39 am     Reply with quote

Two separate problems.
Remember conters will wrap. Your 'icounter' value, will be <3, for 3/256th of the time, whatever else is going on. It'll count 0,1,2,3.......252,253,254,255,0,1,2,3....
Rtcc_flag, will always end up true (you are setting it 'true' in the interrupt), so only a few uSec after you set it false, it'll be true again.
As it stands, the RTCC interrupt will trigger every 512 instruction times, so you will get the three movements forward, then a pause of 129536 processor instruction cycles (253 counts 512 instruction cycles/count), which at 20MHz, is 1/38th of a second, and then it'll do three movement cycles again.
Consider something like:
Code:

#INT_RTCC
void rtcc_isr(void)
{
   if (icounter<3) {
      rtcc_flag = TRUE;
      iCounter++;
   }
}

This way, the increment, will stop when the count gets to 3.
To restart, all you do in the 'main', is set icounter to zero.

Best Wishes
wind
Guest







it's working !!! it's working ~~~
PostPosted: Thu Oct 11, 2007 7:05 pm     Reply with quote

Thanks Ttelmah. I got my desired result by referring to your solution. Thanks a lot.
Following is my working code, i am not sure whether it is 100% correct or not, well, at least it stops. LOL ~~ Laughing

Code:
#include <16F877>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#define PWM_PIN  PIN_B1
#define RTCC_PRELOAD (64 - 39)

int8 iCounter=0;
int16 i=0;
short rtcc_flag;             

#INT_RTCC
void rtcc_isr(void);


//---------------------------
void main()
{
rtcc_flag = FALSE;
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);   
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);


while(1)
   {
      if(iCounter<5  && rtcc_flag == TRUE) 
      {
      output_high(PIN_D5);                 
           
            for(i=0; i<1000; i++)       //1000 steps = 1 revolution
           
            {
               delay_us(180);           
               output_high(PIN_B1);     
               delay_us(55);
               output_low(PIN_B1);
               delay_us(55);
            }
               iCounter++;              //increment for revolution counter       
      }
     
      else
      {
      delay_us(800);                   
      output_low(PIN_D5);               
      disable_interrupts(INT_RTCC);   
     
      }
   
   }
}

//====================================
#INT_RTCC
void rtcc_isr(void)
{
rtcc_flag = TRUE;
}

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