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

Delays and Interrupts: a little problem...

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



Joined: 05 Sep 2007
Posts: 46
Location: Londrina - Brazil

View user's profile Send private message

Delays and Interrupts: a little problem...
PostPosted: Wed Jun 03, 2009 11:15 am     Reply with quote

Hello Fellows...

One of the biggest problems when using delays and interrupts is that the compiler must disable interrupts while calling the delay function. (to prevent re-entrancy).

Some applications must use lots of delays to accomplish its tasks (like make some bit-bang communications timing just fine).
I wonder what programming techniques do you use to make less use of delays (when a delay is needed) that makes possible calling delays, and using interrupts at the same time.
Or, just some technique to make a precise delay time, not using delay_us, delay_ms or delay_cycles.

Thanks!
_________________
Give a man a fish and you'll feed him for a day. Teach a man to fish, and you'll feed him for a lifetime.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 03, 2009 11:35 am     Reply with quote

Quote:

One of the biggest problems when using delays and interrupts is that the
compiler must disable interrupts while calling the delay function. (to
prevent re-entrancy).

You can fix this problem by creating a 2nd instance of the #use delay
library code. Example:
http://www.ccsinfo.com/forum/viewtopic.php?t=27345
ckielstra



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

View user's profile Send private message

PostPosted: Wed Jun 03, 2009 5:45 pm     Reply with quote

A technique I oftern use for timers is to create interrupt driven 'software timers'.

For every timer you want you create a global variable:
Code:
int8 MyTimer1;
int8 MyTimer2;
int8 MyTimer3;


Setup timer0 to fire (about) every 100ms. This will give you a timer range of 0.1 to 25.5sec
Code:
// CLOCK CRYSTAL FREQUENCY
#define XTAL_FREQ          16000000

// value for timer0 to fire every 100 ms
#define MS_100              (int16)(0x0000 - (XTAL_FREQ / (4 * 64 * TIMER0_PER_SECOND)))

#define ONE_SECOND        ( 10)   // 10 interrupts per second
#define TWO_SECONDS       (  2 * ONE_SECOND)
#define FIVE_SECONDS      (  5 * ONE_SECOND)
#define TEN_SECONDS       ( 10 * ONE_SECOND)


void main()
{
  setup_timer_0(RTCC_INTERNAL | RTCC_DIV_64);
  enable_interrupts(INT_TIMER0);
  enable_interrupts(GLOBAL);

  ...
}



Create a timer0 interrupt handler. Every software timer will count down to zero. If a counter reaches zero it will stop.:
Code:
//-----------------------------------------------------------------------------
// Timer0 interrupt, triggers every 100 miliseconds.
// Used for multiple software based timers.
//-----------------------------------------------------------------------------
#int_timer0
void timer0_isr()
{
  set_timer0(MS_100);        // fire again in 100 ms

  // Many software count down timers...
  if (MyTimer1)    MyTimer1--;
  if (MyTimer2)    MyTimer2--;
  if (MyTimer3)    MyTimer3--;
}


Example code to flash a led:
Code:
#define LED   PIN_A5

while(1)
{
  output_low(LED);
  MyTimer1 = TEN_SECONDS;
  while(MyTimer1);

  output_high(LED);
  MyTimer1 = TWO_SECONDS;
  while(MyTimer1);
}
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