Arduino Polska Forum
Komunikacja SPI - wysyłanie stringa - Wersja do druku

+- Arduino Polska Forum (https://forum.arduinopolska.pl)
+-- Dział: Korzystanie z Arduino (https://forum.arduinopolska.pl/dzial-korzystanie-z-arduino)
+--- Dział: Programowanie w Arduino (https://forum.arduinopolska.pl/dzial-programowanie-w-arduino)
+--- Wątek: Komunikacja SPI - wysyłanie stringa (/watek-komunikacja-spi-wysy%C5%82anie-stringa)



Komunikacja SPI - wysyłanie stringa - mglowinski93 - 28-01-2018

Witam,
mam problem z wysyłaniem stringa po magistrali SPI.
Do tej pory zrobiłem wysyłanie stałej wartości i to działa.

Połączenia:
[Obrazek: SPI-connection--1024x958.png]




Kod MASTER-a (Arduino Leonardo):

Kod:
#include <SPI.h>

void setup (void) {
pinMode(8,OUTPUT);
Serial.begin(9600); //set baud rate to 115200 for usart
digitalWrite(8, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV64);//divide the clock by 8
}

void loop (void) {
char c;
digitalWrite(8, LOW); // enable Slave Select

// send test string
for (const char * p = "Hello, world!\r" ; c = *p; p++)
{
SPI.transfer (c);
Serial.print(c);
}
digitalWrite(8, HIGH); // disable Slave Select
delay(2000);
}


Kod SLAVE-a (Arduino UNO):

Kod:
#include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;

void setup (void) {
Serial.begin (9600);
pinMode(12, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect) // SPI interrupt routine
{

byte c= SPDR; // read byte from SPI Data Register
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == '\r') //check for the end of the word
process = true;
}
}

void loop (void) {

if (process) {

process = false; //reset the process
Serial.println (buff); //print the array on serial monitor
indx= 0; //reset button to zero
}
delay(2000);
}


Byłbym wdzięczny za wskazówki jak wysłać string za pomocą magistrali SPI. Moim celem jest wysłanie odczytu z czujnika temperatury (DS18B20) podłączonego do Arduino Leonardo i wyświetleniu go na LCD sterowanym z Arduino UNO.


Pozdrawiam
Mateusz


RE: Komunikacja SPI - wysyłanie stringa - Smaczek - 29-01-2018

Witam,
A czy powyżej zamieszczony przez Ciebie kod do komunikacji działa?
Pozdrawiam,
Tomek.