常驻嘉宾
- 积分
- 4494
- 威望
- 2681
- 贡献
- 921
- 兑换币
- 241
- 注册时间
- 2012-2-4
- 在线时间
- 446 小时
|
NRF2401.c
///////////////////////////////////////////
#include "common.h"
#include "HAL_GPIO.h"
#include "NRF2401.h"
#define WIRE PTB
#define NRF PTB
#define MOSI 1
#define MISO 2
#define SCK 3
#define SCE 4
#define CSN 5
#define IRQ 6
unsigned char TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
unsigned char RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接受地址
char sta;
void Delay(int16 s)
{
unsigned int i;
for(i=0; i<s; i++);
for(i=0; i<s; i++);
}
void inerDelay_us(uint8 n)
{
for(;n>0;n--);
}
/****************************************************************************************************
/*函数:uint SPI_RW(uint uchar)
/*功能:NRF24L01的SPI写时序
/****************************************************************************************************/
uint8 SPI_RW(uint16 unchar)
{
uint16 bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
if(unchar & 0x80)//output 'uchar', MSB to MOSI
{
LPLD_GPIO_Set_b(NRF,MOSI,1);
}
else
{
LPLD_GPIO_Set_b(NRF,MOSI,0);
}
unchar = (unchar << 1); // shift next bit into MSB..
LPLD_GPIO_Set_b(NRF,SCK,1); // Set SCK high..
unchar |= LPLD_GPIO_Get_b(NRF,MISO); // capture current MISO bit
LPLD_GPIO_Set_b(NRF,SCK,0); // ..then set SCK low again
}
return(unchar); // return read uchar
}
/****************************************************************************************************
/*函数:uchar SPI_Read(uchar reg)
/*功能:NRF24L01的SPI时序
/****************************************************************************************************/
uint8 SPI_Read(uint8 reg)
{
uint8 reg_val;
LPLD_GPIO_Set_b(NRF,CSN,0); // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
LPLD_GPIO_Set_b(NRF,CSN,1); // CSN high, terminate SPI communication
return(reg_val); // return register value
}
/****************************************************************************************************/
/*功能:NRF24L01读写寄存器函数
/****************************************************************************************************/
uint16 SPI_RW_Reg(uint8 reg, uint16 value)
{
uint16 status;
LPLD_GPIO_Set_b(NRF,CSN,0); // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
LPLD_GPIO_Set_b(NRF,CSN,1); // CSN high again
return(status); // return nRF24L01 status uchar
}
/****************************************************************************************************/
/*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
/*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
/****************************************************************************************************/
uint16 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 uchars)
{
uint16 status,uchar_ctr;
LPLD_GPIO_Set_b(NRF,CSN,0); // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status uchar
for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)
pBuf[uchar_ctr] = SPI_RW(0); //
LPLD_GPIO_Set_b(NRF,CSN,1);
return(status); // return nRF24L01 status uchar
}
/*********************************************************************************************************
/*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
/*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
/*********************************************************************************************************/
uint16 SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars)
{
uint16 status,uchar_ctr;
LPLD_GPIO_Set_b(NRF,CSN,0); //SPI??
status = SPI_RW(reg);
for(uchar_ctr=0; uchar_ctr<uchars; uchar_ctr++) //
SPI_RW(*pBuf++);
LPLD_GPIO_Set_b(NRF,CSN,1); //??SPI
return(status); //
}
/****************************************************************************************************/
/*函数:void SetRX_Mode(void)
/*功能:数据接收配置
/****************************************************************************************************/
void SetRX_Mode(void)
{
LPLD_GPIO_Set_b(NRF,SCE,0);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ????????,16?CRC ,???
LPLD_GPIO_Set_b(NRF,SCE,0);
inerDelay_us(130);
}
/******************************************************************************************************/
/*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
/*功能:数据读取后放如rx_buf接收缓冲区中
/******************************************************************************************************/
uint8 nRF24L01_RxPacket(uint8* rx_buf)
{
uint8 revale=0;
sta=SPI_Read(STATUS); // ????????????????
if(sta&0x40) // ?????????
{
LPLD_GPIO_Set_b(NRF,SCE,0); //SPI??
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
revale =1; //????????
}
SPI_RW_Reg(WRITE_REG+STATUS,sta); //??????RX_DR,TX_DS,MAX_PT????1,???1???????
return revale;
}
/***********************************************************************************************************
/*函数:void nRF24L01_TxPacket(unsigned char * tx_buf)
/*功能:发送 tx_buf中数据
/**********************************************************************************************************/
void nRF24L01_TxPacket(uint8 * tx_buf)
{
LPLD_GPIO_Set_b(NRF,SCE,0); //StandBy I??
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // ???????
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // ????
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ????????,16?CRC,???
LPLD_GPIO_Set_b(NRF,SCE,1); //??CE,??????
inerDelay_us(10);
}
/****************************************************************************************
/*NRF24L01初始化
//***************************************************************************************/
void init_NRF24L01(void)
{
LPLD_GPIO_Init(WIRE,MISO,0,0,0);
LPLD_GPIO_Init(WIRE,IRQ,0,0,0);
LPLD_GPIO_Init(WIRE,SCE,1,0,0);
LPLD_GPIO_Init(WIRE,SCK,1,0,0);
LPLD_GPIO_Init(WIRE,CSN,1,0,0);
LPLD_GPIO_Init(WIRE,MOSI,1,0,0);
inerDelay_us(100);
LPLD_GPIO_Set_b(NRF,SCE,0); // chip enable
LPLD_GPIO_Set_b(NRF,CSN,1); // Spi disable
LPLD_GPIO_Set_b(NRF,SCK,0); // Spi clock line init high
LPLD_GPIO_Set_b(NRF,IRQ,1);
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 频道0自动 ACK应答允许
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址只有频道0,如果需要多频道可以参考Page21
SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
}
NRF2401.h
#ifndef _NRF2401_H
#define _NRF2401_H
//*********************************************NRF24L01*************************************
#define TX_ADR_WIDTH 5 // 5 uints TX address width
#define RX_ADR_WIDTH 5 // 5 uints RX address width
#define TX_PLOAD_WIDTH 32 // 20 uints TX payload
#define RX_PLOAD_WIDTH 32 // 20 uints TX payload
extern unsigned char TX_ADDRESS[TX_ADR_WIDTH]; //????
extern unsigned char RX_ADDRESS[RX_ADR_WIDTH]; //????
//***************************************NRF24L01?????*******************************************************
#define READ_REG 0x00 // ??????
#define WRITE_REG 0x20 // ??????
#define RD_RX_PLOAD 0x61 // ????????
#define WR_TX_PLOAD 0xA0 // ???????
#define FLUSH_TX 0xE1 // ???? FIFO??
#define FLUSH_RX 0xE2 // ???? FIFO??
#define REUSE_TX_PL 0xE3 // ??????????
#define NOP 0xFF // ??
//*************************************SPI(nRF24L01)?????****************************************************
#define CONFIG 0x00 // ??????,CRC??????????????
#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 // ??1??????
#define RX_ADDR_P2 0x0C // ??2??????
#define RX_ADDR_P3 0x0D // ??3??????
#define RX_ADDR_P4 0x0E // ??4??????
#define RX_ADDR_P5 0x0F // ??5??????
#define TX_ADDR 0x10 // ???????
#define RX_PW_P0 0x11 // ????0??????
#define RX_PW_P1 0x12 // ????0??????
#define RX_PW_P2 0x13 // ????0??????
#define RX_PW_P3 0x14 // ????0??????
#define RX_PW_P4 0x15 // ????0??????
#define RX_PW_P5 0x16 // ????0??????
#define FIFO_STATUS 0x17 // FIFO???????????
//**************************************************************************************
uint8 SPI_RW(uint16 unchar);
uint8 SPI_Read(uint8 reg);
uint16 SPI_RW_Reg(uint8 reg, uint16 value);
uint16 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 uchars);
uint16 SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars);
void SetRX_Mode(void);
uint8 nRF24L01_RxPacket(uint8* rx_buf);
void nRF24L01_TxPacket(uint8 * tx_buf);
void init_NRF24L01(void);
#endif
有什么错误的地方请指教啊
|
|