
VMA322
V. 02 – 08/05/2019
9
©Velleman nv
*
* Description:
* Writes one unsigned char to nRF24L01, and return the unsigned char read
* from nRF24L01 during write, according to SPI protocol
**************************************************/
unsigned char SPI_RW(unsigned char Byte)
{
unsigned char i;
for(i=0;i<8;i++) // output 8-bit
{
if(Byte&0x80)
{
SPI_PORT |=MOSI; // output 'unsigned char', MSB to MOSI
}
else
{
SPI_PORT &=~MOSI;
}
SPI_PORT|=SCK; // Set SCK high..
Byte <<= 1; // shift next bit into MSB..
if(SPI_IN & MISO)
{
Byte |= 1;
// capture current MISO bit
}
SPI_PORT&=~SCK; // ..then set SCK low again
}
return(Byte); // return read unsigned char
}
/**************************************************/
/**************************************************
* Function: SPI_RW_Reg();
*
* Description:
* Writes value 'value' to register 'reg'
/**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
unsigned char status;
SPI_PORT&=~CSN; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
SPI_PORT|=CSN; // CSN high again
return(status); // return nRF24L01 status unsigned char
}
/**************************************************/
/**************************************************
* Function: SPI_Read();
*
* Description:
* Read one unsigned char from nRF24L01 register, 'reg'