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.

Digital clock running is to late

Status
Not open for further replies.

erwin4838

Member level 1
Member level 1
Joined
Mar 4, 2012
Messages
32
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Tangerang-indonesia
Activity points
1,478
Dear all
i have a problem with my project RUNNING HOUR +TIMER CONTROLL
the problem is digit second in digital running hour is to late,not in one second
rh.JPG
in this project i'm use software AvrOspII
this my source code
Code:
'----------------------------------
' Running Hour
'
' OUTPUT : LCD 2X16
'----------------------------------
$regfile = "m16def.dat"
$crystal = 16000000
Dim Jam As Word , Menit As Byte , Detik As Byte
Dim J_set As Word , M_set As Byte , D_set As Byte
Dim Menu As Byte , F_start As Byte , F_scan As Byte , F_led As Byte
Config Timer1 = Timer , Prescale = 256
Portd.0 = 1
Portd.1 = 1
Portd.2 = 1
Portd.3 = 1
Portd.4 = 1
Portd.5 = 0
Ddrd.0 = 0
Ddrd.1 = 0
Ddrd.2 = 0
Ddrd.3 = 0
Ddrd.4 = 0
Ddrd.5 = 1                                                  ' Led

On Timer1 Interupsi_timer1
Enable Ovf1

Config Lcdpin = Pin , Db4 = Portb.3 , Db5 = Portb.4 , Db6 = Portb.5 , Db7 = Portb.6 , E = Portb.2 , Rs = Portb.1
Config Lcd = 16 * 2
Cursor Off
Cls
Lcd "  Running Hour "
Lowerline
Lcd " Timer Controll "
Wait 2
Cls
Lcd "Hour :"
Lowerline
Lcd "Sett :"

Timer1 = 3035
Restart:
'------baca nilai awal timer---
Readeeprom Jam , 5
Readeeprom Menit , 7
Readeeprom Detik , 8
If Jam = &HFFFF Then Jam = 0
If Menit = &HFF Then Menit = 0
If Detik = &HFF Then Detik = 0
Locate 1 , 7
   If Jam < 10 Then
        Lcd "000" ; Jam ; ":"
   Elseif Jam < 100 Then
        Lcd "00" ; Jam ; ":"
   Elseif Jam < 1000 Then
        Lcd "0" ; Jam ; ":"
   End If
   If Menit < 10 Then
        Lcd "0" ; Menit ; ":"
   Else
        Lcd Menit ; ":"
   End If
   If Detik < 10 Then
        Lcd "0" ; Detik
   Else
        Lcd Detik
   End If
'-------------------------------
'------baca nilai awal set time---
Readeeprom J_set , 1
Readeeprom M_set , 3
Readeeprom D_set , 4
If J_set = &HFFFF Then J_set = 0
If M_set = &HFF Then M_set = 0
If D_set = &HFF Then D_set = 0
Locate 2 , 7
   If J_set < 10 Then
        Lcd "000" ; J_set ; ":"
   Elseif J_set < 100 Then
        Lcd "00" ; J_set ; ":"
   Elseif J_set < 1000 Then
        Lcd "0" ; J_set ; ":"
   End If
   If M_set < 10 Then
        Lcd "0" ; M_set ; ":"
   Else
        Lcd M_set ; ":"
   End If
   If D_set < 10 Then
        Lcd "0" ; D_set
   Else
        Lcd D_set
   End If
'-------------------------------
F_scan = 1
Gosub Scan_tombol
Do
   Locate 1 , 7
   If Jam < 10 Then
       Lcd "000" ; Jam ; ":"
   Elseif Jam < 100 Then
       Lcd "00" ; Jam ; ":"
   Elseif Jam < 1000 Then
       Lcd "0" ; Jam ; ":"
   End If
   '---------
   If Menit < 10 Then
       Lcd "0" ; Menit ; ":"
   Else
      Lcd Menit ; ":"
   End If
    '---------
   If Detik < 10 Then
       Lcd "0" ; Detik
   Else
      Lcd Detik
   End If
   Gosub Scan_tombol
   '----control
   If Jam >= J_set And Menit >= M_set And Detik >= D_set Then
      'Portd.5 = 1
      F_led = 1
   End If


Loop
End
'----------------------
Scan_tombol:
Do
'UP
   If Pind.0 = 0 Then
      If Menu = 1 And J_set < 9999 Then
         Incr J_set
         Locate 2 , 7
         If J_set < 10 Then
            Lcd "000" ; J_set ; ":"
         Elseif J_set < 100 Then
            Lcd "00" ; J_set ; ":"
         Elseif J_set < 1000 Then
            Lcd "0" ; J_set ; ":"
         End If

      Elseif Menu = 2 And M_set < 59 Then
         Incr M_set
         Locate 2 , 12
         If M_set < 10 Then
            Lcd "0" ; M_set ; ":"
         Else
            Lcd M_set ; ":"
         End If
      Elseif Menu = 3 And D_set < 59 Then
         Incr D_set
         Locate 2 , 15
         If D_set < 10 Then
            Lcd "0" ; D_set
         Else
            Lcd D_set
         End If
      End If
      Waitms 250
'DOWN
   Elseif Pind.1 = 0 Then
      If Menu = 1 And J_set > 0 Then
         Decr J_set
         Locate 2 , 7
         If J_set < 10 Then
            Lcd "000" ; J_set ; ":"
         Elseif J_set < 100 Then
            Lcd "00" ; J_set ; ":"
         Elseif J_set < 1000 Then
            Lcd "0" ; J_set ; ":"
         End If

      Elseif Menu = 2 And M_set > 0 Then
         Decr M_set
         Locate 2 , 12
         If M_set < 10 Then
            Lcd "0" ; M_set ; ":"
         Else
            Lcd M_set ; ":"
         End If
      Elseif Menu = 3 And D_set > 0 Then
         Decr D_set
         Locate 2 , 15
         If D_set < 10 Then
            Lcd "0" ; D_set
         Else
            Lcd D_set
         End If
       End If
      Waitms 250
'OK
   Elseif Pind.2 = 0 Then
      If Menu = 0 Then
         Menu = 1

      Elseif Menu = 1 Then
         Menu = 2
         Writeeeprom J_set , 1
      Elseif Menu = 2 Then
         Menu = 3
         Writeeeprom M_set , 3
      Elseif Menu = 3 Then
         Menu = 0
         Writeeeprom D_set , 4
      End If
      Waitms 250

'START
   Elseif Pind.4 = 0 And F_start = 0 Then
      Start Timer1
      Enable Interrupts
      F_start = 1
      F_scan = 0
      Waitms 250
'STOP/PAUSE
   Elseif Pind.4 = 1 And F_start = 1 Then
      Stop Timer1
      Disable Interrupts
      F_start = 0
      F_scan = 0
      Waitms 250
'RESET
   Elseif Pind.3 = 0 Then
      Menu = 0
      Jam = 0
      Menit = 0
      Detik = 0
      J_set = 0
      M_set = 0
      D_set = 0
      F_led = 0
      Writeeeprom J_set , 1
      Writeeeprom M_set , 3
      Writeeeprom D_set , 4
      Writeeeprom Jam , 5
      Writeeeprom Menit , 7
      Writeeeprom Detik , 8
      Waitms 250
      Goto Restart
   End If
Loop Until F_scan = 0
Return

Interupsi_timer1:
   Stop Timer1
   Detik = Detik + 1
   Writeeeprom Detik , 8
   If Detik >= 60 Then
      Menit = Menit + 1
      Writeeeprom Menit , 7
      If Menit >= 60 Then
         Jam = Jam + 1
         Writeeeprom Jam , 5
         If Jam >= 9999 Then
            Jam = 0
            Menit = 0
            Detik = 0
            Writeeeprom Jam , 5
            Writeeeprom Menit , 7
            Writeeeprom Detik , 8
         End If
         Menit = 0
      End If
      Detik = 0
   End If
   Timer1 = 3035
   Start Timer1
   If F_led = 1 Then Portd.5 = Not Portd.5
Return

Please help me,thanks for u attention
 

You use stop timer, then many line instructions, then start timer. its add more time cycles. Don't stop the timer, then reset timer1 value immediately at first line of interrupt service.


Code Basic4GL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Interupsi_timer1:
   Timer1 = 3035
   Detik = Detik + 1
   Writeeeprom Detik , 8
   If Detik >= 60 Then
      Menit = Menit + 1
      Writeeeprom Menit , 7
      If Menit >= 60 Then
         Jam = Jam + 1
         Writeeeprom Jam , 5
         If Jam >= 9999 Then
            Jam = 0
            Menit = 0
            Detik = 0
            Writeeeprom Jam , 5
            Writeeeprom Menit , 7
            Writeeeprom Detik , 8
         End If
         Menit = 0
      End If
      Detik = 0
   End If
   If F_led = 1 Then Portd.5 = Not Portd.5
Return



- - - Updated - - -

do you really want to make real application or it just for your college homework? because this way is very bad. store clock value to EEPROM. in one hour you have wrote 3600 times data to EEPROM. no longer, you EEPROM is weared out, and can't save any data. try to use RTC.
 

Hello!


Beside the problem pointed by Arch Zone, it depends on how you use the timers.

I don't want to read your code, but a classic beginner problem is this:

For example, if you do like this (pseudo-code) :

fire_timer_once();

on_interrupt() {
do_your_rtc_processing();
fire_timer_once();
}

Then of course the clock will run late, because even if your timer is set to 1 second,
your processing needs time to be performed.
The solution is to use a repetitive timer setup.

fire_ticking_timer(); // This sets your timer to repeated interrupts, 1 second.

on_timer_tick() {
do_your_rtc_processing();
}

Doing this, as long as your RTC processing takes less than 1 second, your clock
will run right and its accuracy should be the accuracy of your crystal.

Dora.
 

this aplication is college homework,i still not yet understand because i have just learnt programming,ok i'll try first and modify ,thaks a lot for master ArchZone and master doraemon
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top