ISR(TIMER1_OVF_vect){ sreg = SREG; digitalWrite(ledPin, digitalRead(ledPin)^ 1); // invert the bit value on the ledPin overflowCounter++; TIMSK1 &= ~(1 << TOIE1); //disable interrupt on timer1 SREG = sreg; } // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. Serial.begin(115200); pinMode(ledPin, OUTPUT); //+Serial.println("\r\nSetup..."); //+Serial.print("\r\nInitializing the w5100 chip and open a TCP socket..."); 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.writeRMSR(0x03); // RX Memory Size Register. First two bits set to 1, this corresponds to assigning all 8 kByte of RX memory for the socket 0 W5100.writeTMSR(0x03); // TX Memory Size Register. First two bits set to 1, this corresponds to assigning all 8 kByte of TX memory for the socket 0 W5100.writeIMR(0x01); // Interrupt Mask Register is set to enable interrupts on socket 0. 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); cli(); TCCR1B = 0; TCCR1A = 0; TCNT1 = 65300; // prescaler TCCR1B |= (1 << CS10); // Time/Counter Control Register B no prescaler needed, since we count amount of ticks sei(); } // the loop routine runs over and over again forever: void loop() { // wait for a second if(measurementsCounter == 1000) while(true){} // endless loop after desired amount of measurements achieved W5100.execCmdSn(s, Sock_CONNECT); while ((W5100.readSnSR(s) & SnSR::ESTABLISHED) != SnSR::ESTABLISHED) { // check Status Register to find out if the connection was established } TCNT1 = 0; // reset counter. Note that the timer counts always, whether interrupt occurs decides bit in TIMSK TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt. Note we OR the current state of the TIMSK, so once set it is always set, if manually not changed while ((W5100.readSnIR(s) & SnIR::RECV) != SnIR::RECV) {} restTicks = TCNT1; // save rest ticks TIMSK1 &= ~(1 << TOIE1); // disable timer1. W5100.writeSnIR(s,SnIR::RECV); // clear the interrupt bit time = (overflowCounter * 65535 + restTicks) * 62.5; measurementsCounter++; overflowCounter = 0; // clear overflowCounter Serial.print("\r\nTerminate Connection"); W5100.execCmdSn(s, Sock_DISCON); while ((W5100.readSnIR(s) & SnIR::DISCON) != SnIR::DISCON) { } W5100.writeSnIR(s,SnIR::DISCON); // clear the interrupt bit Serial.print("\r\nDisconnected."); }