nimblemotors 23 Posted December 12, 2011 Share Posted December 12, 2011 I need the mclock to be consistant for pwm and use the 32khz aclock to sync it. I've used the code below to do this. I have two issues, one is it seems I must divide Aclk by 8 so it works, otherwise I think it is too fast. My real issue is how to I keep it in sync? In my previous application, i have timer outputs that I know have dead time where I can use CCTL2 (which is only control with Aclk input) during the deadtime to sync, but in my current situation, it isn't clear when I have time to resynch the mclock, the PWM on TA2 is almost constant. Am I just going to have to find some dead time to do it? Also, wouldn't it be valid to assume it doesn't need to be adjust as long as the internal chip temp reading doesn't change too much? // // At reset we adjust DCO to 4Mhz // setdco() { unsigned short ov, lastmclk, mclk; lastmclk = mclk = mticks = 0; TACTL = TASSEL_SMCLK + TACLR + MC_CONT; CCTL2 = CAP | SCS | CCIS_1 | CM_1; // capture, CCIxB=Aclk, rising edge, no interrupts lastmclk = TAR; for(dco=0; dco<255; dco++) { DCOCTL = dco; ov = 0; // poll the capture flag waiting for the 32k clock to tick up while (1) { if ((CCTL2 & COV) == COV) { // overflow CCTL2 &= ~(COV); // clear flag ov = 1; break; } else if ((CCTL2 & CCIFG) == CCIFG) { // capture occured CCTL2 &= ~(CCIFG); // clear flag break; } } if (!ov) { mclk = CCR2; // get mclk count at capture mticks = mclk - lastmclk; lastmclk = mclk; if (mticks > 1023) break; // done } } } main() { DCOCTL = 0xE0; // DCO=7, MOD=0. BCSCTL1 = (DIVA1 | DIVA0) | 0x07; // Aclk/8 32k now 4k, RSEL=7, 4Mhz at 3v // check if we have a working 32k ACLK TACTL = TASSEL_ACLK + TACLR + MC_CONT; delay(800); // wait if (TAR > 0) { setdco(); // adjust DCO clock to 4Mhz setdco(); // adjust DCO clock to 4Mhz } else { Reboot(); // reboot } ... Quote Link to post Share on other sites
Rickta59 589 Posted December 12, 2011 Share Posted December 12, 2011 You could probably benefit from using an external clock signal instead of trying to use the DCO. Check out this great article by oPossum on how to re-purpose the 12MHz square wave coming out of the FET: http://www.43oh.com/forum/viewtopic.php?f=8&t=1335 That hack and its code snippet show you how to use an external clock signal instead of the 32.768kHz crystal. -rick 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.