代码一:
1 //****************************************************************************** 2 //Description:SPI slave talks to SPI master using 3-wire mode. Data is sent 3 //to the master starting at 0x00 and increments. Received data from the 4 //master is expected to start at 0xff and decrements with each transmission. 5 //****************************************************************************** 6 #include <msp430x14x.h> 7 char MST_data = 0x00,SLV_data = 0XFF; 8 void main() 9 { 10 WDTCTL = WDTPW + WDTHOLD; 11 12 P6DIR |= BIT0; 13 P6OUT |= BIT0; 14 P3SEL = 0X0E; //P3.1,2,3 SPI option select 15 P3DIR |= BIT1; 16 P3OUT |= BIT1; 17 18 U0CTL |= SWRST + SYNC + MM +CHAR; //master spi 8-bit 19 U0TCTL |= CKPH + SSEL1 + STC; 20 U0TCTL &= ~CKPL; 21 22 U0BR0 = 0X02; //SPICLK = SMCLK/2 23 U0BR1 = 0X00; 24 U0MCTL = 0X00; 25 26 ME1 = USPIE0; 27 U0CTL &= ~SWRST; 28 IE1 |= URXIE0; //receive enable 29 _EINT(); 30 31 while(1) 32 { 33 TXBUF0 = MST_data; 34 LPM0; 35 } 36 } 37 #pragma vector=USART0RX_VECTOR 38 __interrupt void SPI_RX() 39 { 40 P6OUT ^= BIT0; 41 while((IFG1 & UTXIFG0)==0); 42 if(U0RXBUF == SLV_data) 43 { 44 SLV_data = SLV_data - 1; 45 MST_data = MST_data + 1; 46 U0TXBUF = MST_data; 47 } 48 else 49 TXBUF0 = MST_data; 50 }
代码二:
1 #include <msp430x14x.h> 2 char MST_data = 0x00,SLV_data = 0xff; 3 void main() 4 { 5 WDTCTL = WDTPW + WDTHOLD; 6 P3SEL = 0X0E; 7 UOCTL = CHAR + SYNC + SWRST; 8 U0TCTL = CKPL + SSEL1 + STC; 9 U0CLT &= ~CKPH; 10 11 ME1 |= USPIE0; 12 U0CTL &= ~SWRST; 13 IE1 |= URXIE0; 14 _EINT(); 15 while(1) 16 { 17 TXBUF0 = SLV_data; 18 LMP4; 19 } 20 } 21 #pragma vector=USART0RX_VECTOR 22 __interrupt void USART0_RX() 23 { 24 while((IFG1 & OFIFG)==0); 25 if(U0RXBUF == MST_data) 26 { 27 SLV_data = SLV_data - 1; 28 MST_data = MST_data + 1; 29 TXBUF0 = SLV_data; 30 } 31 else 32 TXBUF0 = SLV_data; 33 }
时间: 2024-11-04 13:08:00