Electronic – Calculating Milliseconds using timers in PIC and synchronizing it with DS1307 timing

rtctimer

I have used DS1307 in my project which gives me the values of Hour, Min, Sec but I also need values of Milliseconds. I am planning to use timers with 1 millisecond timing and then synchronizing it according to seconds of DS1307.
I have made the program for 1ms delay but the problem is I am not sure if it is running with 1ms or not because I used led blinking and we cannot see led blinking at 1ms. My first question is, is there any way I can be sure that it is giving 1ms delay.?

Now lets say if its exact 1ms timer. Now how to sync it with DS1307 timing. My total application is like if I receive inputs then I have to show which inputs are turned ON on UART with exact timings (including Milliseconds). For example, if input 4 in ON then:

/*
**some code
*/
 if(PORTEbits.RB5 == 1)
 {
   putsUART1("Input 4 ON, Time:  ");
   putsUART(TIME);
 }

Now this TIME should contain the values of HH:MM:SS:MS. Till now I am able to show HH:MM:SS only. I also cannot use sqw/out because its custom made PCB and that pin is left blank. Pleas help

EDIT:

I have included count++ in ISR which is incrementing at 1ms. In while(1) I am printing the values of count after reading sec in RTC_read function

 while(1)
 {
    RTC_read();

    sprintf(TIME,"%s%s:%s%s:%s%s",hr2,hr1,min2,min1,sec2,sec1);
    putsUART1(TIME);
    putsUART1(" ");
    putsUART1(COUNT);
    putsUART1("\n");
 }

void __ISR(_TIMER_2_VECTOR, ipl3) Timer2Handler(void)
{
  mT2ClearIntFlag();
  count++;

}

RTC_read function:

/*
* some code
*/

 //read sec
   while(!I2CReceivedDataIsAvailable(RTC_I2C_BUS));
   Nop();
   sec = I2CGetByte(RTC_I2C_BUS); 
   AckI2C2();
   while (!I2CAcknowledgeHasCompleted(I2C2)); 

   /*
    * Milliseconds
    */
   if(count >= 999)
   {
       count = 0;
   }
   else
   {
       sprintf(COUNT,"%d",count);

   }

Best Answer

I would suggest that your idea for millisecond resolution is somewhat unrealistic as the time it takes to read the HH:MM:SS out of the RTC chip at typical I2C data rates will have time latency variation greater than a millisecond.

Reporting millisecond resolution is also debatable when your reporting medium is a UART serial port operating at a bit rate that is slower than a millisecond per bit.

I encourage you to instead take your custom circuit board and unsolder the DS1307 chip and replace it with a pinout compatible M41T81S chip from ST Micro. (Package compatible too if you have deployed the SO-8 type device).

This latter device conveniently contains an additional time keeping register that provides you with tenths and hundredths of seconds resolution counts. Reading out HH:MM:SS.ss is compatible with the net 10msec resolution.

The M41T81S also has a very nice calibration feature that allows the internal timekeeping functions to be adjusted to trim for minor frequency variations of the RTC 32.768kHz crystal. I have a prototype unit here that has this chip used in it that once calibrated has been continuing to display the correct time (within a few seconds) for nearly a year already. I doubt that you can achieve that with your DS1307 unless you have designed its XTAL circuit with trimming capabilities.