DS1307 reading procedure triggered by external interrupt

Hi all,

I have some experience with AVR/GCC programming and using arduino nowadays. The problem that I have is that I’m trying to read the time register from a DS1307 I2C clock using standards functions that I found on google.

The thing is that I’ve connected the SQW/OUT pin of the DS1307 to Arduino’s PIN02 wich is configured as an external interrupt. So whenever DS1307 refreshes its time registers and change the state of SQW/OUT pin, the external interrupt function on arduino will execute the reading function meanwhile the main loop can do any other stuff; but the thing is that IT DOESN’T WORK, it only works when i put the time reading procedure on the main loop.

Thanks in advace for your help, attached you can see my code:

PD: I suspect that the problem are the ‘pre-cooked’ arduino’s I2C functions that have lots of stuff ‘under the hood’ wich I don’t like it. Thinking seriously to go back and use AVR/GCC

sketch_jul30a.ino (2.45 KB)

Your interrupt code is Waaaaayyyyyyy too long to execute… don’t do Serial Print in an interrupt as it requires interrupt itself…

And I’m not sure where you configured anything for SQW/OUT

The SQW/OUT pin is either an open drain output or a square wave output.

Bit 4: Square-Wave Enable (SQWE). This bit, when set to logic 1, enables the oscillator output. The frequency of the square-wave output depends upon the value of the RS0 and RS1 bits. With the square-wave output set to 1Hz, the clock registers update on the falling edge of the square wave. On initial application of power to the device, this bit is typically set to a 0.

If the SQWE bit is 0 then the level of the pin is controlled by the OUT bit of control register 7.

If the SQWE bit is 1 then a square wave is output derived by dividing the 32768Hz crystal frequency. Depending on the setting of the RS0 and RS1 bits you can get 1Hz, 4096Hz, 8192Hz and 32768Hz.

(also the pin is open drain output so you need an external 4,7K Ohms pull up resistor on the signal).

Using I2C requires interrupts that are turned off during your interrupt routine. You can't read the 1307 from inside an interrupt. You can either let the interrupt set a flag and have loop check that flag to see if you should read the 1307


You can be smart about it. If you know what time it is once, and you know how often the SQW/OUT pin is supposed to pulse then you never have to read the time from the 1307 again.