test program to communicate using spi using unos
parent
7c323228f8
commit
85ba040e49
@ -0,0 +1,66 @@
|
||||
#include <SPI.h>
|
||||
|
||||
void setup (void)
|
||||
{
|
||||
Serial.begin (115200);
|
||||
Serial.println ();
|
||||
|
||||
digitalWrite(SS, HIGH); // ensure SS stays high for now
|
||||
SPI.begin ();
|
||||
|
||||
// Slow down the master a bit
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV8);
|
||||
} // end of setup
|
||||
|
||||
byte transferAndWait (const byte what)
|
||||
{
|
||||
byte a = SPI.transfer (what);
|
||||
delayMicroseconds (20);
|
||||
return a;
|
||||
} // end of transferAndWait
|
||||
|
||||
void loop (void)
|
||||
{
|
||||
|
||||
byte a, b, c, d;
|
||||
|
||||
// enable Slave Select
|
||||
digitalWrite(SS, LOW);
|
||||
|
||||
transferAndWait ('a'); // add command
|
||||
transferAndWait (10);
|
||||
a = transferAndWait (17);
|
||||
b = transferAndWait (33);
|
||||
c = transferAndWait (42);
|
||||
d = transferAndWait (0);
|
||||
|
||||
// disable Slave Select
|
||||
digitalWrite(SS, HIGH);
|
||||
|
||||
Serial.println ("Adding results:");
|
||||
Serial.println (a, DEC);
|
||||
Serial.println (b, DEC);
|
||||
Serial.println (c, DEC);
|
||||
Serial.println (d, DEC);
|
||||
|
||||
// enable Slave Select
|
||||
digitalWrite(SS, LOW);
|
||||
|
||||
transferAndWait ('s'); // subtract command
|
||||
transferAndWait (10);
|
||||
a = transferAndWait (17);
|
||||
b = transferAndWait (33);
|
||||
c = transferAndWait (42);
|
||||
d = transferAndWait (0);
|
||||
|
||||
// disable Slave Select
|
||||
digitalWrite(SS, HIGH);
|
||||
|
||||
Serial.println ("Subtracting results:");
|
||||
Serial.println (a, DEC);
|
||||
Serial.println (b, DEC);
|
||||
Serial.println (c, DEC);
|
||||
Serial.println (d, DEC);
|
||||
|
||||
delay (1000); // 1 second delay
|
||||
} // end of loop
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Pins: -> D10 : SS (Slave Select)
|
||||
* -> D11 : MOSI (Master Out Slave In) (Communication maitre -> esclave)
|
||||
* -> D12 : MISO (Master In Slave Out) (Communication esclave -> maitre)
|
||||
* -> D13 : SCLK (Serial Clock)
|
||||
* Informations : -> Si la pin SS n'est pas configuré en mode OUTPUT, le périphérique
|
||||
* sera considéré comme un esclave.
|
||||
* -> Ici, pas besoin de définir une pin SS car c'est un esclave
|
||||
*
|
||||
*/
|
||||
|
||||
// Inclusion des bibliothèques
|
||||
#include <SPI.h>
|
||||
|
||||
//Variables
|
||||
const int MOSI_PIN = 11;
|
||||
const int MISO_PIN = 12;
|
||||
const int SCLK_PIN = 13;
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Configuration des pins du SPI
|
||||
pinMode(MOSI_PIN,INPUT);
|
||||
pinMode(MISO_PIN,OUTPUT);
|
||||
pinMode(SCLK_PIN,INPUT);
|
||||
|
||||
// Initialisation du port série
|
||||
Serial.begin(9600);
|
||||
|
||||
// Initialisation du bus SPI
|
||||
SPI.begin();
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV4); // Diviseur d'horloge du bus SPI (choisir la fréquence de l'horloge en quart de l'horloge du microcontroleur)
|
||||
//SPI.setBitOrder(MSBFIRST); // Sens d'envoi des bits (most significant bit first)
|
||||
//SPI.setDataMode(SPI_MODE0); // Mode de transmission des données (parité ety phase de l'horloge)
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Transmettre des informations
|
||||
byte incomingByte = 0x0;
|
||||
incomingByte = SPI.transfer(0x0); // se qu'on recois = transfert(ce qu'on transmet)
|
||||
Serial.println(incomingByte,HEX);
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
|
Loading…
Reference in New Issue