Pages: [1]   Go Down
Author Topic: Interfacing Arduino with mBed LPC1768 via SPI- seems to be unreliable?  (Read 583 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to send some data between an Arduino UNO (master) and an mBed LPC1768 microcontroller board (slave). To test that this will work I have written a simple Arduino sketch that sends 255 bytes (just ints, 1-255), waits for one second, repeats forever. The program on my mBed simply prints the recieved values to the serial port which I'm viewing with putty, and also increments a counter every time a byte is recieved. This way the mBed slave knows if it's recieved the correct byte. But my problem is that it doesn't seem to work- at least not very well. usually 3 or 4 bytes are recieved before it reports a mismatch, and I have tried at every possible clock speed, even tried with a 1ms delay between each byte.
I tried this same test program with 2 mBed boards and it worked just fine- for some reason with an Arduino as the master it is unreliable. Can anybody shed any light on this?

MASTER (ARDUINO)
Code:
#include <SPI.h>

void setup (void)
{

  digitalWrite(SS, HIGH);  // ensure SS stays high for now
  SPI.begin ();

  SPI.setClockDivider(SPI_CLOCK_DIV32);
 



void loop (void)
{
  for (int i = 1; i < 256; i++){  //send 255 bytes
    digitalWrite(SS, LOW);
    SPI.transfer (i);
     }
  digitalWrite(SS, HIGH);
 
  delay (1000);  // 1 second delay between the next 255 bytes
 
}  // end of loop


SLAVE (MBED)
Code:
#include "mbed.h"

Serial pc(USBTX, USBRX);
SPISlave spi(p5, p6, p7, p8); //mosi, miso, sclk

int main() {
pc.baud(115200);
int counter = 0;
int x;
    while(1) {
    if(spi.receive()){
        counter++;
        x = spi.read();
        pc.printf("%d", x);
        pc.printf("  ");
            }
   
    if(counter != x){
        pc.printf("mismatch at byte no. ");
        pc.printf("%d", counter);
        pc.printf(", counter is at ");
        pc.printf("%d", counter);
        pc.printf(" and the value received is ");
        pc.printf("%d\n\r", x);
        break;
        }
    if(counter == 255){
   
    x = 0;
            counter = 0;
        }
       
    }
}
Logged

Pages: [1]   Go Up
Jump to: