我的代码,双向通信。时好时坏,知道什么原因。
void RTX_Comm()
{
digitalWrite(CE, 0);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P2, RX_ADDRESS2, 1); // Use the same address on the RX device as the TX device
SPI_Write_Buf(WRITE_REG + RX_ADDR_P3, RX_ADDRESS3, 1); // Use the same address on the RX device as the TX device
SPI_Write_Buf(WRITE_REG + RX_ADDR_P4, RX_ADDRESS4, 1); // Use the same address on the RX device as the TX device
SPI_Write_Buf(WRITE_REG + RX_ADDR_P5, RX_ADDRESS5, 1); // Use the same address on the RX device as the TX device
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 + RX_PW_P1, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
SPI_RW_Reg(WRITE_REG + RX_PW_P2, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
SPI_RW_Reg(WRITE_REG + RX_PW_P3, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
SPI_RW_Reg(WRITE_REG + RX_PW_P4, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
SPI_RW_Reg(WRITE_REG + RX_PW_P5, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
SPI_RW_Reg(WRITE_REG + EN_AA, 0x3f); // Enable Auto.Ackipe0 3f:543210 ack.
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x3f); // Enable Pipe0 recive 3f 543210
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 + DYNENABLE, 0x3f); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
//SPI_RW_Reg(WRITE_REG + DYNACK, 0x06); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a);//
digitalWrite(CE, 1); // Set CE pin high to enable RX device
}
void RX_Mode()
{
currMode = 0;
digitalWrite(CE, 0);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:RX. RX_DR enabled..
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,MyselfAddr, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
SPI_Write_Buf(WRITE_REG + RX_ADDR_P1, RX_ADDRESS1, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
SPI_Write_Buf(WRITE_REG +TX_ADDR, MyselfAddr, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
digitalWrite(CE, 1); // Set CE pin high to enable RX device
// This device is now ready to receive one packet of 16 unsigned chars payload from a TX device sending to address
// '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
}
void TX_Mode(unsigned char num)//发射
{
currMode = 1;
digitalWrite(CE, 0);//
SPI_Write_Buf(WRITE_REG+TX_ADDR,TransferAddr,5);//
SPI_Write_Buf(WRITE_REG+RX_ADDR_P0,TransferAddr,5); //
SPI_Write_Buf(WR_TX_PLOAD,tx_buf,num);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); //
digitalWrite(CE, 1); // Set CE pin high to enable RX device
}
这样双向设置是可以的,有试过。
但是在转换收发模式时,有时好,有时坏。不知道什么原因?
是不是与时间有关??我打算修改一下延迟试试。 |