Jump to content
43oh

Am I using the crystal?


Recommended Posts

I have written some code and it is not working as I expected. It is trouble with timers and clocks. I working towards building a little analog clock using the msp430 as a motor driver. The code I have here is sort of a small first step.

 

I am using the Launchpad platform and gcc. The crystal that came with the launchpad has been soldered on and I used some test code found at Justin's Tech to test the crystal and it is working (the led blinked of and on).

 

It is supposed to blink the led on P1.6 every 10 seconds and when the user defined button on the Launchpad is pressed reset Timer A and start counting again. It works ok but but is running about a second fast every couple hours. I am sort of at a loss as to why.

 

Here is the line of code that I am using to set BCSCTL3. Am I doing this rightt?

BCSCTL3 |= XT2S_0 + LFXT1S_0 +XCAP_3;  // clock range + 32768 crystal + xtal has 12.5 pF caps

 

Here is the whole listing:

#include 
#include 

#define BUTTON BIT3
#define TRIGGER 32764 // ACLK clock freq.

int count = 0;  // keep track of number of seconds elapsed

interrupt(PORT1_VECTOR) PORT_1(void) {
 P1IFG &= ~BUTTON;     // clear interrupt flag
 P1OUT ^= BIT0;        // toggle LED on P1.0
 TACTL |= TACLR;       // clear counter to start from 0
 count = 0;
}

interrupt (TIMERA0_VECTOR) ta_handler() {
 if (count == 4) {       // count to 5 (* 2 sec)
   P1OUT ^= BIT6 ;       // toggle LED on P1.0
   count = 0;
 }
 else {count++;}
}

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

 P1OUT = 0;                         // initialize LED off
 P1DIR = BIT0 + BIT6;               // P1.0 output

 // configure button
 P1REN |= BUTTON;     // enable internal pullup
 P1OUT |= BUTTON;     // set pullup
 P1IES |= BUTTON;     // high to low
 P1IFG &= ~BUTTON;    // clear interrupt flag
 P1IE  |= BUTTON;     // enable interrupt

 // configure clock
 BCSCTL3 |= XT2S_0 + LFXT1S_0 +XCAP_3;  // clock range + 32768 crystal + xtal has 12.5 pF caps
 TACCR0 = TRIGGER;                      // count to
 TACCTL0 |= CCIE;                       // Enable timer A intetrupt
 TACTL = TASSEL_1 + ID_1+ MC_1 + TACLR; // ACLK + Div 2 + Up Mode + Clear timer

 P1OUT ^= BIT0;                     // toggle LED on P1.0

 __bis_SR_register(LPM0_bits + GIE); // enable global interrupts & go into low-power mode

 for (; {
 }
} // main

 

Thank you for any help.

Gary

Link to post
Share on other sites

It is supposed to blink the led on P1.6 every 10 seconds and when the user defined button on the Launchpad is pressed reset Timer A and start counting again. It works ok but but is running about a second fast every couple hours. I am sort of at a loss as to why.

 

Crystals are not a perfect time source. Every crystal will have a slightly different frequency and the frequency is dependent on things like loading capacitance (which includes physical circuit capacitance) and temperature. Experiment with different loading caps (internal or external) as well as temperature (place it in a refrigerator or cold outside and wrapped in a heating pad) to see how the oscillation frequency changes.

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.

×
×
  • Create New...