Greetings I've been trying to find a nice way to determine what CPU clock is set too in a MSP430F5342 system.
I have a 32768hz crystal (20ppm) for the RTC and I have it set up to use either the DCO (if XT2 doesn't exist) and set it too 7372800hz using the 32768 XT1 clock source.
However "in case of crystal installed" as XT2, I would like too know what to set the baud rate registers too.
I came up with "this" code however it doesn't seem to exactly come out with close to consistent (or correct) results.
uint32_t get_base_clock(void) { uint16_t temp; uint16_t old_UCSCTL4; uint32_t value; #define CYCLE_DELAY 64 #define CYCLE_LOAD 4 // get old 4 thing old_UCSCTL4 = UCSCTL4; // get the ACLK setting here temp = old_UCSCTL4 & SELA_7; // shift temp to the correct location temp >>= 8; // set CPU clock to be ACLK value UCSCTL4 = temp | (old_UCSCTL4 & ~SELM_7); // 16 bit SMCLK CONTINUOUS CLEAR /1 TB0CTL = CNTL__16|TBCLGRP_0|TASSEL__SMCLK|TBCLR | MC__CONTINUOUS|ID__1; // set input clock to SMCLK //TB0CTL = MC__UP | TASSEL__SMCLK | ID__1; __delay_cycles(CYCLE_DELAY - CYCLE_LOAD); TB0CTL = MC__STOP; // 2 // CYCLE_DELAY - CYCLE_LOAD CPU clocks have passed // get tb0r value value = TB0R; // restore the clocks to old settings UCSCTL4 = old_UCSCTL4; //value = temp; // multiply it by the REF0 clock rate /CYCLE_DELAY value *= (REFO_CLK/CYCLE_DELAY); return value; } //----------------------------------------------------------------------
Suggestions, thoughts, diatribes on bad coding practice (humor)?
SMCLK is the same as MCLK by the way in the UCS configuration thus this should work. It at least doesn't give me 0hz.
Stephen