Arduino mega2560 problem with DS3132 RTC

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

Hello
I am using RTC DS3132, I can write and read, the only thing I can not do is change the frequency of the SQW output, it remains fixed at 1HZ even if I enter from 0 to 3, the strange thing is that even if I insert a option that is not provided for example 6, FC8 does not report an error
I am using Arduino Mega 2560
thank you

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Can you post your code?

I'm currently working on a new DS3231 component - which should also apply to DS3232 - I'll upload the latest version - which now supports SQW - here : viewtopic.php?f=80&t=22188

It's by no means complete yet - it's not a component yet, and I haven't implemented all the date functions or12h code yet.. But alarms (and hopefully SQW) work - and there is a register dump so you can check the values to RS1RS2 and INTCN (which needs to be 0 to enable the SQW)

Martin

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3231 RTC

Post by maxtisc »

hello martin, thanks for your replay, the code and test and it will be part of a more complex code, I initialize and send the instruction to modify rs1-rs2, with the led I check that the program is running, but with the oscilloscope not I see the change of frequency (always 1HZ out) SQW, do you recommend downloading the component of the topic you indicated?
thank you
Attachments
arduino_RTC.fcfx
(27.54 KiB) Downloaded 47 times

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

I've tested my EnableSQW - and the results look good on oscilloscope - tested for 0-3 amd all results look good...
Note - I don't test the range passed for rate (so passing a value other than 0-3 will give 'unexpected' results - it would be asy to add .RateSelect = .RateSelect & 0b11 if you want to be certain that it will only accept values in range.
Note that I also rely on true = 1 (and false = 0) - I tend to assume for MCU use where a set program is running that this is OK (it's only when user input rears it's ugly head that error checking becomes a must!)

The FC component also seems to work fine - a very simple program shows that sqw can be enabled at any speed (Rate 0..3)

I notice in your code that the interrupt handler (secondo) has an infinite loop (while(1)) - this will lock up your MCU (note that in the code posted this interrupt is not enabled) - you also enable and disable an interrupt in the interrupt handler. I'd guess that this probably won't do what you expect. However - this shouldn't affect the SQW output on the RTC clock SQW pin?
the test
For some reason the 'attatchments' tab is disabled tonight - so I can't post the demo code - in my 'demo' you use EnableSQW(1,false) (1kHz not battery backed) for example
Using the FC component RTC::Initialise() 100msDelay SQWIntControl(1,1, 0) gives the same result.

It is also possible to 'lock-up' the RTC chip - it doesn't seem to accept commands and the SQW output remains as previously set. I'm looking at ways to unlock it (note this seems like the lockup that occurred if no restart was received on i2c when reading data (which was a bug in the component some years ago - resetting the time corrected this?)

Martin

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

hello martin
to make things simpler I changed the test code to a minimum, I connected the led B7 to visually see if something changes, but I see the led that turns on and off at the frequency of 1Hz, even changing the parameters, unfortunately I'm out of the office and I don't have the instruments available, but arduino and my test cards are always with me.

Martin writes:
"I notice in your code that the interrupt handler (second) has an infinite loop (while (1)) - this will lock up your MCU (note that in the code posted this interrupt is not enabled) - you also enable and disable an interrupt in the interrupt handler. I'd guess that this probably won't do what you expect."

yes I know it was just a test, but if in some conditions I need to exclude an interrupt, what can I do?

Martin writes:
"Using the FC component RTC :: Initialise () 100msDelay SQWIntControl (1,1, 0) gives the same result."

I tried to delay, but it didn't work

I am attaching 2 videos with 2 different settings

thank you
Attachments
VID_20200701_231042.zip
(2.7 MiB) Downloaded 48 times
arduino_RTC.fcfx
(10.75 KiB) Downloaded 49 times

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

A very simple blinkie - using the 1Hz SQW. Note i've used an Arduino Nano - so you'll need to change the LED pin (I've used a property for this) I'm just blinking the built-in LED on B5
blinkie.fcfx
(10.16 KiB) Downloaded 40 times
Note that this will work without sleeping the MCU (it will repeatedly output 0 or 1 to the pin) - 1khz (or above) just appear 'on' - though should be 50% as bright..

Martin

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

hello Martin, it's about what I did in my second post, if you notice in the two videos I changed the frequency select first with 1 and then with 3 but the led always flashes at 1Hz
thanks

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Can you get my example to work? If no - RTC may be 'stuck'.

I'm just about to upload my 'component' version - I've added a RestartRTC macro (which might do the trick - clears EOSC and OSF flags)
If not then run the Arduino DS3231 set example or pull the power (inc battery)

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Just noticed:

Your code should give a 0.5s blinker. The clock outputs 500ms high then 500ms low pulse - the 1s is measured rising edge to rising edge (or falling to falling). Outputting the pin level should their give 0.5a on 0.5s off..

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

Hi Martin
I tried your code but there was no change, I pulled off the power and also the battery
thanks

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

And did that work?

Other things to check - pull ups on the sda and scl lines ( most rtc breakouts have them installed).
Can you read the time from rtc?

Martin

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

no with your code it doesn't work
with my code I read and write the hour
yes the resistances are there and they are from 4,7K

today I did tests with a ds1307 with my original code for ds3231 (changing the properties of the RTC), but it doesn't work, I don't read and I don't write, strangely changing the module and inserting the ds3231 I read the time
thanks

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Just tested on an Arduino Mega - so LED set to 13(B7), SQW connected to D2 (Int4) SDA, SCL VCC and GND connected.

- the RTC does as expected - outputs a square wave (checked with oscilloscope)

The Arduino doesn't wake from sleep on interrupt however. So remove the C code (sleep) in the loop - and everything works fine (so interrupt is working). I'm not sure why the Mega 2560 doesn't wake here - I'll check the datasheet - but maybe need to use a different interrupt pin?

Just checked - and interrupts 7-4 only work on level (so LOW to wake the MCU) - however FC only offers FALLING or RISING here, so won't work.

Moving the SQW connection to INT2 (RX1) and re-enabling the sleep - and everything works AOK.

Martin

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

hi Martin
can you publish the test code for mega2560 that i try too?
thanks

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Tested on Arduino Mega R3
blinkie.fcfx
(10.09 KiB) Downloaded 32 times
Note that the SQW pin needs to be connected to RX1 (pin 19) for this to work (as is). As mentioned if you omit (or disable) the C block which sleeps the MCU - this will still work. Int4 (pin 2) doesn't work on edge change interrupt to wake the MCU - hence the use of INT2 here - but could be used if the MCU wasn't slept.

Displays 'full' brightness for 1s - at the start so that 1kHz (and above) difference is visible (your eyes can't cope with 1kHz flash - so should just have 50% brightness)

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Did you manage to get this to work?

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

hi Martin
I'm sorry for the delay with which I reply
I tested your code by modifying my hardware, with your settings I see the led blinking at a frequency of 1Hz, I changed the parameters of the SQW output, but the led always blinks at 1Hz, I probably explained myself badly in the first post , I need to change the frequency of the SQW output, to 1024Hz (option 1) or better to 4096 (option 2), and this the SQWandINTcontrol module does not seem to do it
thank you very much for your interest

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Strange it's not working for you - works with all parameters (0..3) giving 1, 1k 4k and 8k output on my kit (checked on the 'scope)
Feels like something must be wrong with the setup - I would guess that the i2c isn't working correctly - are you using a breakout board or 'bare' chip - most boards have pull-ups built in but not all.. seems unlikely, but could it be a faulty board? Do you have anything else connected?
Not doing what I did - powered rtc off a couple of pins and forgotten to set them to 0 and 1.
Cr2032 flat?

Have you tried with my library - the source is on the post - so you can check the values of the rtc registers. (I need post an updated version - but I'll modify the above to use it)

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Here I've modified my code - cycles through sqw speeds - not much to see at other than 1hz. But also dumps rtc regs to UART.. 0x0E is the one to watch.

SQW to D2 here (so we can use UART)
ds3231 test.fcfx
(67.67 KiB) Downloaded 39 times
Martin

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

hello
I downloaded your code and I saw that it has many more functions, so I downloaded your library and copied it inside the component folder (correct?), but when on FC8 I look for DS3231 I can't find it. (what is the correct procedure?)
I use a card where a 24C32 type memory is also mounted which is not currently used
pull-up resistors are present
nothing else is connected to the I2C line
CR2032 is ok
thanks
Attachments
IMG_20200712_164752.jpg
IMG_20200712_164752.jpg (97.34 KiB) Viewed 2003 times
RTC_ds3231_zs042_schematic.png
RTC_ds3231_zs042_schematic.png (56.29 KiB) Viewed 2003 times

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Did you try the above project. It is standalone - don't worry about the extra macros - they would be hidden in the library. I wanted to use dump register here - which I'd taken out of the library.
Looks like card i used:)

To install the library - copy it to a new location. Then point flowcode at the folder options-locations (from memory - I'm on my phone)

Martin

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

Hi Martin
I tried your code, from what I understand every 5 seconds you change the setting of the SQW output, I attach the file captured by the serial port, I looked with the oscilloscope but the output is always set to 1Hz
Attachments
capture.txt
(17.77 KiB) Downloaded 31 times

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

The value for rate in 0x0E look correct - something else is going wrong.. I've used the same hardware here - and the result is as expected. Checked the 32khz signal too and all good.

I think I see the problem - bit 7 of 0x0f (osf or oscillator stop flag) is set to 1 - try adding a ResetRtc (or was it restart - on my phone again) towards the top of the program after initialise..

Martin

maxtisc
Posts: 107
Joined: Mon Dec 23, 2013 9:34 pm
Has thanked: 2 times
Been thanked: 9 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by maxtisc »

Hi Martin
freq 32khz is ok
I tried to insert the reset macro after the initialisation but there were no changes on the operation.
I looked at the DS3231 datasheet and the 0x0E register seems to be commanded correctly
I try to buy another card maybe mine is broken and maybe we are going crazy for nothing, I will keep you informed
thank you

mnf
Valued Contributor
Valued Contributor
Posts: 1063
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 413 times
Contact:

Re: Arduino mega2560 problem with DS3132 RTC

Post by mnf »

Ok - good idea. One other idea to try - use the arduino IDE and use the ds3231 set time example. I used this extensively when the fc component used to lock the rtc. I'll have look see if it does anything else other than clear the OSF flag.. If you want to play you can use SendByte to write the registers...

Martin

Post Reply