|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2011-7-26 11:42:51
|
显示全部楼层
/*************************************************
使用该模块驱动时要需修改nRF24L01_PIN()中的管脚配置
并修改24L01_PIN_function被定义为哪个寄存器即可调用驱动
***************************************************/
#include <lpc17xx.h>
#include "nRF24L01.h"
typedef unsigned char BYTE;
typedef unsigned char uchar;
/**************************************************
管脚的配置情况,注意:此函数必须在main()中先调用
**************************************************/
void nRF24L01_PIN() //注意(修改地方)
{ LPC_PINCON->PINSEL0=0x00000000;
LPC_GPIO0->FIODIR |= 0x0000003f;
}
/*******************************************
在PIN管脚定义中24L01_PIN_function被定义为寄存器
*******************************************/
#define function_24L01_PIN_1 ( LPC_GPIO0->FIOSET0 ) //注意(修改地方)
#define function_24L01_PIN_0 ( LPC_GPIO0->FIOCLR0 )
/**************************************************
PINS:
-3.3V (1) - 空脚 (2)
- CE (3) = P0.0 - CSN (4) = P0.3
- SCK (5) = P0.1 - MOSI (6) = P0.4
- MISO(7) = P0.2 - IQR (8) = P0.5
- GND (9) - 空脚 (10)
****************************************************/
#define RF_CE (1 << 0)
#define RF_SCK (1 << 1)
#define RF_MISO (1 << 2)
#define RF_CSN (1 << 3)
#define RF_MOSI (1 << 4)
#define RF_IQR (1 << 5)
/**************************************************/
/* Pin MISO setting to 0 or 1 */
#define nRF_MISO(x) ((x) ? (function_24L01_PIN_1 = RF_MISO) : (function_24L01_PIN_0 = RF_MISO));
/* Pin MOSI setting to 0 or 1 */
#define nRF_MOSI(x) ((x) ? (function_24L01_PIN_1 = RF_MOSI) : (function_24L01_PIN_0 = RF_MOSI));
/* Pin SCK setting to 0 or 1 */
#define nRF_SCK(x) ((x) ? (function_24L01_PIN_1 = RF_SCK) : (function_24L01_PIN_0 = RF_SCK));
/* Pin CE setting to 0 or 1 */
#define nRF_CE(x) ((x) ? (function_24L01_PIN_1= RF_CE) : (function_24L01_PIN_0 = RF_CE));
/* Pin CSN setting to 0 or 1 */
#define nRF_CSN(x) ((x) ? (function_24L01_PIN_1 = RF_CSN) : (function_24L01_PIN_0= RF_CSN));
/* Pin IRQ setting to 0 or 1 */
#define nRF_IRQ(x) ((x) ? (function_24L01_PIN_1 = RF_IQR) : (function_24L01_PIN= RF_IQR));
/**************************************************/
uchar TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address发送到地址0的数据
uchar RX_ADDRESS[RX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address接收地址0的数据
//通道0地址1
uchar TX_ADDRESS1[TX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC2}; // Define a static TX address发送到地址1的数据
uchar RX_ADDRESS1[RX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC2}; // Define a static TX address接收地址1的数据
//通道0地址2
uchar TX_ADDRESS2[TX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC3}; // Define a static TX address发送到地址2的数据
uchar RX_ADDRESS2[RX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC3}; // Define a static TX address接收地址2的数据
//通道0地址3
uchar TX_ADDRESS3[TX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC4}; // Define a static TX address发送到地址3的数据
uchar RX_ADDRESS3[RX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC4}; // Define a static TX address接收地址3的数据
//通道0地址4
uchar TX_ADDRESS4[TX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC5}; // Define a static TX address发送到地址4的数据
uchar RX_ADDRESS4[RX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC5}; // Define a static TX address接收地址4的数据
//通道0地址5
uchar TX_ADDRESS5[TX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC6}; // Define a static TX address发送到地址5的数据
uchar RX_ADDRESS5[RX_ADR_WIDTH] = {0xC2,0xC2,0xC2,0xC2,0xC6}; // Define a static TX address接收地址5的数据
/********************************************
用于设置status寄存器中的这三位表示的事件发生时
是否通过IRQ引脚来反映,1,不反映在IRQ上,0反映
在IRQ上
*********************************************/
uint8_t sta;
uint8_t RX_DR;
uint8_t TX_DS;
uint8_t MAX_RT;
/*******************************************/
void inerDelay_us(unsigned char n)
{
unsigned int i,j;
for(i=0; i<n; i++)
for(j=0; j<800; j++);
}
/**********************************************
初始化
**********************************************/
void init_io(void)
{
inerDelay_us(1000);
nRF_CE(0);
nRF_CSN(1);
nRF_SCK(0);
}
/********************************************************************
SPI读写函数
写入一个字节并返回一个字节
ch:需要写入的字节
*********************************************************************/
uint8_t SPI_RW(uchar byte)
{
uint8_t bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
if(byte&0x80)
//MOSI = (byte & 0x80);output 'byte', MSB to MOSI
{
nRF_MOSI(1);
}
else
{
nRF_MOSI(0);
}
// IO0CLR = MOSI ;//更改部分
byte = (byte << 1); // shift next bit into MSB..
nRF_SCK(1);
if( LPC_GPIO0->FIOPIN0& RF_MISO )// 逐个接收 MISO端口的数据。将数据存在 BUF_ISD的最高位
byte|=0x01;
nRF_SCK(0);
}
return(byte); // return read byte
}
/**************************************************
Function: SPI_RW_Reg();
Description:
Writes value 'value' to register 'reg'
/**************************************************/
uchar SPI_RW_Reg(BYTE reg, BYTE value)
{
uchar status;
nRF_CSN(0); // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
nRF_CSN(1);
return(status); // return nRF24L01 status byte
}
/**************************************************
Function: SPI_Read();
Description:
Read one byte from nRF24L01 register, 'reg'
/**************************************************/
BYTE SPI_Read(BYTE reg)
{
BYTE reg_val;
nRF_CSN(0) ;
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
nRF_CSN(1) ;
return(reg_val); // return register value
}
/**************************************************/
/**************************************************
Function: SPI_Read_Buf();
Description:
/**************************************************/
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
uchar status,byte_ctr;
nRF_CSN(0) ;
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
pBuf[byte_ctr] = SPI_RW(0);
nRF_CSN(1) ;
return(status); // return nRF24L01 status byte
}
/************************************************************************************************
函数: SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,bytes:写入数据的个数
************************************************************************************************/
uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
uchar status,byte_ctr;
nRF_CSN(0) ;
status = SPI_RW(reg);
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) //
SPI_RW(*pBuf++);
nRF_CSN(1) ;
return(status);
}
/********************************************************************************/
/*函数: SetRX_Mode(void)
/*功能:数据接收配置
/********************************************************************************/
unsigned char SetRX_Mode( unsigned char * p )//修改为指针地址,方便调用
{
nRF_CE(0) ; //使用地址0
SPI_RW_Reg(WRITE_REG+STATUS,sta);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, p, RX_ADR_WIDTH); // Use the same address on the RX device as the TX device
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
SPI_RW_Reg(WRITE_REG + RF_CH, 0);
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收
nRF_CE(1) ;
inerDelay_us(200);
}
/**************************************************/
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
unsigned char revale=0;
sta=SPI_Read(STATUS);
RX_DR = sta&0x40;
TX_DS = sta&0x20;
MAX_RT = sta&0x10;
if(RX_DR)
{
nRF_CE(0) ;
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
revale =1;//we have receive data
}
SPI_RW_Reg(WRITE_REG+STATUS,sta);
return revale;
}
/**************************************************
Function: nRF24L01_TxPacket();
Description:
This function initializes one nRF24L01 device to
TX mode, set TX address, set RX address for auto.ack,
fill TX payload, select RF channel, datarate & TX pwr.
PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
ToDo: One high pulse(>10us) on CE will now send this
packet and expext an acknowledgment from the RX device.
/**************************************************/
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
nRF_CE(0) ;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH);
inerDelay_us(10);//增加部分
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a);
SPI_RW_Reg(WRITE_REG + RF_CH, 0);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);
inerDelay_us(100);
nRF_CE(1) ;
inerDelay_us(100);
}
/**************************************************/ |
|