[LaunchPad] 使用两块launchpad SPI传数据

luooove   2014-10-18 21:10 楼主
用两块launchpad传SPI数据,一端发送一端接收,接收一端再用串口传给电脑显示。这是项目中一小段调试的程序。 发送程序
  1. #include
unsigned char MST_Data, SLV_Data;
  • int main(void)
    {
    volatile unsigned int i;
  • WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
    P1OUT = 0x00; // P1 setup for LED & reset output
    P1DIR |= BIT0 + BIT5; //
    P1SEL = BIT1 + BIT2 + BIT4;
    P1SEL2 = BIT1 + BIT2 + BIT4;
    UCA0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
    UCA0CTL1 |= UCSSEL_2; // SMCLK
    UCA0BR0 |= 0x02; // /2
    UCA0BR1 = 0; //
    UCA0MCTL = 0; // No modulation
    UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
    IE2 |= UCA0RXIE; // Enable USCI0 RX interrupt
  • P1OUT &= ~BIT5; // Now with SPI signals initialized,
    P1OUT |= BIT5; // reset slave
  • __delay_cycles(75); // Wait for slave to initialize
  • MST_Data = 0x01; // Initialize data values
    while(1)
    {
  • UCA0TXBUF = MST_Data; // Transmit first character
    MST_Data++;
    __delay_cycles(1000);
    }
  • //__bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
    }
    接收程序 #include int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer while (!(P1IN & BIT5)); // If clock sig from mstr stays low, // it is not yet in SPI mode // P1DIR |= BIT6; // P1OUT |= BIT6; BCSCTL1 = CALBC1_16MHZ; // Set DCO DCOCTL = CALDCO_16MHZ; P1SEL = BIT1 + BIT2 + BIT5 + BIT6 + BIT7; // P1.1 = RXD, P1.2=TXD P1SEL2 = BIT1 + BIT2 + BIT5 + BIT6 + BIT7; UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 0x82; // 16MHz 9600 UCA0BRX=1666=0x0682 UCA0BR1 = 0x06; // 16MHz 9600 UCA0MCTL = UCBRS2 + UCBRS1; // Modulation UCBRSx = 6 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt UCB0CTL1 = UCSWRST; // **Put state machine in reset** UCB0CTL0 |= UCCKPL + UCMSB + UCSYNC; // 3-pin, 8-bit SPI master UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** IE2 |= UCB0RXIE; // Enable USCI0 RX interrupt __bis_SR_register(LPM4_bits + GIE); // Enter LPM4, enable interrupts } #pragma vector=USCIAB0RX_VECTOR __interrupt void USCI0RX_ISR(void) { while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready? UCA0TXBUF = UCB0RXBUF; // TX -> RXed character } 测试通过可以。

    回复评论 (2)

    红果果的串口程序。。。
    点赞  2014-10-22 13:00
    引用: 花花花2 发表于 2014-10-22 13:00
    红果果的串口程序。。。

    怎么?
    点赞  2014-10-23 19:01
    电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
      写回复