const unsigned long Bank1_Reg0_13[]={ //latest config txt
void Init_RFM70()
{
int8 Temp;
SwitchToBank(0); // Not needed, Bank should be 0 after reset
SPI_Write_Reg(CONFIG, 0b00001110); // Enable CRC - 2 bytes CRC - POWER UP - PTX
SPI_Write_Reg(EN_AA, 0b00000000); // Disable Auto Acknowledge
SPI_Write_Reg(EN_RXADDR, 0b00000001); // Enable data pipe 0
SPI_Write_Reg(SETUP_AW, 0b00000011); // 5 bytes address
SPI_Write_Reg(SETUP_RETR, 0b00000000); // Re-Transmit disabled
SPI_Write_Reg(RF_CH, 23); // Set the frequency channel to 23
Temp = SPI_Read_Reg(RF_SETUP); // Read existing values in register
Temp = Temp & 0b11110111; // Air Data Rate 1Mbps
SPI_Write_Reg(RF_SETUP, Temp); // Write value to register
SPI_Write_Cmd(ACTIVATE, 0x73); // Activate registers: R_RX_PL_WID - W_ACK_PAYLOAD - W_TX_PAYLOAD_NOACK
SwitchToBank(1);
// Write predefined value 0x404B01E2 to register 0x00
CSN = 0; // Start SPI communication
SPI_RW(0x00|W_REGISTER); // Select register 0x00 and indicate a write operation
SPI_RW(0x40); // Write 1 byte
SPI_RW(0x4B); // Write 1 byte
SPI_RW(0x01); // Write 1 byte
SPI_RW(0xE2); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
// Write predefined value 0xC04B0000 to register 0x01
CSN = 0; // Start SPI communication
SPI_RW(0x01|W_REGISTER); // Select register 0x01 and indicate a write operation
SPI_RW(0xC0); // Write 1 byte
SPI_RW(0x4B); // Write 1 byte
SPI_RW(0x00); // Write 1 byte
SPI_RW(0x00); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
// Write predefined value 0xD0FC8C02 to register 0x02
CSN = 0; // Start SPI communication
SPI_RW(0x02|W_REGISTER); // Select register 0x02 and indicate a write operation
SPI_RW(0xD0); // Write 1 byte
SPI_RW(0xFC); // Write 1 byte
SPI_RW(0x8C); // Write 1 byte
SPI_RW(0x02); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
// Write predefined value 0x99003941 to register 0x03
CSN = 0; // Start SPI communication
SPI_RW(0x03|W_REGISTER); // Select register 0x03 and indicate a write operation
SPI_RW(0x99); // Write 1 byte
SPI_RW(0x00); // Write 1 byte
SPI_RW(0x39); // Write 1 byte
SPI_RW(0x41); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
// Write predefined value 0xD99E860B to register 0x04
CSN = 0; // Start SPI communication
SPI_RW(0x04|W_REGISTER); // Select register 0x04 and indicate a write operation
SPI_RW(0xF9); // Write 1 byte
SPI_RW(0x9E); // Write 1 byte
SPI_RW(0x86); // Write 1 byte
SPI_RW(0x0B); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
// Write predefined value 0x24067FA6 to register 0x05
CSN = 0; // Start SPI communication
SPI_RW(0x05|W_REGISTER); // Select register 0x05 and indicate a write operation
SPI_RW(0x24); // Write 1 byte
SPI_RW(0x06); // Write 1 byte
SPI_RW(0x7F); // Write 1 byte
SPI_RW(0xA6); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
// Write predefined value 0x00731200 to register 0x0C (in reversed order!)
CSN = 0; // Start SPI communication
SPI_RW(0x0C|W_REGISTER); // Select register 0x0C and indicate a write operation
SPI_RW(0x00); // Write 1 byte
SPI_RW(0x12); // Write 1 byte
SPI_RW(0x73); // Write 1 byte
SPI_RW(0x00); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
// Write predefined value 0x0080B436 to register 0x0D (in reversed order!)
CSN = 0; // Start SPI communication
SPI_RW(0x0D|W_REGISTER); // Select register 0x0D and indicate a write operation
SPI_RW(0x36); // Write 1 byte
SPI_RW(0xB4); // Write 1 byte
SPI_RW(0x80); // Write 1 byte
SPI_RW(0x00); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
/* Data is already written to RFM70
// Write predefined value 0x404B01E2 to register 0x00 (in reversed order!)
CSN = 0; // Start SPI communication
SPI_RW(0x0E|W_REGISTER); // Select register 0x0E and indicate a write operation
SPI_RW(0x41); // Write 1 byte
SPI_RW(0x20); // Write 1 byte
SPI_RW(0x08); // Write 1 byte
SPI_RW(0x04); // Write 1 byte
SPI_RW(0x81); // Write 1 byte
SPI_RW(0x20); // Write 1 byte
SPI_RW(0xCF); // Write 1 byte
SPI_RW(0xF7); // Write 1 byte
SPI_RW(0xFE); // Write 1 byte
SPI_RW(0xFF); // Write 1 byte
SPI_RW(0xFF); // Write 1 byte
CSN = 1; // Set CSN high (end SPI communication)
*/
}
// Put payload in the TX FIFO
CSN = 0; // Start SPI communication
SPI_RW(W_TX_PAYLOAD); // Write command
SPI_RW(0x31); // Write some random data to the FIFO for transmission
CSN = 1; // Set CSN high (end SPI communication)
// High pulse om CE to transmit packet
CE = 1;
delay_ms(1); // More than 10µs
CE = 0;
/**************************************************/
// Took this function from the Hope code
int8 SPI_RW(int8 value)
{
int8 bit_ctr;
for(bit_ctr=0; bit_ctr<8; bit_ctr++) // output 8-bit
{
if(value & 0x80)
{
MOSI=1;
}
else
{
MOSI=0;
}
value = (value << 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
value |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(value); // return read UINT8
}
/**************************************************/
// Write a command + data
void SPI_Write_Cmd(int8 command, int8 value)
{
CSN = 0; // Start SPI communication
SPI_RW(command); // Write command
SPI_RW(value); // Write command data
CSN = 1; // Set CSN high (end SPI communication)
}
/**************************************************/
// Write 1 byte to a register
void SPI_Write_Reg(int8 reg, int8 value)
{
CSN = 0; // Start SPI communication
SPI_RW(reg|0x20); // Select register to write to and indicate a write operation
SPI_RW(value); // Write value to register
CSN = 1; // Set CSN high (end SPI communication)
}
/**************************************************/
// Read a register
int8 SPI_Read_Reg(int8 reg)
{
int8 value;
CSN = 0; // Start SPI communication
SPI_RW(reg); // Select register to read from
value = SPI_RW(0); // Read register value
CSN = 1; // Set CSN high (end SPI communication)
return(value); // Return register value
}
/**************************************************/
// Switch to a bank, inspired by the Hope code
void SwitchToBank(int1 bank)
{
int8 BankStatus;
BankStatus = SPI_Read_Status(); // Read STATUS register
BankStatus = BankStatus & RBANK; // Extract the RBANK bit
// Check if the RBANK bit is indicating a different bank than the wanted
if(((bank == 0) && (BankStatus != 0)) || ((bank == 1) && (BankStatus == 0)))
{
SPI_Write_Cmd(ACTIVATE, 0x53); // Toggle the register bank
}
}
/**************************************************/
// The SwitchToBank() function uses another version of the read from register function
// We don't need to select a register to read from, to get data from the STATUS register (it is shifted out during the first byte write)
int8 SPI_Read_Status()
{
int8 value;
CSN = 0; // Start SPI communication
value = SPI_RW(NOP); // Read STATUS register
CSN = 1; // End SPI communication
return(value); // Return register value
}
SPI_Write_Reg(RX_PW_P0, 1); // 1 byte in RX payload in data pipe 0
Oh, in my init code, I first read the RF_SETUP register, change the bits and then writes it back. I do this because some of the bits are reserved, and to make sure I don't change them, I have to do it this way. Who knows, maybe they change away from the reset values at some point, so I'll better be safe. I'll do this with all registers containing reserved bits.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?