Electronics > Open Source Hardware

Square wave on ds1307 RTC

(1/1)

catfish08:
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: ---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();
}
--- End code ---


Rerouter:
page 9 of the datasheet,
http://datasheets.maximintegrated.com/en/ds/DS1307.pdf

write 0x10 into register 0x07

johnboxall:
Schematic:


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

Sketch:


--- Code: ---/*
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);
}

--- End code ---

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

Navigation

[0] Message Index

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod