Hello Alice_Yang
I have tried the SPI example with Open LPC1768(Master) microcontroller and Arduino Mega 2560(Slave).
problem: The data is not getting at the slave arduino.
I have tried the SPI communication between two arduino mega(MASTER & SLAVE). In that case SPDR is updating. Whatever data i am sending from master, it is getting receive at slave device.
** In case of open LPC1768 it is not working. I am receiving 0 at slave.
SPI master code (Example code from cmsis library)
I did some changes in spi_master.c
1. mode 0->CPHA & CPOL =0
SPI_ConfigStruct.CPHA = SPI_CPHA_FIRST;
SPI_ConfigStruct.CPOL = SPI_CPOL_HI;
2. #define SPI_DATABIT_SIZE 8
/** Max buffer length */
#define BUFFER_SIZE 1
3.
void Buffer_Init(void)
{
uint32_t i;
for (i = 0; i < BUFFER_SIZE; i++) {
Tx_Buf[i] = i+1;
Rx_Buf[i] = 0;
}
}
Arduino slave code
//SPI SLAVE (ARDUINO)
#include<SPI.h>
#define LEDpin 13
volatile boolean received;
volatile byte Slavereceived,Slavesend;
void setup()
{
Serial.begin(115200);
pinMode(LEDpin,OUTPUT); // Setting pin 7 as OUTPUT
pinMode(MISO,OUTPUT); //Sets MISO as OUTPUT (Have to Send data to Master IN
SPI.beginTransaction(SPISettings(2500000, MSBFIRST, SPI_MODE0));
SPCR |= _BV(SPE); //Turn on SPI in Slave Mode
SPCR |= _BV(SPIE);
received = false;
//SPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation
}
ISR (SPI_STC_vect) //Inerrrput routine function
{
Slavereceived = SPDR; // Value received from master if store in variable slavereceived
received = true; //Sets received as True
}
void loop()
{
if(received) //Logic to SET LED ON OR OFF depending upon the value recerived from master
{
if (Slavereceived==1)
{
digitalWrite(LEDpin,HIGH); //Sets pin 7 as HIGH LED ON
Serial.println("Slave LED ON");
Serial.println(Slavereceived);
}else
{
digitalWrite(LEDpin,LOW); //Sets pin 7 as LOW LED OFF
Serial.println("Slave LED OFF");
Serial.println(Slavereceived);
}
Slavesend = 2;
SPDR = Slavesend; //Sends the value to master via SPDR
delay(1000);
}
}