tocs 1 Posted March 1, 2012 Share Posted March 1, 2012 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 chibiace 1 Quote Link to post Share on other sites
DanAndDusty 62 Posted March 1, 2012 Share Posted March 1, 2012 #define TRIGGER 32764 // ACLK clock freq. Only had time for a quick look (getting son ready for school)... but I spotted that line.. The fequency is 32768 and you need to set CCR0 to 32767 (because there is a tick for 0 I believe) tocs 1 Quote Link to post Share on other sites
tocs 1 Posted March 1, 2012 Author Share Posted March 1, 2012 It must be a typo or something having another set of eyes is sometimes useful. I am going to change things and check it out and let every one know. Thank you very much. Gary Quote Link to post Share on other sites
rockets4kids 204 Posted March 2, 2012 Share Posted March 2, 2012 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. tocs 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.