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

📄 main.c

📁 MSP430的SPI接口的例子程序。连接NRF24L01实现定长的数据包通信
💻 C
字号:
//******************************************************************************
//  MSP430F22x4 Demo - USCI_A0 IrDA External Loopback Test, 8MHz SMCLK
//
//  Description: This example transmits bytes through the USCI module
//  configured for IrDA mode, and receives them using an external loopback
//  connection. The transfered sequence is 00h, 01h, 02h, ..., ffh. The
//  received bytes are also stored in memory starting at address RxData.
//  In the case of an RX error the LED is lighted and program execution stops.
//  An external loopback connection has been used as it allows for the
//  connection of a scope to monitor the communication, which is not possible
//  when using the internal loopback.
//  ACLK = n/a, MCLK = SMCLK = BRCLK = CALxxx_8MHZ = 8MHz
//
//              MSP430F22x4
//            -----------------
//        /|\|              XIN|-
//         | |                 |
//         --|RST          XOUT|-
//           |                 |
//           |     P3.5/UCA0RXD|--+   external
//           |     P3.4/UCA0TXD|--+   loopback connection
//           |                 |
//           |                 |
//           |             P1.0|--->  LED
//           |                 |
//
//  A. Dannenberg
//  Texas Instruments Inc.
//  April 2006
//  Built with IAR Embedded Workbench Version: 3.41A
//******************************************************************************
#include "msp430x22x4.h"
#include "nrf24l01.h"
unsigned char RxByte;
volatile unsigned char RxData[256];
unsigned char TxByte;
volatile unsigned int i;
unsigned int MyNum = 5;//应该提供写入flash的功能,从flash导入,应该保证代码是一样的
unsigned int PacNum = 0;
#define SOF 0x00
#define EOF 0xFF
unsigned char state;//当前的状态
unsigned char error;// 错误信息,发送是查看
unsigned char thorder;//主机发来的命令
#define WST 0x00 //等待开始
#define WEN 0xFF //等待结束,关于变长的协议在考虑
#define WDA 0x99 //等待数据
#define WCR 0xCC //等待crc
#define PacLen 0x10//报文中数据包的数量
#define ERR_UST 0x01 //意外的开头
#define ERR_UEN 0x02 //意外的结尾
#define ERR_CRC 0x03 //校验错误

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  DCOCTL = CALDCO_16MHZ;                     // Load 8MHz constants
  BCSCTL1 = CALBC1_16MHZ;
  P3DIR |= 0x41;
  P3OUT |= 0x41;                             //P3.0 output direction 24L01的csn
  P3SEL |= 0x3E;                            // Use P3.4/P3.5 for USCI_A0,选了好多口
  UCA0CTL1 |= UCSWRST;                      // Set SW Reset
  UCA0CTL1 = UCSSEL_2 + UCSWRST;            // Use SMCLK, keep SW reset
  UCA0BR0 = 69;                             // 8MHz/52=153.8KHz
  UCA0BR1 = 0;
  UCA0MCTL = UCBRF_1;              // Set 1st stage modulator to 1
  UCA0IRTCTL = UCIRTXPL2 + UCIRTXPL0 + UCIRTXCLK + UCIREN;
                                            // Pulse length = 6 half clock cyc
                                            // Enable BITCLK16, IrDA enc/dec
  UCA0CTL1 &= ~UCSWRST;                     // Resume operation
  
  //以下是b口的设置
  UCB0CTL1 |= UCSWRST; 
  UCB0CTL1 |= UCSSEL_2 + UCSWRST;                     // SMCLK
  UCB0CTL0 |= UCMSB + UCMST + UCSYNC+0x02;       // 3-pin, 8-bit SPI mstr, MSB 1st//高低电平忘记了最后一个参数再调
  UCB0BR0 = 0x08;
  UCB0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  //b口设置结束
  
  
  state = WST;
  PacNum = 0;
    IE2 |= UCA0RXIE;                        // Enable RX int
    __bis_SR_register(GIE);        // Enter LPM0 w/ interrupts
 /*while (1)
  {
    while (!(IFG2 & UCB0TXIFG));            // USCI_A0 TX buffer ready?
    UCB0TXBUF = 0x55;                       // Byte to SPI TXBUF
    PacNum = 0;
  }*/
    nrf24l01_initialize_debug(false, 1, false); 
    unsigned char data[5];
    data[0] = 0x55;
    data[1] = 0x55;
    data[2] = 0x55;
    data[3] = 0x55;
    data[4] = 0x55;
    unsigned char test = 0x12;
    unsigned char send = 0x55;
    while(1)
    {
      nrf24l01_write_tx_payload(data,1,true);
    }
}

#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void)
{
  RxByte = UCA0RXBUF;                       // Get RXed character
  if (RxByte == SOF)
  {
     if(state != WST)
    {
       error = ERR_UST;
    }
    state = WDA;
    PacNum = 0;
  }
  else if(RxByte == EOF)
  {
    if (state != WEN)
    {
      error = ERR_UEN;
    }
    state = WST;
    PacNum = 0;
  }
  else
  {
    if (state == WDA)
    {
      //计算crc
      PacNum++;
      if (PacNum == MyNum)
      {
        //send thething
        thorder = RxByte;
      }
      if(PacNum == PacLen)
      {
        state = WCR;
      }
    }
    else if(state == WCR)
    {
      //校验,报错
      state = WST;
    }
    else
    {
      error = WST;//或者WEN
      PacNum = 0;
    }
  }
}

⌨️ 快捷键说明

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