Go Down

Topic: Two SPI on Arduino UNO? (Read 1 time) previous topic - next topic

Fajkowsky

Hello , I have problem because I want to connect two device to my arduino. First is wireless module RFM70 and graphic display ST7565R. Both are with SPI interface.

I am using DOGM128 library for display and code for RFM70 from here

RFM70
ST7565R

mikesbaker

Make a second CS line on any digital pin:


MarkT

Yes, you definitely need one CS line per device.  One of them can be the SPI SS pin (which must be set as an output in any case), the others can be any free pin.

You have to be careful mixing different devices on the SPI bus, as they may have different settings for CPHA, CPOL and clock speed.  If so then you have to reprogram the SPI registers everytime you switch from one library to the other (its even worse if a library uses an interrupt routine that talks SPI).
[ I won't respond to messages, use the forum please ]

Fajkowsky

So if I want to choose one of the device I can do something like this?
Code: [Select]
...
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
...

When RFM70 CS pin is connect to arduino pin 2 if I choose it I disable ST7565R?


Grumpy_Mike

Quote
I can do something like this?

Not quite.
You drop the CE line just before you do a transfer and raise it straight after.
So you drop the appropriate CE line, not quite what you have shown.

Fajkowsky

Okey, I try change PIN_SS of st7565r but I have big problem. When I use deafult chip select pin (10) SCK is working normally when I changed PIN_SS to 5 SCK doesnt work(led diode stops blink).

I change values in dogm128.h in section #ifndef ADA_ST7565P_HW .

When I change only PIN_A0_DEFAULT to another I/O pin then it is working.


Fajkowsky

I connect rfm70 and st7565r and I wrote program:

Code: [Select]
#include <TouchScreen.h>
#include <Dogm.h>

//Wireless----------------------------------------------------
#include "RFM70.h"

RFM70class RFM70;
byte RFM70_buf[MAX_PACKET_LEN];  //RFM70 data buffer
unsigned long RFMSendTime = 0;   //next send time
int RFMSendSpeed=1;           //send packet every 1000 msec
//------------------------------------------------------------
int a0Pin = PIN_A0_DEFAULT;
Dogm dogm(7);

TouchScreen ts(3, 1, 0, 2);
int s,t = 0;              // s {10 - 100}  t {5 - 55}
int car_speed, car_turn = 0;  //car_speed {5 - 95 } car_turn {3 - 53}
int coords[2];


void setup(void){

  Serial.begin(9600);
  dogm.setContrast(10);
  //Wireless----------------------------------------------------
  RFM70.begin();
  RFM70.Initialize();
  RFMSendTime = millis();
  //------------------------------------------------------------
  pinMode(10, OUTPUT);
  pinMode(6, OUTPUT);
}

void read_speed(){
  if((coords[0] > 200) && (coords[0] < 800) && (coords[1] > 200) && (coords[1] < 815)) {
      Serial.print(", Speed:");
        s = constrain(map(coords[0], 200, 800, 10, 100), 10, 100);
        t = constrain(map(coords[1], 200, 800, 5, 55), 5, 55);
      Serial.print(s);
      Serial.print(":");
      Serial.println(t);
  }
}


void draw(){
    dogm.setHLine(20,110,30);  //speed
    dogm.setVLine(65,8,55);    // turn
   
    dogm.setVLine(120,0,63); 
    dogm.setHLine(0,119,5);
    dogm.setHLine(0,119,60); 
   
//wykrywanie przeszkód
/*if ( lewo == true ){
dogm.setBox(0,61,119,63);  // góra , czyli przeszkoda po lewej   
}

if (przodek == true){
dogm.setBox(121,0,127,63); 
}

if ( prawo == true ) {
dogm.setBox(0,0,119,4);
}  */
   
    dogm.setBox(115 - s,58-t,125 - s,64-t);

}
void loop(){
 
  DOG_PGM_P p;
  // zczytanie z panelu
  ts.read(coords);
  Serial.print(coords[0]);
  Serial.print(",");
  Serial.println(coords[1]);
 
  // komunikacja
 
  [b]digitalWrite(10,LOW);[/b]
  dogm.start();
  do{
  digitalWrite(10,LOW);
    read_speed();
    draw();
    car_speed = 115 - s;   //car_speed {5 - 95 } car_turn {3 - 53}
    car_turn =  58 - t;
  [b]digitalWrite(10,HIGH);  [/b]
   
   [b] digitalWrite(6,LOW);[/b]
    rfmTask();
 
    if (millis() > RFMSendTime)                   //send packet
    {                                                   
      RFMSendTime = millis() + RFMSendSpeed;
      RFM70_buf[0]=0X41;     
      RFM70_buf[1]=0X42;
      RFM70_buf[2]=0X43;
      RFM70_buf[3]=0X44;           
      RFM70.Send_Packet(WR_TX_PLOAD,RFM70_buf,3); 
    }
[b] digitalWrite(6,HIGH);[/b]
 
  } while (dogm.next() );
 
 
}

void rfmTask()                //RFM70 event handler
{
  if (RFM70.RfmInterrupt())
  {
    if (RFM70.TxDataSentInterrupt())
    {
       Serial.println("Data sent OK");
       RFM70.SwitchToRxMode();
    }
    if (RFM70.TxDataSentErrorInterrupt())
    {
       Serial.println("Data sent error");
       RFM70.SwitchToRxMode();
    }
  }
}


On pin 10 i have chip select display, on pin 6 i have chip select RFM70. And only display working. I dont know what I doing wrong.

Grumpy_Mike

I don't see you setting pin 5 to be an output.

Fajkowsky

#8
Jun 14, 2012, 02:28 pm Last Edit: Jun 14, 2012, 03:00 pm by Fajkowsky Reason: 1
Why I must pin 5 be as output? I dont use this pin.

Grumpy_Mike

Sorry I thought that was your second enable line.

However you still need to put the pin low and high immediately before and after the data transfer, not just some time before and some time after like you have. It should embrace each transfer, not just down at the start of a number of transfers and up after the batch.

Also it looks like the RFM70.h library is using the default pins as well as your own. You might want to try moving both devices off pin 10, onto pins you do control.

Go Up