Other Parts Discussed in Thread: CC430F6137
Having trouble using the 26MHz crystal (XT2) to drive the UART clock on a cc430F6137. My code below shouldn't work but it does... 90% of the time. It is supposed to work from XT2 with a DIV_2, BR0=32 and BR1=3. There's some background below if needed. Thanks!!$%^@$!@#!#
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
PMAPPWD = 0x02D52; // Get write-access to port mapping regs
P2MAP6 = PM_UCA0RXD; // Map UCA0RXD output to P2.6
P2MAP7 = PM_UCA0TXD; // Map UCA0TXD output to P2.7
PMAPPWD = 0; // Lock port mapping registers
P2DIR |= BIT7; // Set P2.7 as TX output
P2SEL |= BIT6 + BIT7; // Select P2.6 & P2.7 to UART function
UCSCTL4 |= SELS__XT2CLK; // SMCLK=XT2
UCSCTL6 &= ~XT2OFF; // Enable XT2 (26MHz)
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL_2; // SMCLK (Hmmm, maybe should be before the reset?)
UCSCTL5 |= DIVS_2; // /2
UCA0BR0 = 0x08; // Why does this work with MIDI at all???
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
while (1) // just sending MIDI commands to play a note
{
while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 0x91; //
while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 0x3C; // send 1010 1010 data
while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 0x7F; // send 1010 1010 data
P1OUT ^= BIT0; // Toggle LED1
__delay_cycles(5000); // pause between transmissions
}
}
Background:
My "clock" crystal is not quite accurate enough for MIDI specs (31.25K +/- 1%) so the DCO/FLL is not an option over the operating range. This MIDI frequency is a common problem in the music industry trying to get 31,250 Hz off a 32,786 crystal (used for time). The above code is picking it up about 90% of the time. I suspect I'm using a different clock like the CPU (which may be changing speeds based on shifting power modes), or just getting lucky because it's close - but no cigar (not good enough).