Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 08:23 05 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Help with ds3231 sqw enable.

Author Message
easterly81
Newbie

Joined: 08/04/2015
Location: United States
Posts: 15
Posted: 08:03am 11 May 2015
Copy link to clipboard 
Print this post

Hello I am pretty new to the micromite and I am trying to enable the square wave output on the DS3231.
Im confused at what byte to send to the control register and how. Any help would be appreciated.


I2C open 100,100
I2C write &h68, 0, 2,&h0e, &h00
pause 40
I2C Close



ds3231 data sheet
http://datasheets.maximintegrated.com/en/ds/DS3231.pdf Edited by easterly81 2015-05-12
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 09:24am 11 May 2015
Copy link to clipboard 
Print this post

  easterly81 said  


I2C open 100,100
I2C write &h68, 0, 2,&h0e, &h00
pause 40
I2C Close






Your code looks correct do remember the SQW pin is open-drain so from the manual
"This open-drain pin requires an external pullup resistor connected
to a supply at 5.5V or less"

Regards
JmanEdited by jman 2015-05-12
 
PicFan
Senior Member

Joined: 18/03/2014
Location: Austria
Posts: 133
Posted: 09:45am 11 May 2015
Copy link to clipboard 
Print this post

Look at page 54 in the manual, I think "RTC SETREG" is your solution ?

Wolfgang
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 10:10am 11 May 2015
Copy link to clipboard 
Print this post

Which manual Wolfgang? The DS3231 manual referenced earlier only has 20 pages.
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 10:40am 11 May 2015
Copy link to clipboard 
Print this post

Page 54 HERE has a description of using the new RTC GETREG and RTC SETREG to manipulate the other registers in the DS3231 a bit easier than using the I2C commands.
 
PicFan
Senior Member

Joined: 18/03/2014
Location: Austria
Posts: 133
Posted: 11:48am 11 May 2015
Copy link to clipboard 
Print this post

Thank you, @viscomjim !
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 06:01pm 11 May 2015
Copy link to clipboard 
Print this post

No matter which method is used to load the DS3231 control register &h0E, the data byte to control the square wave output signal is:
[code]
&h40 --- 1Hz
&h48 --- 1024Hz
&h50 --- 4096Hz
&h51 --- 8192Hz

examples

I2C open 100,100
I2C write &h68, 0, 2,&h0e, &h51 ' set 8192Hz
pause 40
I2C Close

or if you have the latest firmware at V4.6b

RTC SETREG &h0E,&h48 ' set 1024Hz
[/code]
 
boss

Senior Member

Joined: 19/08/2011
Location: Canada
Posts: 268
Posted: 03:50am 12 May 2015
Copy link to clipboard 
Print this post


RTC SETREG and RTC GETREG doesn't work for ARMmite b26
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 06:14am 12 May 2015
Copy link to clipboard 
Print this post

  BobD said   No matter which method is used to load the DS3231 control register &h0E, the data byte to control the square wave output signal is:
[code]
&h40 --- 1Hz
&h48 --- 1024Hz
&h50 --- 4096Hz
&h51 --- 8192Hz

examples

I2C open 100,100
I2C write &h68, 0, 2,&h0e, &h51 ' set 8192Hz
pause 40
I2C Close

or if you have the latest firmware at V4.6b

RTC SETREG &h0E,&h48 ' set 1024Hz
[/code]

I fear that's not so easy! Sorry!
If you choose this way then &h58(&B01011000) --- 8192Hz makes more sense.

Anyway thanks for posting your code example!

BUT it is realy more complex.
Better seems to read first the content of the ControlRegister and then OR (write) the appropriate bit(s).

You can replace RTC SETREG for DS3231 with this code (for MMBasic >=4.5):

function RTCSETREG(register,value)
I2C open 100,100
I2C write &h68, 0, 2, register, value
I2C Close
RTCSETREG=MM.I2C
end function


This is the code for manipulating the square wave output for Arduinos (by Eric Ayars):

void DS3231::enableOscillator(bool TF, bool battery, byte frequency) {
// turns oscillator on or off. True is on, false is off.
// if battery is true, turns on even for battery-only operation,
// otherwise turns off if Vcc is off.
// frequency must be 0, 1, 2, or 3.
// 0 = 1 Hz
// 1 = 1.024 kHz
// 2 = 4.096 kHz
// 3 = 8.192 kHz (Default if frequency byte is out of range)
if (frequency > 3) frequency = 3;
// read control byte in, but zero out current state of RS2 and RS1.
byte temp_buffer = readControlByte(0) & 0b11100111;
if (battery) {
// turn on BBSQW flag
temp_buffer = temp_buffer | 0b01000000;
} else {
// turn off BBSQW flag
temp_buffer = temp_buffer & 0b10111111;
}
if (TF) {
// set ~EOSC to 0 and INTCN to zero.
temp_buffer = temp_buffer & 0b01111011;
} else {
// set ~EOSC to 1, leave INTCN as is.
temp_buffer = temp_buffer | 0b10000000;
}
// shift frequency into bits 3 and 4 and set.
frequency = frequency << 3;
temp_buffer = temp_buffer | frequency;
// And write the control bits
writeControlByte(temp_buffer, 0);
}

void DS3231::enable32kHz(bool TF) {
// turn 32kHz pin on or off
byte temp_buffer = readControlByte(1);
if (TF) {
// turn on 32kHz pin
temp_buffer = temp_buffer | 0b00001000;
} else {
// turn off 32kHz pin
temp_buffer = temp_buffer & 0b11110111;
}
writeControlByte(temp_buffer, 1);
}

bool DS3231::oscillatorCheck() {
// Returns false if the oscillator has been off for some reason.
// If this is the case, the time is probably not correct.
byte temp_buffer = readControlByte(1);
bool result = true;
if (temp_buffer & 0b10000000) {
// Oscillator Stop Flag (OSF) is set, so return false.
result = false;
}
return result;
}


Best regards
Michael Edited by twofingers 2015-05-13
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 09:40am 12 May 2015
Copy link to clipboard 
Print this post

Good point, you definitely want to check the other bits in control register $H0E to make sure you are not messing with anything else but your output frequency bits (bits 3 and 4).

I just had to add that to make my 500th post.Edited by viscomjim 2015-05-13
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 09:59am 12 May 2015
Copy link to clipboard 
Print this post

  viscomjim said  ...
I just had to add that to make my 500th post.


Congratulation!

Edited by twofingers 2015-05-13
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024