[solved] 1Hz square wave on Memoire

[solved] 1Hz square wave on Memoire

Message non lude John Murray » Mer 28 Déc 2011 22:49

Hi

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.
John Murray
 
Messages: 7
Inscription: Jeu 15 Déc 2011 16:15

Re: 1Hz square wave on Memoire

Message non lude Lionel » Jeu 29 Déc 2011 18:27

Hi,
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.beginTransmission(addr);
      Wire.send( reg);
      Wire.send( data);
     Wire.endTransmission();
}


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.
Image - Distributeur officiel Arduino - Conception de shields
Avatar de l’utilisateur
Lionel
 
Messages: 734
Inscription: Mar 21 Déc 2010 09:52
Localisation: Toulouse

Re: 1Hz square wave on Memoire

Message non lude John Murray » Jeu 29 Déc 2011 19:06

Thanks Lionel

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

Regards

John
John Murray
 
Messages: 7
Inscription: Jeu 15 Déc 2011 16:15

Re: 1Hz square wave on Memoire

Message non lude Lionel » Jeu 29 Déc 2011 19:22

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, http://todbot.com/blog/
 *
 */

#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(addr,DEC);
  Serial.print( (result==0) ? " found!":"       ");
  Serial.print( (addr%4) ? "\t":"\n");
}


byte start_address = 1;
byte end_address = 200;

// standard Arduino setup()
void setup()
{
    Wire.begin();

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

    Serial.print("starting scanning of I2C bus from ");
    Serial.print(start_address,DEC);
    Serial.print(" to ");
    Serial.print(end_address,DEC);
    Serial.println("...");

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

    Serial.println("\ndone");
}

// standard Arduino loop()
void loop()
{
    // Nothing to do here, so we'll just blink the built-in LED
    digitalWrite(13,HIGH);
    delay(300);
    digitalWrite(13,LOW);
    delay(300);
}


Regards
Image - Distributeur officiel Arduino - Conception de shields
Avatar de l’utilisateur
Lionel
 
Messages: 734
Inscription: Mar 21 Déc 2010 09:52
Localisation: Toulouse


Retourner vers Support for Snootlab Products

Qui est en ligne

Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 1 invité

cron