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 StatesPosts: 15 |
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 |
||||
jman Guru Joined: 12/06/2011 Location: New ZealandPosts: 711 |
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 Jman |
||||
PicFan Senior Member Joined: 18/03/2014 Location: AustriaPosts: 133 |
Look at page 54 in the manual, I think "RTC SETREG" is your solution ? Wolfgang |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
Which manual Wolfgang? The DS3231 manual referenced earlier only has 20 pages. |
||||
viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
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: AustriaPosts: 133 |
Thank you, @viscomjim ! |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
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: CanadaPosts: 268 |
RTC SETREG and RTC GETREG doesn't work for ARMmite b26 |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1133 |
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 |
||||
viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
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. |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1133 |
Congratulation! |
||||
Print this page |