Hello
I try to create a sketch for receiving data using SPI from Arduino UNO R3 (as master) to Arduino WIFI R2 (as slave). I tried sucessfully from Arduino UNO R3 to Arduino UNO R3 but I can't do it from Arduino UNO R3 to Arduino WIFI R2. Someone have an example of a sketch which do that ?
I use Arduino IDE 1.8.19
Thi is the sketch I tried unsucessfully
#include "Arduino.h" #include "SPI.h"
char buf [13]; // arr_th : tt.tt;hh.hh\n\0 soit 13 caracteres le caractere \n est utilisé comme flag de fin de transmission (ça aurait pu être n'importe quel caractere sauf \0)
volatile byte pos;
volatile boolean process_it;
void setup()
{
Serial.begin(115200); Serial.println(FILE); Serial.println("Slave Uno WIFI Rev2 test!");
// 1 CIPO MISO | 2 +5v | 3 CLK/SS | 4 COPI (non utilisé) MOSI | 5 RST | 6 GND
//
//| ____ 5 3 1
//| |BLOC| x x x
//|_ |WIFI| x x x |
// \ |____| 6 4 2 /
// ____/
pinMode(SS, INPUT); // broche ? ICSP ou D8 (venant de D10 de l'UNO R3)
pinMode(MOSI,INPUT); // broche 4 ICSP ou D11 (venant de D11 de l'UNO R3)
pinMode(MISO, OUTPUT); // broche 1 ICSP ou D12 (non utilisé venant de DX de l'UNO R3)
pinMode(SCK, INPUT); // broche 3 ICSP ou D13 (venant de D13 de l'UNO R3)
PORTMUX.TWISPIROUTEA |= PORTMUX_SPI0_ALT2_gc; //J’ai compris ce qui se passait après tout. Les broches SPI par défaut pour l’ATmega4809 sont PORTA 4-7 et pour utiliser les broches numériques 8, 10-13 sur la carte qui sont PORTE 0-3, j’ai dû utiliser le multiplexeur de port en premier lieu dans setup() :
SPI0.CTRLA |= SPI_ENABLE_bm; //enable SPI
SPI0.INTCTRL |= SPI_IE_bm; //enable interrupts
pos = 0; // buffer empty
memset(buf , '\0' , sizeof(buf));
process_it = false; // get ready for an interrupt
}
ISR(SPI0_INT_vect) //Interrupt service routine for SPI interrupts
{
if (process_it == false)
{
byte c = SPI0.DATA; // grab byte from SPI Data Register
Serial.print(c); Serial.print(" : ");Serial.println(pos);
buf [pos] = c;
pos ++;
if (pos > 12) {process_it = true;}
}
}
Sorry your code is not readable, post it using the forum guidelines <?>. Also post an annotated schematic showing how you have it wired including grounds, power, and power supplies.
Thank you gilshultz and sorry for bad utilization of the forum. This is the sketch:
// I try to read temperature and humidity on a web browser using Arduino .. but I'm beginner in Arduino .. and in english ... I hope that will be comprehensible
// I try it in a "pedagogic goal"
// First, I tried to do it with 2 Arduino Uno R3 using SPI and an ethernet shield for SPI slave ... but ethernet and SPI can not work together in this case
// However, SPI trasmission works fine. I connected a LCD screen using I2C and it displays correctly temperature and humidity
// So I try now to use an Arduino WIFI R2 (atmega4809) with this sketch
// I hope that Arduino WIFI R2 allows correct functionnality with both SPI and WIFI
// This sketch :
// * runs not correctly - on an Arduino WIFI Rev2
// * Arduino WIFI Rev2 is connected to USB for reading results in serial monitor (is it a cause of error ?)
// * should read data sent by an Arduino UNO R3 using SPI as master
// - data is temperature and humidity given by a DHT20 on UNO R3 master
// - data format : tt.tt;hh.hh\n\0 tt.tt is temperature in degree C and hh.hh is humidity in %
// this code is based on post 'Help on SPI for Uno Wifi Rev. 2' in Arduino forum ... but I'm beginner in Arduino and I not really understood it
// however, I wrote a sketch for an Arduino UNR R3 as slave and it runs correctly
#include "Arduino.h"
#include "SPI.h" // legacy SPI library
char buf [13]; // arr_th : tt.tt;hh.hh\n\0 so 13 characteres '\n' is used as 'flag' for end of transmission
volatile byte pos;
volatile boolean process_it;
void setup()
{
Serial.begin(115200); Serial.println(__FILE__); Serial.println("Slave Uno WIFI Rev2 test!");
// 1 CIPO MISO | 2 +5v | 3 CLK/SS | 4 COPI (not used) MOSI | 5 RST | 6 GND
//
//| ____ 5 3 1
//| |BLOC| x x x
//|_ |WIFI| x x x ___________|
// \ |____| 6 4 2 /
// \_______________/
pinMode(SS, INPUT); // green wire pin ? ICSP or pin D8 ? (from UNO R3 pin D10) : HOW SHALL I CONNECT SS
pinMode(MOSI,INPUT); // yellow wire pin 4 ICSP or pin D11 ? (from UNO R3 pin D11)
pinMode(MISO, OUTPUT); // pin 1 ICSP or pin D12 ? (from UNO R3 pin D12 NOT USED IN THIS CASE SLAVE ONLY RECIEVE)
pinMode(SCK, INPUT); // orange wire pin 3 ICSP or pin D13 ? (from UNO R3 pin D13)
PORTMUX.TWISPIROUTEA |= PORTMUX_SPI0_ALT2_gc; // I don't understand this line
SPI0.CTRLA |= SPI_ENABLE_bm; //enable SPI ... but I don't understand this line
SPI0.INTCTRL |= SPI_IE_bm; //enable interrupts ... but I don't understand this line
pos = 0; // buffer empty
memset(buf , '\0' , sizeof(buf));
process_it = false; // get ready for an interrupt
}
ISR(SPI0_INT_vect) //Interrupt service routine for SPI interrupts ... but I don't understand functionnment of the interruptions
{
if (process_it == false)
{
byte c = SPI0.DATA; // grab byte from SPI Data Register
Serial.print(c); Serial.print(" : ");Serial.println(pos);
// In this case c is always 0 or 1
buf [pos] = c;
pos ++;
if (pos > 12) {process_it = true;}
}
}
void loop()
{
if (process_it)
{
int i;
//for (i = 0 ; i < 13 ; i++) {Serial.print (buf[i]);}
// example 20.02;50.01 20.2 temperature in degree C 50.01 percent of humidity
//Serial.print (buf[0]); // should be '2'
//Serial.print (buf[1]); // should be '0'
//Serial.print (buf[2]); // should be '.'
//Serial.print (buf[3]); // should be '0'
//Serial.print (buf[4]); // should be '2'
//Serial.print (buf[5]); // should be ';'
//Serial.print (buf[6]); // should be '5'
//Serial.print (buf[7]); // should be '0'
//Serial.print (buf[8]); // should be '.'
//Serial.print (buf[9]); // should be '0'
//Serial.print (buf[10]); // should be '1'
//Serial.print (buf[11]); // should be '\n'
//Serial.print (buf[12]); // should be '\0'
if ((buf[2] == '.') && (buf[5] == ';') && (buf[8] == '.') && (buf[11] == '\n')) // && and
{
for (i = 0 ; i < 12 ; i++) {Serial.print (buf[i]);}
}
else {Serial.println("Invalid data");}
pos = 0;
memset(buf , '\0' , sizeof(buf));
process_it = false;
}
}
This picture displays a good SPI transmission using 2 Arduino Uno R3 (DHT 20 and SSD1306/1315 are connected on the master and LCD is connected on the slave)
At the end, I'd like to measure temperature and humidity and give the results in throught a html page. The first Arduino (UNO R3) just gets temperature and humidity and transmits this data throught SPI. With the second Arduino, (UNO WIFI R2), I tried to retreive this datas throught SPI (still without success at present ...) and display it using Web server. Now I realize it's maybe impossible. I think that using simultaneously SPI and Webserver is impossible, due to interruptions utilization.
Is it true ?
I am aware of my low level of English ... sorry. I hope what I wrote will remain understandable ...