到现在,我已经能够成功地在Tx模式下设置一个模块,而在Rx模式下成功设置另一个模块。并能够在单向通信中获得成功的结果。
Rx模式的功能。
void NRF24_startListening(void)
{
//Power up and set to RX mode
NRF24_write_register(REG_CONFIG, NRF24_read_register(REG_CONFIG) | (1UL<<1) |(1UL <<0));
//clearing status register
NRF24_write_register(REG_STATUS, (1 << BIT_RX_DR) | (1 << BIT_TX_DS) | (1 << BIT_MAX_RT));
//Set CE HIGH to start listenning
NRF24_ce(1);
//Restore pipe 0 address if exists
if(pipe0_reading_address)
NRF24_write_registerN(REG_RX_ADDR_P0, (uint8_t *)(&pipe0_reading_address), 5);
//Flush buffers
if (NRF24_read_register(REG_FEATURE) & (1 << BIT_EN_ACK_PAY)) {
NRF24_flush_tx();
}
//NRF24_flush_rx();
//Wait for 130 uSec for the radio to come on
HAL_Delay(1);
}
Tx模式的功能。
void NRF24_stopListening(void)
{
NRF24_ce(0);
HAL_Delay(1);
if (NRF24_read_register(REG_FEATURE) & (1 << BIT_EN_ACK_PAY)) {
HAL_Delay(1);
NRF24_flush_tx();
}
NRF24_write_register(REG_CONFIG, NRF24_read_register(REG_CONFIG) & ~(1 << BIT_PRIM_RX));
NRF24_write_register(REG_EN_RXADDR, NRF24_read_register(REG_EN_RXADDR) | (1 << BIT_ERX_P0));
}
用于切换模式的功能。我已经重新初始化了整个SPI总线的切换模式。
void vNRF24_StartAs(NEW_MODE xNewMode) {
NRF24_powerDown();
HAL_Delay(100);
NRF24_begin(RF_CS_GPIO_Port, RF_CS_Pin, RF_CE_Pin, hspi2);
NRF24_setAutoAck(true);
NRF24_setChannel(52);
NRF24_setPayloadSize(32);
if (xNewMode == RX_MODE) {
NRF24_openReadingPipe(1, TxpipeAddrs);
NRF24_startListening();
}
else {
NRF24_stopListening();
NRF24_openWritingPipe(TxpipeAddrs);
}
}
我在Rx和Tx模式之间切换的主要代码
while (1)
{
if (xMode == RX_MODE)
{
if(NRF24_available())
{
NRF24_read(myRxData, 32);
myRxData[32] = '\r'; myRxData[32+1] = '\n';
vSerial_Transmit(myRxData, 32);
HAL_GPIO_TogglePin(BLUE_GPIO_Port, BLUE_Pin);
vNRF24_StartAs(TX_MODE);
xMode = TX_MODE;
HAL_Delay(5000);
}
}
if (xMode == TX_MODE) {
if(NRF24_write(myTxData, 32)) {
vSerial_Transmit("Transmit OK!!\r\n", strlen("Transmit OK!!\r\n"));
vNRF24_StartAs(RX_MODE);
xMode = RX_MODE;
HAL_Delay(5000);
}
else {
vSerial_Transmit("Transmit FAIL!!\r\n", strlen("Transmit FAIL!!\r\n"));
}
}
}
我试图以两种不同的方式初始化两个模块。两者之间的前两个事务是模块完美的。然后,这两个模块都以某种方式进入Tx模式。有什么建议吗?
致谢。