Jump to content
43oh

Using Timer to implement a "sleep / wait" function


Recommended Posts

Hello all,

 

I am struggling to set up a timer to implement a "wait" function.

 

Basically, I have a piece of code that lights up a few LED's, then I want to wait a couple seconds and turn them off.

The way I have it implemented at the moment:

 

  • Timer configuration (in main function)
  // DCO configuration
  DCOCTL  = CALDCO_1MHZ; //Set DCO to 1 MHz
  BCSCTL1 = CALBC1_1MHZ; // Calibrate internal clock for 1 MHz
  
  // Timer_A configuration
  TACTL = TASSEL_2 + TACLR + ID_3 + MC_0;
  CCTL0 |= CCIE;
  TACCR0 = 125; // 1 ms period
  • ISR routine
#pragma vector = PORT1_VECTOR
__interrupt void button( void )
{  
(...)
  // Does stuff, lights LED's
(...)
  // Wait a little - start timer, then
  TACTL |= MC_1;
  
  // Clears interrupt flag
  P1IFG &= ~BIT3;
}
  • Timer A interrupt
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A0 (void)
{
  i++;
  turn_leds_off();
  if (i >= TIMEWAIT)
  {
    i = 0;
    // Turns leds off
    turn_leds_off();
    
    // Stops timer
    TACTL |= MC_0;
  }
  else
     return;
}

The counter "i" is incremented until it reaches the value of constant TIMEWAIT I set at the beggining of the code. In theory my timer has a period of 1 ms, so if TIMEWAIT is set to 3000, it will go inside the IF statement after 3 seconds, turn the LED's off and stop the timer by setting MC_0.

 

It doesn't work though. What am I doing wrong?

Link to post
Share on other sites

I do have the watch dog line. Reset is also fine, the set up is in the launch pad.

 

What happens is that the leds are turned on but then instead of turning off after the set time, they never turn off. I placed a breakpoint inside the timer ISR and it seems to never stop there. Or, to put it more precisely, if I keep hitting the button executes the routine inside the button ISR every time correctly, but only sometimes stops inside the timer ISR, suggesting that in certain cirscunstances the timer is being triggered, but not consistently for some reason...

Link to post
Share on other sites

showing bits of code fragments does not allow us to see the problem.

 

they are just too many variables.

what is i? char, unsigned char, int, short? it need to be > 3000.

did u turn on general interrupt?

u have two turn_leds_off(), one outside your if statement which will run every 1ms (i haven't check the timing though).

what's in your turn_leds_off()?

 

if u are trying to do simple things like turn off led in 3 secs, simplify your code to bear minimal and it will be more clear.

Link to post
Share on other sites

Yes, you need to be very careful about when you assume you have access to which clock sources. When in LPM4, you don't have any clocksources available, in LPM3 you can only use the ACLK clock (so only the LFXT1 or VLO clock source).

In my projects, I mostly reside in LPM0 to cause a "wait until", or in LPM3 when I want to go to low power mode. In the latter case I make sure to wake up at a timer (sources from ACLK) or a pin interrupt.

Link to post
Share on other sites
  • 2 weeks later...

Yes, you need to be very careful about when you assume you have access to which clock sources. When in LPM4, you don't have any clocksources available, in LPM3 you can only use the ACLK clock (so only the LFXT1 or VLO clock source).

 

This is essentially true for the 2xx family chips including the ValueLine ones in the LaunchPad, but in the more advanced MSP430 MCUs there's a clock request system that overrides the LPM bits and keeps clocks that are needed for enabled peripherals (like UARTs) active.  Or re-enables them (e.g., as I read it the USCI UART is supposed to be able to enable the baud rate clock from LPM when it detects RX activity, and turn it off when idle).  Also be aware that there are a lot of chip errata related to this neat feature.

 

When you're playing with LPM on these chips, forgetting to turn off a peripheral can explain why you don't see the power usage you expect.  See "5.2.11 Operation From Low-Power Modes, Requested by Peripheral Modules" in the 5xx/6xx user's guide for more information.

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...