Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Time update problem with DS1307 + pic18f25k22

Status
Not open for further replies.
i need to count pulses per 1/2 sec so i used this code

Code:
 char temp;
T0CON.TMR0ON = 1;
   TMR0L = 0;
   TMR0H = 0;
   Delay_ms(500); // Wait for 0.5 sec
   T0CON.TMR0ON = 0;    // Stop the timer

   temp = (256*TMR0H + TMR0L);

how to replace delay with timer delay in this?
 

is this solution for time lagging? or not updating of ds1307 ?
 

What pulses do you want to count? External pulses? If yes, then you have to use T0CKI or T1CKI pins to count pulse. Explain your project in detail.
 

yehh,, i used T0CKI pis as input for speed sensor (hall sensor), my project is for motorbike,
Digital display system,
it shows speed, trip, date and time, engin temp,fuel level,battery level, and some warnings.
 

hello,

to add a delay of 0,5sec
you can use the output 1Hz of DS1307
square forme 0,5sec ON , 0,5sec OFF
and link to an unsued input ex : RC2

Code:
char temp;
   While(RC2==0); // wait for rising edge 
   T0CON.TMR0ON = 1;
   TMR0L = 0;
   TMR0H = 0;

   while(PORTCbits.RC2==1);    // Wait for 0.5 sec
   T0CON.TMR0ON = 0;    // Stop the timer

   temp = (256*TMR0H + TMR0L);






Concerning I2C, there is some parameters linked to Fosc
not the same for 16Mhz and 64Mhz
i don't know how works I2C library in MikroC
but example in C18 for FOSC 10Mhz or 20Mhz :
Code:
.....
// module I2C a 100KHz
   OpenI2C(MASTER, SLEW_OFF);
   SSPADD = 24 ;  // at 10Mhz et  49 at 20MHz;
   IdleI2C();
   StartI2C();
.. etc ..

and maybe you can use Capture mode with CCP1 and Timer1 !
signal on RC0 = input of timer1
syncho to rising edge on RC2 = 1Hz
so measure the frequency in Hz
 
Last edited:
i already did PCB, so, i cant use 1hz o/p of ds1307 to MCU :sad:
and my problem is time becoming laggy, for 1 hr 5 minits lags. what is this for?
 

hello,

Maybe your 32khz quartz is not adapted for DS1307..
Can you check the duration of SQWOUT 1hz with a periodmeter ?
must be 1000 000 µS ! +-5 ppm at room temperature 25°C

for your delai of 1sec , you can use a timerX 16 bits in a waiting loop, no use of interrupt.
example with timer1 with Q=20Mhz
and a serie of NOP to final adjustement to exactly 1 seconde delai.

Code:
 unsigned long CumulTE;


void main()
{


//.......
// configure le TIMER1
  T1CON=0x00;
  T1CONbits.RD16=1;       // timer mode 16 bits
  T1CONbits.T1CKPS1=1;    // Prescaler =1:8
  T1CONbits.T1CKPS0=1;    // Prescaler
  T1CONbits.T1OSCEN=0;    // Timer1 oscillateur shutt off
  T1CONbits.T1SYNC=1;     // No Synchro
  T1CONbits.TMR1CS=0;     // internal clock = Quartz Fosc/4
  T1CONbits.TMR1ON=0;     // desactive Timer1
  IPR1bits.TMR1IP=1;      // set hihh priority for Timer1
  WriteTimer1(3035);      // 62500*8*4*1/QMhz =>  =100 msec
.......
CumulTE=0; 
PIR1bits.TMR1IF=0; 
PIE1bits.TMR1IE=0;      // Disable Interrupt Timer1

// ===== delay for 1 seconde =================
T1CONbits.TMR1ON=1;   // arme timer1
do
{ 
if(PIR1bits.TMR1IF==1)
{
// init timer1 avec 3035  pour 100mS =>  donc 62500 cycles at 20Mhz
// 1 cycle = 4/FQuartz= 4/20= 1/5 de µS ou 0,2µS
// ajustement de 3040-3035 = +5 sur init timer => -5 x (prescaler=8)= -40 cycles de 0,2µS  
// soit => -8µS sur 100mS   (ou -80µS sur 1sec )   
// on diminue la duree totale en augentant la valeur d'init timer1    
 WriteTimer1(3040); // value must be over theorical value , so global delay less than 1sec.
 PIR1bits.TMR1IF = 0;
 // reglage fin duree de mesure
//Final adjustement by adding 0,2µS values    
 _asm
 nop // +0.2µS
  nop
   nop
    nop
     nop
      nop
       nop
        nop
         nop
          nop
           nop
 
 _endasm
 //  correction globale sur 100ms => -8µS + 1,2µs => -6.8µS
  count_100mS = count_100mS  + 1;
 }               
 }while (count_100mS < 10 );     soit 1 seconde ecoulee 
.....
 
now,
i removed pic, and put my circuite aside for 15 days, not even touched it, today i switch it on and it got 1 day delay,
there is any other problem i think
 

1rst step :
Check the duration of the output of DS1307 SQWOUT with a led -- R=1K--->+5V -> must gives 1hz ?
count number of lighting within one minute ... must be 60
is your battery OK > 3,0V ?
 
hey...
I've changed DS1307, and its kind of working, ill update answer tomorrow, when it confirmed.

paulfjujo, There is no fluctuation at SQWOUT pin, always switch on
 

Timer0 Interrupt Code for 0.5 sec at 4 MHz.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Timer0
//Fosc = 4 MHz
//Prescaler 1:8; TMR0 Preload = 3035; Actual Interrupt Time : 500 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  T0CON  = 0x82;
  TMR0H  = 0x0B;
  TMR0L  = 0xDB;
  GIE_bit = 1;
  TMR0IE_bit = 1;
}
 
void Timer0_Interrupt() iv 0x0008 ics ICS_AUTO {
    TMR0IF_bit = 0;
    TMR0H = 0x0B;
    TMR0L = 0xDB;
    //Enter your code here
}

 
Ohhhh,, Thanx a lot for code, im glad............
But im using timer 0 for counting external pulse. i can use timer 1 like above code?
 

What is the max RMP? You can use T0 or T1CKI. My code is for Timer delay using interrupts. You can use external interrupt pin. When RB0/INT0 goes high or low depending upon edge select, turn on Timer and clear INT0 interrupt flag. Then on next INT0 interrupt stop timer. Timer value gives time for 1 rotation. use it to calculate RPM. If RPM is 1 then timer interrupt should be used with a count variable which holds no of timer overflows.


Zip and post your full project files. I will debug and see what is causing the time lagging.

https://ww1.microchip.com/downloads/en/devicedoc/41214a.pdf
 
Last edited:
heyyy...

It's working, i dint changed any of the codes, just replaced DS1307, now its perfect, no delay, no lag


Thanks a lot for all your support and help, thank jayanth you helped me a lot..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top