⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fet140_spi0_slav.c

📁 msp430f14x 系列片内外设的实例代码
💻 C
字号:
//******************************************************************************
//   MSP-FET430P140 Demo - USART0 SPI 3-Wire SPI Slave
//
//   Description: SPI slave talks to SPI master using 3-wire mode. Data is sent
//   to the master starting at 0xFF and decrements. Received data from the
//   master is expected to start at 0x00 and increments with each transmission.
//   Prior to initial data exchange, master pulses RST for complete reset.
//   USART0 RX ISR is used to handle communication, CPU normally in LPM4.
//   ACLK = n/a, MCLK = SMCLK = DCO ~ 800kHz
//
//                MSP430F149
//             -----------------
//         /|\|              XIN|-  
//          | |                 |
//          | |             XOUT|-
// Master---+-|RST              |
//            |             P3.1|<- Data In (SIMO0)
//            |                 |
//            |             P3.2|-> Data Out (SOMI0)
//            |                 |
//            |             P3.3|<- Serial Clock In (UCLK)
//
//  Z.Albus
//  Texas Instruments, Inc
//  September 2003
//  Built with IAR Embedded Workbench Version: 1.26B
//  December 2003
//  Updated for IAR Embedded Workbench Version: 2.21B
//******************************************************************************
#include <msp430x14x.h>

char MST_Data = 0x00, SLV_Data = 0xFF;

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
  P3SEL = 0x0E;                         // Setup P3 for SPI mode  
  U0CTL = CHAR + SYNC + SWRST;          // 8-bit, SPI, Slave
  U0TCTL = CKPL + STC;                  // Polarity, UCLK, 3-wire
  ME1 = USPIE0;                         // Module enable
  U0CTL &= ~SWRST;                      // SPI enable
  IE1 |= URXIE0;                        // Recieve interrupt enable
  _EINT();                              // Enable interrupts
  
  while (1)
  { 
    TXBUF0 = SLV_Data;                  // Ready TXBUF0 w/ 1st character
    LPM4;                               // Enter LPM4
  }
} // End Main

#pragma vector=USART0RX_VECTOR
__interrupt void SPI0_rx (void)
{   
  while ((IFG1 & UTXIFG0) == 0);        // USART0 TX buffer ready?
  if (U0RXBUF == MST_Data)              // Test for correct character RX'd
  { 
    SLV_Data = SLV_Data -1;             // Decrement incoming data mask
    MST_Data = MST_Data +1;             // Increment out-going data
    TXBUF0 = SLV_Data;
  }
  else
    TXBUF0 = SLV_Data;                      
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -