Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Clock accuracy

Status
Not open for further replies.

alphacat

New Member
I use 8051's RTC timer in order to keep track of current time.
Every time the timer expires, I increment a variable tick by 1, and when tick equals NUMBER_OF_TICKS_IN_SECOND I increment current time by 1 second (and clears tick).

My problem is finding the exact value of NUMBER_OF_TICKS_IN_SECOND for the time ticking to be accurate.

Currently, I have an error of 3 minutes every 10 hours.
Is there any efficient to find the correct value?

Moreover, are there any tips for reaching high accuracy of time ticking?

For example, currently some of the timers constantly operate and some of them sometimes operate and sometimes not.
Is it bad for time accuracy that the system doesnt operate always at the same level, with respect to timers?
 
Last edited:
With C is more difficult to correct the clocking time.

Without the code to look, it is a bit difficult to give some suggestions, but you could just do a correction, for example 10 hours = 600 minutes.

3 minutes / 600 = 0.005, so every 0.005 minute, you skip one count.

0.005 minutes = 0.3 seconds.

For example, if a second takes 50000 ticks, then each tick last about 20 microseconds.

0.3 second / 20 micro-second = 15,000.

So each 15,000 ticks, you skip one tick counting.
 
My problem is finding the exact value of NUMBER_OF_TICKS_IN_SECOND for the time ticking to be accurate.
Generally you would need a reference clock to compare against. An example is the 1pps clock from a GPS module.
 
I have done this kind of project before 4 years. Project title="Digital Clock with Alarm"
Below is the code for that (assembly language)
See if it can help you...


Code:
[COLOR="SeaGreen"];################################################################
                ;       Port 2 = DATA BUS
                ;       p0.0   =  EN
                ;       p0.1   =  R/W
                ;       p0.2   =  RS
                ;       p1.0   = RESET KEYS 
;################################################################[/COLOR]
        org 0000h
        jmp start
        org 000bh
        jmp intrupt
        org 001bh
        jmp intrupt1

start   mov tl0,#b0h
        mov th0,#3ch
        mov tmod,#11h
        mov ie,#8ah

        call reg_initial
        call lcd_init
        mov dptr,#table1
        call disp_tble
        setb tr0

back    call display
        call keys
        call Alrm_compare
        jmp back


intrupt mov tl0,#b0h
        mov th0,#3ch
        djnz 62h,int_ret
        mov 62h,#20
        call incre
int_ret reti


                
keys    jb p1.0,key_sett                ;For RESETTING all operations.
        call debounc
        jb p1.0,key_sett
        jnb p1.0,$
        call reg_initial
        mov dptr,#table1
        call disp_tble

key_sett setb p1.1                      ; For sensing the SETTING KEY
         jb p1.1,key_phon
         call debounc
         jb p1.1,key_phon
         jnb p1.1,$

         mov 53h,#00h
         call set_mode                   
key_phon setb p1.6
         jb p1.6,key_ret
         call debounc
         jb p1.6,Alram
         jnb p1.6,$
         call phone_book

Alram    setb p1.4
         jb p1.4,stop_watch
         call debounc
         jb p1.4,stop_watch
         jnb p1.4,$
         call Set_Alram


stop_watch      setb p1.5
                jb p1.6,key_ret
                call debounc
                jb p1.6,key_ret
                jnb p1.6,$
                call StopWatch
                call lcd_start
key_ret         ret
                

set_mode clr tr0
         jb p1.1,lp12
         call debounc
         jb p1.1,lp12
         jnb p1.1,$
         inc 53h
         mov a,53h
         cjne a,#08,lp12
         mov 53h,#00h
lp12     setb p1.2
         jb p1.2,decre_key
         call debounc
         jb p1.2,decre_key
         jnb p1.2,$
         mov a,53h
         cjne a,#01,inc_min
         inc r0                         ;For incrementing the seconds
         cjne r0,#60,lp8
         mov r0,#00h
lp8      call display
         

inc_min  mov a,53h
         cjne a,#02,inc_hour
         inc r1                         ;For incrementing the minutes
         cjne r1,#60,lp10
         mov r1,#00h
lp10     call display

inc_hour mov a,53h
         cjne a,#03,incre_day
         inc r2                         ;For incrementing the hours
         cjne r2,#24,lp11
         mov r2,#00h
lp11     call display

incre_day  mov a,53h
           cjne a,#04,incre_date           
           inc r4
           inc r4
           inc r4
           cjne r4,#21,lp20
           mov r4,#00
lp20       call display 

incre_date mov a,53h
         cjne a,#05,incre_mnth 
         inc r3
         call date_compar
         cjne r3,#32,lp21
         mov r3,#01
lp21     call display

incre_mnth mov a,53h
         cjne a,#06,incre_year       
         inc r6
         cjne r6,#13,lp22
         mov r6,#01
lp22     call display

incre_year  mov a,53h
         cjne a,#07,decre_key
         inc r7
         cjne r7,#100,lp23
         mov r7,#00
lp23     call display


decre_key setb p1.3
          jb p1.3,esc_key               ;For decremening key
          call debounc
          jb p1.3,esc_key
          jnb p1.3,$
          mov a,53h
          cjne a,#01,dec_min
          dec r0                        ;For decrementing the seconds
          cjne r0,#ffh,lp15
          mov r0,#59
lp15      call display
         
dec_min   mov a,53h
          cjne a,#02,dec_hour
          dec r1                         ;For decrementing the minutes
          cjne r1,#ffh,lp16
          mov r1,#59
lp16      call display

         
dec_hour  mov a,53h        
          cjne a,#03,decre_day
          dec r2                         ;For decrementing the hours
          cjne r2,#ffh,lp17
          mov r2,#23
lp17      call display

decre_day  mov a,53h
           cjne a,#04,decre_date           
           dec r4
           dec r4
           dec r4
           cjne r4,#fdh,lp24
           mov r4,#18
lp24       call display 

decre_date mov a,53h
         cjne a,#05,decre_mnth
         dec r3
         cjne r3,#00,lp25
         call date_compar
         mov r3,74h
lp25     call display

decre_mnth mov a,53h
         cjne a,#06,decre_year       
         dec r6
         cjne r6,#00,lp26
         mov r6,#12
lp26     call display

decre_year  mov a,53h
         cjne a,#07,esc_key
         dec r7
         cjne r7,#ffh,lp27
         mov r7,#99
lp27     call display


esc_key   setb p1.7
          jb p1.7,set_mode1
          call debounc
          jb p1.7,set_mode1
          jnb p1.7,$
          setb tr0
          ret

set_mode1 jmp set_mode
          
disp_tble
        mov a,#01h
        call command

        mov r7,#00h
next1   mov a,r7                        ;For clearing the Acc
        movc a,@a+dptr
        cjne a,#'%',next                      
        mov a,#c0h
        call command
        inc r7
        jmp next1
next    cjne a,#'$',lp1
        ret
lp1     call data
        inc r7
        jmp next1

table1  DB "SUN 01/01/2000  %  00:00:00     $"
;          80123456789abcdefC0123456789abcdef
;              -  /  /    -- --  :  :  ------


display mov a,r0                        ;For displaying the SECONDS
        call divisn
        mov 43h,#c8h        
        mov 44h,#c9h
        call disp

        mov a,r1                        ;For displaying the MINUTES
        call divisn
        mov 43h,#c5h
        mov 44h,#c6h
        call disp

        mov a,r2                        ;For displaying the HOURS
        call divisn
        mov 43h,#c2h
        mov 44h,#c3h
        call disp

        mov dptr,#Days
        mov 73h,r4
        mov r5,#80h
loop4   mov a,r5
        call command
        mov a,73h
        movc a,@a+dptr
        call data
        inc 73h
        inc r5
        cjne r5,#83h,loop4

        mov a,r3                        ;For displaying the DATE 
        call divisn
        mov 43h,#84h
        mov 44h,#85h
        call disp

        mov a,r6                        ;For displaying the MONTH
        call divisn
        mov 43h,#87h
        mov 44h,#88h
        call disp
        ret

        mov a,r7                        ;For incremening the YEAR
        call divisn
        mov 43h,#8ch
        mov 44h,#8dh
        call disp       

        ret

lcd_start mov a,#83h
        call space

        mov a,#86h
        call slash

        mov a,#89h
        call slash

        mov a,#8eh
        call space

        mov a,#8fh
        call space

        mov a,#8ah
        call command
        mov a,#'2'
        call data
            
        mov a,#8bh
        call command
        mov a,#'0'
        call data

        mov a,#c0h
        call space

        mov a,#c1h
        call space

        mov a,#c4h
        call collon

        mov a,#c7h
        call collon
 
        mov a,#cah
        call space

        mov a,#cbh
        call space

        mov a,#cch
        call space

        mov a,#cdh
        call space

        mov a,#ceh
        call space

        mov a,#cfh
        call space

        ret
;          80123456789abcdef%C0123456789abcdef$
;              -  /  /    --  --  :  :  ------

space   call command
        mov a,#' '
        call data
        ret


collon  call command
        mov a,#':'
        call data
        ret
       
slash   call command
        mov a,#'/'
        call data
        ret                 

divisn  mov b,#0ah
        div ab                  ;Divide the content of Acc by content of B
        mov 40h,a               ;For storing the dat in Acc at 40h
        mov 41h,b               ;For storing the data in Reg. B at 41h
        ret

disp    mov a,43h
        call command
        mov a,40h
        add a,#30h      
        call data

        mov a,44h
        call command
        mov a,41h
        add a,#30h
        call data
        ret                    

incre   inc r0
        cjne r0,#60,M1_ret
        mov r0,#00h

        inc r1
        cjne r1,#60,M1_ret
        mov r1,#00h

        inc r2
        cjne r2,#24,M1_ret
        mov r2,#00h
       
        inc r4                          ;For displaying the Days
        inc r4
        inc r4
        cjne r4,#21,incdate
        mov r4,#00

incdate inc r3                          ;For displaying the date
        call date_compar                   
        mov a,r3        
        cjne a,74h,M1_ret
        mov r3,#01


        inc r6                          ;For displaying the MONTH
        cjne r6,#13,M1_ret
        mov r6,#01

        inc r7                          ;For incremening the YEAR
        cjne r7,#99,M1_ret
        mov r7,#00h

M1_ret  ret


Days DB "SUNMONTUEWEDTHUFRISAT"


date_compar     cjne r6,#01,FEB
                mov 74h,#32

FEB             cjne r6,#02,MAR
                mov 74h,#29
   
MAR             cjne r6,#03,MAR
                mov 74h,#32

APR             cjne r6,#04,MAY
                mov 74h,#31

MAY             cjne r6,#05,JUN
                mov 74h,#32

JUN             cjne r6,#06,JUL
                mov 74h,#31

JUL             cjne r6,#07,AUG
                mov 74h,#32

AUG             cjne r6,#08,SEP
                mov 74h,#32
                                
SEP             cjne r6,#09,OCT
                mov 74h,#31

OCT             cjne r6,#10,NOV
                mov 74h,#32

NOV             cjne r6,#11,DEC
                mov 74h,#31

DEC             cjne r6,#12,M_RET
                mov 74h,#32


;Date DB "JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC"
;         31  28  31  30  31  30  31  31  30  31  30  31

M_RET           ret

lcd_init mov a,#38h                     ;For displaying the 5 X 7 matrix
        call command

        mov a,#38h
        call command

        mov a,#38h
        call command

        mov a,#38h
        call command

        mov a,#0ch                      ;For displaying the cursor
        call command

        mov a,#01h                      ;For clearing the LCD
        call command

        mov a,#06h                      ;For shift right operation
        call command
        ret


command mov p2,a
        clr p0.2                        ;For enabeling the command register
        clr p0.1                        ;For Write operation
        setb p0.0                       ;For making enable pin of LCD HIGH
        nop                             ;NO OPERATION
        nop
        nop
        clr p0.0                        ;For making enable pin of LCD LOW
        call delay
        ret
                               
data    mov p2,a
        clr p0.1                        ;For making the R/W pin LOW for WRITE OPERN
        setb p0.2                       ;For making RS pin HIGH, selecting data reg.
        setb p0.0                       ;For making the Enable HIGH 
        nop
        nop
        nop
        clr p0.0                        ;For making the enable pin LOW 
        call delay
        ret
        
delay   mov 31h,#04h                    ;Delay of 2 ms
back2   mov 30h,#ffh
back1   djnz 30h,back1
        djnz 31h,back2
        ret
            

                 
reg_initial        mov b,#0ah
                   mov r0,#00h
                   mov r1,#00h
                   mov r2,#00h
                   mov r3,#01
                   mov r4,#00 
                   mov r5,#80h
                   mov r6,#01
                   mov r7,#00h
                   mov 53h,#00h
                   mov 62h,#20
                   mov 71h,#00
                   mov 73h,#00
                   mov 74h,#00
                   mov 7ah,#00          ;For SEC. Position in ALRAM
                   mov 7bh,#00          ;For HOUR Position in ALRAM

                   ret

phone_book  mov dptr,#phone_list0
            call disp_tble

incre_key  jb p1.2,chk_decky           ;Checking the incerment key
           call debounc
           jb p1.2,chk_decky
           jnb p1.2,$
           inc 71h
           mov a,71h
           cjne a,#06,chk1_decky
           mov 71h,#01
chk1_decky call phn_disp

chk_decky  jb p1.3,Esc_key             ;Checking the decerment key
           call debounc
           jb p1.3,phn_disp
           jnb p1.3,$
           dec 71h
           mov a,71h
           cjne a,#00h,chk2_disp
           mov 71h,#05
chk2_disp  call phn_disp


Esc_key    jb p1.7,incre_key
           call debounc
           jb p1.7,incre_key
           jnb p1.7,$
           call lcd_start
           call display
           ret

phn_disp   mov a,71h
           cjne a,#01,phno2
           mov dptr,#phone_list1
           call disp_tble              ;Display ph no.1

phno2      cjne a,#02,phno3
           mov dptr,#phone_list2
           call disp_tble              ;Display ph no.2

phno3      cjne a,#03,phno4
           mov dptr,#phone_list3
           call disp_tble              ;Display ph no.3

phno4      cjne a,#04,phno5
           mov dptr,#phone_list4
           call disp_tble              ;Display ph no.4

phno5      cjne a,#05,phno6
           mov dptr,#phone_list5
           call disp_tble              ;Display ph no.5

phno6      cjne a,#06,phno7  
           mov dptr,#phone_list6
           call disp_tble              ;Display ph no.6                     

phno7      cjne a,#07,phno8
           mov dptr,#phone_list7
           call disp_tble              ;Display ph no.7

phno8      cjne a,#08,Esc_key
           mov dptr,#phone_list8
           call disp_tble              ;Display ph no.8

           ret

phone_list0 DB "PhoneBook %Esc to Return$"
phone_list1 DB "SUMAN DAS % 1040129 $"
phone_list2 DB "MANISH DIXIT % 7453557 $"
phone_list3 DB "S.M.BADAVE % 631047 $"
phone_list4 DB "RUSHI.R.SHINDE % 481765 $"
phone_list5 DB "RAMAKRISHNA% 9364159 $"
phone_list6 DB "A.V.Subba Rao % 076404$"
phone_list7 DB "Office Number % 671302$"
phone_list8 DB "     Di       % 650120$"

                    
debounc mov 31h,#0ah                    ;Delay of 5 ms for key rebounce 
bak2    mov 30h,#fah
bak1    djnz 30h,bak1
        djnz 31h,bak2
        ret


Set_Alram       jb p1.1,lp28
                call debounc
                jb p1.1,lp28
                jnb p1.1,$
                inc 55h
                mov a,55h
                cjne a,#02,lp28
                mov 55h,#00h

lp28            call Alrm_disp
                setb p1.2
                jb p1.2,Alrm_dec
                call debounc
                jb p1.2,Alrm_dec
                jnb p1.2,$

                mov a,55h
                cjne a,#00,lp29
                inc 80h
                mov a,80h
                cjne a,#60,lp29
                mov 80h,#00

lp29            call Alrm_disp
                cjne a,#01,Alrm_dec
                inc 81h
                mov a,81h
                cjne a,#24,Alrm_dec
                mov 81h,#00

Alrm_dec        call Alrm_disp
                setb p1.3         
                jb p1.3,Alrm_esc
                call debounc
                jb p1.3,Alrm_esc
                jnb p1.3,$

                mov a,55h
                cjne a,#01,lp31
                dec 7ah
                mov a,7ah
                cjne a,#ffh,lp31
                mov 7ah,#59

lp31            call Alrm_disp
                cjne a,#02,Alrm_esc
                dec 7bh
                mov a,7bh
                cjne a,#ffh,Alrm_esc
                mov 7bh,#23

Alrm_esc        call Alrm_disp
                setb p1.7
                jb p1.7,Set_Alram
                call debounc
                jb p1.7,Set_Alram
                jnb p1.7,$

                ret

Alrm_disp       mov a,6ah                      ;For displaying the MINUTES
                call divisn
                mov 43h,#c5h
                mov 44h,#c6h
                call disp
        
                mov a,6bh                       ;For displaying the HOURS
                call divisn
                mov 43h,#c2h
                mov 44h,#c3h
                call disp
                ret
                        
Alrm_compare    mov a,r2
                cjne a,6ah,Alrm_ret
                mov a,r1
                cjne a,6bh,Alrm_ret
                setb p3.0                         ;ON the BUZZER
lp37            jb p1.7,lp37         
                call debounc
                jb p1.7,lp37 
                jnb p1.7,$
                clr p3.0
Alrm_ret        ret


intrupt1       mov tl1,#afh
                mov th1,#3ch
                djnz 64h,intr_ret
                mov 64h,#02
                call incre_StpWtch
intr_ret        reti

StopWatch       jb p1.2,StopWatch      ;For STARTING the STOP-WATCH
                call debounc
                jb p1.2,StopWatch     
                jnb p1.2,$

                mov tl1,#afh
                mov th0,#3ch
                setb tr1

lp39            call disp_StpWtch
                jb p1.0,lp38            ;For RESETTING the STOP-WATCH
                call debounc
                jb p1.0,lp38
                jnb p1.0,$  

lp38            jb p1.3,lp39            ;For STOPPING the STOP-WATCH
                call debounc
                jb p1.3,lp39
                jnb p1.3,$

                clr tr1
                call disp_StpWtch

lp40            jb p1.7,lp40            ;For EXITING from STOP-WATCH MODE
                call debounc
                jb p1.7,lp40          
                jnb p1.7,$

                ret


incre_StpWtch   inc 7ah
                mov a,7ah
                cjne a,#10,M4_ret
                mov 7ah,#00h

                inc 7bh
                mov a,7bh
                cjne a,#60,M4_ret
                mov 7bh,#00h

                inc 7ch
                mov a,7ch
                cjne a,#60,M4_ret
                mov 7ch,#00h

M4_ret          ret

disp_StpWtch    mov a,#01h
                call command

                mov a,7ah
                call divisn
                mov 43h,#c8h        
                mov 44h,#c9h
                call disp

                mov a,7bh                       ;For counting the MINUTES
                call divisn
                mov 43h,#c5h
                mov 44h,#c6h
                call disp
        
                mov a,7ch                        ;For counting the HOURS
                call divisn
                mov 43h,#c2h
                mov 44h,#c3h
                call disp
                
                mov a,#c4h
                call collon
        
                mov a,#c7h
                call collon 

                ret
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top