Real time clock using 8051 timer and interrupt

Status
Not open for further replies.

nitaibrata

Newbie
Joined
Jun 24, 2020
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
12
hello am new to microcontroller ,i will be glad if my error could be pointed out please.am writing a code so that real time clock can be displayed but the problem is it doesn´t increment and even so i have to set the time which i think is not ideal if it is rtc.here is my code:
Code:
#include "config.c"
#include "def_pinos.h"
#include <stdio.h>


unsigned int hora, min, seg;
unsigned int check;




//a period = (65535 - 50000) = 3CAF (0.05 s/pulse) */
/* 0.05 x 20 = 1 s/pulse */


void delay_ms(unsigned int tempo){

    for (;tempo;tempo--){
        TR0 = 0;
        TF0 = 0;
        TL0 = 0xAF;
        TH0 = 0x3C;
        TR0 = 1;                                                                                   
        while(TF0 == 0);
    }
}


void set_time (char Shora, char Smin, char Sseg)
{
    hora = Shora;
    min = Smin;
    seg = Sseg;
}


void print_time ()
{   
    char h1, h2, m1, m2, s1, s2;
    h1 = hora/10 + '0';
    h2 = hora%10 + '0';
    m1 = min/10 + '0';
    m2 = min%10 + '0';
    s1 = seg/10 + '0';
    s2 = seg%10 + '0';


    putchar (h1);
    putchar (h2);
    putchar (':');
    putchar (m1);
    putchar (m2);
    putchar (':');
    putchar (s1);
    putchar (s2);
}

  

void time_inc(void)    
{
   
    check++;                     

    if(check==1000)              
     { 
      check=0;                    
       seg++;


    if(seg>59)
     {  
       seg=0;
       min++;


   if(min>59)
     {  
       min=0;
       hora++;
  

   if (hora==24)
     {
      hora=0;
      }
  }
              }
        }

}

void incrementaTempo () __interrupt 5 //Interrupção a cada 1s
{
    seg++;
    if (seg<1)
    return;
    seg = 0;
    time_inc();
   
}   



void main () {
   
    Init_Device();

    SFRPAGE = LEGACY_PAGE;

    TCON=0x00;
    TMOD=0x01;
    IE=0x83;
    TL0 = 0x00;
    TH0 = 0xB7;
    TR1 = 1;
     EA=1;
     ET0=1;
    
    set_time (01,02,00);
   
     print_time ();
    time_inc();
    delay_ms(1000);


    }


void putchar (char c) {
   SBUF0=c;
   while(TI0==0);
   TI0=0;
}
void int_serial(void) __interrupt 4{
    unsigned char c;
    while(!RI0);                           // Wait until character received completely
    c=SBUF0;                       // Read the character from buffer reg
    RI0=0;  

}
 
Last edited by a moderator:

sorry, i have no idea why your code doesn't work

however, it is in serious need of documentation

make your variable names have meaning to a reader
for example, TR0, TF0, TL0, and TH0 only vary in one character,
are meaningless to me a a reader, and in 6 months will be meaningless to you too.

put in some comments that describe what each section does.
each section that starts with void (name) (variables or void)
should have a few lines saying what that section accomplishes

any part of the code that is there for debugging and is not actually part of the
code that accomplishes something should be indicated as such, so you and
a reader can know what is the necessary working parts and what is there for troubleshooting
 

hello,

i build this clock with AT89C2051 , using timer to build sec,mn,hour
i get a shift of 2mn within a laps of 21 days .. need to adjust the value of timer interrupt..
it was a challenge for me ,because i am working with PIC familly .
In my case, not enough pins on this MCU to handle an RTC DS3231 to get a good time accuracy.

link for details : **broken link removed**
 

Attachments

  • Horloge_SHE878_AT89C2051_180606.c.txt
    15.1 KB · Views: 78

Hi,

2 minutes in 21 days is about 60ppm. You may find a better xtal or try to calibrate them with a variable capacitor.

Klaus
 

it was a Hardware pakage from RPC .. so poor quality for the quartz ...
Now i use RTC DS3231 with PIC18F .. no problemo
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…