Jump to content
43oh

Question about Timers and loops


Recommended Posts

My question is this: can I use a while loop to suspend a program until a value in a global variable is >= a constant.  

I'm using timer0 to interrupt every one second.  I update a global integer value by one every time the interrupt method executes.  However, when my program reaches the while loop, it locks up my program.  Does the while loop prevent the interrupt from firing, or is the while loop putting a lock on the variable and not allowing the interrupt statement to update.  The code I have is as follows and I am new to hardware programming.  

 

// initialize timer 

 

TA0CCR0 = 12000;   // Count limit (16 bit)
     TA0CCTL0 = CCIE + OUTMOD_3+ CM_0+ CCIS_0;                 // Enable Timer A0 interrupts, bit 4=1
     TA0CTL = TASSEL_1 + MC_1;
     __enable_interrupt();
 
//interrupt method plus global variable declaration 
volatile int delay_clock =0;
volatile int blink_clock =0;
__attribute__((interrupt(TIMER0_A0_VECTOR)))
void oneSecondHandler(void){
delay_clock++;
blink_clock++;
 
}

 

 

//while loop

 

void wait() {

 

blink_clock = 0;

while( blink_clock < 5){

wait_ghost = 111 // so the compiler doesn't throw out the code}

 

}

 

ANY help would be appreciated.  I am developing this for a bluetooth IPhone application 

Link to post
Share on other sites

The timer has a higher priority than the main method, so the interrupt will run when you're in a while block.  As long as you make sure that the variable is set as volatile, you should be fine.  

 

Another way of doing this would be to drop into LPM0 (this put the main block to sleep) and exit LPM0 in your interrupt when the condition is met.  I could probably point you to a good code sample, but it depends on what chip and compiler you're using.

Link to post
Share on other sites

The timer has a higher priority than the main method, so the interrupt will run when you're in a while block.  As long as you make sure that the variable is set as volatile, you should be fine.  

 

Another way of doing this would be to drop into LPM0 (this put the main block to sleep) and exit LPM0 in your interrupt when the condition is met.  I could probably point you to a good code sample, but it depends on what chip and compiler you're using.

I'm using hte MSP430G2113 chip, and the compiler is unknown to me, I just assumed its a standard ANSI C compiler.

 

maybe i should just attach my code, but its not getting stuck in the while loop in hte main function: I have an Emocco Bluetooth dev board attached to the TI board and pretty much all functionality is handled with interrupts.  One of the interrupts handles commands from the bluetooth board to set values. If a start command comes in, it looks at an array of commands (basically turn LED on or off) and starts iterating through the array, the problem is introducing the delay while a light is on or off.  

 

hope that helps and I would love to see some examples pertaining to this problem 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...