Jump to content

DCO calibration using Aclock

Recommended Posts

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
 unsigned short ov, lastmclk, mclk;

 lastmclk = mclk = mticks = 0;
 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;
     else if ((CCTL2 & CCIFG) == CCIFG) { // capture occured
CCTL2 &= ~(CCIFG);     // clear flag
   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
 delay(800);  // wait
 if (TAR > 0) {
   setdco();  // adjust DCO clock to 4Mhz
   setdco();  // adjust DCO clock to 4Mhz
 else {
   Reboot(); // reboot

Link to post
Share on other sites

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:




That hack and its code snippet show you how to use an external clock signal instead of the 32.768kHz crystal.



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.

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...