PDA

View Full Version : clock runs reverse and fast:(



HYETİK
- 27th February 2009, 20:10
I want to use lcd clock code that I got from MELAB sample codes page, I manipulated it to use with my 7 segment clock project but I confront by some problems:(

Need help.bcoz clock is not work properly.
Osc:4mhz

I attached code and proteus sim. file


'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : HASAN YETİK TOBB-ETÜ ELEKTRİK-ELEKTRONİK MÜH. *
'* Notice : Copyright (c) 2009 HASAN YETİK *
'* : All Rights Reserved *
'* Date : 27.02.2009 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
' LCD clock program using On Interrupt
' Uses TMR0 and prescaler. Watchdog Timer should be
' set to off at program time and Nap and Sleep should not be used.
' Buttons may be used to set time

'*****GİRİŞ VE ÇIKIŞlAR*****
TRISA=0 'PORTA TAMAMI ÇIKIŞ
PORTA=0
TRISB=0 'PORTB TAMAMI ÇIKIŞ
PORTB=0
PORTC=0
'*****DEĞİŞKENLERİ TANIMLA*****
DEFINE OSC 4
A VAR BYTE
C VAR BYTE
SAY VAR BYTE
BEKLE VAR BYTE
BEKLE=2
hour var byte ' Define hour variable
dhour var byte ' Define display hour variable
minute var byte ' Define minute variable
second var byte ' Define second variable
ticks var byte ' Define pieces of seconds variable
update var byte ' Define variable to indicate update of LCD
i var byte ' Debounce loop variable

Pause 100 ' Wait for LCD to startup

hour = 0 ' Set initial time to 00:00:00
minute = 0
second = 0
ticks = 0

update = 1 ' Force first display

' Set TMR0 to interrupt every 16.384 milliseconds
OPTION_REG = $55 ' Set TMR0 configuration and enable PORTB pullups
INTCON = $a0 ' Enable TMR0 interrupts
On Interrupt Goto tickint

PORTD = 0 ' PORTB lines low to read buttons
TRISD = %11001111 ' Enable buttons


' Main program loop - in this case, it only updates the LCD with the time
mainloop:
' Check any button pressed to set time
If PORTD.7 = 0 Then decmin
If PORTD.5 = 0 Then incmin

' Check for time to update screen
chkup: If update = 1 Then
Lcdout $fe, 1 ' Clear screen

' Display time as hh:mm:ss
dhour = hour ' Change hour 0 to 12
If (hour // 12) = 0 Then
dhour = dhour + 12
Endif

' Check for AM or PM
If hour < 12 Then
GOSUB EKRANAYAZ
Else
GOSUB EKRANAYAZ
Endif

update = 0 ' Screen updated
Endif

Goto mainloop ' Do it all forever


' Increment minutes
incmin: minute = minute + 1
If minute >= 60 Then
minute = 0
hour = hour + 1
If hour >= 24 Then
hour = 0
Endif
Endif
Goto debounce

' Decrement minutes
decmin: minute = minute - 1
If minute >= 60 Then
minute = 59
hour = hour - 1
If hour >= 24 Then
hour = 23
Endif
Endif

' Debounce and delay for 250ms
debounce: For i = 1 to 25
Pause 10 ' 10ms at a time so no interrupts are lost
Next i

update = 1 ' Set to update screen

Goto chkup


' Interrupt routine to handle each timer tick
disable ' Disable interrupts during interrupt handler
tickint: ticks = ticks + 1 ' Count pieces of seconds
If ticks < 61 Then tiexit ' 61 ticks per second (16.384ms per tick)

' One second elasped - update time
ticks = 0
second = second + 1
If second >= 60 Then
second = 0
minute = minute + 1
If minute >= 60 Then
minute = 0
hour = hour + 1
If hour >= 24 Then
hour = 0
Endif
Endif
Endif

update = 1 ' Set to update LCD

tiexit: INTCON.2 = 0 ' Reset timer interrupt flag
Resume

EKRANAYAZ:
HIGH PORTB.7
PORTC=1
A=DHOUR DIG 1
GOSUB AL
LOW PORTC.0
POKE PORTB,A
PAUSE BEKLE
A=DHOUR DIG 0
GOSUB AL
HIGH PORTC.0
LOW PORTC.1
POKE PORTB,A
PAUSE BEKLE
''DAKİKA
A=MINUTE DIG 1
GOSUB AL
HIGH PORTC.1
LOW PORTC.2
POKE PORTB,A
PAUSE BEKLE
A=MINUTE DIG 0
GOSUB AL
HIGH PORTC.2
LOW PORTC.3
POKE PORTB,A
PAUSE BEKLE+1
HIGH PORTC.3
RETURN
AL:
LOOKUP A,[$3F,$6,$5B,$4F,$66,$6D,$7D,$7,$7F,$6F],A
Return
END

Dave
- 28th February 2009, 21:05
HYETİK, What do you mean it runs in reverse? As far as running fast, check your oscillator settings..

Dave Purola,
N8NTA

HYETİK
- 3rd March 2009, 09:49
OSC setting is ok (4mhz)

sorry for my poor English ;
I wanted to say clock count backward.. 24:00, 23:59, 23:58 ... and fast

Dave
- 3rd March 2009, 11:44
HYETİK, What are the values for the pullup resistors on these port pins?

If PORTD.7 = 0 Then decmin
If PORTD.5 = 0 Then incmin

Maybe its not counting time but decrementing the time in an endless loop?

Dave Purola,
N8NTA