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.

How to run two threads same time in msp430g2553

Other Parts Discussed in Thread: MSP430G2553, MSP430G2231

I want run real time clock (RTC) in separate thread and remaining program as main thread.

In main thread i will get the real time clock value and based on that i will un main thread.

please give some solution how to use real time clock in msp430g2553 and how to use threads in this micro controller 

  

  • The MSPs only have one CPU core and therefore only one thing can be done at a time.

    However, the interrupt architecture allows you to interrupt current code execution based on events. These may be periodic events from a timer (which one can simply count for a time value). And some MSPs do even have a built-in RTC that runs in hardware, so you only need to read its registers if you need the current time.

    Multithreading isn't impossible (I've done my own thread scheduler some time ago), and there are some 'operating systems' which provide multiple thread handling. But it costs resources and has some overhead (including the need for multiple stacks) and the resources on MSPs are rather limited.

  • In short: to run such a trivial function like RTC, you do not need to use multithreading. It will be enermous overkill. Use real time clock timer interrupt (IRQ) routine instead as we all do.

  • RTC interrupt with superloop template code on random lo-end msp with 32khz crystal connected would look like this:

    #include <msp430x20x3.h>

    void main(void)
    {
    WDTCTL = WDT_ADLY_1000; // WDT 1s interval timer
    IE1 |= WDTIE; // Enable WDT interrupt

    _BIS_SR(GIE);

    while(1)  // superloop :)

    {
    // Here you do what you were going to do in main thread

    }

    }

    #pragma vector=WDT_VECTOR
    __interrupt void watchdog_timer (void)
    {
     // this routine is called each second so you can do your RTC math
    }


  • i am new to this msp430. i can use watch dog timer directly.most of the example they are stoping the watch dog timer and doing there  operations.

    if it ok, using with the above code it will generate interrupt for every second.

    other wise Based  on timers how to generate interrupt for every 1 sec if the clock frequency is 32KHZ  or 16MHZ.please provide some code

  • ASHOK VUYYURU said:
    please provide some code

    Before asking for more code, have you looked at the examples already provided?

    Look on the product page: http://www.ti.com/product/msp430g2553#softTool - click on 'Show More' to see the full list...

    While you're there, also take time to review the Data Sheet, Family User's Guide, Application Notes, videos, online training, and other resources...

    There's also a Wiki: http://processors.wiki.ti.com/index.php/MSP430

     

  • Hi Ashok,

                 You really follow Andy suggestions, just to make you know how these thing working I am writing this to you. Lets consider that you have 16MHZ clock. if you want a interrupt for every second using timers, there are many ways, these you know once you go through 1)sample codes 2.) data sheet 3) blogs and wiki or video tutorials. I will go through 1 one the way to generate a 1second interrupt. I am using many resources here so as to give you a look of possibilities, if you go on reading resources suggested by Andy  you can simplify it to minimum level. You can generate 1sec interval from any clock setting in MSP430 some times using 1 timer or combination of timers. I have made this code in the beginning days of my interaction with MSP. Have a look.

    /*------------------------------------GOPI-Notes---------------------------------------------
     * The objective here is is to use TimerA,Interrupts and create a delay of
     * 1 second.This delay is used to blink LED for debugging purpose.
     */
    /*-----------------------------------------------------------------------------------------*/
    //******************************************************************************
    //  MSP430G2xx1 Demo - Timer_A, Toggle P1.0, CCR0 Cont. Mode ISR, DCO SMCLK
    //
    //  Description: Toggle P1.0 using software and TA_0 ISR. Toggles every
    //  50000 SMCLK cycles. SMCLK provides clock source for TACLK.
    //  During the TA_0 ISR, P1.0 is toggled and 50000 clock cycles are added to
    //  CCR0. TA_0 ISR is triggered every 50000 cycles. CPU is normally off and
    //  used only during TA_ISR.
    //  ACLK = n/a, MCLK = SMCLK = TACLK = default DCO
    //
    //           MSP430G2xx1
    //         ---------------
    //     /|\|            XIN|-
    //      | |               |
    //      --|RST        XOUT|-
    //        |               |
    //        |           P1.0|-->LED
    //
    //  D. Dang
    //  Texas Instruments Inc.
    //  October 2010
    //  Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
    //******************************************************************************
    #define red_led         BIT0
    #define green_led       BIT6
    #include <msp430g2231.h>

    void main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
      //Setting up  DCO at 1HZ.The following two instructions does that
      //what about other frequencies 4HZ,8HZ, and 16HZ.
      //--------------1MHZ----------------------------------------------------------
      /*
      BCSCTL1 = CALBC1_1MHZ;                    // Set range
      DCOCTL = CALDCO_1MHZ;                     // Set DCO step + modulation
      */
      //--------------4MHZ----------------------------------------------------------
      /*
      BCSCTL1 = CALBC1_4MHZ;                    // Set range
      DCOCTL = CALDCO_4MHZ;                     // Set DCO step + modulation */
      //--------------8MHZ----------------------------------------------------------
      /*
      BCSCTL1 = CALBC1_8MHZ;                    // Set range
      DCOCTL = CALDCO_8MHZ;                     // Set DCO step + modulation
      */
      //--------------16MHZ---------------------------------------------------------
      /*
      BCSCTL1 = CALBC1_16MHZ;                    // Set range
      DCOCTL = CALDCO_16MHZ;                     // Set DCO step + modulation
      */
      //------------------------------------------------------------------------------------------------------
      /*----------------------Calculations-----------------------------------------------------------------------
      * We need 1 second delay.Frequency That I have is 1MHZ after dividing it by 8 in TACTL available frequency
      * is 125000 HZ. DCO frequency = 1 MHZ TACTL got it down to DCO/8 = 125000
      * Now what is the value to be written in to TACCR0 (Timer A Capture Compare Register) to capture timer count
      * when it reaches this(TACCR0) value. = delay required*available frequency = 1*125000 = 0X1E848
      * This value can't fit in TACCR0 register.So select clock source with less frequency.
      * -------------select ACLK with VLO activated it gives clock from internal very low power low frequency oscillator
      *              with frequency of 12kHZ.Therefore || frequency available = 12000 ||delay required = 1 seconds
      *                                                   Timer value should be = delay * frequency available.
      *                                                   TACCR0 = 1 * 12000 = 0X2EE0
      */

      /* The basic clock system control register 3 controls the oscillator
       * the auxilliary clock uses.  Change ACLK from external timer crystal
       * oscillator to internal very low-power oscillator (VLO) clock,
       * which runs at appoximately 12kHz.
       */

      BCSCTL3 |= LFXT1S_2;                                     //Using VLO frequncy for ACLK

      // Setting up LED's for debugging purpose
      P1DIR |= red_led+green_led;                            // Port1 pin0---red_led-output
                                                                                         // Port1 pin6--grn led--output
      //-----------------------Configuring TimerA with Interrupts------------------------------------------------------

     
      TACCTL0 = CCIE;                                         // CCR0 interrupt enabled
                                                                                        //NOTE-is TACCTL0 and CCTL0 are one and same.
      TACTL = TASSEL_1 + MC_1;                               // ACLK,continuous mode
      TACCR0 = 12000;
      /* The value at which TimerA1 interrupt triggers. */
      TACCR1 = 12000;
      //-------------------Pusing operation into Low Power Mode with interrupts enabled-----------------------
      _BIS_SR(LPM0_bits + GIE);                            // Enter LPM0 w/ interrupt
      //--------------------------------------------------------------------------------------------------------
    }

    // Timer A0 interrupt service routine
    #pragma vector=TIMERA0_VECTOR
    __interrupt void Timer_A (void)
    {

      TACCTL1 &= ~CCIFG;                                   // clear the interrupt flag
      P1OUT ^= (BIT0 + BIT6);                           // Toggle P1.0  
    }

    Regards,

    Sri.

  • #include <msp430g2553.h>
    int hour,minute,sec,light_red,light_green;
    void main(void)
    {
    WDTCTL = WDT_ADLY_1000; // WDT 1s interval timer
    P1DIR |= BIT0+BIT6;
    IE1 |= WDTIE; // Enable WDT interrupt


    _BIS_SR(GIE);

    while(1) ; // superloop :)


    }

    #pragma vector=WDT_VECTOR
    __interrupt void watchdog_timer (void)
    {
    // this routine is called each second so you can do your RTC math
    sec++;

    if(sec == 60){
    minute++;
    light_red++;
    if(light_red==2){
    light_red = 0;
    P1OUT = BIT0;//for every two min light will blink


    light_green++;

    if(light_green==2){
    light_green = 0;
    P1OUT = BIT6;//for every four min light will blink


    }
    }
    sec = 0;
    }
    if(minute == 60){
    hour++;
    minute = 0;
    }
    if(hour == 24){
    hour = 0;
    }
    }

    In this ISR routine is not working looks like LED not blinking for every two min and four min please check it is there any mistake in my code.

  • #define red_led BIT0
    #define green_led BIT6
    #include <msp430g2553.h>
    int hour,minute,sec,light_red,light_green;
    void main(void)
    {

    WDTCTL = WDTPW + WDTHOLD; // Stop WDT
    //Setting up DCO at 1HZ.The following two instructions does that
    //what about other frequencies 4HZ,8HZ, and 16HZ.
    //--------------1MHZ----------------------------------------------------------

    BCSCTL1 = CALBC1_1MHZ; // Set range
    DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation

    //--------------4MHZ----------------------------------------------------------
    /*
    BCSCTL1 = CALBC1_4MHZ; // Set range
    DCOCTL = CALDCO_4MHZ; // Set DCO step + modulation */
    //--------------8MHZ----------------------------------------------------------
    /*
    BCSCTL1 = CALBC1_8MHZ; // Set range
    DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation
    */
    //--------------16MHZ---------------------------------------------------------
    /*
    BCSCTL1 = CALBC1_16MHZ; // Set range
    DCOCTL = CALDCO_16MHZ; // Set DCO step + modulation
    */
    //------------------------------------------------------------------------------------------------------
    /*----------------------Calculations-----------------------------------------------------------------------
    * We need 1 second delay.Frequency That I have is 1MHZ after dividing it by 8 in TACTL available frequency
    * is 125000 HZ. DCO frequency = 1 MHZ TACTL got it down to DCO/8 = 125000
    * Now what is the value to be written in to TACCR0 (Timer A Capture Compare Register) to capture timer count
    * when it reaches this(TACCR0) value. = delay required*available frequency = 1*125000 = 0X1E848
    * This value can't fit in TACCR0 register.So select clock source with less frequency.
    * -------------select ACLK with VLO activated it gives clock from internal very low power low frequency oscillator
    * with frequency of 12kHZ.Therefore || frequency available = 12000 ||delay required = 1 seconds
    * Timer value should be = delay * frequency available.
    * TACCR0 = 1 * 12000 = 0X2EE0
    */

    /* The basic clock system control register 3 controls the oscillator
    * the auxilliary clock uses. Change ACLK from external timer crystal
    * oscillator to internal very low-power oscillator (VLO) clock,
    * which runs at appoximately 12kHz.
    */

    BCSCTL3 |= LFXT1S_2; //Using VLO frequncy for ACLK

    // Setting up LED's for debugging purpose
    P1DIR |= red_led+green_led; // Port1 pin0---red_led-output
    // Port1 pin6--grn led--output
    //-----------------------Configuring TimerA with Interrupts------------------------------------------------------


    TACCTL0 = CCIE; // CCR0 interrupt enabled
    //NOTE-is TACCTL0 and CCTL0 are one and same.
    TACTL = TASSEL_1 + MC_1; // ACLK,continuous mode
    TACCR0 = 12000;
    /* The value at which TimerA1 interrupt triggers.*/
    TACCR1 = 12000;
    //-------------------Pusing operation into Low Power Mode with interrupts enabled-----------------------
    _BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
    //--------------------------------------------------------------------------------------------------------
    while(1);
    }


    // Timer A0 interrupt service routine
    #pragma vector=TIMERA0_VECTOR                                    //here it is showing ERROR
    __interrupt void Timer_A (void)
    {

    // TACCTL1 &= ~CCIFG; // clear the interrupt flag
    sec++;
    P1OUT = 0x00;
    if(sec == 60){
    minute++;
    light_red++;
    if(light_red==2){
    light_red = 0;
    P1OUT = BIT0;//for every two min light will blink


    light_green++;

    if(light_green==2){
    light_green = 0;
    P1OUT = BIT6;//for every four min light will blink


    }
    }
    sec = 0;
    }
    if(minute == 60){
    hour++;
    minute = 0;
    }
    if(hour == 24){
    hour = 0;
    }
    }

    in marked place showing error in ccs please provide correct solution

  • ASHOK VUYYURU said:
    #pragma vector=TIMERA0_VECTOR                                    //here it is showing ERROR

    some times it depends on version of CCS u r using, can u post the error

  • you r not toggling led in here instead u r writting new valie to register use these


                P1OUT ^= BIT0;//for every two min light will blink
             
                P1OUT ^= BIT6;//for every four min light will blink
               

  • HI ,

    This is the code for MSP430g2231 u need to make changes to fit for msp430g2553 as far as interruppt was concerned u should change that to

    #pragma vector=TIMER_A0_VECTOR

  • sri-sri said:
    u need to make changes to fit for msp430g2553 as far as interruppt was concerned u should change that to
    #pragma vector=TIMER_A0_VECTOR

    Actually, teh 2553 has two TimerA modules, so the right vector would be TIMER0_A0_VECTOR.
    But I think TIMER_A0_VECTOR is available as an alias.

  • Jens-Michael Gross said:
    so the right vector would be TIMER0_A0_VECTOR.

       Ya I am sorry its a typo, it is supposed to be like TIMER0_A0_VECTOR

    Thank you.

  • I wonder why the vector names are not listed in the device datasheet. There's a table that lists IFG bits and module names and such for the vector table, but not the vector names. :(

  • Weird I had the same Typo... :)

    This resolved the issue switching to the MSP430_2553

    #ifdef __GNUC__
    __attribute__((interrupt(TIMER0_A0_VECTOR)))
    void Timer_A(void)
    #else
    #pragma vector=TIMER0_A0_VECTOR
    __interrupt void Timer_A (void)
    #endif


    Thanks guys!

  • Weird I had the same Typo... :)

    This resolved the issue switching to the MSP430_2553

    #ifdef __GNUC__
    __attribute__((interrupt(TIMER0_A0_VECTOR)))
    void Timer_A(void)
    #else
    #pragma vector=TIMER0_A0_VECTOR
    __interrupt void Timer_A (void)
    #endif


    Thanks guys!

  • i want to implement real time clock and temperature value in msp430g2553 can any one send me code urgent!!!!!!

  • durga dhandapani said:
    i want to implement real time clock and temperature value in msp430g2553 can any one send me code urgent!!!!!!

    Wow! URGENT!!

    Here we can guide you, not do your job for you. If you want to hire programmer, then you shall look for websites dedicated to (freelance) IT jobs market.

    Try again and tell what's your problem to implement what you need.

  • Andy Neil said:

    Andy,

    I like the links you included. I often find myself wanting to say RTFM, but in a polite way.

  • Andy, you forgot to point out that the quesiton itself is completely off-topic and reviving an old thread with a completely unrelated post is definitely the wrong way to convince someone else to do the job one originally had to do by oneself.

  • durga dhandapani wrote the following post at Apr 01 2013 04:54 AM:

    durga dhandapani said:

    i want to implement real time clock and temperature value in msp430g2553 can any one send me code urgent!!!!!!

    Did any of the responders notice the date of that post?

  • old_cow_yellow said:
    Did any of the responders notice the date of that post?

    Yes. But I don't consider things that happen every day as an April Fool's joke.

**Attention** This is a public forum