I'm working on a project that requires all 12 PWM pins to operate 4 RGB LEDs and I want to use the Ethernet shield. The problem is that the Ethernet shield uses pin 10 (PWM) on the Arduino Mega.
So I thought I could change the CS pin in the Ethernet library to another pin I don't use and rewire it to the shield later.
The changes I made to the s5100.h file:
#if defined(__AVR_ATmega1280_DFR__)
inline static void initSS() { DDRL |= _BV(0); };
inline static void setSS() { PORTL &= ~_BV(0); };
inline static void resetSS() { PORTl |= _BV(0); };
#define WORKED
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
inline static void initSS() { DDRB |= _BV(4); };
inline static void setSS() { PORTB &= ~_BV(4); };
inline static void resetSS() { PORTB |= _BV(4); };
#define N_WORKED
#elif defined(__AVR_ATmega32U4__)
inline static void initSS() { DDRB |= _BV(6); };
inline static void setSS() { PORTB &= ~_BV(6); };
inline static void resetSS() { PORTB |= _BV(6); };
#elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
inline static void initSS() { DDRB |= _BV(0); };
inline static void setSS() { PORTB &= ~_BV(0); };
inline static void resetSS() { PORTB |= _BV(0); };
#else
inline static void initSS() { DDRB |= _BV(2); };
inline static void setSS() { PORTB &= ~_BV(2); };
inline static void resetSS() { PORTB |= _BV(2); };
#endif
I use the WORKED en N_WORKED defines to verify that my changes work.
PORTL0 is pin 49 if I understand this schematic correctly (http://arduino.cc/en/uploads/Main/arduino-mega-schematic.pdf).
This is the sketch I used to check if it works:
/*
DHCP-based IP printer
This sketch uses the DHCP extensions to the Ethernet library
to get an IP address via DHCP and print the address obtained.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 12 April 2011
modified 9 Apr 2012
by Tom Igoe
*/
/*
By defining __AVR_ATmega1280_DFR__ a other pin then Digital pin 10 is used for Ethenet CS.
Instead Digital pin 49 is used.
*/
#define __AVR_ATmega1280_DFR__
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(19200);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
#if defined(WORKED)
Serial.println("worked0");
#endif
#if defined(N_WORKED)
Serial.println("didn't work0");
#endif
Serial.println("Connecting...");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
#if defined(WORKED)
Serial.println("worked1");
#endif
#if defined(N_WORKED)
Serial.println("didn't work1");
#endif
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}
The problem is that neither WORKED or N_WORKED seems to be defined and the Ethernet shield still only works if I connect pin 10 of the Arduino to the shields CS.
I thought I wasn't that bad in programming C/C++ but I can't see what I'm doing wrong here, can someone help me with this?
Thanks in advanced!
Hardware used:
-Arduino Ethernet shield R3
-DFRobot Arduino Mega1280 compatible
Software used:
-Arduino SDK 1.0.1
-Default Ethernet library