I'm transitioning from PIC to MSP430 and it is painful.
I'm trying to get some I2C code working (communication with a clock), and I think I am missing something fundamental about how variables work. I'm setting up some global variables at the beginning of the code, but the values seem to be ignored later on when I try to wirte to the I2C TX buffer.That is, I can get the I2C hardware to work properly (as monitored with an oscilloscope) when I fill the TX buffer directly (e.g. "UCB0TXBUF = 0xF3"), but using a variable (e.g. "UCB0TXBUF = TXData[0]) fails to write the correct byte to the buffer. Could someone tell me why this doesn't work or what I should read that will make sense of this issue (Yes, I've done google and other searches ad nauseum). Should I be looking into pointers or volatiles (whatever those are?) Thanks for any help.
#include "msp430fr5738.h"
unsigned int TXData[]= {0xF5,0x00,0xE0,0x8F};// Pointer to TX data
unsigned char cntr = 0;
unsigned char TXByteCtr;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
// Init SMCLK = MCLk = ACLK = 1MHz
CSCTL0_H = 0xA5;
CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting = 8MHz
CSCTL2 = SELA_3 + SELS_3 + SELM_3; // set ACLK = MCLK = DCO
CSCTL3 = DIVA_3 + DIVS_3 + DIVM_3; // set all dividers to 1MHz
// Configure Pins for I2C
P1SEL1 |= BIT6 + BIT7; // Pin init
UCB0CTLW0 |= UCSWRST; // put eUSCI_B in reset state
UCB0CTLW0 |= UCMODE_3 + UCMST + UCSSEL_2;// I2C master mode, SMCLk
UCB0BRW = 0x8; // baudrate = SMCLK / 8
UCB0CTLW1 |= UCASTP_2; // generates an automatic stop condition after all bytes are sent
UCB0TBCNT = 0x04; // number of bytes to send
UCB0I2CSA = 0x68; // slave address
UCB0CTLW0 &=~ UCSWRST; //clear reset register
UCB0IE |= UCTXIE0 + UCNACKIE; //transmit and NACK interrupt enable
while (UCB0CTLW0 & UCTXSTP); // Ensure stop condition got sent
UCB0CTLW0 |= UCTR + UCTXSTT; // I2C TX, start condition
_bis_SR_register(GIE);
// __bis_SR_register(LPM0_bits + GIE) ; // LPM3 + Enable interrupt
// __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
#pragma vector = USCI_B0_VECTOR
__interrupt void USCIB0_ISR(void)
{
switch(__even_in_range(UCB0IV,0x1E))
{
case 0x00: break; // Vector 0: No interrupts break;
case 0x02: break;
case 0x04:
UCB0CTLW0 |= UCTXSTT; //resend start if NACK
break; // Vector 4: NACKIFG break;
case 0x18:
UCB0TXBUF = TXData[cntr];
cntr++ ; //increment text counter
break; // Vector 26: TXIFG0 break;
default: break;
}
}