Jump to content
Sign in to follow this  
micker

Launchpad RTC lags behind

Recommended Posts

I'm trying to make a simple clock using the launchpad with the 32kHz crystal included.

 

I'm using the MSP430G2452 to drive a simple LED binary display.

 

My problem is the clock lags behind, a couple of minutes every hour, which I think is pretty substantial.

 

Here's my code:

 

#include 
#include 

#define ROWH   BIT0
#define ROWM   BIT1

#define BTN0   BIT3;

int seconds = 0;
int minutes = 2;
int hours = 12;

void display(void);
void increaseTime(void);


int main(void) {
  WDTCTL = WDTPW + WDTHOLD; //Stop WDT

  /* -- Timer interrupt -- */
  BCSCTL3 |= LFXT1S_0 | XCAP_3; //Set ACLK to use external clk + 12.5pF cap
  TACTL = TASSEL_1 | MC_1;
  TACCTL0 = CCIE; //Enable the interrupt for TACCR0 match
  TACCR0 = 32767;

  /* -- Ports -- */
  P1OUT = 0;
  P1DIR |= ~BTN0;
  P2OUT = 0;
  P2DIR = 0xFF; // P2 is all out

  /* -- Buttons interrupt -- */
  P1IES |= BIT3;    // high -> low is selected with IES.x = 1.
  P1IFG &= ~BIT3;   // To prevent an immediate interrupt, clear the flag for P1.3 before enabling the interrupt.
  P1IE |= BIT3;     // Enable interrupts for P1.3

  /* -- LPM + Global interrupts -- */
  eint();
  LPM3;

  /* -- main loop -- */
  while(1){
     display();
     LPM3;
  }
}


interrupt(TIMERA0_VECTOR) TIMER_ISR(void) {
  increaseTime();
}

interrupt(PORT1_VECTOR) P1_ISR(void) {
  switch(P1IFG&BIT3) {
     case BIT3:
        P1IFG &= ~BIT3;
        LPM3_EXIT;
        return;
     default:
        P1IFG = 0;
        return;
  }
}


void increaseTime(void){
  seconds += 1;
  if(seconds > 60) {
     seconds = 0;
     minutes++;
     if(minutes == 61) {
        minutes = 0;
        hours++;
        if(hours == 24) hours = 0;
     }
  }
}

void display(void){
  int i;
  int j=0;

  while(j<4000){
     for(i=0; i<200; i++){P2OUT = 0;}
     P1OUT = ~ROWH;
     P2OUT = hours;
     for(i=0; i<200; i++){P2OUT = 0;}
     P1OUT = ~ROWM;
     P2OUT = minutes;

     j++;
   }
   P2OUT = 0;
}

 

Whatever you do in the timer interrupt function, the timer rolls over and keeps running right? Or does the timer hold while the interrupt is being executed?

 

I guess I could compensate for the lag by adding a few seconds every so often, but I'd find out if there's something wrong with my code first. I also tried this on 2 different launchpads using 2 different MSP430G2452. I'm using msp430-gcc 3.2.3 and MSPDebug version 0.15.

 

I have also tried using the wacthdog as a timer using:

WDTCTL = WDT_ADLY_1000 + WDTPW;
IE1 |= WDTIE;

With the same result, which leads me to believe this has something to do with the crystal/caps or the way I use the internal caps.

Share this post


Link to post
Share on other sites

I tried it without the cap option, to no avail however. And the crystal should have 12.5pF caps, as both stated on the packaging and the wiki site.

 

And as far as I can see, both SugarAddicts code and the TI RTC library does the same thing I do. I tried SugarAddicts method, with the same result, but I can't really try TI's since I'm using msp430-gcc.

 

I'm thinking it is a hardware problem with the g2452.. I'll check using different devices, or maybe adding some external caps.

Share this post


Link to post
Share on other sites

I swapped out the 2231 for a 2452 on one of my clocks and it is still working perfectly. Try putting another chip ni that launchpad and verify the crystal is not bad. If the crystal isn't bad, try a different 2452 and see if your chip is bad. If your using the same setup then there may be something different in there. You are using the crystal that came with the launchpad, yes?

Share this post


Link to post
Share on other sites

From a quick glance at your code, it looks like in your increaseTime function you're adding an extra second and an extra minute each time. It should be

 

if(seconds > 59) ...
 if(minutes == 60)

Share this post


Link to post
Share on other sites

I Think that did it. :oops:

 

I can't believe it was something that easy (slash stupid). But when you mess around with new stuff I forget the basics of everything else I guess.

 

Thanks a lot for looking through the code.

(That must have felt good :P)

Share this post


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.

Sign in to follow this  

×
×
  • Create New...