㈠ 24L01无线模块怎么用
/****************************************Copyright (c)**************************************************
**
**------------------------------------------------------------------------------------------------------
** Modified by: liu jiandong
** Modified date: 2006.9.14
** Version: ver 2
** Descriptions: test for second board
**
********************************************************************************************************/
#include "config.h"
/***************************************************/
#define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH 20 // 20 bytes TX payload
uint8 TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
uint8 rx_buf[TX_PLOAD_WIDTH];
uint8 *tx_buf;
uint8 flag;
/**************************************************/
#define CE 0x01000000; //P1.24 //CE
#define CSN 0x02000000; //P1.25 //CSN
//#define SCK 0x00000010; //P0.4 //SCK
//#define MOSI 0x00000040; //P0.6 //MOSI
//#define MISO 0x00000020; //P0.5 //MISO
#define MASK_CE 0xFeFFFFFF;
#define MASK_CSN 0xFdFFFFFF;
//#define MASK_SCK 0xFFFFFFeF;
//#define MASK_MOSI 0xFFFFFFbF;
//#define MASK_MISO 0xFFFFFFdF;
/**************************************************/
/**************************************************
Function: init_io();
Description:
flash led one time,chip enable(ready to TX or RX Mode),
Spi disable,Spi clock line init high
**************************************************/
void Init24L01(void)
{
SCS = 0x03;
/*设置GPIO口输入输出方向 1为输出,0为输入*/
FIO1DIR = (FIO1DIR & 0xFeFFFFFF) | CE; //CE输出
FIO1DIR = (FIO1DIR & 0xFdFFFFFF) | CSN; //CSN输出
// FIO0DIR = (FIO0DIR & 0xFFFFFFeF) | SCK; //SCK输出
// FIO0DIR = (FIO0DIR & 0xFFFFFFbF) | MOSI; //MOSI输出
/* 设置管脚连接,P0.30为EINT3*/
PINSEL1 = (PINSEL1 & 0xcFFFFFFF) | 0x20000000;
EXTMODE = 0x00; /* 设置EINT3为电平触发 */
//IRQEnable(); // 使能IRQ中断
MSPI_Init();
}
/**************************************************/
/**************************************************/
void DelayNS(uint32 dly)
{
uint32 i;
for ( ; dly>0; dly--)
for (i=50000; i>0; i--);
}
/**************************************************/
/********************************************************************************************************
** 函数名称:MSPI_Init()
** 函数功能:初始化SPI接口,设置为主机。
** 入口参数:无
** 出口参数:无
*********************************************************************************************************/
void MSPI_Init(void)
{
PINSEL0 = (PINSEL0 & 0xFFFFaaFF) | 0x00005500;
//PINSEL0 = (PINSEL0 & (~(0xFF << 8))) | (0x55 << 8) ;
S0SPCCR = 0x52; // 设置SPI时钟分频
S0SPCR = (0 << 3) | // CPHA = 0, 数据在SCK 的第一个时钟沿采样
(0 << 4) | // CPOL = 0, SCK 为高有效
(1 << 5) | // MSTR = 1, SPI 处于主模式
(0 << 6) | // LSBF = 0, SPI 数据传输MSB (位7)在先
(0 << 7) ; // SPIE = 0, SPI 中断被禁止
// 设置数据位n,当n=0时,数据位为16。
}
/**************************************************
Function: SPI_RW();
Description:
Writes one byte to nRF24L01, and return the byte read
from nRF24L01 ring write, according to SPI protocol
**************************************************/
uint8 SPI_RW(uint8 byte)
{
//hardware spi
S0SPDR = byte;
while((S0SPSR & 0x80) == 0); // 等待SPIF置位,即等待数据发送完毕
return(S0SPDR);
//software spi
/*uint8 bit_ctr,retbyte=0x00;
uint32 byte1;
byte1 = byte;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
FIO0MASK = MASK_MOSI;
FIO0PIN = (byte1 & 0x00000080)>>1;//MOSI = (byte & 0x80); // output 'byte', MSB to MOSI
byte1 = (byte1 << 1); // shift next bit into MSB..
FIO0MASK = MASK_SCK;
FIO0SET = SCK;
//SCK = 1; // Set SCK high..
FIO0MASK = MASK_MISO;
retbyte |= ((FIO0PIN<<2)&0x00000080)>>7;//byte |= MISO; // capture current MISO bit
FIO0MASK = MASK_SCK;
FIO0CLR = SCK;
retbyte = retbyte<<1;
//SCK = 0; // ..then set SCK low again
}
return(retbyte); // return read byte
*/
}
/**************************************************/
/**************************************************
Function: SPI_RW_Reg();
Description:
Writes value 'value' to register 'reg'
**************************************************/
uint8 SPI_RW_Reg(uint8 reg, uint8 value)
{
uint8 status;
FIO1MASK = MASK_CSN;
FIO1CLR = CSN; //CSN = 0; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
FIO1MASK = MASK_CSN;
FIO1SET = CSN; //CSN = 1; // CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Read();
Description:
Read one byte from nRF24L01 register, 'reg'
**************************************************/
uint8 SPI_Read(uint8 reg)
{
uint8 reg_val;
FIO1MASK = MASK_CSN;
FIO1CLR = CSN;//CSN = 0; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
FIO1MASK = MASK_CSN;
FIO1SET = CSN; //CSN = 1; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
/**************************************************/
/**************************************************
Function: SPI_Read_Buf();
Description:
Reads 'bytes' #of bytes from register 'reg'
Typically used to read RX payload, Rx/Tx address
**************************************************/
uint8 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 bytes)
{
uint8 status,byte_ctr;
FIO1MASK = MASK_CSN;
FIO1CLR = CSN; //CSN = 0; // Set CSN low, init SPI tranaction
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); // Perform SPI_RW to read byte from nRF24L01
FIO1MASK = MASK_CSN;
FIO1SET = CSN; //CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Write_Buf();
Description:
Writes contents of buffer '*pBuf' to nRF24L01
Typically used to write TX payload, Rx/Tx address
**************************************************/
uint8 SPI_Write_Buf(uint8 reg, uint8 *pBuf, uint8 bytes)
{
uint8 status,byte_ctr;
FIO1MASK = MASK_CSN;
FIO1CLR = CSN;//CSN = 0; // Set CSN low, init SPI tranaction
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++);
FIO1MASK = MASK_CSN;
FIO1SET = CSN;//CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: RX_Mode();
Description:
This function initializes one nRF24L01 device to
RX Mode, set RX address, writes RX payload width,
select RF channel, datarate & LNA HCURR.
After init, CE is toggled high, which means that
this device is now ready to receive a datapacket.
**************************************************/
void RX_Mode(void)
{
FIO1MASK = MASK_CE;
FIO1CLR = CE;//CE=0;
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_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..
FIO1MASK = MASK_CE;
FIO1SET = CE;//CE = 1; // Set CE pin high to enable RX device
// This device is now ready to receive one packet of 16 bytes payload from a TX device sending to address
// '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
}
/**************************************************/
/**************************************************
Function: TX_Mode();
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 TX_Mode(void)
{
FIO1MASK = MASK_CE;
FIO1CLR = CE;//CE=0;
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_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
FIO1MASK = MASK_CE;
FIO1SET = CE;//CE=1;
}
/**************************************************/
/**************************************************
Function: check_ACK();
Description:
check if have "Data sent TX FIFO interrupt",if TX_DS=1,
all led light and after delay 100ms all led close
**************************************************
void check_ACK()
{
uchar test;
test=SPI_Read(READ_REG+STATUS); // read register STATUS's
test=test&0x20; // check if have Data sent TX FIFO interrupt (TX_DS=1)
if(test==0x20) // TX_DS =1
{
P0=0x00; // turn on all led
delay100(); // delay 100ms
P0=0xff;
}
}
**************************************************/
/**************************************************
Function: ISR_int0() interrupt 0;
Description:
if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;
if RX_DR=1,read the payload from RX_FIFO and set flag;
**************************************************/
void EINT3_Exception(void)
{
uint8 sta;
OS_ENTER_CRITICAL();
//Beep(20);
sta=SPI_Read(STATUS); // read register STATUS's value
if(sta & 0x40) // if receive data ready (RX_DR) interrupt
{
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
flag=1;
}
if(sta & 0x10)
{
SPI_RW_Reg(FLUSH_TX,0);
}
SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
/* 等待外部中断信号恢复为高电平
若信号保持为低电平,中断标志会一直置位。*/
while ((EXTINT & 0x08) != 0)
{
EXTINT = 0x08; /* 清除EINT0中断标志 */
}
VICVectAddr = 0x00; // 中断处理结束
OS_EXIT_CRITICAL();
}
/**************************************************
Function: void nRF_GetData();
Description:
**************************************************/
uint8* nRF_GetData(void)
{
uint8 *dat="0";
if(flag)
{
//RX_Mode();
dat = rx_buf;
}
return dat;
}
/**************************************************
Function: void nRF_SendData();
Description:
**************************************************/
void nRF_SendData(uint8 *dat)
{
tx_buf = dat; // Save to tx_buf[0]
TX_Mode(); // set TX Mode and transmitting
SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
DelayNS(50);
RX_Mode();
}
/**************************************************
Function: uint8 get_rf_flag();
Description:
**************************************************/
uint8 get_rf_flag(void)
{
if(flag)
return 1;
else
return 0;
}
/**************************************************
Function: uint8 get_rf_flag();
Description:
**************************************************/
void set_rf_flag(uint8 flg)
{
flag = flg;
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
㈡ 关于NRF24L01通信的问题
你这样说没有说明白,我这里附一份调试过的代码,用ST的stm32芯片实现,上面的说明应该是很清楚了,你照猫画虎基本上能成功的,试试吧。
额,好吧,后面的内容超出字数要求了,加不上去,你可以用其他方法问我要剩余的代码,而且这里有部分注释因为字数的原因也被删除了。
--------------------------------------------------------
**文 件 名: nrf24l01.c
**创 建 人: pylon_zlq
**版 本 : v0.1
**最后修改日期: 2011年10月29日
**描 述: 1. 2.4G无线模块驱动程序
** 2. 24L01的指令分类规律读写寄存器为一大类,其他操作为一大类,如下:
** a. 读写寄存器是高三位来区分, 0b000X XXXX (read), 0b001X XXXX(write),其中 X XXXX(低5位)是
** 寄存器地址
** b. 读接收缓存指令: 0x61(用高三位来区别其他指令, 0b011)
** c. 写发送缓存指令: 0xA0(同上)
** d. 清TX FIFO: 0xE1(同上)
** e. 清RX FIFO: 0xE2(同上)
** f. 重使用上包数据: 0xE3(同上)
** g. NOP,空操作: 0xFF(同上)
** 3. 由于收发FIFO最多有32字节,因此本模块仅使用32字节长度的数据收发
** 4. 由于通讯地址最多有5字节,因此本模块仅使用5字节的通道地址(收发方一致)
** 5. 从实际使用情况来看,读写接口分为这么几种:读写单字节的寄存器,读写多字节的通讯地址(也是寄存器的一种),
** 读写通讯缓存,总共6类读写接口
#include "includes.h"
#define NRF24L01_CE_0() {GPIO_SetBit_0(GPIOA, E_PIN_3);}
#define NRF24L01_CE_1() {GPIO_SetBit_1(GPIOA, E_PIN_3);}
#define NRF24L01_SCK_0() {GPIO_SetBit_0(GPIOA, E_PIN_5);}
#define NRF24L01_SCK_1() {GPIO_SetBit_1(GPIOA, E_PIN_5);}
#define NRF24L01_MOSI_0() {GPIO_SetBit_0(GPIOA, E_PIN_7);}
#define NRF24L01_MOSI_1() {GPIO_SetBit_1(GPIOA, E_PIN_7);}
#define NRF24L01_CS_0() {GPIO_SetBit_0(GPIOB, E_PIN_4);}
#define NRF24L01_CS_1() {GPIO_SetBit_1(GPIOB, E_PIN_4);}
#define NRF24L01_MISO_STT() (GPIO_GetLevel(GPIOA, E_PIN_6))
#define NRF24_RX_TX_LEN 32 //本模块仅使用32字节长度的通讯
#define NRF24_ADDR_LEN 5 //本模块仅使用5字节长度的通讯地址(收发方一致)
#define NFR24_RX_CH_1 0 //RX 1通道
#define NFR24_RX_CH_2 1 //RX 2通道
#define NFR24_RX_CH_3 2 //RX 3通道
#define NFR24_RX_CH_4 3 //RX 4通道
#define NFR24_RX_CH_5 4 //RX 5通道
#define NFR24_RX_CH_6 5 //RX 6通道
#define NFR24_TX_CH 6 //TX 通道
//NRF24L01寄存器操作命令
#define NRF24_READ_REG_CMD 0x00 //读配置寄存器,低5位为寄存器地址
#define NRF24_WRITE_REG_CMD 0x20 //写配置寄存器,低5位为寄存器地址
#define NRF24_RD_RX_PLOAD 0x61 //读RX有效数据,1~32字节
#define NRF24_WR_TX_PLOAD 0xA0 //写TX有效数据,1~32字节
#define NRF24_FLUSH_TX 0xE1 //清除TX FIFO寄存器.发射模式下用
#define NRF24_FLUSH_RX 0xE2 //清除RX FIFO寄存器.接收模式下用
#define NRF24_REUSE_TX_PL 0xE3 //重新使用上一包数据,CE为高,数据包被不断发送.
#define NRF24_NOP 0xFF //空操作,可以用来读状态寄存器
//SPI(NRF24L01)寄存器地址
#define NRF24_CONFIG 0x00 //配置寄存器地址;bit0:1接收模式,0发射模式;bit1:电选择;bit2:CRC模式;bit3:CRC使能;
//bit4:中断MAX_RT(达到最大重发次数中断)使能;bit5:中断TX_DS使能;bit6:中断RX_DR使能
#define NRF24_EN_AA 0x01 //使能自动应答功能 bit0~5,对应通道0~5
#define NRF24_EN_RXADDR 0x02 //接收地址允许,bit0~5,对应通道0~5
#define NRF24_SETUP_AW 0x03 //设置地址宽度(所有数据通道):bit1,0:00,3字节;01,4字节;02,5字节;
#define NRF24_SETUP_RETR 0x04 //建立自动重发;bit3:0,自动重发计数器;bit7:4,自动重发延时 250*x+86us
#define NRF24_RF_CH 0x05 //RF通道,bit6:0,工作通道频率;
#define NRF24_RF_SETUP 0x06 //RF寄存器;bit3:传输速率(0:1Mbps,1:2Mbps);bit2:1,发射功率;bit0:低噪声放大器增益
#define NRF24_STATUS 0x07 //状态寄存器;bit0:TX FIFO满标志;bit3:1,接收数据通道号(最大:6);bit4,达到最多次重发
//bit5:数据发送完成中断;bit6:接收数据中断;
#define NRF24_MAX_TX 0x10 //达到最大发送次数中断
#define NRF24_TX_OK 0x20 //TX发送完成中断
#define NRF24_RX_OK 0x40 //接收到数据中断
#define NRF24_OBSERVE_TX 0x08 //发送检测寄存器,bit7:4,数据包丢失计数器;bit3:0,重发计数器
#define NRF24_CD 0x09 //载波检测寄存器,bit0,载波检测;
#define NRF24_RX_ADDR_P0 0x0A //数据通道0接收地址,最大长度5个字节,低字节在前
#define NRF24_RX_ADDR_P1 0x0B //数据通道1接收地址,最大长度5个字节,低字节在前
#define NRF24_RX_ADDR_P2 0x0C //数据通道2接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define NRF24_RX_ADDR_P3 0x0D //数据通道3接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define NRF24_RX_ADDR_P4 0x0E //数据通道4接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define NRF24_RX_ADDR_P5 0x0F //数据通道5接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define NRF24_TX_ADDR 0x10 //发送地址(低字节在前),ShockBurstTM模式下,RX_ADDR_P0与此地址相等
#define NRF24_RX_PW_P0 0x11 //接收数据通道0有效数据宽度(1~32字节),设置为0则非法
#define NRF24_RX_PW_P1 0x12 //接收数据通道1有效数据宽度(1~32字节),设置为0则非法
#define NRF24_RX_PW_P2 0x13 //接收数据通道2有效数据宽度(1~32字节),设置为0则非法
#define NRF24_RX_PW_P3 0x14 //接收数据通道3有效数据宽度(1~32字节),设置为0则非法
#define NRF24_RX_PW_P4 0x15 //接收数据通道4有效数据宽度(1~32字节),设置为0则非法
#define NRF24_RX_PW_P5 0x16 //接收数据通道5有效数据宽度(1~32字节),设置为0则非法
#define NRF24_FIFO_STATUS 0x17 //FIFO状态寄存器;bit0,RX FIFO寄存器空标志;bit1,RX FIFO满标志;bit2,3,保留
//bit4,TX FIFO空标志;bit5,TX FIFO满标志;bit6,1,循环发送上一数据包.0,不循环;
static const uchar gCuc_TXAddr[NRF24_ADDR_LEN]={0x34,0x43,0x10,0x10,0x01}; //发送地址
static const uchar gCuc_RXAddr[NRF24_ADDR_LEN]={0x34,0x43,0x10,0x10,0x01}; //发送地址
uchar guc_TXRX_Stt;
/*********************************************************************************************************
**函数名称: void NRF24L01Initial(void)
**输入: none
**输出: none
**说明: PA3---WIRELESS_CE, PA5---SCK, PA6---MISO, PB4---WIRELESS_CS, PA7---MOSI, PA2---WIRELESS_INT
**/
void NRF24L01Initial(void)
{
//1.配置io口线时钟
RCC_APB2ENR |= IOPAEN+IOPBEN; //port a, port b,
RCC_APB2RSTR &= ~(IOPAEN+IOPBEN); //port a, port b,
//2.配置io口,SCK,MOSI,MISO三根线交由spi外设自动控制,其初始化也交由spi驱动程序完成
//WIRELESS_CE, output
SetOutput(GPIOA, E_PIN_3, E_OUT_PP, E_OUT_SPD_50M);
//WIRELESS_INT, pull up input
SetInput(GPIOA, E_PIN_2, INPUT_TYPE_FLOAT, E_IN_PULL_UP);
//WIRELESS_CS, push-pull output
SetOutput(GPIOB, E_PIN_4, E_OUT_PP, E_OUT_SPD_50M);
//这个是防止flash片在cs变低可能造成的MISO线电平冲突的问题
SetOutput(GPIOB, E_PIN_6, E_OUT_PP, E_OUT_SPD_50M);
GPIO_SetBit_1(GPIOB, E_PIN_6);
//3. enable rnf24l01, unselect rnf24l01
NRF24L01_CE_0();
NRF24L01_CS_1();
guc_TXRX_Stt = 0;
}