Author Topic: Square wave on ds1307 RTC  (Read 10497 times)

0 Members and 1 Guest are viewing this topic.

Offline catfish08Topic starter

  • Newbie
  • Posts: 2
Square wave on ds1307 RTC
« on: September 26, 2013, 10:27:32 am »
Hii All,
This is first time i use Arduino Uno. Could anyone advise me how to enable the RTC 1Hz square wave?
and how to connected led of the sqw out pin on ds1307,


Code: [Select]
void initChrono()
{
  set_time();
  set_date();
}
//=======================================================
void get_time()
{
  Wire.beginTransmission(104);
  Wire.write(0);
 
  Wire.endTransmission();
  Wire.requestFrom(104,3);
  seconds  = bcdToDec(Wire.read() & 0x7f);
  minutes  = bcdToDec(Wire.read());
  hours    = bcdToDec(Wire.read() & 0x3f); 
}
//=======================================================
void get_date()
{
  Wire.beginTransmission(104);
  Wire.write(3);
  Wire.endTransmission();
  Wire.requestFrom(104,4);
  dayOfWeek  = bcdToDec(Wire.read());
  dayOfMonth = bcdToDec(Wire.read());
  month          = bcdToDec(Wire.read());
  year             = bcdToDec(Wire.read()); 
}
//=======================================================
void set_time()
{
  Wire.beginTransmission(104);
  Wire.write(0);
 
  Wire.write(decToBcd(seconds));
  Wire.write(decToBcd(minutes));
  Wire.write(decToBcd(hours));
   
  Wire.endTransmission(); 
}
//=======================================================
void set_date()
{
  Wire.beginTransmission(104);
  Wire.write(3);
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
 
  Wire.endTransmission();
}


 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: Square wave on ds1307 RTC
« Reply #1 on: September 26, 2013, 10:42:45 am »
page 9 of the datasheet,
http://datasheets.maximintegrated.com/en/ds/DS1307.pdf

write 0x10 into register 0x07

 

Offline johnboxall

  • Supporter
  • ****
  • Posts: 652
  • Country: au
  • You do nothing, you get nothing.
    • Books, services and more:
Re: Square wave on ds1307 RTC
« Reply #2 on: September 30, 2013, 04:42:36 am »
Schematic:


You can put a 560R resistor and LED off the SQW pin to GND. Crystal is 32.768 kHz.

Sketch:

Code: [Select]
/*
DS1307 Square-wave machine
 Used to demonstrate the four different square-wave outputs from Maxim DS1307
 See page nine of data sheet for more information
 John Boxall - tronixstuff.com
 */

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // each I2C object has a unique bus address, the DS1307 is 0x68

void setup()
{
 Wire.begin();
}

void sqw1() // set to 1Hz
{
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.write(0x07); // move pointer to SQW address
 Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary)
 Wire.endTransmission();
}

void sqw2() // set to 4.096 kHz
{
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.write(0x07); // move pointer to SQW address
 Wire.write(0x11); // sends 0x11 (hex) 00010001 (binary)
 Wire.endTransmission();
}

void sqw3() // set to 8.192 kHz
{
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.write(0x07); // move pointer to SQW address
 Wire.write(0x12); // sends 0x12 (hex) 00010010 (binary)
 Wire.endTransmission();
}

void sqw4() // set to 32.768 kHz (the crystal frequency)
{
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.write(0x07); // move pointer to SQW address
 Wire.write(0x13); // sends 0x13 (hex) 00010011 (binary)
 Wire.endTransmission();
}

void sqwOff()
// turns the SQW off
{
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.write(0x07); // move pointer to SQW address
 Wire.write(0x00); // turns the SQW pin off
 Wire.endTransmission();
}

void loop()
{
 sqw1();
 delay(5000);
 sqw2();
 delay(5000);
 sqw3();
 delay(5000);
 sqw4();
 delay(5000);
 sqwOff();
 delay(5000);
}

More Arduino and RTC at http://tronixstuff.com/tag/ds1307/


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf