现在,用两块430单片机进行SPI的通信,希望是从机发送数据,主机接收数据并返回。编写程序后发现主机没有主动产生时钟,现在用的是3线的SPI,是否需要4线的才能实现?
- //******************************************************************************
- //作为主机,接收SPI数据,并转换后发送回去,然后上传串口
- #include <msp430.h>
- unsigned char MST_Data,SLV_Data;
- void Serial_init()
- {
- P10SEL = 0x30; // P3.4,5 = USCI_A0 TXD/RXD
- UCA3CTL1 |= UCSWRST; // **Put state machine in reset**
- UCA3CTL1 |= UCSSEL_2; // SMCLK
- UCA3BR0 = 6; // 1MHz 9600 (see User's Guide)
- UCA3BR1 = 0; // 1MHz 9600
- UCA3MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0,
- // over sampling
- UCA3CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
- //UCA3IE |= UCRXIE; // Enable USCI_A0 RX interrupt
- }
- int main(void)
- {
- WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
- Serial_init();
- //P1OUT |= 0x02; // Set P1.0 for LED
- // Set P1.1 for slave reset
- // P1DIR |= 0x03; // Set P1.0-2 to output direction
- P9SEL |= 0x0E; // P3.5,4,0 option select
- UCB2CTL1 |= UCSWRST; // **Put state machine in reset**
- UCB2CTL0 |= UCMST+UCSYNC+UCCKPL+UCMSB; // 3-pin, 8-bit SPI master
- // Clock polarity high, MSB
- UCB2CTL1 |= UCSSEL_2; // SMCLK
- UCB2BR0 = 0x02; // /2
- UCB2BR1 = 0; //
- // UCA0MCTL = 0; // No modulation
- UCB2CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
- UCB2IE |= UCRXIE; // Enable USCI_A0 RX interrupt
- //P1OUT &= ~0x02; // Now with SPI signals initialized,
- // P1OUT |= 0x02; // reset slave
- __delay_cycles(100); // Wait for slave to initialize
- __bis_SR_register(GIE); // Enter LPM4, enable interrupts
- while(1);
- }
- #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
- #pragma vector=USCI_B2_VECTOR
- __interrupt void USCI_B2_ISR(void)
- #elif defined(__GNUC__)
- void __attribute__ ((interrupt(USCI_B2_VECTOR))) USCI_B2_ISR (void)
- #else
- #error Compiler not supported!
- #endif
- {
- switch(__even_in_range(UCB2IV,4))
- {
- case 0:break; // Vector 0 - no interrupt
- case 2: // Vector 2 - RXIFG
- while (!(UCA3IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
- UCA3TXBUF = 0xff-UCB2RXBUF; // TX -> RXed character
- while (!(UCB2IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
- UCB2TXBUF = 0xff-UCB2RXBUF; // TX -> RXed character
- break;
- case 4:break; // Vector 4 - TXIFG
- default: break;
- }
- }
复制代码
此帖出自
小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
不对吧,3线也可以双向通信。
一周热门 更多>