Page 1 sur 1

[solved] 1Hz square wave on Memoire

Message non luPosté: Mer 28 Déc 2011 22:49
de John Murray

I have my Snootlab Memoire (using a DS1307 RTC) connected to my Arduino Uno. Could anyone advise me how to enable the RTC 1Hz square wave? I think I need to write a '1' to bit 4 of the DS1307 control register, and to set RS0 and RS1 both to '0' , but I'm not sure how to do this from the Arduino over the I2C bus. Any help would be appreciated.

Re: 1Hz square wave on Memoire

Message non luPosté: Jeu 29 Déc 2011 18:27
de Lionel
according to DS1307 datasheet, you just have to put 0x10 to register 0x07 of DS1307
You can do it like this

Code: Tout sélectionner
#include <Wire.h> // include wire library for I2C

void Send(byte addr, byte reg, byte data); // declare fonction to send msg to I2C

void setup()
Wire.begin(); // join i2c bus (address optional for master)
// Set RTC SQW to 1 Hz,
// DS1307 address is 0x68,
// write 0x10 (binary is 10000) to 0x07 register
// to set sqw pin to 1Hz
Send(0x68, 0x7, 0x10);

void loop()

void Send(byte addr, byte reg, byte data)

      Wire.send( reg);
      Wire.send( data);

You just have to send this once, because the RTC remember the settings using backup battery,
so you can check this code is working using the Files/examples/digital/button with a jumper wire between SQW and digital pin 2, put a led between D13 and GND (or look the led on the arduino board), the led will blink with 1Hz frequency

Hope it will be ok for your needs.

Re: 1Hz square wave on Memoire

Message non luPosté: Jeu 29 Déc 2011 19:06
de John Murray
Thanks Lionel

That sorted it! The thing I can't figure is how you knew the DS1307 address was 0x68??



Re: 1Hz square wave on Memoire

Message non luPosté: Jeu 29 Déc 2011 19:22
de Lionel
You're welcome,

you can find the DS1307 adress inside the datasheet (page 12) in binary 01101000 which is 0x68 in hexadecimal notation

Or, you can use an I2C bus scanner :) (but because the DS1307 address was out of the default end address, you have to change the byte end address in the code to find it)... hmm not really clear maybe ;)

Code: Tout sélectionner
 * I2CScanner.pde -- I2C bus scanner for Arduino
 * 2009, Tod E. Kurt,

#include "Wire.h"
extern "C" {
#include "utility/twi.h"  // from Wire library, so we can do bus scanning

// Scan the I2C bus between addresses from_addr and to_addr.
// On each address, call the callback function with the address and result.
// If result==0, address was found, otherwise, address wasn't found
// (can use result to potentially get other status on the I2C bus, see twi.c)
// Assumes Wire.begin() has already been called
void scanI2CBus(byte from_addr, byte to_addr,
                void(*callback)(byte address, byte result) )
  byte rc;
  byte data = 0; // not used, just an address to feed to twi_writeTo()
  for( byte addr = from_addr; addr <= to_addr; addr++ ) {
    rc = twi_writeTo(addr, &data, 0, 1);
    callback( addr, rc );

// Called when address is found in scanI2CBus()
// Feel free to change this as needed
// (like adding I2C comm code to figure out what kind of I2C device is there)
void scanFunc( byte addr, byte result ) {
  Serial.print("addr: ");
  Serial.print( (result==0) ? " found!":"       ");
  Serial.print( (addr%4) ? "\t":"\n");

byte start_address = 1;
byte end_address = 200;

// standard Arduino setup()
void setup()

    Serial.println("\nI2CScanner ready!");

    Serial.print("starting scanning of I2C bus from ");
    Serial.print(" to ");

    // start the scan, will call "scanFunc()" on result from each address
    scanI2CBus( start_address, end_address, scanFunc );


// standard Arduino loop()
void loop()
    // Nothing to do here, so we'll just blink the built-in LED