#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?
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);
}
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.
ich glaube ich habe die Ursache gefunden!
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);
}