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

What is the best way to trigger an interrupt after X ms ?

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



Joined: 30 Dec 2010
Posts: 6

View user's profile Send private message

What is the best way to trigger an interrupt after X ms ?
PostPosted: Thu Dec 30, 2010 5:55 pm     Reply with quote

I want to trigger an interrupt after X ms.
I currently use this code, is there a better or "proper" way to do it ?
Code:

int16 time; //ms
int t1count=0;
int t1count_end;
int16 t1time;
int1 flag=0;


#INT_TIMER1
void timer1()
{
   if (t1count>=t1count_end)
   {
      Flag=1;
      disable_interrupts(INT_TIMER1);
   }
   else t1count++;

}

void main(void)
{
   setup_oscillator(OSC_4MHZ);
   SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
   enable_interrupts(GLOBAL);
   time=1000; //set time in ms , this can be changed
   t1count_end=(125*time)>>16; //calculate the no of times t1 int is done
   t1time=~((125*time)&65535); //enhance precision


   set_timer1(t1time);
   t1count=0;
   enable_interrupts(INT_TIMER1);
   while(1); //flag becomes 1 in X time
   
}

//125*time converts ms into timer value (for T1_DIV_BY_8 )
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Dec 30, 2010 8:18 pm     Reply with quote

Better and 'proper' are subjective terms..

Better.. How ?? perhaps you can cut code that is more efficient in code space used, number of variables(RAM) used, etc.

'proper'..by whose 'standards' ! In my case, I remember cutting code in just 8Kbytes of 1702 EPROM. The system (still in use today) scans 512 remote devices via single wire, corrects for line loss, controls these remotes and retrieves data on a scheduled basis with several layers of alarms as well as printer and keyboard functions. For me 'proper' requires very, very tight code maximizing every byte.
'proper' for others might mean very wordy code(takes up space) but easier to understand, Some feel only interrupt driven code is 'proper'.

Every program ever made can be 'tweaked' to be 'better' and 'proper', at the cost of time and energy, yet still does the original procedure.

The bottom line is, it if works for you and you're happy, leave it alone.
adi1133



Joined: 30 Dec 2010
Posts: 6

View user's profile Send private message

PostPosted: Thu Dec 30, 2010 8:27 pm     Reply with quote

Is there a better built-in function or some other alternative to this ?
I just think my code is too bulky for such a simple feature
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 30, 2010 8:41 pm     Reply with quote

What is your overall purpose for doing this ? This information will help
us to give you advice.
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Thu Dec 30, 2010 8:47 pm     Reply with quote

Do you want to change the interrupt timing at runtime?

Do you want to wake the processor with the interrupt?

Do you want it only to interrupt once or EVERY x period of time?

Start here and see if this helps:
http://www.ccsinfo.com/forum/viewtopic.php?t=22467
adi1133



Joined: 30 Dec 2010
Posts: 6

View user's profile Send private message

PostPosted: Thu Dec 30, 2010 8:51 pm     Reply with quote

I use this to stop a motor, I want to change the interrupt timing during runtime, I do not use sleep and I only want to interrupt once, not every x period of time.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Dec 31, 2010 3:33 am     Reply with quote

As 'comments', a couple of things come to mind:
1) What resolution is needed?.
2) Is the interrupt going to do anything more than setting the flag?.

If (for instance), you were 'happy' with a 10mSec resolution, then a simpler long term solution, is to use timer2, and set this up to interrupt every 10mSec. Then the timer2 code, becomes:
Code:

int16 counter;

#INT_TIMER2
void tick(void) {
    if (counter) --counter;
}


Then all you need do, is set 'counter' to the number of 10mSec 'ticks' you want, and it'll go to zero, when this time has completed.

If the second is 'false', then it is worth realising that the timer interrupt 'flag', is just that, a flag, which can be used just like any other variable (after a bit definition).
So if you set the interrupt counter to a value which will set the interrupt flag the required time in the future, clear the interrupt flag, and leave interrupts _disabled_, you can test the interrupt flag, just like your 'flag' variable, and it'll go 'true' when the time expires. No need for an interrupt handler at all...

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 31, 2010 8:08 pm     Reply with quote

This thread on multi-tasking has sample code that may help:
http://www.ccsinfo.com/forum/viewtopic.php?t=17189
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