高级会员
积分 777
金钱 777
注册时间 2012-4-23
在线时间 66 小时
2 金钱
本帖最后由 haoyu729 于 2016-2-26 22:12 编辑
使用STM8S103F3的SPI接口驱动nrf24l01工作。程序执行自检时,可以正确收到5个A9。执行到 if( nRF24L01_TxPacket(TxBuf)==TX_OK)//发送数据就不对。跟到里面 到unsigned char nRF24L01_TxPacket(unsigned char * tx_buf)里面时返回数据一直都是1E。
怀疑过SPI配置,但没有找到原因。希望那位帮位看看,谢谢!
IO配置,发送配置如图
void NRF24l01_Configuration(void)
{
// GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);//LED初始化
//无线模块的IO口初始化
//MISO 读取数据 上拉输入,没有外部中断
GPIO_Init(NRF24L01_CE_PORT, (GPIO_Pin_TypeDef)(NRF24L01_CE_PIN ), GPIO_MODE_OUT_PP_LOW_FAST);//ce
GPIO_Init(NRF24L01_CS_PORT, (GPIO_Pin_TypeDef)(NRF24L01_CS_PIN ), GPIO_MODE_OUT_PP_LOW_FAST);//cs
GPIO_Init(NRF24L01_SCK_PORT, (GPIO_Pin_TypeDef)(NRF24L01_SCK_PIN ), GPIO_MODE_OUT_PP_LOW_FAST);//sck
GPIO_Init(NRF24L01_MOSI_PORT, (GPIO_Pin_TypeDef)(NRF24L01_MOSI_PIN ), GPIO_MODE_OUT_PP_LOW_FAST);//MOSI
GPIO_Init(NRF24L01_MISO_PORT, (GPIO_Pin_TypeDef)(NRF24L01_MISO_PIN), GPIO_MODE_IN_PU_NO_IT);//MISO
GPIO_Init(NRF24L01_IRQ_PORT, (GPIO_Pin_TypeDef)(NRF24L01_IRQ_PIN ), GPIO_MODE_IN_PU_NO_IT); //IRQ
SPI_Cmd(DISABLE);
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_4, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_LOW,
SPI_CLOCKPHASE_1EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT,(uint8_t)0x07);
SPI_Cmd(ENABLE);/* Enable the SPI*/
GPIO_WriteLow(NRF24L01_CE_PORT, NRF24L01_CE_PIN);
GPIO_WriteHigh(NRF24L01_CS_PORT, NRF24L01_CS_PIN);
GPIO_WriteLow(NRF24L01_SCK_PORT, NRF24L01_SCK_PIN);
}
/**************************************************
Function: SPI_Read_Write_Byte();
Description:
Writes one byte to nRF24L01, and return the byte read
from nRF24L01 during write, according to SPI protocol
**************************************************/
uchar SPI_Read_Write_Byte(uchar byte)(同原子哥的程序)
{
/*!< Wait until the transmit buffer is empty */
uchar retry=0;
while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET)
{
retry++;
if(retry>200)
return 0;
}
SPI_SendData(byte);/*!< Send the byte */
retry=0;
while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET)
{
retry++;
if(retry>200)return 0;
}
byte = SPI_ReceiveData();
return(byte);
}
/**************************************************
Function: nRF24L01_TxPacket();
Description:
**************************************************/
unsigned char nRF24L01_TxPacket(unsigned char * tx_buf)
{
u8 stat;
GPIO_WriteLow(NRF24L01_CE_PORT,NRF24L01_CE_PIN);
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH);
GPIO_WriteHigh(NRF24L01_CE_PORT,NRF24L01_CE_PIN);
while(GPIO_ReadInputPin(NRF24L01_IRQ_PORT, NRF24L01_IRQ_PIN)!=0);//等待发送中断
stat=SPI_Read_Reg(STATUS);//读取状态寄存器的值
SPI_Write_Reg(WRITE_REG+STATUS,stat); // 清除TX_DS或MAX_RT中断标志 ----------------------------执行到此处返回一直是1E,(用原子哥自检函数,是可以正确写入与读出)
if(stat&MAX_TX)//达到最大重发次数
{
SPI_Write_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器
return MAX_TX;
}
if(stat&TX_OK)//发送完成
{
return TX_OK;
}
return 0xff;//其他原因发送失败
}
void main(void)
{
uchar TxBuf[TX_PLOAD_WIDTH]= {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x39,0x3a,0x3b,
0x3c,0x3d,0x3e,0x3f,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x39,0x3a,0x3b,0x3c,0x3d,0x3e};
CLK_HSICmd(ENABLE);//开始内部高频RC
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);
NRF24l01_Configuration() ;
Delay(2000);
if(NRF24L01_Check()==0)
{
TX_Mode();
Delay(2000);
}
while(1)
{
if( nRF24L01_TxPacket(TxBuf)==TX_OK)//发送数据
{
GPIO_WriteHigh(GPIOD, GPIO_PIN_0);
Delay(0xFFFF);
GPIO_WriteLOW(GPIOD, GPIO_PIN_0);
}
}
}
我来回答