Hi,
using a MEGA Board, I want to test a sensor that communicates through SPI. I have tried hard but can't have SPI to work. As fisrt test, I have not connected the sensor but used a scope to see SCLK and MOSI signals. All I get is nada, nothing at all. Here is the code:
/* Tests SPI on MEGA Board
*/#define SCK_PIN 52 // clock [DIGITAL 52, CPU 20]
#define MISO_PIN 50 // Master IN [DIGITAL 50, CPU 22]
#define MOSI_PIN 51 // Master OUT [DIGITAL 51, CPU 21]
#define SS_PIN 53 // Slave Select [DIGITAL 53, CPU 19]/* Send and receive one byte using SPI
*/
byte SPI_WrRd(byte Data)
{
SPDR = Data;
while(!(SPSR & (1 << SPIF)))
;
return SPDR; // data read}
/* Send and receive one byte using SPI
with a delay
*/byte SPI_WrDlRd(byte Data, byte Delai)
{SPDR = Data;
if (Delai > 0)
delayMicroseconds(Delai);while(!(SPSR & (1 << SPIF)))
;return SPDR; // data read
}
void setup()
{
byte Temp;// Initialisation du module SPI
SPSR = 1; // doubler l'horloge
// Il semble que le capteur fonctionne MSB first donc DORD à 0
SPCR = 0 | (1 << SPE) | (1 << MSTR) | (1 << SPR1);
#ifdef NOTEST
pinMode(SCK_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);
pinMode(SS_PIN, OUTPUT); // will not be used
#endifDDRB = DDRB | (1 << PB0) | (1 << PB1) | (1 << PB2);
Temp = SPSR; // empty registers after initialization
Temp = SPDR; // SPSR = status, SPDR = DataSerial.begin(9600);
Serial.flush();
Serial.println("");
Serial.println("Starting");}
void loop()
{// Serial.println("Hello World");
SPI_WrRd(0x55);
}
Can someone tell me what I am doing wrong ? I should see a 500 KHz square wave on SCLK, and the data change on MOSI.
Thanks