7 years, 4 months ago.

mbed lpc1768 with arduino mega spi communication

How can I make spi communication between lpc1768 and arduino mega 2560? Thanks in advance...

1 Answer

7 years, 4 months ago.

Hi,

Connet both MISO, MOSI, SCLk and CS signals, set one board as master, the other as slave, set a communication syntax, and voila. SPI lines musn't be too long though ( <1m, maybe even less if using high communication rates).

Accepted Answer

Thanks Pierre.. I have made a problem but I have a problem I am reading a pot of arduinodan 0-1023 and I am sending to lpc1768, lpc1768 reading 0-255 How can I fix this? Here is my code;

arduino code

  1. include <SPI.h>

void setup (void) { Serial.begin(9600); digitalWrite(SS, HIGH); ensure SS stays high for now SPI.begin ();

SPI.setClockDivider(SPI_CLOCK_DIV32);

}

void loop (void) {

int sensorDegeri = analogRead(A0); Serial.println(sensorDegeri); digitalWrite(SS, LOW); SPI.transfer (sensorDegeri);

digitalWrite(SS, HIGH);

delay (1000);

}

lpc code;

  1. include "mbed.h"

Serial pc(USBTX, USBRX); SPISlave spi(p11, p12, p13, p14); mosi, miso, sclk,ss

int main() { pc.baud(115200); int x; while(1) { if(spi.receive()){ x = spi.read(); pc.printf("\r\n%d \n", x); }

} }

posted by EKREM KELES 08 Dec 2016

That's because your SPI works in 8 bits mode and 0-1023 precision needs a 10 bits width. 2 solutions : make it work in 16 bits, or send 2 bytes successively. Other solution : discard the LSB of your converted analog value. You don't need a 10 bits precision when you read a pot value, do you ?

posted by Pierre David 08 Dec 2016

Very thanks, good day :)

posted by EKREM KELES 08 Dec 2016

I have one more question. Could you help me when it's available? I am sending information to the life line using the arduino mega spi 2515 module. How can I read this information with lpc1768?

Can Send ;

  1. include <mcp_can.h>
  2. include <SPI.h>

MCP_CAN CAN0(53); Set CS to pin 10

void setup() { Serial.begin(115200);

Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled. if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!"); else Serial.println("Error Initializing MCP2515...");

CAN0.setMode(MCP_NORMAL); Change to normal mode to allow messages to be transmitted }

byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

void loop() { send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data); if(sndStat == CAN_OK){ Serial.println("Message Sent Successfully!"); } else { Serial.println("Error Sending Message..."); } delay(100); }

can receive;

  1. include <mcp_can.h>
  2. include <SPI.h>

long unsigned int rxId; unsigned char len = 0; unsigned char rxBuf[8]; char msgString[128];

MCP_CAN CAN0(53); Set CS to pin 10

void setup() { Serial.begin(115200);

Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled. if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!"); else Serial.println("Error Initializing MCP2515...");

CAN0.setMode(MCP_NORMAL); Set operation mode to normal so the MCP2515 sends acks to received data.

pinMode(CAN0_INT, INPUT); Configuring pin for /INT input

}

void loop() { if(!digitalRead(CAN0_INT)) If CAN0_INT pin is low, read receive buffer { CAN0.readMsgBuf(&rxId, &len, rxBuf); Read data: len = data length, buf = data byte(s)

if((rxId & 0x80000000) == 0x80000000) Determine if ID is standard (11 bits) or extended (29 bits) sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len); else sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);

Serial.print(msgString);

if((rxId & 0x40000000) == 0x40000000){ Determine if message is a remote request frame. sprintf(msgString, " REMOTE REQUEST FRAME"); Serial.print(msgString); } else { for(byte i = 0; i<len; i++){ sprintf(msgString, " 0x%.2X", rxBuf[i]); Serial.print(msgString); } }

Serial.println(); } }

How can i do the receiver code with lpc1768 ?

posted by EKREM KELES 08 Dec 2016

Check this : https://developer.mbed.org/handbook/CAN

posted by Pierre David 08 Dec 2016