Go Down

Topic: CC1101 and SD Card reader (SPI Problem) (Read 232 times) previous topic - next topic

Chrypapa

Jan 23, 2019, 04:14 pm Last Edit: Jan 24, 2019, 01:53 pm by Chrypapa
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
Code: [Select]
#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.

Robin2

Some of the SD Card modules don't work nicely with other SPI devices. There is a workaround but you will need to search the Forum for it as I don't have any SD Card module myself.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pylon

Many cheap Chinese SD card modules don't handle the MISO signal line not correctly as they don't tri-state it if CS is pulled high. For some of them you can fix that but as the used chip is simply a quad-driver you have to take care that you fix it correctly for your device. Even if the use the same chip, the wiring may be differently and you have to fix it different from the tutorials on the net.

If you're good at soldering and if you're able to reverse engineer the traces of your module to a schematic, you might try the fix. Otherwise I recommend to buy an SD card reader from a vendor with support (Adafruit, Sparkfun, etc.).

Chrypapa

So it is the sd card module fault that the rf module won't initialize?

Robin2

So it is the sd card module fault that the rf module won't initialize?
That's easily tested by trying a program without code for the sd card module and without the module connected.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Chrypapa

That's easily tested by trying a program without code for the sd card module and without the module connected.
I already tried that, everything works fine by its own. I searched a lot yesterday and you are both probably right. It's the sd card reader fault. I haven't  yet found a way to fix it, but thank you for helping me.

gbafamily

If one device supports software SPI, connect it to some other pins.

Chrypapa

If one device supports software SPI, connect it to some other pins.
Where can i found out if any device supports software SPI?


Also,i did some tests both the ss pins of the sd card module and the rf module get succesfully HIGH or LOW, so i think the only problem is that the cc1101 doesn't initialize.

Chrypapa

Also some times my sd card doesn't initialize without changing anything and some minutes later it does.
I would also like to ask if by changing spi modes, if it would help or even set the bit order.

I really can't tell what spi mode my rf modules need(i know about the sd card-> mode0) or the bit order.

https://github.com/veonik/arduino-cc1101

I use this library for my rf modules.

Any help would be great.

gbafamily

Look at the examples included with the library. The sdfat library includes an example named softwarespi. If that fails, look at the source code. You can learn a lot from library source code.

jremington

#10
Jan 24, 2019, 10:31 pm Last Edit: Jan 24, 2019, 10:32 pm by jremington
Another option is to not use the SD card, and instead a serial port data logger like the Sparkfun OpenLog. Works really well with either software or hardware serial.

Your Arduino program will be much smaller as a result.

Chrypapa

Well i tried software spi with only the sd and it worked. Then i put the rf module (via hardware spi) and it also worked and i got the message from the modules. But when i tried it again the modules wouldn't communicate. I tried my old programm with the rf modules and they still cannot communicate.

Do you have any idea what might changed?

pylon

Quote
Well i tried software spi with only the sd and it worked. Then i put the rf module (via hardware spi) and it also worked and i got the message from the modules. But when i tried it again the modules wouldn't communicate. I tried my old programm with the rf modules and they still cannot communicate.
Did you change the pins for software SPI? You cannot use the same pins to do software SPI and hardware SPI as the problem you have is a hardware problem. Your SD card reader is blocking the MISO pin.

Go Up