test program to communicate using spi using unos
This commit is contained in:
parent
7c323228f8
commit
85ba040e49
@ -5,9 +5,7 @@ SPIClass mySPI (&sercom2, 3, 5, 4, SPI_PAD_0_SCK_3, SERCOM_RX_PAD_1); //digital
|
||||
|
||||
void setup() {
|
||||
SerialUSB.begin(9600);
|
||||
|
||||
// do this first, for Reasons
|
||||
mySPI.begin();
|
||||
mySPI.begin(); //initiate the SPI communicaton
|
||||
|
||||
// Assign pins 3, 4, 5 to SERCOM & SERCOM_ALT
|
||||
pinPeripheral(3, PIO_SERCOM_ALT);
|
||||
@ -18,8 +16,8 @@ void setup() {
|
||||
uint8_t i=0;
|
||||
void loop() {
|
||||
mySPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
|
||||
int recu = 0;
|
||||
recu = mySPI.transfer(2);
|
||||
int recu = 0; //create recu
|
||||
recu = mySPI.transfer(2); //send and recieve using spi
|
||||
SerialUSB.println(recu);
|
||||
mySPI.endTransaction();
|
||||
delay(100);
|
||||
|
66
SPI_UNO/masterSPI/masterSPI.ino
Normal file
66
SPI_UNO/masterSPI/masterSPI.ino
Normal file
@ -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
|
46
SPI_UNO/slaveSPI/slaveSPI.ino
Normal file
46
SPI_UNO/slaveSPI/slaveSPI.ino
Normal file
@ -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
Block a user