MSP430F149学习之路——SPI

代码一:

 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

MSP430F149学习之路——SPI的相关文章

MSP430F149学习之路——时钟1

代码一: /************************** 功能:LED每隔1秒闪烁一次 ****************************/ #include <msp430x14x.h> void main() { P6DIR = 0XFF; P6OUT = 0X00; TACCR0 = 4095; TACCTL0 = CCIE; TACTL = TASSEL_1 + ID_3 + MC_1; _EINT(); LPM3; } #pragma vector=TIMERA0_VE

MSP430F149学习之路——LED

1 #include <msp430x14x.h> 2 3 void int_clk(); 4 void delay(int i); 5 void main() 6 { 7 WDTCTL = WDTPW + WDTHOLD; 8 int_clk(); 9 P6DIR = 0XFF; 10 P6OUT = 0Xff; 11 while(1) 12 { 13 P6OUT = 0XFF; 14 delay(20000); 15 P6OUT = 0x00; 16 delay(20000); 17 }

MSP430F149学习之路——PWM信号

代码一: 1 /******************************* 2 程序功能:ACLK=32768Hz 3 PWM波 T=512/32768 占空比75% 4 *********************************/ 5 6 #include <msp430x14x.h> 7 void main() 8 { 9 WDTCTL = WDTPW + WDTHOLD; 10 TACTL = TASSEL_1 + TACLR; 11 12 CCTL0 = 0X00; 13

MSP430F149学习之路——UART

代码一: 1 #include <msp430x14x.h> 2 void int_clk() 3 { 4 BCSCTL1 &= ~XT2OFF; 5 BCSCTL2 |= SELM_2 + SELS; 6 do 7 { 8 IFG1 &= ~OFIFG; 9 for(int i=0;i<100;i++) 10 _NOP(); 11 }while((IFG1 & OFIFG)!=0); 12 IFG1 &= ~OFIFG; 13 } 14 void int

MSP430F149学习之路——按键

代码一: 1 /********************************** 2 程序功能:用按键控制LED灯熄灭 3 ***********************************/ 4 #include <msp430x14x.h> 5 6 void int_clk(); 7 void main() 8 { 9 WDTCTL = WDTPW + WDTHOLD; 10 int_clk(); 11 P1DIR = 0Xf0; 12 P1OUT = 0Xff; 13 P6DIR

MSP430F149学习之路——捕获/比较模式

1.捕获模式 1 #include <msp430x14x.h> 2 unsigned int last0=0,last1=0; 3 unsigned int k=0,j=0; 4 void mian(void) 5 { 6 WDTCTL = WDTPW + WDTHOLD; 7 TACTL = TACLR + TASSEL_2 + ID_3 + MC_3; 8 9 TACCTL0 = CM_3 + CCIS_0 + SCS + CAP + CCIE; 10 TACCTL1 = CM_3 +

MSP430F149学习之路——按键与LED

代码 1 /*********************************** 2 程序功能:用按键控制LED状态 3 缺点:按键按下后无法改变LED灯的状态,可以利用板上的复位键^~^ 4 *************************************/ 5 #include <msp430x14x.h> 6 7 int k,i=0; 8 void int_clk(); 9 void led(int n); 10 void main() 11 { 12 WDTCTL = WD

MSP430F149学习之路——AD

代码一:Timer_A触发转换 1 #include <msp430x14x.h> 2 void main() 3 { 4 WDTCTL = WDTPW + WDTHOLD; 5 P6SEL |= BIT0; 6 TACTL |= TASSEL_2 + MC_2; 7 TACCTL1 = OUTMOD_3; 8 TACCR1 = 0X0FFF; 9 ADC12CTL0 = ADC12ON + SHT0_2; 10 ADC12CTL1 = SHS_1 + SHP; 11 while(1) 12

MSP430F149学习之路——蓝牙模块

注意蓝牙模块的接法! 1 #include <msp430x14x.h> 2 char data[100]; 3 int k=0; 4 void int_clk() 5 { 6 BCSCTL1 &= ~XT2OFF; 7 BCSCTL2 |= SELM_2 + SELS; 8 do 9 { 10 IFG1 &= ~OFIFG; 11 for(int i=0;i<100;i++) 12 _NOP(); 13 }while((IFG1 & OFIFG)!=0); 14