DS1307 Synchronizing Problem

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys I'm making 4 digits seven segments clock/Timer.

I have not use the SQW pin in DS1307.

How do you all synchronizing your seconds with the uC program?How often do you all read DS 1307?

Note that in the ISR I'm doing the multiplexing.

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I have not use the SQW pin in DS1307.
So do you WANT to use it or not? It's an easy way of doing the synch with an interrupt.

Otherwise you will need to read the seconds register, say every 100ms, and compare it with the previous valid reading. Synch when the 2 don't match. But this could be 100/200ms out of synch.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi John thanks for your info.

Personally I don't like to read I2C every time like 10 times a second.Is there any wrong reading I2C lots of times!!Not sure.

I think using the SQW pin is lot easier scenario.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely you only read the DS1307 when you want to know the time.

For example, if you want to display the time continuously, you read every second.
If you only display hours and minutes, you only need to read every 60 seconds.

If you use the SQW pin to interrupt, you know that the second has just changed.
Otherwise, you can just set an AVR timer to read every second. (having done a little synchronisation loop to get started)

Note that a 'RadioCode' clock tends to read the MSF signal very occasionally. The rest of the time it is just free-running to conserve batteries.

Likewise, your PC only fetches the time from the Internet on an occasional basis.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello David from your post I have changed my mind not to use SQW pin.

I plan to read DS1307 every second.

Can you tell me how often do I have to read DS1307.In other words the Timer delay?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can do a 1 second interrupt with Timer1 in CTC mode.

volatile char flag_second;   // flag to get set by ISR()

ISR(TIMER1_COMPA_vect)
{
    flag_second = 1;
}

...

   TCCR1A = 0;
   TCCR1B = (1<<WGM12)|(5<<CS10);   // CTC mode, div1024
   OCR1A = (F_CPU/1024/1) - 1;      // 1 second
   TIMSK |= (1<<OCIE1A);            // enable Compare IRQ
   sei();                           // enable global IRQs
   ...

    while (1) {
        if (flag_second) {
             flag_second = 0;  // reset the flag
             read_DS1307();
             display_time();
        }
        ...                    // do other useful things
    }

Untested. SFR names may vary for your AVR. Note how simple the ISR() is.
Also note that you will probably be writing / reading about 8 bytes on the TWI bus. This will take about one millisecond.

Young people are convinced that they can read LCDs in less than a millisecond. Personally, my eyes do not work that fast.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Joseph1 wrote:
...I have changed my mind not to use SQW pin.
So you are going to use it or not? It's not clear...

I feed a 1Hz SQW into a pin change interrupt pin and there's:
- one line of code in the interrupt

//if pin is low, seconds have just changed
//if pin is high, ignore
if (DS1307_SQW == 0){DS1307Dirty = 1}

- two lines of code in the main loop

if (DS1307Dirty == 1){
   read_DS1307();
   DS1307Dirty = 0;
}

Very simple. Very robust.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello guys I'm not using SQW pin.

The thing is I'm multiplexing 4 seven segments in the ISR (Interrupt driven multiplex routine).

I'm doing similar to Davids method. I set up a timer 4mS interrupt rate because I have 4 SSD (Seven Segments) 4 X 4 = 16mS = 62.5Hz

In the main routine I use the same timer & check if it is 1S reached, if it reached then read DS1307, read only minutes & hours. Not reading seconds.

The updated values will be loaded to the 4 seven segments after each frame ends in the ISR.

In this method the accuracy of the seconds will depend on uC XT or RC.

Is my method ok?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you are already using a timer to run the multiplex, you might just as well use it for counting seconds. e.g. when it counts to 250 (250 x 4ms = 1 second) set your DS1307Dirty flag.

Yes. Of course you will stray after a while e.g. you set the flag at 12.999 seconds. By the time you have read the DS1307 registers, it is 13.000 seconds.

If you want the display to alter 'exactly' on the second, you either use the SQW o/p or you read the seconds register more frequently.

Even if you are using the internal RC for your AVR clock, it will not stray that far in a minute. So you could choose to 'synchronise' every minute or hour. i.e. you reset your '250th_tick' variable when the second changes.

Your multiplex is totally oblivious of how often you use the I2C bus. Your I2C bus will not 'wear out' with how many times you read / write to it.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See my refresh rate is 16mS.In this time I set the DS1307Dirty flag.But the code reads DS in the main routine but update the display after each frame ends so there is a 16mS delay.I think this is not a problem.

Quote:
you reset your '250th_tick' variable when the second changes.

I can't understand this david.I'm not resetting any variable & I'm not reading seconds register from DS1307.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why not say what you are displaying?
If you are simply doing HH:MM then you really do not care whether the MM changes at 59.999 seconds or at 0.999 seconds.
Personally, I would like a HH:MM:SS display to change exactly on the second (or near enough).

Regarding your multiplex ISR(), you know that it occurs every 4ms. Presumably you have a counter that goes 0,1,2,3,0,1 ... to illuminate the required digit for the next 4ms period. You simply add another variable that counts up to 250 then sets the 'one_second' flag.

You can create whole AVR applications that 'run' off of a regular 4ms interrupt. e.g. multiplex displays, scan keyboards, read DS1307s, fry bacon, make cups of tea ...

You probably only eat bacon at breakfast (i.e on the 21600000th IRQ)
I drink tea every 900000th IRQ. You might read the DS1307 every 250th IRQ.

Note that if you use a PCF8563 you could use the alarm function to start making breakfast.

David.