Page 1 of 1

RTC Component

Posted: Wed Jul 19, 2017 7:03 pm
by mnf
Just playing with the RTC component - to use a DS3231 with an Arduino.

I was unsuccessful - so tried the DS3231 component uploaded by Jordy and got thing pretty much working as I wished - including an interrupt to wake the Arduino from sleep mode. (Though not all plain sailing - see attachment for details)

I then returned to the 'official' RTC component.

First problem - the i2c address for the clock chip should be writable.
Next - setting the address to that of my RTC (0x57) gave no result.

Looking at the C generated for example for SetYear:

Code: Select all

void FCD_0a3a1_RTC1__SetYear(MX_UINT8 FCL_YEAR)
{
    //Local variable definitions
    MX_UINT8 FCL_DATA;
    if (FCL_YEAR < 100)
    {
        #if (0)
        //Code has been optimised out by the pre-processor
        #else
            #if (0) // 2 == 0
            //Code has been optimised out by the pre-processor
            #else
                #if (1) // 2 == 1 || 2 == 2
                    FCL_DATA = FCL_YEAR % 10;
                    FCL_DATA = FCL_DATA + ((FCL_YEAR / 10) << 4);
                    //Comment:
                    //DS1307 & DS3231 
                    FC_CAL_I2C_Master_Start_1();
                    FC_CAL_I2C_Master_TxByte_1(0x57);               *****  i2c write start *****
                    FC_CAL_I2C_Master_TxByte_1(6);
                    FC_CAL_I2C_Master_TxByte_1(FCL_DATA);
                    FC_CAL_I2C_Master_Stop_1();
                // #else
                //Code has been optimised out by the pre-processor
                #endif
            #endif
        #endif
    // } else {
    }
I have added the ** i2c write ** (and changed the address to hex) - however should this really be (0x57 << 1) for an i2c write command?
Or am I missing something here?
sleep.fcfx
(14.73 KiB) Downloaded 351 times
DS3231 example with comments as to some problems

[
t1.fcfx
(10.91 KiB) Downloaded 335 times
RTC example - doesn't work.

Martin

Re: RTC Component

Posted: Mon Jul 24, 2017 2:31 pm
by Benj
Hi Martin,
Next - setting the address to that of my RTC (0x57) gave no result.
Looking at the DS3231 datasheet shouldn't the address be set to 0xD0?

Re: RTC Component

Posted: Mon Jul 24, 2017 6:36 pm
by mnf
That's the default - but no result using that either..

Should still be 0xD0 << 1 for i2c writes?

This gives 11:51:51 repeatedly..... Even with a set at the start of code..

Re: RTC Component

Posted: Tue Jul 25, 2017 5:58 pm
by Benj
Hello,

I believe the 0xD0 already includes the shift.

Write = 0xD0
Read = 0xD0 | 0x01

Sounds like reading may be working? but writing is not. Or neither are working and you are simply reading uninitialised RAM values.

Are you using external pull up resistors on the I2C SDA, SCL signals?

Re: RTC Component

Posted: Wed Jul 26, 2017 3:10 pm
by mnf
Yes - pullups in place.

Note that the DS3231 example does work - so wiring roughly correct (hopefully)

I've tried with D0 as i2c address (though perhaps the name is incorrect here)

I'll play some more and see if I can get to work - or work out why not working....

Re: RTC Component

Posted: Fri Jul 28, 2017 8:43 pm
by mnf
Some more experimenting - this time I used a data-logger shield with an inbuilt DS1307.

Using the Arduino library examples SetTime and ReadTest - reveal that the hardware works.

Uploading a simple FlowCode example - read the time - stops the clock chip (checking value by reloading ReadTest). Re-running SetTime restarts it.

An 'empty' flowcode program which only initialises the RTC and UART doesn't have the same effect but any read - ie GetHours(0) - stops the clock...

Looking at the C generated by using the DS3231 library and the FlowCode RTC library the main difference I see is that the DS3231 library (which works!) has an I2C Restart before reading data:

Thus from ReadTemperature (DS3231 library):

Code: Select all

    FCD_005f1_I2C_Master1__Start();
    FCD_005f1_I2C_Master1__TransmitByte(FCV_0d1f1_DS3231__WRITE);
    FCD_005f1_I2C_Master1__TransmitByte(0x11);
    FCD_005f1_I2C_Master1__Restart();
    FCD_005f1_I2C_Master1__TransmitByte(FCV_0d1f1_DS3231__READ);
    FCL_TEMPHIGH = FCD_005f1_I2C_Master1__ReceiveByte(0);
    FCL_TEMPLOW = FCD_005f1_I2C_Master1__ReceiveByte(1);
    FCD_005f1_I2C_Master1__Stop();
The RTC library generates:

Code: Select all

             
                FC_CAL_I2C_Master_Start_1();
                FC_CAL_I2C_Master_TxByte_1(208);    // i2c address << 1 | 0
                FC_CAL_I2C_Master_TxByte_1(2);       // Address to read
****   Need an i2c restart here????
                FC_CAL_I2C_Master_TxByte_1(208 | 0x01);   // i2x address << 1 | 1 - read from  
                FCL_DATA = FC_CAL_I2C_Master_RxByte_1(1);   // Get 1 byte..
                FC_CAL_I2C_Master_Stop_1();
Note this is from GetHours and I've removed a few pre-processor directives to tidy (but not alter) the code.

Looking at the Arduino RTC library code - it also seems to have a restart at the same point.

The 11:51:51 seems to be the 'clock is stopped' default memory values.

Martin

Re: RTC Component

Posted: Sat Jul 29, 2017 3:21 pm
by mnf
Playing with this has also highlighted another bug...

The 'C' code item box can be resized - however the actual edit box and buttons etc are not affected - only the bounding box moves..... After closing and reopening the box the correct (ie enlarged) control is displayed.

Also in the 'C' code view - why do page up and down keys not work..

Re: RTC Component

Posted: Sat Jul 29, 2017 3:29 pm
by mnf
I tried adding an i2c restart - and indeed this seems to work.

I cut and pasted the generated C code for GetMin into a macro and then edited (I've commented where I added the restart)

This certainly works for GetMin - and I think should fix the other read routines although I don't have time to test this at present.....

The following snippet of FlowCode demonstrates:
ds1307.fcfx
(11.23 KiB) Downloaded 368 times
Martin

Re: RTC Component

Posted: Mon Jul 31, 2017 3:12 pm
by Benj
Hi Martin,

That's brilliant, many thanks for letting me know. I have applied the fix to the component and hopefully it should work correctly for you now.

Simply drop the attached file into your "Flowcode 7/components" directory, restart Flowcode and hopefully the component should now work correctly for you.
RTC.fcpx
(9.82 KiB) Downloaded 397 times
Let me know how you get on.

Re: RTC Component

Posted: Mon Jul 31, 2017 11:05 pm
by mnf
Thanks,

Certainly seems to work perfectly for reading and writing to RTC - tested with DS1307 and DS3231 (on Arduino ATMega328p using an Uno R3 and a Nano - I'd also rather like to get it working with an ATTiny85 - but that's a project for another day).

Haven't tested all the macros - I'll have a play with interrupts on the DS3231 in more detail - on the DS1307 I got a 1Hz (and 60Hz) interrupt working without difficulty.

Martin