Arduino Uno+Ethernet Shield. Can't use Timer-Interrupt if W5100 library is used

Hello, community,

I am using Arduino Uno + Ethernet Shield. I implement a client application that runs on Arduino. It builds a tcp connection to a remote server. Server sends one byte of data when connection is established. On Arduino I want to measure time it takes to receive that packet starting from the moment when connection was established.

To accomplish that I use timer1 interrupt. I tested my code without doing networking stuff and my ISR is called whenever timer1 overflow occurs. In that ISR I just make the LED blicking.

But when I use W5100 library, then ISR doesn't get called.

I also tried using the Timer1 library. The effect is the same: timing with Timer1 works fine as long as I don't use w5100.

I couldn't find any related info, so any help or thoughts would be very appreciated.

Here is my code:

ISR(TIMER1_OVF_vect){
	digitalWrite(ledPin, digitalRead(ledPin)^ 1);	// invert the bit value on the ledPin
}
void setup()   {

	Serial.begin(115200);
	pinMode(ledPin, OUTPUT); // use the led to see if ISR was called. That is the LED must be turned high in the ISR

	W5100.init(); //
	W5100.writeSnMR(s, SnMR::TCP); // set type of socket in Mode Register

	W5100.setMACAddress(smac); // set own mac address
	W5100.setIPAddress(src_ip_addr); // set own ip address
	W5100.writeSnPORT(s, src_port); // set own port (for each socket different)
	W5100.setSubnetMask(subnet_mask);
	W5100.setGatewayIp(gateway_ip);

	W5100.writeSnDHAR(s, dst_mac); // set destination mac address
	W5100.writeSnDIPR(s, dst_ip_addr); // set destination ip
	W5100.writeSnDPORT(s, dst_port); // set destination port

	W5100.execCmdSn(s, Sock_OPEN);
//+	Serial.print("\r\nDone opening socket");
//+	Serial.print("\r\n");

	memset(data, 0, PAYLOAD_SIZE);
	strcpy(data, "A");
	len = strlen(data);

	TCCR1B = 0;
	TCCR1A = 0;

	TCNT1 = 0; //	set value of the counter to zero
	//TCCR1B |= (1 << CS10);	// choose no prescaler
	TCCR1B |= (1 << CS12);	// choose 256 prescaler
	
	TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt.
}

void loop(){}

To accomplish that I use timer1 interrupt.

Why? You know when the connection is established. Record that time.

You know when data is received. Record that time.

Subtract one time from the other to get the interval.

No need to (mis)use timer interrupts at all.

Hello, PaulS,

thanks for the answer. The reason why I use timer is because I want to count hardware ticks to get most precise measurements. This is important for me. I assume you was talking about the function millis(), which in my case not really helpful.

The other thing is that I generally would like to understand why my code doesn't work and how I can fix it. Also, I bet there are many use cases when mutual use of Timers and Ethernet Shield functionality is desired.

Cheers!

I just found there is micros() function, but it is still not what I need and I still would like to know why using W5100 with Timer Interrupt doesn't work.

I have solved the problem. well kind of... I just took a counter variable and incremented its value in the ISR and in main routine and just printed out the value of this variable. The value is incremented, however the LED doesn't blink.