Arduino MEGA2560 SPI mit RFM22B funzt nicht?

Hi,

ich bekomme meine RFM22B am Arduino MEGA2560 Board irgendwie nicht zum laufen. Dasselbe RFM22B Modul funzt am UNO Board mit dem gleichen Sketch und der RadioHead library (RadioHead: RadioHead Packet Radio library for embedded microprocessors) problemlos???

Angeschlossen habe ich das eigentlich auch richtig...

GND----------GND-\ (ground in)
SDN-/ (shutdown in)
3V3----------VCC (3.3V in)
interrupt 0 pin D2-----------NIRQ (interrupt request out)
SS pin D53----------NSEL (chip select in)
SCK pin D52----------SCK (SPI clock in)
MOSI pin D51----------SDI (SPI Data in)
MISO pin D50----------SDO (SPI data out)

#include <SPI.h>
#include <RH_RF22.h>

// Singleton instance of the radio driver
RH_RF22 rf22;

void setup()
{
  Serial.begin(57600);
  Serial.print("booting...");

  if (!rf22.init())
    Serial.println("init failed");
  // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
}

void loop()
{
  Serial.println("Sending to rf22_server");
  // Send a message to rf22_server
  uint8_t data[] = "Hello World!";
  rf22.send(data, sizeof(data));

  rf22.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_RF22_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (rf22.waitAvailableTimeout(500))
  {
    // Should be a reply message for us now
    if (rf22.recv(buf, &len))
    {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is rf22_server running?");
  }
  delay(400);
}

Der Sketch hängt nach dem "booting.." und ich sehe noch "init" im Ausgabefenster aber das wars...
Ich vermute, das dass irgendwie an der SPI Schnittstellen Config liegt und das ich an der was verändern muss...?

Evtl kann man das mit RH_RF22::RH_RF22 (uint8_t slaveSelectPin = SS,uint8_t interruptPin = 2, RHGenericSPI &spi = hardware_spi) einstellen, aber leider habe ich da keinen Plan was da eingestellt werden muss?

Gruß
Snoops

Bist Du sicher daß SS pin D53 richtig ist?
Der SS Ausgang wechselt nicht automatisch wenn Du von einem UNO auf eienn Mega umsteigst.
Grüße Uwe

hi,

nachdem Du den slave-select im sketch nicht änderst, kann es mit 53 nicht funken. nimm doch als SS den gleichen wie am UNO.

gruß stefan

Hi,

wenn ich den SS pin/NSEL (chip select in) auf D10 stecke tut sich auch nix, ausser das im Ausgabefenster "init failed" anstatt nur "init" erscheint.

Bist Du sicher daß SS pin D53 richtig ist?
Der SS Ausgang wechselt nicht automatisch wenn Du von einem UNO auf eienn Mega umsteigst.

Nee, eigentlich nicht. Wie ändere ich den SS Pin im Sketch?

# define SS 53

Gruß
Snoops

Hi,

ich habe die SPI Schnittstelle mal ans Oszi angeschlossen.

Auf dem UNO Board sieht es OK aus (siehe Anhang), mal abgesehen von den Signalverläufen die evtl. von meinem Messaufbau kommen…

Dann gleicher Code auf dem MEGA2560 Board (siehe Anhang). Wie es scheint kommt via SCK (Pin52) gar kein Clock Signal raus.

WTF ist das???

Testcode:

#include <SPI.h>

// -------------------- SPI Daten ----------------------------------------------------------
// pins used for the connection with the sensor
// the other you need are controlled by the SPI library):
const int chipSelectPin = 10;


//==============================================================================================================
//                         Zusatzfunktionen
//==============================================================================================================
static void showNibble (byte nibble) {
  char c = '0' + (nibble & 0x0F);
  if (c > '9')
    c += 7;
  Serial.print(c);
}

static void showByte (byte value) {
    showNibble(value >> 4);
    showNibble(value);
}


//==============================================================================================================
//                          Initialisieren
//==============================================================================================================
void setup() {
  // Analogem Pin in den Ausgabemodus setzen
   pinMode(A0, OUTPUT);  //nur setzten keine Funktion
   pinMode(A1, OUTPUT);  //nur setzten keine Funktion
   pinMode(A2, OUTPUT);  //nur setzten keine Funktion
   pinMode(A3, OUTPUT);  //nur setzten keine Funktion
   pinMode(A4, OUTPUT);  //nur setzten keine Funktion
   pinMode(A5, OUTPUT);  //nur setzten keine Funktion

  Serial.begin(57600);
  Serial.print("Baudrate ");
  Serial.println(57600);
  Serial.println("RFM22B SPI Test V1.0");
  Serial.print("booting...");
 
 
  // initalize the  data ready and chip select pins:
  pinMode(chipSelectPin, OUTPUT);

  // start the SPI library:
  SPI.begin();
  
  //Sets the order of the bits shifted out of and into the SPI bus, either LSBFIRST (least-significant bit first)
  //or MSBFIRST (most-significant bit first).
  SPI.setBitOrder(MSBFIRST);

  //Sets the SPI clock divider relative to the system clock. On AVR based boards, the dividers available are
  //2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter
  //the frequency of the system clock (4 Mhz for the boards at 16 MHz).
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  
  //Sets the SPI data mode: that is, clock polarity and phase. See the Wikipedia article on SPI for details.
  // Möglich ist       SPI_MODE0 / SPI_MODE1 / SPI_MODE2 / SPI_MODE3
  SPI.setDataMode(SPI_MODE0);

  delay(30);

   digitalWrite(chipSelectPin, LOW);
   // send the device the register you want to read:
   SPI.transfer(0x00);
   // send a value of 0 to read the first byte returned:
   byte result = SPI.transfer(0x00);
   showByte(result);
   digitalWrite(chipSelectPin, HIGH);

  Serial.println(" done!");
}

//==============================================================================================================
//                         Arbeitsschleife
//==============================================================================================================
void loop()
{

   digitalWrite(chipSelectPin, LOW);
   // send the device the register you want to read:
   SPI.transfer(0x00);
   // send a value of 0 to read the first byte returned:
   byte result = SPI.transfer(0x00);
   showByte(result);
   Serial.println();
   digitalWrite(chipSelectPin, HIGH);

delay (5000);
}

Gruß
Snoops

pinMode(53, OUTPUT); // make the Arduino SPI Master

10 auf dem Uno.

Ob du 53 oder 10 oder einen anderen verwendest, um deinen SPI Slave tatsächlich zu aktivieren, bleibt dir überlassen. Kannst ja auch mehrere Slaves steuern, die dann jeder einen eigenen SS Pin brauchen.

Aber irgendwas muss dem Arduino ja sagen, dass er kein SPI Slave ist.

Hi,

noch zur Anmerkung der Beispiel Code ist vom UNO, beim MEGA ist chipSelectPin = 53. Aber das erklärt nicht, warum ich beim SCK kein Clock Signal sehe?

Gruß
Snoops

Wenn dein Arduino SPI Slave ist, macht er keinen CLK Takt

Hi,

soweit so gut! Aber der ChipSelect/SS Pin ist doch bereits im Sketch als OUTPUT definiert und es geht trotzdem nicht??

const int chipSelectPin = 53;
.
.
.

void setup() {

  // initalize the  data ready and chip select pins:
  pinMode(chipSelectPin, OUTPUT);

  // start the SPI library:
  SPI.begin();
.
.
.

Gruß
Snoops

Moin,

ich glaube ich habe die Ursache gefunden! :grin:
Man muss den SCK Pin 52 als OUTPUT definieren, dann funzts mit der Clock. Aber warum steht der Sch.. nirgendwo geschrieben, oder habe ich eine Leseschwäche, bzw. warum interressiert das den ATmega328 nicht und beim 2560 gehts nur wenn man die Pins definiert? Fragen über Fragen...

Hier mal der Code mit dem es spielt:

#include <SPI.h>
/*  MEGA Board 
 *
 * RFM12S IRQ  PIN3
 * SPI_SS      53    // PL0, pin xx
 * SPI_MOSI    51    // PB2, pin 21
 * SPI_MISO    50    // PB3, pin 22
 * SPI_SCK     52    // PB1, pin 20
 *
 *
 */
// -------------------- SPI Daten ----------------------------------------------------------
// pins used for the connection with the sensor the other you need are controlled by the SPI library):
const int SPI_chipSelectPin = 53;
const int SPI_MOSI = 51;
const int SPI_MISO = 50;
const int SPI_SCK  = 52;

//==============================================================================================================
//                         Zusatzfunktionen
//==============================================================================================================
static void showNibble (byte nibble) {
  char c = '0' + (nibble & 0x0F);
  if (c > '9')
    c += 7;
  Serial.print(c);
}

static void showByte (byte value) {
    showNibble(value >> 4);
    showNibble(value);
}


//==============================================================================================================
//                          Initialisieren
//==============================================================================================================
void setup() {
  // Analogem Pin in den Ausgabemodus setzen
   pinMode(A0, OUTPUT);  //nur setzten keine Funktion
   pinMode(A1, OUTPUT);  //nur setzten keine Funktion
   pinMode(A2, OUTPUT);  //nur setzten keine Funktion
   pinMode(A3, OUTPUT);  //nur setzten keine Funktion
   pinMode(A4, OUTPUT);  //nur setzten keine Funktion
   pinMode(A5, OUTPUT);  //nur setzten keine Funktion

  Serial.begin(57600);
  Serial.print("Baudrate ");
  Serial.println(57600);
  Serial.println("RFM22B SPI Test V1.0");
  Serial.print("booting...");
 
 
  // initalize the  data ready and chip select pins:
  pinMode(SPI_chipSelectPin, OUTPUT);
  pinMode(SPI_MOSI, OUTPUT);
  pinMode(SPI_SCK, OUTPUT);
  
  pinMode(SPI_MISO, INPUT);


  // start the SPI library:
  SPI.begin();
  
  //Sets the order of the bits shifted out of and into the SPI bus, either LSBFIRST (least-significant bit first)
  //or MSBFIRST (most-significant bit first).
  SPI.setBitOrder(MSBFIRST);

  //Sets the SPI clock divider relative to the system clock. On AVR based boards, the dividers available are
  //2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter
  //the frequency of the system clock (4 Mhz for the boards at 16 MHz).
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  
  //Sets the SPI data mode: that is, clock polarity and phase. See the Wikipedia article on SPI for details.
  // Möglich ist       SPI_MODE0 / SPI_MODE1 / SPI_MODE2 / SPI_MODE3
  SPI.setDataMode(SPI_MODE0);

  delay(30);

   digitalWrite(SPI_chipSelectPin, LOW);
   // send the device the register you want to read:
   SPI.transfer(0x00);
   // send a value of 0 to read the first byte returned:
   byte result = SPI.transfer(0x00);
   showByte(result);
   digitalWrite(SPI_chipSelectPin, HIGH);

  Serial.println(" done!");
}

//==============================================================================================================
//                         Arbeitsschleife
//==============================================================================================================
void loop()
{
   digitalWrite(SPI_chipSelectPin, LOW);
   // send the device the register you want to read:
   SPI.transfer(0x00);
   // send a value of 0 to read the first byte returned:
   byte result = SPI.transfer(0x00);
   showByte(result);
   Serial.println();
   digitalWrite(SPI_chipSelectPin, HIGH);
   
delay (500);   
   
   digitalWrite(SPI_chipSelectPin, LOW);
   // send the device the register you want to read:
   SPI.transfer(0x01);
   // send a value of 0 to read the first byte returned:
   result = SPI.transfer(0x00);
   showByte(result);
   Serial.println();
   digitalWrite(SPI_chipSelectPin, HIGH);

delay (5000);
}

Gruß
Snoops