代码如下, 不知道是什么问题就是出不来,SPI用的430内部的SPI功能,求指导
#include "msp430x14x.h"
//nRF24L01的数据宽度,地址宽度,以及数据定义
#define TX_ADR_WIDTH 5
#define RX_ADR_WIDTH 5
#define RX_PLOAD_WIDTH 5
#define TX_PLOAD_WIDTH 5
unsigned char TX_ADDRESS[]={0x35,0x43,0x10,0x10,0x03};
unsigned char RX_ADDRESS[RX_ADR_WIDTH]={0xef,0xef,0xef,0xef,0xef};
unsigned char tx[5] ={0x01,0x02,0x03,0x04,0x05};
unsigned char rx_buf[5];
#define CSN_HIGH P2OUT |=BIT3//P3.1控制SPI使能
#define CSN_LOW P2OUT &=~BIT3
#define CE_HIGH P2OUT |=BIT2//P3.2控制芯片发射使能
#define CE_LOW P2OUT &=~BIT2
#define CONFIG 0X00 //配置寄存器地址
#define EN_AA 0X01 //自动应答寄存器地址
#define EN_RXADDR 0X02 //接收地址使能
#define SETUP_AW 0X03 //设置地址宽度
#define SETUP_RETR 0X04 //建立自动重发
#define RF_CH 0X05 //射频通道
#define RF_SETUP 0X06 //射频寄存器
#define STATUS 0X07 //状态寄存器
#define OBSERVE_TX 0X08 //发送检测寄存器
#define CD 0X09 //载波检测
#define RX_ADDR_P0 0X0A //数据通道0接收地址
#define RX_ADDR_P1 0X0B
#define RX_ADDR_P2 0X0C
#define RX_ADDR_P3 0X0D
#define RX_ADDR_P4 0X0E
#define RX_ADDR_P5 0X0F
#define TX_ADDR 0X10 //发送地址寄存器地址
#define RX_PW_P0 0X11 //接收地址通道0有效数据宽度
#define RX_PW_P1 0X12
#define RX_PW_P2 0X13
#define RX_PW_P3 0X14
#define RX_PW_P4 0X15
#define RX_PW_P5 0X16
#define FIFO_STATUS 0X17//FIFO状态寄存器
//SPI命令字
#define READ_REG 0X00//读寄存器命令
#define WRITE_REG 0X20//写寄存器命令
#define RD_RX_PLOAD 0X61//读有效数据命令
#define WR_TX_PLOAD 0XA0//写有效数据命令
#define FLUSH_TX 0XE1//清除TX_FIFO应用于发射模式
#define FLUSH_RX 0XE2//清除RX_FIFO应用于接收模式
#define REUSE_TX_PL 0XE3//重新使用上一包有效数据
// 430 SPI
#define Slave_M U0CTL &=~MM
#define Master_M U0CTL |=MM
#define Set_9600 {U0BR0 =0X03; U0BR1=0X00;}
#define Set_4800 {U0BR0 =0X06; U0BR1=0X00;}
// 430 SPI
#define Slave_M U0CTL &=~MM
#define Master_M U0CTL |=MM
#define Set_9600 {U0BR0 =0X03; U0BR1=0X00;}
#define Set_4800 {U0BR0 =0X06; U0BR1=0X00;}
void Clock_Init(void);
void SPI_Init(void);
void NRF24l01_Init(void);
void Master_Tx(unsigned char t_data);
unsigned char SPI_Read(unsigned char reg);
unsigned char SPI_RW_Reg(unsigned char reg,unsigned char value);
unsigned char SPI_RW(unsigned char byte);
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes);
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes);
unsigned char NRF24L01_Check(void);
void RX_Mode(unsigned char *rx_buf);
void TX_Mode(unsigned char *tx_buf);
unsigned char kai;
unsigned char sta;
unsigned char kai;
void main( void )
{
WDTCTL = WDTPW + WDTHOLD;
Clock_Init();
SPI_Init();
NRF24l01_Init();
//kai =NRF24L01_Check();
while(1)
{
TX_Mode(tx);
kai =SPI_Read(STATUS);
__delay_cycles(100);
if(kai==0x2e)
{
__delay_cycles(1000);
//__delay_cycles(1000);
}
}
}
unsigned char NRF24L01_Check(void)
{
unsigned char buf[5]={0XA5,0XA5,0XA5,0XA5,0xA5};
unsigned char i;
SPI_Write_Buf(WRITE_REG+TX_ADDR,buf,5);//写入5个字节的地址.
SPI_Read_Buf(TX_ADDR,buf,5); //读出写入的地址
for(i=0;i<5;i++)if(buf
!=0XA5)break;
if(i!=5)return 1; //检测24L01错误
return 0; //检测到24L01
}
void Clock_Init(void)
{
BCSCTL1 &=~XT2OFF;
//BCSCTL1 |=XTS;
do
{
IFG1 &=~OFIFG;
__delay_cycles(256);
}
while((IFG1 & OFIFG)==OFIFG);
BCSCTL2 |=SELM_2+DIVM_3;
}
void NRF24l01_Init(void)
{
CE_LOW;
CSN_HIGH;
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
//SPI_RW_Reg(WRITE_REG +SETUP_AW,0X02);
SPI_RW_Reg(WRITE_REG + RF_CH, 0); // Select RF channel 40
//SPI_RW_Reg(WRITE_REG +SETUP_RETR,0X00);
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
//SPI_RW_Reg(WRITE_REG + CONFIG, 0x0F); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
CE_HIGH;
}
void SPI_Init(void)
{
U0CTL =SWRST;
ME1 |= USPIE0; // Enable USART0 SPI mode
U0CTL |=SYNC+CHAR+MM; //SPI 8bit_data
U0TCTL |=CKPL+SSEL0+STC; //ACLK 3-pin mode CKPH+
U0MCTL =0X000; // no modulation
Set_9600;
U0CTL &= ~SWRST; // Initialize USART state machine
P3SEL |=BIT1+BIT2+BIT3; //SET I/O for SPI
P2DIR |= BIT3 +BIT2;
}
unsigned char SPI_Read(unsigned char reg)
{
unsigned char r_data;
CSN_LOW;
SPI_RW(reg);
r_data =SPI_RW(0);
CSN_HIGH;
return r_data;
}
unsigned char SPI_RW(unsigned char byte)
{
while (!(IFG1 & UTXIFG0));
U0TXBUF =byte;
__delay_cycles(200);
return U0RXBUF;
}
unsigned char SPI_RW_Reg(unsigned char reg,unsigned char value)
{
unsigned char status;
CSN_LOW;
status =SPI_RW(reg);
SPI_RW(value);
CSN_HIGH;
return status;
}
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
unsigned char status,byte_ctr;
CSN_LOW;
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
{
SPI_RW(*pBuf++);
}
CSN_HIGH;
return(status); // return nRF24L01 status byte
}
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
unsigned char status,byte_ctr;
CSN_LOW;
status =SPI_RW(reg);
for(byte_ctr =0;byte_ctr<bytes;byte_ctr++)
{
pBuf[byte_ctr] =SPI_RW(0);
}
CSN_HIGH;
return (status);
}
void RX_Mode(unsigned char *rx_buf)
{
unsigned char sta;
CE_LOW;
//SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
//SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
//SPI_Read_Buf(RX_ADDR_P0,rx_buf,TX_ADR_WIDTH);
SPI_RW_Reg(WRITE_REG+CONFIG,0X0F);
CE_HIGH;
__delay_cycles(140);
sta =SPI_Read(STATUS);
if(sta & 0x40)
{
CE_LOW;
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,RX_PLOAD_WIDTH);
}
SPI_RW_Reg(WRITE_REG +STATUS,sta);
}
void TX_Mode(unsigned char *tx_buf)
{
CE_LOW;
//SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
//SPI_Read_Buf(RX_ADDR_P0,rx_buf,TX_ADR_WIDTH);
//SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
__delay_cycles(2000);
CE_HIGH;
}
此帖出自小平头技术问答
一周热门 更多>