This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553 UART + SPI simultaneously - problems

Other Parts Discussed in Thread: MSP430G2553

Dear TI community,

I've read more answers about this topic but I think none fit my problem. I'm using MSP430G2553 and trying to get working UART (UCA0) and SPI (UCB0) simultaneously. I've made initialization functions uartInit() and spiInit(). If I run only uartInit(), UART works fine - same as if i run only spiInit(), then SPI works OK.

If I run both of them, there are only some mess on MISO MOSI and SCK pins. Am I missing something?

I'm very sorry If I missed some topic with solution on this, I'm stuck here few hours. Thank you for your comments.

LJ

--------------------------- CODE ------------------------------- functions.c

void uartInit()
{
	// reset UCA0 peripheral
	UCA0CTL1 |= UCSWRST; 

	UCA0CTL1 |= 0x02;                   // SMCLK
	UCA0BR0 |= 104;                     // ~9600bd
	UCA0BR1 = 0;                         
	UCA0MCTL = UCBRS2 + UCBRS0;         // Modulation UCBRSx = 5

	P1SEL = BIT1 | BIT2;				// set P1.2 and P1.1 to UART
	P1SEL2 = BIT1 | BIT2;

	UCA0CTL1 &= ~UCSWRST;               // Initialize USCI state machine
	// UC0IE |= UCA0RXIE;               // Enable USCI_A0 RX interrupt
}

void spiInit()
{
	// reset UCB0 peripheral
	UCB0CTL1 |= UCSWRST; 

	UCB0CTL1 |= 0x02;                     // SMCLK
	UCB0CTL0 |=  UCMSB + UCMST + UCSYNC;  // 3-pin, 8-bit SPI master
	
	UCB0BR0 |= 0x02;                      // 500kHz
	UCB0BR1 = 0;                             

	P1SEL = BIT7 | BIT6 | BIT5 ;		  // set P1.5 P1.6 P1.7 modes to SPI
	P1SEL2 = BIT7 | BIT6 | BIT5 ;
	P1DIR |= BIT4; 
	P2DIR |= BIT0;						  // CS
	P2OUT |= BIT0;

	UCB0CTL1 &= ~UCSWRST;                 // Initialize USCI state machine
}

------------------------------------------ main.c

int main(void)
{
    WDTCTL = WDTPW + WDTHOLD;     
    DCOCTL = 0;                                     // Select lowest DCOx and MODx settings<
    BCSCTL1 = CALBC1_1MHZ;         // Set DCO
    DCOCTL = CALDCO_1MHZ;
 
    uartInit();
    spiInit();

..... etc

  • How do you use them in your further code?
  • main.c
    int main(void) { WDTCTL = WDTPW + WDTHOLD; DCOCTL = 0; // Select lowest DCOx and MODx settings< BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; P1DIR |= BIT0; // P1.0 as output P1OUT &= ~BIT0; // LED OFF uartInit(); spiInit(); printString("\n\rMSP430 initialized\n\r"); __delay_cycles(50); writeRegister(0x00, 0x02); while(1) { P1OUT ^= BIT0; // Blink LED __delay_cycles(100000); P1OUT ^= BIT0; // Blink LED __delay_cycles(2000000); } }

    functions.c

    void uartInit()
    {
    	// reset UCA0 peripheral
    	UCA0CTL1 |= UCSWRST; 
    
    	UCA0CTL1 |= 0x02;                                      // SMCLK
    	UCA0BR0 |= 104;                                         // ~9600bd
    	UCA0BR1 = 0;                         
    	UCA0MCTL = UCBRS2 + UCBRS0;         // Modulation UCBRSx = 5
    
    	P1SEL = BIT1 | BIT2;				        // set P1.2 and P1.1 to UART
    	P1SEL2 = BIT1 | BIT2;
    
    	UCA0CTL1 &= ~UCSWRST;                     // Initialize USCI state machine
    	// UC0IE |= UCA0RXIE;                               // Enable USCI_A0 RX interrupt
    }
    
    void spiInit()
    {
    	// reset UCB0 peripheral
    	UCB0CTL1 |= UCSWRST; 
    
    	UCB0CTL1 |= 0x02;                                                 // SMCLK
    	UCB0CTL0 |=  UCMSB + UCMST + UCSYNC;  // 3-pin, 8-bit SPI master
    	
    	UCB0BR0 |= 0x02;                                                   // 500kHz
    	UCB0BR1 = 0;                             
    
    	P1SEL = BIT7 | BIT6 | BIT5 ;		                     // set P1.5 P1.6 P1.7 modes to SPI
    	P1SEL2 = BIT7 | BIT6 | BIT5 ;
    	P1DIR |= BIT4; 
    	P2DIR |= BIT0;						            // CS
    	P2OUT |= BIT0;
    
    	UCB0CTL1 &= ~UCSWRST;                 // Initialize USCI state machine
    }
    
    void printString(char* string)
    {
    	int i = 0;
    	while(string[i] != '\0')
    	{
    		UCA0TXBUF = string[i];
    		while(!(IFG2 & UCA0TXIFG));
    		i++;
    	}	
    }
    
    uint8_t writeRegister(uint8_t address, uint8_t data)
    {
    	P2OUT &= ~BIT0;						// CS low
    	while (!(IFG2 & UCB0TXIFG));
    	UCB0TXBUF = (address & 0b00011111) | WRITE_REG;         
    	while (!(IFG2 & UCB0TXIFG));                            // USCI_B0 TX buffer ready?
    	__delay_cycles(20);
    	UCB0TXBUF = data;                     
    	while (!(IFG2 & UCB0TXIFG));                            // USCI_B0 TX buffer ready?
    	while (!(IFG2 & UCB0RXIFG));                            // USCI_B0 RX buffer ready?
    	P2OUT |= BIT0;
    	return UCB0RXBUF;
    }

     

  • Your UART init and SPI init need to set different bits in P1SEL and P1SEL2. Do not clear the other guy's bits while you are doing that.

    A lot of people use |= when they should have used =. In this case, you used = when you should have used |= .
  • OCY has an eagle eye!
  • Eagle Eye indeed! Maybe I should drink less coffee. Thank you!
  • Lukas and Dennis,

    Thanks for the complement. But I may have logs in my eye ;)

    --OCY

**Attention** This is a public forum