在线时间392 小时
UID2053772
ST金币0
蝴蝶豆40
注册时间2013-11-3
论坛元老
- 最后登录
- 2020-12-5
|
楼主 |
发表于 2015-11-13 10:23:35
|
显示全部楼层
a0a.1 32b0c
本帖最后由 dzzwoaizi 于 2015-11-13 10:43 编辑
第二节:如何驱动XN297L模块,这个模块和大众用的RNF24L01功能是一样的!都是2.4G模块,XN297L传输的距离会更远一些哦!我这里用的是QFN20L030封装的哦!还有两种分装你们可以自己搜搜哦!
下面看一些资料
1.引脚定义
2.按照惯例驱动芯片需要6个IO口和VCC.GND。也就是8根线就能解决了。
这里SPI是模拟的,你也可以用硬件SPI,但是要是移植到另一个板子那就麻烦了!
首先确定一下利用那6个IO口吧!
简单一点!就用PC0~PC5吧!反正是模拟SPI,用哪个口都无所谓、
再看配置,
IRQ ——> PC0 ;//设置为输入IO口
MISO ——> PC1 ;//设置为输入IO口
MOSI ——> PC2 ;//设置为推挽输出IO口
SCK ——> PC3 ;//设置为推挽输出IO口
CSN ——> PC4 ; //设置为推挽输出IO口
CE ——> PB5 ; //设置为推挽输出IO口
相必IO口配置大家都会把!不会的话就看看官方demo,设置完后别忘了输出口全部为低电平哦!
3。下面看看SPI怎么模拟吧!
//写一个字节
void SpiWrite(u8 Dat)
{
u8 i;
for(i=0; i<8; i++)
{
if(Dat & 0x80) Rf_MOSI=1;
else Rf_MOSI=0;
Rf_SCK=1;
RfDelayUs(2);
Rf_SCK=0;
Dat<<=1;
}
}
//读一个字节
void SpiWrite(u8 Dat)
{
u8 i;
for(i=0; i<8; i++)
{
if(Dat & 0x80) Rf_MOSI=1;
else Rf_MOSI=0;
Rf_SCK=1;
RfDelayUs(2);
Rf_SCK=0;
Dat<<=1;
}
}
这两个函数就完成了8位的读写数据,简单吧!这里的Rf_MOSI和Rf_SCK等都是IO操作,下面就不在提示了!
如何读N个数据和写N个数据呢!简单的,看下面代码
//读N个数据,
//Addr:地址 Rdat:读出的数据保存的地址,Length:长度
void SpiReadDat(u8 Addr, u8 *Rdat, u8 Length)
{
u8 i;
Rf_CSN=0;
SpiWrite(Addr);
for(i=0; i<Length; i++) Rdat=SpiRead();
Rf_CSN=1;
}
//写N个数据
//Addr:地址 Rdat:写的数组首地址,Length:长度
void SpiWriteDat(u8 Addr, u8 *Rdat, u8 Length)
{
u8 i;
Rf_CSN=0;
SpiWrite(Addr);
for(i=Length; i>0; i--)
{
SpiWrite(*Rdat++);
}
Rf_CSN=1;
}
3.XN297L有两种模式,一种是普通型的,一种是增强行的!
普通型:只管发送数据,不管接收方收没收到数据,
增强型:发送数据后等待接收方的应答信号,没收到后会自动重发,(如果还没收到)达到从发字数就就不在重发,如果收到,再继续发送其他的数据!
4.配置底层!
/******************************************************************************
XN297L_Initial
Initial RF in ENHANCE_MODE
******************************************************************************/
void RF_ENHANCE_MODE_Init(void)
{
u8 BB_cal_data[] = {0x0A,0x6D,0x67,0x9C,0x46}; //XN297L
u8 RF_cal_data[] = {0xF6,0x37,0x5D}; //XN297L
u8 RF_cal2_data[] = {0x45,0x21,0xEF,0x2C,0x5A,0x40}; //XN297L
u8 Dem_cal_data[] = {0x01}; //XN297L
u8 Dem_cal2_data[] = {0x0B,0xDF,0X02}; //XN297L
Rf_CE=0;
ucCurrent_Channel = DEFAULT_CHANNEL;
/*********************use xn297 Enhance*********************/
SpiWriteReg(RST_FSPI, 0x5A); //SoftWare Reset
SpiWriteReg(RST_FSPI, 0xA5);
SpiWriteReg(FLUSH_TX, 0);
SpiWriteReg(FLUSH_RX, 0);
SpiWriteReg(WRITE_REG + STATUS, 0x70);
SpiWriteReg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
while( SpiReadReg(READ_REG + EN_RXADDR) != 0x01);
SpiWriteReg(WRITE_REG + SETUP_AW, 0x03); // address witdth is 5 bytes
while( SpiReadReg(READ_REG + SETUP_AW) != 0x03);
SpiWriteReg(WRITE_REG + RF_CH, DEFAULT_CHANNEL); // select RF channel 2440MHz
while( SpiReadReg(READ_REG + RF_CH) != DEFAULT_CHANNEL);
SpiWriteReg(WRITE_REG + RX_PW_P0, BUFSIZE); // 6 bytes
while( SpiReadReg(READ_REG + RX_PW_P0) != BUFSIZE);
SpiWriteDat(WRITE_REG + TX_ADDR, RfId, 5); // writes TX_Address to XN297
SpiWriteDat(WRITE_REG + RX_ADDR_P0, RfId, 5); // RX_Addr0 same as TX_Adr for AutoAck
SpiWriteDat(WRITE_REG + BB_CAL, BB_cal_data, sizeof(BB_cal_data));
SpiWriteDat(WRITE_REG + RF_CAL2, RF_cal2_data, sizeof(RF_cal2_data));
SpiWriteDat(WRITE_REG + DEM_CAL, Dem_cal_data, sizeof(Dem_cal_data));
SpiWriteDat(WRITE_REG + RF_CAL, RF_cal_data, sizeof(RF_cal_data));
SpiWriteDat(WRITE_REG + DEM_CAL2, Dem_cal2_data, sizeof(Dem_cal2_data));
SpiWriteReg(WRITE_REG + DYNPD, 0x00); //YUAN LAI 0X01
SpiWriteReg(WRITE_REG +FEATURE, 0x00); //²»Ê¹ÄÜspi¿ØÖÆCE
SpiWriteReg(WRITE_REG + RF_SETUP, RF_POWER);
SpiWriteReg(WRITE_REG + SETUP_RETR, RETRY_MAX); // retry max is 5
SpiWriteReg(WRITE_REG + EN_AA, 0x01); // Enable auto.Ack:pipe0
//SpiWriteReg(ACTIVATE, 0x73);
if(BUFSIZE < 33)
{
SpiWriteReg(WRITE_REG +FEATURE, 0x00); // 32byte mode
}
else
{
SpiWriteReg(WRITE_REG +FEATURE, 0x18); // 64byte mode
}
//SpiWriteReg(ACTIVATE, 0x73);
}//end RF_Init
/******************************************************************************
XN297L_Initial
Initial RF in NORMAL_MODE
******************************************************************************/
void RF_NORMAL_MODE_Init(void)
{
u8 BB_cal_data[] = {0x0A,0x6D,0x67,0x9C,0x46}; //XN297L
u8 RF_cal_data[] = {0xF6,0x37,0x5D}; //XN297L
//u8 RF_cal2_data[] = {0x45,0x21,0xEF,0xAC,0x5A,0x50}; //XN297L
u8 RF_cal2_data[] = {0x45,0x21,0xEF,0x2C,0x5A,0x40};
u8 Dem_cal_data[] = {0x00}; //XN297L
u8 Dem_cal2_data[] = {0x0B,0xDF,0X02}; //XN297L
Rf_CE=0;
ucCurrent_Channel = DEFAULT_CHANNEL;
/*********************use xn297 NORMAL*********************/
SpiWriteReg(RST_FSPI, 0x5A); //SoftWare Reset
SpiWriteReg(RST_FSPI, 0xA5);
SpiWriteReg(FLUSH_TX, 0);
SpiWriteReg(FLUSH_RX, 0);
SpiWriteReg(WRITE_REG + STATUS, 0x70);
SpiWriteReg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
while( SpiReadReg(READ_REG + EN_RXADDR) != 0x01);
SpiWriteReg(WRITE_REG + SETUP_AW, 0x03); // address witdth is 5 bytes
while( SpiReadReg(READ_REG + SETUP_AW) != 0x03);
SpiWriteReg(WRITE_REG + RF_CH, DEFAULT_CHANNEL); // select RF channel 2440MHz
while( SpiReadReg(READ_REG + RF_CH) != DEFAULT_CHANNEL);
SpiWriteReg(WRITE_REG + RX_PW_P0, BUFSIZE); // 6 bytes
while( SpiReadReg(READ_REG + RX_PW_P0) != BUFSIZE);
SpiWriteDat(WRITE_REG + TX_ADDR, RfId, 5); // writes TX_Address to XN297
SpiWriteDat(WRITE_REG + RX_ADDR_P0, RfId, 5); // RX_Addr0 same as TX_Adr for AutoAck
SpiWriteDat(WRITE_REG + BB_CAL, BB_cal_data, sizeof(BB_cal_data));
SpiWriteDat(WRITE_REG + RF_CAL2, RF_cal2_data, sizeof(RF_cal2_data));
SpiWriteDat(WRITE_REG + DEM_CAL, Dem_cal_data, sizeof(Dem_cal_data));
SpiWriteDat(WRITE_REG + RF_CAL, RF_cal_data, sizeof(RF_cal_data));
SpiWriteDat(WRITE_REG + DEM_CAL2, Dem_cal2_data, sizeof(Dem_cal2_data));
SpiWriteReg(WRITE_REG + DYNPD, 0x00); //YUAN LAI 0X01
SpiWriteReg(WRITE_REG +FEATURE, 0x00);
SpiWriteReg(WRITE_REG + RF_SETUP, RF_POWER);
SpiWriteReg(WRITE_REG + SETUP_RETR, 0x00); // Disable Retrans
SpiWriteReg(WRITE_REG + EN_AA, 0x00); // Disable auto.Ack:pipe0
if(BUFSIZE < 33)
{
SpiWriteReg(WRITE_REG +FEATURE, 0x00); // 32byte mode
}
else
{
SpiWriteReg(WRITE_REG +FEATURE, 0x18); // 64byte mode
}
}//end RF_Init
//两种模式随便
哪一种都可以!
|
|