This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553 Timer and Delays

Other Parts Discussed in Thread: MSP430G2553, MSP430G2211, MSP430WARE

I tried the delay subroutine using CCS v5 in C language for the launchpad equipped with MSP430G2553 chip but the compiler would just skip the delay subroutine! Anyone knows why? Also using Timer0 A1 interrupt was unsuccessful. The ISR wasn't getting serviced at all!! I am new to the MSP430 playground and the launchpad. Please help.

  • Hi Karan,

    it would be much easier if you post your code and we can have a look at it. Two things I have up in my mind:

    - did you stop the watchdog timer?

    - are you defining loop variables as volatile?

    For the first, if you do not disable the watchdog it will reset your device after a very short amount of time. For the second, if you do not (and it's easy to forget and happens to me sometimes) declare variables for such delay loops as volatile your compliler will optimze it out of existence.

    Welcome to the MSP430 ;-)

  • Hey Jan,

    Thanks:)

    Yes I stopped the WDT but not declared the variables as volatile, not a usual habit!! Will try it right away!

  • I had the same problem trying to set up Timer1.  Even tried to do it with Grace but that didn't help... So i just used the watchdog timer interrupt to get what I needed done... But never did figure out how to correctly setup the Timer1 interrupts...  When debuging the interrupt flag registers were all moved under the Special Function Registers, IE1, IE2, IFG1, IFG2.  You should be able to step through and watch which interrupt is actually hiting.

  • Karan Gurnani said:
    the compiler would just skip the delay subroutine

    That's the expected behavior. An empty for loop has (seen from the program flow) the only effect of setting th eloop variable to the end value. Which is what the compiler generates, sicne it is the job of the compiler to produce the fastest and smalles code possible.
    The compiler doesn't know that you really want to produce useless code that wastes time.
    For delays, tehr eis teh compiler-specific intrinsic funciton __delay-cycles(), what is not a real funciton, but an instruction that makes the compiler generate waste code that executes for exactly the given number of processor cycles. Since it is no real function, you cannot step into it. You need to go into assembly view to see what it does.
    However, the better way of creatign delays is the use of a timer. That's what timers are made for.

    Karan Gurnani said:
    using Timer0 A1 interrupt was unsuccessful. The ISR wasn't getting serviced at all

    Depends on how you configured it. Without knowing your code, nobody can even guess what you made wrong. There are many possibilities:

    • you didn't globally enable interrupts (set the GIE bit)
    • you didn't set the proper IE bit to activate the specific timer intrrupt
    • the timer was not running
    • you didn't specify a proper clock source (default is external clock, which requires external clock signal and port configuration)
    • the clock you picked is stopped or has 0Hz (e.g. ACLK was selected and no crystal is there on XT1)
    • you wrote the wrong ISR (each timer has two ISRs, one for the CCR0 interrupt and one for all others)

    Each of these rather basic mistakes alone will keep your ISR from being called. And there are several more failure situations for more sophisticated timer uses.

  • Jens-Michael Gross said:
    Without knowing your code, nobody can even guess what you made wrong.

    okay so here is my code I'm trying to run on my G2553.

    #include <msp430.h>

    #define RLY1 BIT6;

    /* Global Variables */
    char i=0;

    void main(void) {
    WDTCTL = WDTPW + WDTHOLD; // disable watchdog

    P1OUT = RLY1;
    P1DIR = RLY1; // P1.6 out to relay (LED)

    BCSCTL1 = CALBC1_1MHZ; // Set DCO to calibrated 1 MHz.
    DCOCTL = CALDCO_1MHZ;

    TACCR0 = 62500 - 1; // A period of 62,500 cycles is 0 to 62,499.
    TACCTL0 = CCIE; // Enable interrupts for CCR0.
    TACTL = TASSEL_2 + ID_3 + MC_1 + TACLR; // SMCLK, div 8, up mode,
    // clear timer

    _enable_interrupt();

    for(;;) { // Do nothing while waiting for interrupts.
    }
    } // main


    /* Interrupt Service Routines */
    #pragma vector = TIMER0_A1_VECTOR
    __interrupt void CCR0_ISR(void) {
    if (++i == 10) {
    P1OUT ^= RLY1;
    i=0;
    }
    } // CCR0_ISR
    
    
    Can you rectify if anything is wrong here?
  • Karan Gurnani said:
    Can you rectify if anything is wrong here?

    Yes. TACCR0 (and onla TACCR0) has its own vector TIMER0_A0_VECTOR.

    Since you don't specify your ISR for this vector, the CPU will jump into the void and eventually reset isntead of callign your ISR.

    Be warned: in TIMER0_A1_VECTOR (used for TAIE as well as for CCR1-n), yui'll have to manually clear the IFG bit that caused the interrupt. Or read the TAIV register. Else you're getting an eternal interrupt loop.
    For TIMER0_A0_VECTOR, however, this is done implicitely (as there is only one thing that calls this ISR) when the ISR is executed, to save you some precious CPU cycles.

  • Jens-Michael Gross said:
    TACCR0 (and onla TACCR0) has its own vector TIMER0_A0_VECTOR.

    Thanks Jens, the program is running fine now! How do you get all the information regarding MSP's? Should read any book or something? 'Cause many books are having coding examples in Assembly language and the ones with C have g2211 or another msp430 device whose some of the codes aren't same as the g2553!

  • Hi, Karan.

    Karan Gurnani said:
    How do you get all the information regarding MSP's? Should read any book or something? 'Cause many books are having coding examples in Assembly language and the ones with C have g2211 or another msp430 device whose some of the codes aren't same as the g2553!

    The place to start, if you haven't already obtained copies of them, is with the TI reference documents. In your case, you would go here:

      MSP430G2211

    and download these documents:

      MSP430x2xx Family User's Guide (Rev. I)
      MSP430G2x01, MSP430G2x11 Mixed Signal Microcontroller (Rev. G)
      MSP430G2x01, G2x11, G2x21, G2x31 Device Erratasheet (Rev. B) 

    The User's Guide describes the various building blocks available for chips in the x2xx series ('F2013, 'G2211, etc.).

    The datasheet and Errata describe the specifics for a given device (e.g. 'G2211).

    If you scroll down the 'G2231 page far enough you will also find links to MSP430 Application Notes and other related documents. These are worth browsing through for ideas and approaches to using the MSP430 peripherals.

    You'll also find information at various places on the 'Web, including these:

      http://www.43oh.com/

      http://processors.wiki.ti.com/index.php/Category:Microcontroller_Projects_-_MSP430

    Searching Amazon.com for "msp430 microprocessor" turns up a number of books, but I don't currently own any of them and can't make any specific recommendations. Perhaps some kind generous soul will make a few suggestions. <grin>

    Hope this helps.

    Frank

  • Hi Karan,

    With CCSv5, which you mentioned you are using, the MSP430 documents and code examples are now accessible right from CCS. Just go to View > TI Resouce Explorer, then in the resource explorer tab that appears, you can navigate in the column on the left to all the documentation and code examples for your particular device. (MSP430Ware > Devices > MSP430G2xx then you can view User's Guide, Datasheets, and Code Examples (in C), even Grace examples.

    Hope this helps you get started!

    Regards,

    Katie

  • Karan Gurnani said:
    How do you get all the information regarding MSP's? Should read any book or something?

    Honestly, I never read a book about the MSP. All the info is from the users guide, the datasheets and ~ 6 years of own experiments. Never used one of the demo codes too.
    Well, having worked with the good old 6502 on the VC20/C64 some 30 years ago, was a big help for coding with limited resources. And knowing assembly language helps understanding the inner workings, even if one then writes in a high-level language. I guess that's why mos tbooks have assembly examples. (including the users guides).

**Attention** This is a public forum