Go Down

Topic: ds3231 SQW enable 1hz practicing (Read 2831 times) previous topic - next topic

parksplit

Hello, i am learning how to use I2C, speccially with ds3231 for starting, there are a lot of libraries to make ds3231 work but i want to learn from scratch, i´ve been readiing some pdf files and for my first attempt i tried to enable SQW function at 1hz here is the code

#include <Wire.h>
int ds3231_address = 0x68; //ds3231 address
void setup()
{

Wire.begin();
delay(100); 
}

void loop()
{
 Wire.beginTransmission(ds3231_address);
 Wire.write(0x0E); //Control Register for SQW (0Eh)
 Wire.write(0xc0); // 11000000 SQUARE-WAVE OUTPUT FREQUENCY but it hsould be 0000000 for 1 hz :
 Wire.endTransmission();
 
}



My doubt is that for enable 1hz datasheet says it should be 0000000 (00h) instead of 11000000 (c0h) but it works for 1hz also c0hi tested it on pic making a clock using 1hz for seconds++ after 1 day clock fomr my cellphone and ds3231 were .synchronized

cattledog

Quote
Ive been reading some pdf files
Are you reading the actual data sheet? https://datasheets.maximintegrated.com/en/ds/DS3231.pdf

Quote
Bit 7: Enable Oscillator (EOSC). When set to logic 0,
the oscillator is started. When set to logic 1, the oscillator
is stopped when the DS3231 switches to VBAT. This bit
is clear (logic 0) when power is first applied. When the
DS3231 is powered by VCC, the oscillator is always on
regardless of the status of the EOSC bit. When EOSC is
disabled, all register data is static.
Bit 6: Battery-Backed Square-Wave Enable (BBSQW).
When set to logic 1 with INTCN = 0 and VCC < VPF, this
bit enables the square wave. When BBSQW is logic 0,
the INT/SQW pin goes high impedance when VCC < VPF.
This bit is disabled (logic 0) when power is first applied.
Setting bit7 will prevent the RTC from advancing the time under battery power. I doubt you want this.
Setting bit6 lets the SQW run under battery power.

The fundamental control of the SQW and the frequency are in bits 4,3 and 2 (RS2,RS1,INTCN) which as you say should be 0's for a 1 HZ square wave.

With the top two bits set, the SQW and RTC will work as set when the RTC is powered from Vcc.


srnet

#2
Sep 24, 2018, 08:55 pm Last Edit: Sep 24, 2018, 08:56 pm by srnet
I recently wrote some code for the DS3231 to use the SQW output to keep a MOSFET switch powered on.

I just used the data sheet to work out what was needed, it was clear, and the code worked first time.

Dangerous teritory to doubt the accuracy of data sheets, if you have problems, 99.9.........%  of the time it will be a 'missunderstanding' on the programmers side.  
http://www.50dollarsat.info/
http://www.loratracker.uk/

parksplit

Hello Mr. Srnet, could you please share your code to analize it.

GolamMostafa

#4
Sep 24, 2018, 09:49 pm Last Edit: Sep 25, 2018, 05:08 am by GolamMostafa
Generation of 1 Hz Square Wave Signal at Pin-3 of the RTC Chip
(1) Connect the RTC Module with Arduino UNO as per following diagram of Fig-1. Let us also connect the R1-LED1 circuit and the pull-up resistors with the TWI/I2C Bus. The SQW-pin of J1 is internally shorted with Pin-3 of the DS3231 Clock Chip.


Figure-1: Connection diagram of DS3231 RTC Module with Arduino UNO's TWI Bus


Figure-2: Register map of DS3231 RTC Chip

(2) To generate 1 Hz signal at the SQW-pin, we need to carry out the following initialization tasks:
(a) INTCN (Interrupt Control) bit of the Control Register (Fig-2) must be cleared.
(b) RS1 and RS0 bits (Rate Select) of the Control Register (Fig-2) must be loaded with 00.

(3)
Let us upload the following sketch into the UNO Kit which will drive the RTC Module. Let us save sketch as ds3231rtc-3.ino.
Code: [Select]
#include<Wire.h>
#define deviceAddress 0b1101000   //0x68
void setup()
{
  Wire.begin();   //TWI Bus is formed  
  Wire.beginTransmission(deviceAddress); //device address and STSRT command are queued
  Wire.write(0x0E); //Control Register Address is queued
  Wire.write(0x00); //Data for Control Register is queued
  Wire.endTransmission(); //queued information are transferred under ACK; STOP
}

void loop()
{
  
}


(4) Compile and upload the sketch.

(5) Check that LED1 is blinking at 1-sec (1 Hz frequency) interval.

(6) Change the content of the Control Register at 0x84 to put LH at the INTCN bit which will disable the generation of the SQW signal. Save the new program as DS3231-2.ino.

(7) Upload the modified sketch and check that the LED1 is not blinking.

(8) Re-run the program with EOSC/ bit at LH state. Check that the LED1 is blinking. This proves that the oscillator circuit is always ON whenever there is voltage supply at the Vcc pin.

parksplit

Thanks a lot Mr. GolamMostafa for your help, i really aprecciate this, i wil print your tutorial  :)  .

Go Up