Hello!
I am new to the MSP430 and I was hoping someone could help me out a bit. I'm trying to connect a SPI ADC (ADS8321) to a MSP430G2553 on the LaunchPad (I really want to learn the way to set up SPI, and I have the ADC). I've been trying to go through the code examples available to learn about SPI and UART, and I'm trying to put two of them together to allow UART to communicate on USCIA0 and SPI with the ADC on USCIB0. The LED on P1.0 is blinking sporadically and as the input changes, which leads me to believe that something is working. My UART_tx() function also gives me a signel "y" character with an umlaut on it. Again, I'm new to this and any suggestions would be helpful and appreciated!
#include <msp430.h> unsigned char MST_Data, SLV_Data, txData; void UART_tx(unsigned char byte); int main(void) { volatile unsigned int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer //Pin Setup P1OUT = BIT1; // P1 setup for LED & reset output P1DIR |= BIT1 + BIT0; //P1.1 for UART_Tx, BIT0 for LED and BIT4 for P1SEL = BIT1 + BIT5 + BIT6 + BIT7; P1SEL2 = BIT5 + BIT6 + BIT7; //Setup SPI on USCI_B0 UCB0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master UCB0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 |= 0x02; // /2 UCA0BR1 = 0; // UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** IE2 |= UCB0RXIE; // Enable USCI0 RX interrupt //Setup UART on USCI_A0 UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 104; // 1MHz/9600 = 104.., lower 8 bit (UCA0BR = 16bit long) UCA0BR1 = 0; // 1MHz/9600 = 104.., higher 8 bit UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1 TA0CCTL0 = OUT; //Timer for transmit UART operation TA0CCTL1 = SCS + CM1 + CAP; //Set TXD Idle as Mark = '1' TA0CTL = TASSEL_2 + MC_2; //SMCLK, start in continuous mode P1OUT &= ~BIT7; // Now with SPI signals initialized, P1OUT |= BIT7; // reset slave __delay_cycles(75); // Wait for slave to initialize MST_Data = 0x01; // Initialize data values SLV_Data = 0x00; UCB0TXBUF = MST_Data; // Transmit first character __bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts UART_tx(MST_Data); } //UART Transmit void UART_tx(unsigned char byte){ //Outputs one byte using the Timer_A UART while(TACCTL0 & CCIE); //Ensure last char got TX'd TA0CCR0 = TAR; //Current state of the TA counter TA0CCR0 += 104; //One bit time till first bit txData = byte; //Load transmit data, e.g. 'A' = 01000001 txData |= 0x100; //Add mark stop bit, e.g. 101000001 txData <<= 1; //Add space start bit, e.g. 1010000010 TA0CCTL0 = OUTMOD0 + CCIE; //Set TXD on, enable counter interrupt } // Test for valid RX and TX character #pragma vector=USCIAB0RX_VECTOR __interrupt void USCIA0RX_ISR(void) { volatile unsigned int i; while (!(IFG2 & UCA0TXIFG)); // USCI_B0 TX buffer ready? if (UCB0RXBUF == SLV_Data) // Test for correct character RX'd P1OUT |= BIT0; // If correct, light LED else P1OUT &= ~BIT0; // If incorrect, clear LED MST_Data = UCB0RXBUF; UCB0TXBUF = MST_Data; // Send next value __delay_cycles(50); // Add time between transmissions to } // make sure slave can keep up