無線收發(fā) 24L01程序
#include
#define uint unsigned int
#define uchar unsigned char
//nRF24L01的數(shù)據(jù)寬度,地址寬度,以及數(shù)據(jù)定義
#define TX_ADR_WIDTH 4
#define RX_PLOAD_WIDTH 4
#define TX_PLOAD_WIDTH 4
uchar TX_ADDRESS[]={0xe7,0xe7,0xe7,0xe7};
uchar da
uchar rx_buf[4];
//引腳定義2274
#define CSN_HIGH P3OUT|=BIT1//P3.1控制SPI使能
#define CSN_LOW P3OUT&=~BIT1
#define CE_HIGH P3OUT|=BIT2//P3.2控制芯片發(fā)射使能
#define CE_LOW P3OUT&=~BIT2
#define IRQ P3IN&BIT3//讀取P3.3的值*/
/***********模擬SPI方式***********/
#define MOSI_HIGH P3OUT|=BIT4
#define MOSI_LOW P3OUT&=~BIT4
#define MISO P3IN&BIT5
#define SCK_HIGH P3OUT|=BIT0
#define SCK_LOW P3OUT&=~BIT0
//24L01寄存器地址
#define CONFIG 0X00//配置寄存器地址
#define EN_AA 0X01//自動應(yīng)答寄存器地址
#define EN_RXADDR 0X02//接收地址使能
#define SETUP_AW 0X03//設(shè)置地址寬度
#define SETUP_RETR 0X04//建立自動重發(fā)
#define RF_CH 0X05//射頻通道
#define RF_SETUP 0X06//射頻寄存器
#define STATUS 0X07//狀態(tài)寄存器
#define OBSERVE_TX 0X08//發(fā)送檢測寄存器
#define CD 0X09//載波檢測
#define RX_ADDR_P0 0X0A//數(shù)據(jù)通道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//發(fā)送地址寄存器地址
#define RX_PW_P0 0X11//接收地址通道0有效數(shù)據(jù)寬度
#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狀態(tài)寄存器
//SPI命令字
#define READ_REG 0X00//讀寄存器命令
#define WRITE_REG 0X20//寫寄存器命令
#define RD_RX_PLOAD 0X61//讀有效數(shù)據(jù)命令
#define WR_TX_PLOAD 0XA0//寫有效數(shù)據(jù)命令
#define FLUSH_TX 0XE1//清除TX_FIFO應(yīng)用于發(fā)射模式
#define FLUSH_RX 0XE2//清除RX_FIFO應(yīng)用于接收模式
#define REUSE_TX_PL 0XE3//重新使用上一包有效數(shù)據(jù)
#define NOP 0XFF//空操作指令
//延時子函數(shù)us
void delay_us(uint i)
{
while(i--)
_NOP();
}
/*//SPI初始化程序2274
void SPI_INIT()
{
UCA0CTL0 |=UCMSB + UCMST + UCSYNC;// 3-pin, 8-bit SPI master
UCA0CTL1 |= UCSSEL_1; // ACLK
UCA0BR0 |= 0x03;//波特率9600
UCA0BR1 = 0;
UCA0MCTL = 0X91;
UCA0CTL1 &= ~UCSWRST;
P3SEL|=BIT0+BIT4+BIT5;
}
//正宗SPI寫一字節(jié)數(shù)據(jù)到24L01,同時返回一個自己的數(shù)據(jù)2274
uchar SPI_RW(uchar byte)
{
while (!(IFG2 & UCA0TXIFG));
UCA0TXBUF=byte;
delay_us(200);
return UCA0RXBUF;
}*/
//模擬SPI方式
uchar SPI_RW(uchar byte)
{
uchar bit_ctr;
uchar a,b;
for(bit_ctr=0;bit_ctr<8;bit_ctr++)
{
a=(byte&0x80);
if(a!=0)
MOSI_HIGH;
else
MOSI_LOW;
byte=(byte<<1);
SCK_HIGH;
b="P3IN"&BIT5;
if(b!=0)
byte+=1;
else;
SCK_LOW;
}
return byte;
}
//向寄存器寫一字節(jié)的數(shù)據(jù),同時返回狀態(tài)字
uchar SPI_RW_Reg(uchar reg,uchar value)
{
uchar status;
CSN_LOW;
status="SPI"_RW(reg);
SPI_RW(value);
CSN_HIGH;
return(status);
}
//向寄存器讀出一字節(jié)的數(shù)據(jù)
uchar SPI_Read(uchar reg)
{
uchar byte;
CSN_LOW;
SPI_RW(reg);
byte="SPI"_RW(0x00);//寫入一個0x00,讀出數(shù)據(jù)
CSN_HIGH;
return byte;
}
//讀出bytes字節(jié)的數(shù)據(jù)
uchar SPI_Read_Buf(uchar reg,uchar *pBuf,uchar bytes)
{
uchar status,byte_ctr;
CSN_LOW;
status="SPI"_RW(reg);//選擇寄存器,并返回狀態(tài)
for(byte_ctr=0;byte_ctr
CSN_HIGH;
return(status);
}
//寫入bytes字節(jié)的數(shù)據(jù)
uchar SPI_RW_Buf(uchar reg,uchar *pBuf,uchar bytes)
{
uchar status,byte_ctr;
CSN_LOW;
status="SPI"_RW(reg);
for(byte_ctr=0;byte_ctr
CSN_HIGH;
return(status);
}
//接收函數(shù),接收返回1表示有數(shù)據(jù)收到
uchar nRF24L01_RxPacket(uchar *rx_buf)
{
uchar sta;
uchar revale="0";
SPI_RW_Reg(WRITE_REG+CONFIG,0X0F);
CE_HIGH;
delay_us(130);
sta="SPI"_Read(READ_REG+STATUS);
while(sta&0x40)
{
CE_LOW;//進入閑置模式
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);/*數(shù)據(jù)寬度未定義*/
revale="0xff";//如果有數(shù)據(jù)收到,則點亮led
}
SPI_RW_Reg(WRITE_REG+STATUS,sta);
return revale;
}
//發(fā)送函數(shù)
void nRF24L01_TxPacket(uchar *tx_buf)
{
//stand by 模式
CE_LOW;
//給發(fā)送寄存器寫入地址,寬度為TX_ADR_WIDTH
SPI_RW_Buf(WRITE_REG+TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);
//給接收寄存器寫入地址,寬度也為TX_ADR_WIDTH
SPI_RW_Buf(WRITE_REG+RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);
SPI_Read_Buf(RX_ADDR_P0,rx_buf,TX_ADR_WIDTH);
//向發(fā)送寄存器寫入TX_PLOAD_WIDTH寬度的數(shù)據(jù),
SPI_RW_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
//配置為PWR_UP位,使能CRC,16位校驗,發(fā)送模式
SPI_RW_Reg(WRITE_REG+CONFIG,0X0E);
CE_HIGH;
delay_us(10);
CE_LOW;
}
//nRF24L01的配置函數(shù)
void nRF24L01_Config()
{
CE_LOW;//芯片使能
CSN_HIGH;//SPI復(fù)位
//使能接收模式
SPI_RW_Reg(WRITE_REG+CONFIG,0X0F);
SPI_Read(CONFIG);
//數(shù)據(jù)通道0自動應(yīng)答
SPI_RW_Reg(WRITE_REG+EN_AA,0X01);
SPI_Read(EN_AA);
//通道0允許
SPI_RW_Reg(WRITE_REG+EN_RXADDR,0X01);
//設(shè)置地址寬度為4字節(jié)
SPI_RW_Reg(WRITE_REG+SETUP_AW,0X02);
//建立自動重發(fā),500+86us,10次重發(fā)
SPI_RW_Reg(WRITE_REG+SETUP_RETR,0X1A);
//設(shè)置工作通道頻率
SPI_RW_Reg(WRITE_REG+RF_CH,0x02);
//設(shè)置工作通道傳輸速率為1Mbps,發(fā)射功率為0dBm
SPI_RW_Reg(WRITE_REG+RF_SETUP,0X03);
//設(shè)置通道0有效數(shù)據(jù)寬度RX_PLOAD_WIDTH
SPI_RW_Reg(WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);
}
void IO_INIT()
{
P3DIR|=BIT0+BIT1+BIT2+BIT4;
}
void main()
{
uchar sta;
WDTCTL="WDTPW"+WDTHOLD;
IO_INIT();
// SPI_INIT();
nRF24L01_Config();
while(1)
{
nRF24L01_TxPacket(da
sta="SPI"_Read(READ_REG+STATUS);
if(sta&0x20);
SPI_RW_Reg(WRITE_REG+STATUS,sta);
delay_us(10000);
}
}
評論