-
-
Save edencakir/4aa485921a8eb0fbec7dc7b8a78b70dc to your computer and use it in GitHub Desktop.
rf code test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
const int MOSIp = 11; | |
const int MISOp = 12; | |
const int SCKp = 13; | |
const int SSp = A4; | |
byte INITIALIZATION_COMMANDS[43][2] = { | |
{0x86, 0x00}, //skipped read section | |
{0x85, 0x00}, | |
{0x87, 0x01}, | |
{0x89, 0x7F}, | |
{0x8A, 0x05}, | |
{0x8B, 0xF4}, | |
{0x8C, 0xED}, | |
{0x8D, 0xFD}, | |
{0x8E, 0x00}, | |
{0xF0, 0x20}, | |
{0xEE, 0x13}, | |
{0xEF, 0xA9}, | |
{0xB0, 0x8C}, | |
{0xB2, 0xFF}, | |
{0xB3, 0x42}, | |
{0xB4, 0x40}, | |
{0xB6, 0x2D}, | |
{0xB7, 0xD4}, | |
{0xB8, 0x00}, | |
{0xB9, 0x00}, | |
{0xBA, 0x73}, | |
{0xBB, 0x6F}, | |
{0xBC, 0x6E}, | |
{0xBD, 0x67}, | |
{0xBE, 0x11}, | |
{0xED, 0X0F}, | |
{0xF9, 0x00}, | |
{0xFA, 0x00}, | |
{0xF1, 0x22}, | |
{0xF2, 0x38}, | |
{0xF3, 0x00}, | |
{0xF4, 0x00}, | |
{0xF5, 0x53}, | |
{0xF6, 0x64}, | |
{0xF7, 0x00}, // skipped read section | |
{0x86, 0x00}, | |
{0x85, 0x00}, | |
{0x87, 0x01}, | |
{0xFE, 0x3F}, | |
{0x88, 0x03}, | |
{0x88, 0x00}, | |
{0x87, 0x05}, | |
{0x85, 0x02} // final read section | |
}; | |
byte DATA_TRANSMISSION_COMMANDS[50][2] = { | |
{0x86, 0x00}, //skipped read section | |
{0x85, 0x00}, | |
{0x87, 0x01}, | |
{0x89, 0x7F}, | |
{0x8A, 0x05}, | |
{0x8B, 0xF4}, | |
{0x8C, 0xED}, | |
{0x8D, 0xFD}, | |
{0x8E, 0x00}, | |
{0xF0, 0x20}, | |
{0xEE, 0x13}, | |
{0xEF, 0xA9}, | |
{0xB0, 0x8C}, | |
{0xB2, 0xFF}, | |
{0xB3, 0x42}, | |
{0xB4, 0x40}, | |
{0xB6, 0x2D}, | |
{0xB7, 0xD4}, | |
{0xB8, 0x00}, | |
{0xB9, 0x00}, | |
{0xBA, 0x73}, | |
{0xBB, 0x6F}, | |
{0xBC, 0x6E}, | |
{0xBD, 0x67}, | |
{0xBE, 0x11}, | |
{0xED, 0X0F}, | |
{0xF9, 0x00}, | |
{0xFA, 0x00}, | |
{0xF1, 0x22}, | |
{0xF2, 0x38}, | |
{0xF3, 0x00}, | |
{0xF4, 0x00}, | |
{0xF5, 0x53}, | |
{0xF6, 0x64}, | |
{0xF7, 0x00}, // skipped read section | |
{0x86, 0x00}, | |
{0x85, 0x00}, | |
{0x87, 0x01}, | |
{0xFE, 0x3F}, | |
{0x88, 0x03}, | |
{0x88, 0x00}, | |
{0xB4, 0x40}, | |
{0xBE, 0x04}, | |
{0xFF, 0xB2},//Data transmission part begins here | |
{0xFF, 0xE4}, | |
{0xFF, 0xA1}, | |
{0xFF, 0x37}, | |
{0x85, 0x04}, // reading skipped | |
{0x87, 0x09} | |
}; | |
const byte RELAY_OFF = 0xA0; | |
const byte RELAY_ON = 0xA1; | |
const byte RELAY_ON_10m = 0xA2; | |
const byte RELAY_OFF_10m = 0xA3; | |
const byte MATCH_MODE = 0xAF; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Code started"); | |
pinMode(SSp, OUTPUT); | |
pinMode(MISO, INPUT); | |
SPI.begin(); | |
writeToRegister(0x87, 0x80); | |
delay(250); | |
readRegister(); | |
delay(2000); | |
initializeRFModule(); | |
transmitData(); | |
} | |
void initializeRFModule() { | |
Serial.println("Initialization started."); | |
for (int i = 0; i < 43; i++) { | |
if ( i == 35 || i == 42) { | |
Serial.println(""); | |
readRegister(); | |
} | |
Serial.print("*"); | |
writeToRegister(INITIALIZATION_COMMANDS[i][0], INITIALIZATION_COMMANDS[i][1]); | |
delay(20); | |
} | |
Serial.println(""); | |
Serial.println("Initialization complete."); | |
} | |
void transmitData() { | |
Serial.println("Transmission started."); | |
for (int i = 0; i < 50; i++) { | |
if ( i == 35 || i == 48) { | |
Serial.println(""); | |
readRegister(); | |
} | |
if ( i == 46 ) { | |
writeToRegister(DATA_TRANSMISSION_COMMANDS[i][0], RELAY_ON); | |
} else { | |
writeToRegister(DATA_TRANSMISSION_COMMANDS[i][0], DATA_TRANSMISSION_COMMANDS[i][1]); | |
} | |
Serial.print("*"); | |
delay(20); | |
} | |
Serial.println(""); | |
Serial.println("Transmission complete."); | |
} | |
void writeToRegister(byte address, byte value) { | |
SPI.beginTransaction(SPISettings(400000, MSBFIRST, SPI_MODE0)); | |
digitalWrite(SSp, LOW); | |
SPI.transfer(address); | |
SPI.transfer(value); | |
digitalWrite(SSp, HIGH); | |
SPI.endTransaction(); | |
} | |
unsigned int readRegister() { | |
byte inByte = 0; // incoming byte from the SPI | |
unsigned int result = 0, result2 = 0; // result to return | |
digitalWrite(SSp, LOW); | |
// send the device the register you want to read: | |
Serial.println("Reading 0x03"); | |
SPI.transfer(0x03); | |
result = SPI.transfer(0xFF); | |
Serial.println(result); | |
Serial.println("Reading 0x04"); | |
SPI.transfer(0x04); | |
result2 = SPI.transfer(0xFF); | |
Serial.println(result2); | |
digitalWrite(SSp, HIGH); | |
delay(50); | |
return (result); | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment