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.

Timer B and Clock issues

Other Parts Discussed in Thread: MSP430G2955

Hello,

I'm working on a project with an MSP430g2955. I have a 32.768 KHz crystal with 12pF capacitors.

I need to generate a 1sec timer and I've chose to use the Timer B.

The problem is that with the timer B enabled, the sistem restart after 1-2 minutes. The hardware reset on the pin 7 is always up, so I don't think that this is the problem.

This is my code for the clock:

void clock_init()
{
	 // P2SEL|=(0x40+0x80);
	  if (CALBC1_1MHZ==0xFF)					// If calibration constant erased
	  {
	    while(1);                               // do not load, trap CPU!!
	  }
	  DCOCTL = 0;                               // Select lowest DCOx and MODx settings
	  BCSCTL1 = CALBC1_1MHZ;                    // Set DCO to 1MHz
	  DCOCTL = CALDCO_1MHZ;
	  do {
	         IFG1 &= ~OFIFG;            		// Clear OSC Fault flag
	         __delay_cycles(50000);      		// Time for flag to set again
	  } while (IFG1 & OFIFG);         			// Loop while OSC Fault flag is set
	  IE1 |= OFIE;                    			// re-enable osc fault interrupt
}

The code for the Timer B is very simple:

void timerb_init()
{
	//TBCCTL0 = CCIE;                           // TBCCR0 interrupt enabled
	TBCCR0 = 32768;
	TBCTL = TBSSEL_1 | MC_1;                  // ACLK, contmode
}

#pragma vector=TIMERB0_VECTOR
__interrupt void Timer_B (void)
{
	if(timerset.sec>0)
		timerset.sec--;
	else
	{
		if(timerset.min>0)
			timerset.min--;
		else
		{
			if(timerset.hour>0)
			{
				timerset.hour--;
				//time_sec=update_time(0x03);
				//time_min=update_time(0x04);
				//time_h=update_time(0x05);
				//time_d=update_time(0x06);
			}
			else
			{
				timerset.day--;
				timerset.hour=23;
			}
			timerset.min=59;
		}
		timerset.sec=59;
	}
}

The timer B is enabled in the main:

			case 13:
				TBCCTL0 |= CCIE;  
				lcd_command(0x30);
				P1OUT|=LEDRED;
				sprintf(str,"%d:%d:%d   ",timerset.hour,timerset.min,timerset.sec);
				lcd_str_send(str,1);
				break;

Do you see any problem in everything I posted?

Thank you for helping me out.

Best regards

Nicola

  • Hi Nicola,

    I don't see any reason your application should reset after 1-2 minutes.  Can you post more code from main?

    FYI, your application may not be interrupt safe.  You should synchronize access to all three fields in the <timerset> data structure when you write them to the LCD.  If a timer interrupt happens after you read the <sec> field but before you read the <min> field, your displayed time could be wrong (temporarily).

    Typically the solution is to mask interrupts, make a local copy of the three fields, and then unmask interrupts before your call to sprintf().

    Jeff

  • The problem is not what I see on the display. My problem is that after 1-2 minutes the program restart. I think that the problem is connected to timerB but I don't understand why this happens.

    void main(void)
    {
    char str[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    
        WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
        clock_init();
        init();
        button_init();
        lcd_init();
        timerb_init();

    Do you have any idea?

    Thank you for helping me out.

    Best regards

    Nicola

  • I still don't see anything wrong.  It is difficult to help only seeing small code fragments.

    I notice you have enabled the interrupt for an oscillator fault.  Do you have an interrupt handler for it?  If not that could be causing the unexpected reset.

    Maybe you could post more code.  I am intrigued by the "case 13:" I saw, because it is strange.  I'm also concerned that if one piece of your code is not interrupt safe then other pieces might also not be safe.

    Jeff

  • I tried to do the same thing without enabling the lcd display. And it works, so I think the problem is a conflict between the lcd and the timer. Maybe as you said there is an interrupt routine that make some problems.

    Thank you so much.

    Best regards

    Nicola Degani

**Attention** This is a public forum