Hi, i have trouble connecting to my arduino uno two spi devices. The first one is a cc1101 rf module and the other one is a SD Card Module.
My wiring for the rf module is on the picture below.
For the wiring of the sd card module i used a CD4050BE and the wiring is on the pictures below.
So for the rf module i used pin 10 as SS and for the sd card i used pin 4.
This is my code
#include <cc1101.h>
#include <ccpacket.h>
#include <SPI.h>
#include <SD.h>
//Pin select για πομποδεκτη
#define CC1101Interrupt 0
#define CC1101_GDO0 2
#define CC1101_SS_PIN 10
//Pin select για SD
#define SD_SS_PIN 4
//Μεταβλητη πομποδεκτη
byte syncWord[2] = {199, 4};
bool packetWaiting;
//Μεταβλητες θεσης
String locat;
int StDir=0;
//Ταυτοτητα αυτοκινητου
const char *ID= "NHE2443";
CC1101 cc1101; //Βιβλιοθηκη πομποδεκτη
File dataFile;//Βιβλιοθηκη SD
//Ελεγχος παραδοσης μηνυματος
void messageReceived()
{
packetWaiting = true;
}
void setup()
{
Serial.begin(9600); // the Serial port of Arduino baud rate.
SPI.begin();
pinMode(SD_SS_PIN, OUTPUT);
pinMode(CC1101_SS_PIN, OUTPUT);
digitalWrite(SD_SS_PIN, HIGH);
digitalWrite(CC1101_SS_PIN, HIGH);
cc1101.init();
cc1101.setSyncWord(syncWord);
cc1101.setCarrierFreq(CFREQ_433);
cc1101.disableAddressCheck();
cc1101.setTxPowerAmp(PA_LongDistance);
// SD Card Initialization
if (SD.begin(4))
{
Serial.println("SD card is ready to use.");
}
else
{
Serial.println("SD card initialization failed");
return;
}
attachInterrupt(CC1101Interrupt, messageReceived, FALLING);
Serial.println("Waiting to enter the highway");
}
void loop()
{
if (StDir == 0)
{
digitalWrite(SD_SS_PIN, HIGH);
digitalWrite(CC1101_SS_PIN, LOW);
Entry_message();
digitalWrite(CC1101_SS_PIN, HIGH);
digitalWrite(SD_SS_PIN, HIGH);
}
else
{
digitalWrite(SD_SS_PIN, HIGH);
digitalWrite(CC1101_SS_PIN, LOW);
Exit_message();
digitalWrite(CC1101_SS_PIN, HIGH);
digitalWrite(SD_SS_PIN, HIGH);
}
}
void Entry_message()
{
if (packetWaiting)
{
detachInterrupt(CC1101Interrupt);
packetWaiting = false;
CCPACKET packet;
if (cc1101.receiveData(&packet) > 0)
{
Serial.println(F("Message Received"));
if (!packet.crc_ok)
{
Serial.println(F("crc not ok"));
}
if (packet.crc_ok && packet.length > 0)
{
String message((const char*)packet.data);
Serial.println(message); //Μηνυμα πομπου
locat=message.substring(4); //Ονομα περιοχης
StDir=message.substring(0,3).toInt(); //Κατευθυνση οχηματος
//Αποστολη ID
detachInterrupt(CC1101Interrupt);
CCPACKET packet;
packet.length = strlen(ID) + 1;
strncpy((char *) packet.data, ID, packet.length);
cc1101.sendData(packet);
Serial.println(F("ID sent"));
cc1101.setCarrierFreq(CFREQ_868);
String Entry = locat; //Διευθυνση εισοδου
SD_Save();
Serial.print("Entry: ");Serial.println(Entry);
Serial.println("");
}
}
attachInterrupt(CC1101Interrupt, messageReceived, FALLING);
}
}
void Exit_message()
{
if (packetWaiting)
{
detachInterrupt(CC1101Interrupt);
packetWaiting = false;
CCPACKET packet;
if (cc1101.receiveData(&packet) > 0)
{
Serial.println(F("Message Received"));
if (!packet.crc_ok)
{
Serial.println(F("crc not ok"));
}
if (packet.crc_ok && packet.length > 0)
{
String message((const char*)packet.data);
Serial.println(message); //Μηνυμα πομπου
locat=message.substring(4); //Ονομα περιοχης
StDir=message.substring(0,3).toInt(); //Κατευθυνση οχηματος
if (StDir==0)
{
//Αποστολη ID
detachInterrupt(CC1101Interrupt);
CCPACKET packet;
packet.length = strlen(ID) + 1;
strncpy((char *) packet.data, ID, packet.length);
cc1101.sendData(packet);
Serial.println(F("ID sent"));
cc1101.setCarrierFreq(CFREQ_433);
StDir=0;
String Exit = locat; //Διευθυνση εισοδου
SD_Save();
Serial.print("Exit: ");Serial.println(Exit);
Serial.println("");
}
}
}
attachInterrupt(CC1101Interrupt, messageReceived, FALLING);
}
}
void SD_Save()
{
digitalWrite(CC1101_SS_PIN, HIGH);
digitalWrite(SD_SS_PIN, LOW);
dataFile = SD.open("NHE2443.txt", FILE_WRITE);
// Αποθηκευση των σταθμων στην SD
dataFile.print("Station: ");
dataFile.println(locat);
dataFile.close();
digitalWrite(CC1101_SS_PIN, HIGH);
digitalWrite(SD_SS_PIN, HIGH);
}
With the same wiring if i use these programms seperetaly everything works fine, but when i try to combine them, nothing works. My rf module doesn't even initialize.
I have read about SPI but i can't make it work.
If someone could help me, i would be thankful.