I’m trying to test the RFM22B transceiver modules using the simplest code possible and it’s not working. First of all, I don’t know if the antenna is the problem. It says on the product page that a 17 cm antenna worked. I soldered a smaller wire on one of the transceiver modules due to space constraints. WIll it really only work if the antenna is this length? As for the code, I read the datasheet for the RFM22B and it shows the SDI format as R/W A5 A4 A3 A2 A1 A0 D7 D6 D5 D4 D3 D2 D1 D0. This is the transmit code I loaded into one Arduino Pro Mini:
//SPSR = status register
//SPDR = data register
//SPCR = control register
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
byte clr;
// R/W = 1 signifies WRITE, R/W = 0 signifies READ
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}
void setup()
{
Serial.begin(9600);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);
}
byte write_transceiver(void)
{
int data;
digitalWrite(SLAVESELECT,LOW);
//data = 1000000111111111
//R/W = 1, address = 1, send "11111111"
data = spi_transfer((char)(0x81FF));
digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
return data;
}
void loop()
{
int data;
data = write_transceiver();
}
This is the receive program I loaded into my other Arduino Pro Mini:
//SPSR = status register
//SPDR = data register
//SPCR = control register
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
byte clr;
// R/W = 1 signifies WRITE, R/W = 0 signifies READ
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}
void setup()
{
Serial.begin(9600);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);
}
byte read_transceiver(void)
{
int data;
digitalWrite(SLAVESELECT,LOW);
//data = 0000000100000000
//R/W = 0, address = 1
data = spi_transfer((char)(0x0100));
digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
return data;
}
void loop()
{
char result;
result = read_transceiver();
Serial.print("result:");
Serial.print(result,BIN);
Serial.print("\n");
}
The result printed is “0” when it should be “11111111” that is received. Can you see anything wrong or do you have any insight into the issues here? Thank you!!