TI中文支持网
TI专业的中文技术问题搜集分享网站

使用官方的MSP430F5529LP配置USCI_BO口的SPI通信遇到的问题

     小弟最近刚刚开始学习MSP430,购买了官方的两块MSP430F5529LP开发板,现在想完成两块板子之间的通信,连接的框图如下

两块板子之间采用三线SPI口连接,传输输出测试时采用UART接口观察,其中两块板子的USCI_AO口配置成UART,USCI_BO口配置成SPI,均配置接受中断,现在测试过程中发现以下两个问题希望可以得到解答:

1、将测试数据从MCU1的UART输入,MCU2的UART能正常接受到数据,但是同时MCU1的UART接受到0X00;

2、将测试数据从MCU2的UART输入,MCU1未能接收到任何数据

主机MCU1代码如下:

/////////////////////////master/////////////////////
#include <msp430f5529.h>
 void clock_init(void);
 void uart_init(void);
 void spi_init(void);
 void led1_init(void);
 void led2_init(void);
 _Bool  uart_state=0;//show the state of uart, if uart_state=0,no data_Bool  spi_state =0;//show the state of spi, if spi_state = 0, no data
void main(void)
{WDTCTL = WDTPW + WDTHOLD;// Stop WDTclock_init();uart_init();spi_init();led1_init();led2_init();__bis_SR_register(GIE);// interrupts enabledwhile(1){if(uart_state){while (!(UCB0IFG&UCTXIFG));// USCI_B0 TX buffer ready?UCB0TXBUF = UCA0RXBUF;uart_state=0;}if(spi_state){while (!(UCA0IFG&UCTXIFG));// USCI_A0 TX buffer ready?UCA0TXBUF = UCB0RXBUF;// TX -> RXed characterspi_state=0;}}
}
/*********************************************************************************
//初始化时钟,选用XT2作为SMCLK和MCLK的频率
***********************************************************************************/
void clock_init(void)
{P5SEL |= BIT2|BIT3; //将IO配置为XT2功能UCSCTL6 &= ~XT2OFF; //使能XT2UCSCTL4 = UCSCTL4&(~(SELA_7))|SELA_1; //先将ACLK配置为VLOCLKUCSCTL3 |= SELREF_2;//将REFCLK配置为REFCLKwhile (SFRIFG1 & OFIFG){UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);// 清除三类时钟标志位// 这里需要清除三种标志位,因为任何一种// 标志位都会将OFIFG置位SFRIFG1 &= ~OFIFG;// 清除时钟错误标志位}UCSCTL4 = UCSCTL4&(~(SELS_7|SELM_7))|SELS_5|SELM_5;//将SMCLK和MCLK时钟源配置为XT2
}

/*********************************************************************************
//初始化串口,宣统P3.3,4作为TXD/RXD,波特率为115200
*********************************************************************************/
void uart_init(void)
{P3SEL = BIT3+BIT4;// P3.3,4 = USCI_A0 TXD/RXDUCA0CTL1 |= UCSWRST;// **Put state machine in reset**UCA0CTL1 |= UCSSEL_2;// CLK = MCLKUCA0BR0 = 0x22;//UCA0BR1 = 0x00;//UCA0MCTL = UCBRS_6+UCBRF_0;// Modulation UCBRSx=3, UCBRFx=0UCA0CTL1 &= ~UCSWRST;// **Initialize USCI state machine**UCA0IE |= UCRXIE;  // Enable USCI_A0 RX interrupt  
}

/*********************************************************************************
//初始LED1,P1.0灯作为调试工具
*********************************************************************************/
void led1_init(void)
{P1DIR |= BIT0;// P1.0 set as outputP1OUT &= ~BIT0;// P1.0 set as 0
}

/*********************************************************************************
//初始LED2,P4.7灯作为调试工具
*********************************************************************************/
void led2_init(void)
{P4DIR |= BIT7;// P1.0 set as outputP4OUT &= ~BIT7;// P1.0 set as 0
}

/*********************************************************************************
//初始化SPI接口
*********************************************************************************/
void spi_init(void)
{P3SEL |= BIT0+ BIT1+BIT2;//P3.0,1,2option select,BIT2= clockUCB0CTL1 |= UCSWRST;UCB0CTL0 |= UCMST+UCSYNC+UCCKPL+UCMSB;UCB0CTL1 |= UCSSEL_2;// SMCLKUCB0BR0 = 0x22;// /2UCB0BR1 = 0x00;////UCB0MCTL = 0;// No modulationUCB0CTL1 &= ~UCSWRST;// **Initialize USCI state machine**UCB0IE |= UCRXIE;// Enable USCI_B0 RX interrupt  
}
/*********************************************************************************
//中断函数,处理串口接受的中断
*********************************************************************************/
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{switch(__even_in_range(UCA0IV,4)){case 0:break;// Vector 0 - no interruptcase 2:// Vector 2 - RXIFG//while (!(UCB0IFG&UCTXIFG));// USCI_B0 TX buffer ready?//UCB0TXBUF = UCA0RXBUF;// TX -> RXed characterif(! uart_state){uart_state = 1;}else{P1OUT |= BIT0;}break;case 4:break;// Vector 4 - TXIFGdefault: break;}
}
/*********************************************************************************
//中断函数,处理SPI接受的中断
*********************************************************************************/
#pragma vector=USCI_B0_VECTOR
__interrupt void USCI_B0_ISR(void)
{switch(__even_in_range(UCB0IV,4)){case 0:break;// Vector 0 - no interruptcase 2:// Vector 2 - RXIFGif(! spi_state){spi_state = 1;}else{P4OUT |= BIT7;}break;case 4:break;// Vector 4 - TXIFGdefault: break;}
}

从机MCU2的代码如下:

/*******************************slave*******************************************/
#include <msp430f5529.h>
 void clock_init(void);
 void uart_init(void);
 void spi_init(void);
 void led1_init(void);
 void led2_init(void);
 _Bool  uart_state=0;//show the state of uart, if uart_state=0,no data_Bool  spi_state =0;//show the state of spi, if spi_state = 0, no data
void main(void)
{WDTCTL = WDTPW + WDTHOLD;// Stop WDTclock_init();uart_init();spi_init();led1_init();led2_init();__bis_SR_register(GIE);//interrupts enabledwhile(1){if(uart_state){while (!(UCB0IFG&UCTXIFG));// USCI_B0 TX buffer ready?UCB0TXBUF = UCA0RXBUF;uart_state=0;}if(spi_state){while (!(UCA0IFG&UCTXIFG));// USCI_A0 TX buffer ready?UCA0TXBUF = UCB0RXBUF;// TX -> RXed characterspi_state =0;}}  
}
/*********************************************************************************
//初始化时钟,选用XT2作为SMCLK和MCLK的频率
***********************************************************************************/
void clock_init(void)
{P5SEL |= BIT2|BIT3; //将IO配置为XT2功能UCSCTL6 &= ~XT2OFF; //使能XT2UCSCTL4 = UCSCTL4&(~(SELA_7))|SELA_1; //先将ACLK配置为VLOCLKUCSCTL3 |= SELREF_2;//将REFCLK配置为REFCLKwhile (SFRIFG1 & OFIFG){UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);// 清除三类时钟标志位// 这里需要清除三种标志位,因为任何一种// 标志位都会将OFIFG置位SFRIFG1 &= ~OFIFG;// 清除时钟错误标志位}UCSCTL4 = UCSCTL4&(~(SELS_7|SELM_7))|SELS_5|SELM_5;//将SMCLK和MCLK时钟源配置为XT2
}

/*********************************************************************************
//初始化串口,宣统P3.3,4作为TXD/RXD,波特率为115200
*********************************************************************************/
void uart_init(void)
{P3SEL = BIT3+BIT4;// P3.3,4 = USCI_A0 TXD/RXDUCA0CTL1 |= UCSWRST;// **Put state machine in reset**UCA0CTL1 |= UCSSEL_2;// CLK = MCLKUCA0BR0 = 0x22;//UCA0BR1 = 0x00;//UCA0MCTL = UCBRS_6+UCBRF_0;// Modulation UCBRSx=3, UCBRFx=0UCA0CTL1 &= ~UCSWRST;// **Initialize USCI state machine**UCA0IE |= UCRXIE;  // Enable USCI_A0 RX interrupt  
}

/*********************************************************************************
//初始LED1,P1.0灯作为调试工具
*********************************************************************************/
void led1_init(void)
{P1DIR |= BIT0;// P1.0 set as outputP1OUT &= ~BIT0;// P1.0 set as 0
}

/*********************************************************************************
//初始LED2,P4.7灯作为调试工具
*********************************************************************************/
void led2_init(void)
{P4DIR |= BIT7;// P1.0 set as outputP4OUT &= ~BIT7;// P1.0 set as 0
}

/*********************************************************************************
//初始化SPI接口
*********************************************************************************/
void spi_init(void)
{P3SEL |= BIT0+ BIT1+BIT2;//P3.0,1,2option select,BIT2= clockUCB0CTL1 |= UCSWRST;UCB0CTL0 |= UCSYNC+UCCKPL+UCMSB;////UCB0MCTL = 0;// No modulationUCB0CTL1 &= ~UCSWRST;// **Initialize USCI state machine**UCB0IE |= UCRXIE;// Enable USCI_B0 RX interrupt  
}
/*********************************************************************************
//中断函数,处理串口接受的中断
*********************************************************************************/
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{switch(__even_in_range(UCA0IV,4)){case 0:break;// Vector 0 - no interruptcase 2:// Vector 2 - RXIFG//while (!(UCB0IFG&UCTXIFG));// USCI_B0 TX buffer ready?//UCB0TXBUF = UCA0RXBUF;// TX -> RXed characterif(! uart_state){uart_state = 1;}else{P1OUT |= BIT0;}break;case 4:break;// Vector 4 - TXIFGdefault: break;}
}
/*********************************************************************************
//中断函数,处理SPI接受的中断
*********************************************************************************/
#pragma vector=USCI_B0_VECTOR
__interrupt void USCI_B0_ISR(void)
{switch(__even_in_range(UCB0IV,4)){case 0:break;// Vector 0 - no interruptcase 2:// Vector 2 - RXIFG//while (!(UCA0IFG&UCTXIFG));// USCI_A0 TX buffer ready?//UCA0TXBUF = UCB0RXBUF;// TX -> RXed characterif(! spi_state){spi_state = 1;}else{P4OUT |= BIT7;}break;case 4:break;// Vector 4 - TXIFGdefault: break;}
}
灰小子:

硬件连接没问题吧?mcu1的simo要和mcu2的miso连接,反之类同。

另外spi通讯分主从机的,要注意。

官网有两个spi例程,可以配套使用。你可以参考下

congcong cao:

回复 灰小子:

感谢您的回复,不过我看官方手册上两个板子的SIMO和MISO分别相连,出现的问题的原因应该是当从机接收到信号后在接受主机信号的同时也会返回从机的txbuf中的信号

赞(0)
未经允许不得转载:TI中文支持网 » 使用官方的MSP430F5529LP配置USCI_BO口的SPI通信遇到的问题
分享到: 更多 (0)