I use CC1101 433MHz RF-module with PanStamp library to make a 2 way communication.
I have finished transmitting and receiving the data, like in the code below:
Transmitting:
#include <cc1101.h>
#include <EEPROM.h>
#include <Wire.h>
#include <PS2X_lib.h>
// Create a objekct
CC1101 cc1101;
PS2X ps2x;
// VARIABELS
int error = 0;
byte type = 0;
// counter to get increment in each loop
byte counter;
byte b;
byte syncWord = 199;
void setup()
{
Serial.begin(9600);
error = ps2x.config_gamepad(9,8,7,6, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
if(error == 0){
Serial.println("Found Controller, configured successful");
}
else if(error == 1)
Serial.println("No controller found, check wiring.");
else if(error == 2)
Serial.println("Controller found but not accepting commands.");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType();
switch(type) {
case 0:
Serial.println("Unknown Controller type");
break;
case 1:
Serial.println("DualShock Controller Found");
break;
}
Serial.println("starter RF");
// reset the counter
counter=0;
Serial.println("initializing...");
// initialize the RF Chip
cc1101.init();
cc1101.setSyncWord(&syncWord, false);
cc1101.setCarrierFreq(CFREQ_433);
cc1101.disableAddressCheck();
//cc1101.setTxPowerAmp(PA_LowPower);
delay(1000);
Serial.print("CC1101_PARTNUM "); //cc1101=0
Serial.println(cc1101.readReg(CC1101_PARTNUM, CC1101_STATUS_REGISTER));
Serial.print("CC1101_VERSION "); //cc1101=4
Serial.println(cc1101.readReg(CC1101_VERSION, CC1101_STATUS_REGISTER));
Serial.print("CC1101_MARCSTATE ");
Serial.println(cc1101.readReg(CC1101_MARCSTATE, CC1101_STATUS_REGISTER) & 0x1f);
Serial.println("device initialized");
//Serial.println("done");
}
void loop()
{
ps2x.read_gamepad(false, false); //read controller and set large motor to spin at 'vibrate' speed
CCPACKET data;
data.length=1;
//byte data = 0;
if(ps2x.ButtonPressed(PSB_RED)){ //will be TRUE if button was JUST pressed
Serial.println("Circle just pressed");
data.data[0] = 20;
Serial.println(data.data[0]);
cc1101.sendData(data);
}
else if(ps2x.ButtonReleased(PSB_RED)){ //will be TRUE if button was JUST released
Serial.println("Circle just released");
data.data[0] = 0;
Serial.println(data.data[0]);
cc1101.sendData(data);
}
}
Receiving:
#include <Wire.h>
#include <EEPROM.h>
#include "cc1101.h"
// The connection to the hardware chip CC1101 the RF Chip
CC1101 cc1101;
int j;
byte x;
byte b;
byte i;
byte syncWord = 199;
long counter = 0;
byte chan = 0;
// a flag that a wireless packet has been received
boolean packetAvailable = false;
/* Handle interrupt from CC1101 (INT0) gdo0 on pin2 */
void cc1101signalsInterrupt(void) {
// set the flag that a package is available
packetAvailable = true;
}
void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("start");
// initialize the RF Chip
cc1101.init();
cc1101.setSyncWord(&syncWord, false);
cc1101.setCarrierFreq(CFREQ_433);
cc1101.disableAddressCheck(); //if not specified, will only display "packet received"
//cc1101.setTxPowerAmp(PA_LowPower);
Serial.print("CC1101_PARTNUM "); //cc1101=0
Serial.println(cc1101.readReg(CC1101_PARTNUM, CC1101_STATUS_REGISTER));
Serial.print("CC1101_VERSION "); //cc1101=4
Serial.println(cc1101.readReg(CC1101_VERSION, CC1101_STATUS_REGISTER));
Serial.print("CC1101_MARCSTATE ");
Serial.println(cc1101.readReg(CC1101_MARCSTATE, CC1101_STATUS_REGISTER) & 0x1f);
attachInterrupt(0, cc1101signalsInterrupt, FALLING);
Serial.println("device initialized");
}
void ReadLQI()
{
byte lqi = 0;
byte value = 0;
lqi = (cc1101.readReg(CC1101_LQI, CC1101_STATUS_REGISTER));
value = 0x3F - (lqi & 0x3F);
Serial.print("CC1101_LQI ");
Serial.println(value);
}
void ReadRSSI()
{
byte rssi = 0;
byte value = 0;
rssi = (cc1101.readReg(CC1101_RSSI, CC1101_STATUS_REGISTER));
if (rssi >= 128)
{
value = 255 - rssi;
value /= 2;
value += 74;
}
else
{
value = rssi / 2;
value += 74;
}
Serial.print("CC1101_RSSI ");
Serial.println(value);
}
void loop()
{
if (packetAvailable) {
Serial.println("packet received");
// Disable wireless reception interrupt
detachInterrupt(0);
ReadRSSI();
ReadLQI();
// clear the flag
packetAvailable = false;
CCPACKET packet;
if (cc1101.receiveData(&packet) > 0) {
if (!packet.crc_ok) {
Serial.println("crc not ok");
}
else if (packet.crc_ok){
if (packet.length > 0) {
Serial.print("packet: len ");
Serial.print(packet.length);
Serial.print(" data: ");
j = 0;
x = packet.data[j];
Serial.print(x);
Serial.print(" ");
Wire.beginTransmission(4);// transmit to device #4
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop tran
Serial.println(".");
}
}
}
// Enable wireless reception interrupt
attachInterrupt(0, cc1101signalsInterrupt, FALLING);
}
}
The CC1101 is a half-duplex type architecture. It cannot transmit while receiving, however it can switch between transmit and receive very quickly.
Then my question is should I implement a Chip Select to be able to communicate as transceiver? If yes, do you have please some idea how to do it? I,m pretty blank.