void Receiver(void) { unsigned char i=0; TXEN = 0; TRX_CE = 1; //进入接收模式 while(DR == 0) //DR=0表示数据未准备好,等待 ; TRX_CE=0; //进入待机模式才可读写SPI RACSN = 0; //内部SPI开始等待命令(1到0的跳变) SpiReadWrite(RRP); /* while(DR == 1) { UART_BUFFER[i] = SpiReadWrite(0); i++; } */ UART_BUFFER[0] = SpiReadWrite(0); for(i=1;i<=UART_BUFFER[0];i++) //****修改处***** { UART_BUFFER[i] = SpiReadWrite(0); } P04 = ~P04; RACSN = 1; // TRX_CE = 1; //这时MCU才从SPI读数据 }
void TransmitPacket(unsigned char b) { RACSN = 0; SpiReadWrite(WTP); SpiReadWrite(b); RACSN = 1; TRX_CE = 1; Delay400us(1); TRX_CE = 0; }
void Init(void) { unsigned char tmp; unsigned char cklf; //serial port TH1 = 243; // 19200@16MHz (when T1M=1 and SMOD=1) CKCON |= 0x10; // T1M=1 (/4 timer clock) PCON = 0x80; // SMOD=1 (double baud rate) //SCON = 0x52; // Serial mode1, enable receiver SCON = 0x72; // Serial mode1, rx+tx TMOD = 0x20; // Timer1 8bit auto reload TR1 = 1; // Start timer1 P0_ALT |= 0x06; // Select alternate functions on pins P0.1 and P0.2 P0_DIR |= 0x02; // P0.1 (RxD) is input SPICLK = 0; // Max SPI clock SPI_CTRL = 0x02; // Connect internal SPI controller to Radio // Switch to 16MHz clock: RACSN = 0; SpiReadWrite(RRC | 0x09); cklf = SpiReadWrite(0) | 0x04; RACSN = 1; RACSN = 0; SpiReadWrite(WRC | 0x09); SpiReadWrite(cklf); RACSN = 1; #if 1 //RF //P0_ALT = 0x00; //P0_DIR = 0xFF; //SPICLK = 0; // Max SPI clock //SPI_CTRL = 0x02; // Connect internal SPI controller to Radio // Configure Radio: RACSN = 0; SpiReadWrite(WRC | 0x03); // Write to RF config address 3 (RX payload) SpiReadWrite(0x01); // One byte RX payload SpiReadWrite(0x01); // One byte TX payload RACSN = 1; RACSN = 0; SpiReadWrite(RRC | 0x01); // Read RF config address 1 tmp = SpiReadWrite(0) & 0xf1; // Clear the power and frequency setting bits RACSN = 1; RACSN = 0; SpiReadWrite(WRC | 0x01); // Write RF config address 1 // Change power defined by POWER and to 433 or 868/915MHz defined by HFREQ above: SpiReadWrite(tmp | (POWER <<2) | (HFREQ << 1)); RACSN = 1; #endif }
void Init(void) //nRF9E5无线模块初始化 { unsigned char tmp,cklf; SPICLK = 0; // Max SPI clock SPI_CTRL = 0x02; // Connect internal SPI controller to Radio // Configure Radio: RACSN = 0; SpiReadWrite(WRC | 0x03); // Write to RF config address 3 (RX payload) SpiReadWrite(0x01); // One byte RX payload SpiReadWrite(0x01); // One byte TX payload RACSN = 1; RACSN = 0; SpiReadWrite(RRC | 0x01); // Read RF config address 1 tmp = SpiReadWrite(0) & 0xf1; // Clear the power and frequency setting bits //PA_PWR[1:0]=00(输出电源-10dBm)Bit1:HFREQ_PLL=0(433MHz) RACSN = 1; RACSN = 0; SpiReadWrite(WRC | 0x01); // Write RF config address 1 SpiReadWrite(tmp | (HFREQ << 1)); // Change to 433 or 868/915MHz defined by HFREQ above: RACSN = 1; // 将时钟频率改为16MHz: RACSN = 0; SpiReadWrite(RRC | 0x09); cklf = SpiReadWrite(0) | 0x1c; RACSN = 1; RACSN = 0; SpiReadWrite(WRC | 0x09); SpiReadWrite(cklf); RACSN = 1; RACSN = 0; SpiReadWrite(WRC | 0x05); //设置本接收器可接受的地址,与tx433里的配置一样才可通信 SpiReadWrite(0x18); SpiReadWrite(0x32); SpiReadWrite(0x40); SpiReadWrite(0xAF); RACSN = 1; RACSN = 0; SpiReadWrite(WRC | 0x03); //设置本接收器可接受的数据宽度,与tx433里的配置一样才可通信 SpiReadWrite(0x20); //接受数据宽度为32个字节 RACSN = 1; }
U8 radio_hal_SpiReadByte(void) { return SpiReadWrite(0xFF); }
void radio_hal_SpiWriteByte(U8 byteToWrite) { SpiReadWrite(byteToWrite); }