//SPI写寄存器 //reg:指定寄存器地址 //value:写入的值 uint8_t NRF24L01_Write_Reg(uint8_t reg,uint8_t value) { uint8_t status; NRF24L01_CS=0; //使能SPI传输 status =spi_read_write_byte(reg);//发送寄存器号 spi_read_write_byte(value); //写入寄存器的值 NRF24L01_CS=1; //禁止SPI传输 return(status); //返回状态值 }
//读取SPI寄存器值 //reg:要读的寄存器 uint8_t NRF24L01_Read_Reg(uint8_t reg) { uint8_t reg_val; NRF24L01_CS = 0; //使能SPI传输 spi_read_write_byte(reg); //发送寄存器号 reg_val=spi_read_write_byte(0XFF);//读取寄存器内容 NRF24L01_CS = 1; //禁止SPI传输 return(reg_val); //返回状态值 }
//在指定位置读出指定长度的数据 //reg:寄存器(位置) //*pBuf:数据指针 //len:数据长度 //返回值,此次读到的状态寄存器值 uint8_t NRF24L01_Read_Buf(uint8_t reg,uint8_t *pBuf,uint8_t len) { uint8_t status,i; NRF24L01_CS = 0; //使能SPI传输 status=spi_read_write_byte(reg);//发送寄存器值(位置),并读取状态值 for(i=0;i<len;i++){ pBuf[i]=spi_read_write_byte(0XFF);//读出数据 } NRF24L01_CS=1; //关闭SPI传输 return status; //返回读到的状态值 }
u16 readAdc(u8 cmd) { u8 high, low; u16 res = 0; spi_read_write_byte(cmd); high = spi_read_write_byte(0x00); low = spi_read_write_byte(0x00); res = high<<8; res = (res | low)>>3; if(cmd == CMD_RDY && res == 4095){ res = 0; } return res; }