One thing to note is that the Ethernet chip, Wiznet 5100, actually only support SPI_MODE0, not SPI_MODE3.
But this shield supports SPI sharing. Just don't put the code for the other SPI devices into the while(client.connected()) loop of the example Webserver code of Ethernet shield.
And the condition "client.connected()" is satisfied only when the client sends out some data after the connection is built. You can remove this requirement by modifying the client.available() function, letting it always return 1 instead of returning the number of bytes received from the client.
I post my codes as it may help others. The task of these codes is to continuously read sensor data via SPI, and send data to PC/router through Ethernet cable. Since Ethernet shield also communicates with Arduino via SPI, the sensor and ethernet shield shares the SPI ports of Arduino. I'm using Arduino UNO R3.
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192,168,0, 2 }; //Be in the same network as your router
int CS=7; // Here we use 7 as the CS pin of the other SPI device
Server server(80);
void setup()
{
pinMode(10, OUTPUT); // Ethernet shield uses 10 as its CS pin
digitalWrite(10, HIGH);
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
SPI.setDataMode(SPI_MODE3);
/* Sensor data reading ...*/
SPI.setDataMode(SPI_MODE0);
Client client_a = server.available();
if(client_a.connected()){
client_a.print(Sensor_data1);
client_a.print(' ');
client_a.println(Sensor_data2);
}
delay(10); //Dependent on the desired data output rate, can be omitted
}