Using I2C in interrupt

Hi! I'm trying to put my RTC in interrupt... Interrupt routine will be done if i give Low from High (FALLING)... Here's my code...

#include <Wire.h>
int second = 0;
int minute = 0;
int hour = 0;
int day = 0;
int date = 0;
int month = 0;
int year = 0;

void setup()
{
Wire.begin();
Serial.begin(9600);
attachInterrupt(0, RTC, FALLING);
}

void loop()
{
//nothing here
}

void RTC()
{
Wire.beginTransmission(0x68);
Wire.write(byte(0));
Wire.endTransmission();
Wire.requestFrom(0x68,9);
{
second = (Wire.read());
minute = (Wire.read());
hour = (Wire.read());
day = (Wire.read());
date = (Wire.read());
month = (Wire.read());
year = (Wire.read());
}
Wire.endTransmission();
Serial.print(date,HEX);
Serial.print(F("/"));
Serial.print(month,HEX);
Serial.print(F("/"));
Serial.println(year,HEX);
Serial.print(hour,HEX);
Serial.print(F(" : "));
Serial.print(minute,HEX);
Serial.print(F(" : "));
Serial.println(second,HEX);
}

but everything in Serial.print doesn't appear when i give resistor pull-up on Pin 2 on Arduino...
Is there anything wrong??
Thanks

The Wire library uses interrupts. You can not use interrupt-driven functions in an ISR, where interrupts are disabled.

Serial.print() uses interrupts to shift data out. Doing that in an ISR is NOT a good idea.

Using the F() macro to keep two characters out of SRAM seems rather pointless.

What are the useless curly braces in RTC() for?

what i'm going to do is like this.
when i press button (with pull-up resistor) then the program will read the time from RTC print it to serial monitor... as simple as that... could it be able to do?

when i press button (with pull-up resistor) then the program will read the time from RTC print it to serial monitor... as simple as that... could it be able to do?

You do not need an interrupt for that.

it's exactly for an example... i have more complex one and it uses I2C in interrupt too.. so i'm trying to use this example to see it's worked or not to have I2C in interrupt...

paulusalexander:
it's exactly for an example... i have more complex one and it uses I2C in interrupt too.. so i'm trying to use this example to see it's worked or not to have I2C in interrupt...

You are probably going to have to change your program flow:

  • Have the Interrupt Service Routine(ISR) set a flag (volatile bool triggered; )
  • In the main loop check the flag, clear the flag, service the request

the ISR should be short and sweet. Do not access external libraries, or complex functions. As the previous posters have intimated, interrupts are disabled while inside an ISR, therefore any routine that uses interrupts will hang. (Serial,Wire,delay,...).

Chuck.

Your Wire.requestFrom() is followed by a Wire.endTransmission(). Please remove that second Wire.endTransmission, since the Wire.requestFrom() is a I2C transmission on its own and the Wire.endTransmission() is only to transmit data after a Wire.write().

The Wire.endTransmission() waits until writing all the data to the I2C bus has finished.
The Wire.requestFrom() waits until all the data has been read from the I2C bus.
Therefor you should not use them in an interrupt handler, and don't use the Serial functions in an interrupt handler either.

Do as PaulS and chucktodd say, that is the only way.

thanks for all explanations written here.... they're absolutely help me... and i think, i won't use this program and try to have another one....