vinicius.jlantunes 50 Posted December 15, 2012 Share Posted December 15, 2012 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? Quote Link to post Share on other sites
simpleavr 399 Posted December 15, 2012 Share Posted December 15, 2012 watchdog? WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer reset pin not tie to VCC? how is your code not working? did it turn on the led and not turn off? or never on? or timing doesn't look right. Quote Link to post Share on other sites
vinicius.jlantunes 50 Posted December 15, 2012 Author Share Posted December 15, 2012 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... Quote Link to post Share on other sites
simpleavr 399 Posted December 15, 2012 Share Posted December 15, 2012 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. vinicius.jlantunes 1 Quote Link to post Share on other sites
vinicius.jlantunes 50 Posted December 15, 2012 Author Share Posted December 15, 2012 The full code is in this other thread, I thought of only putting pieces of code here as not to distract from the main problem. But you're right, this assumes the problem would be in one of these pieces which may not be the case. Thanks! Quote Link to post Share on other sites
SirPatrick 35 Posted December 15, 2012 Share Posted December 15, 2012 Here is a snippet of code that waits a certain amount of time and then toggles a digital output pin To make this code start on a button press just initially configure the time to be stopped (MC_0). Then in your button ISR do TACTL |= MC_1; vinicius.jlantunes 1 Quote Link to post Share on other sites
vinicius.jlantunes 50 Posted December 15, 2012 Author Share Posted December 15, 2012 Thank you all for your help. In principle my code was correct and very similar to Sir Patrick's, but one fundamental mistake - I was in LPM4 while using SMCLK, which is only enabled in LPM1 and above. Quote Link to post Share on other sites
roadrunner84 466 Posted December 17, 2012 Share Posted December 17, 2012 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. vinicius.jlantunes 1 Quote Link to post Share on other sites
pabigot 355 Posted December 29, 2012 Share Posted December 29, 2012 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. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.