ساعة رقمية باستخدام المقاطعة بالمؤقت TMR0 ولغة Basic والمترجم Proton
الدائرة الكهربية :
البرنامج :
كود:
;Interrupt-TMR0-Clock-Project
Device = 16F628A
Xtal = 4
All_Digital = true
Declare LCD_DTPin PORTB.4
Declare LCD_RSPin PORTB.2
Declare LCD_ENPin PORTB.3
Dim hour As Byte ;Define Variable hour
Dim minute As Byte ;Define Variable minute
Dim second As Byte ;Define Variable second
Dim counting As Byte ;Define Variable counter
Dim actual As Byte ;Define Variable LCD update
Dim x As Byte ;Define Variable x loop counting
hour = 12 ; Set time 12:00:00
minute = 0
second = 0
counting= 0
actual= 1
;Set TMR0 interrupt every 32768 microseconds
OPTION_REG = %1010110 ;setting and enable TMR0 Sets PORTB pullups
INTCON = %10100000 ;Enable TMR0 internal interrupt
On Interrupt GoTo interrup
start:
If PORTB.0 = 0 Then incmin ;button to equalize minutes
If PORTB.1 = 0 Then inchr ;button to equalize hours
updated: If actual = 1 Then ;check whether to update LCD
Cls
Print At 1,1,"ENG.FATHALLA"
Print At 2,4,Dec2 hour,":",Dec2 minute,":",Dec2 second
actual = 0 ;updated screen
EndIf
GoTo start
; *********************** match the hour *****************
incmin: minute = minute + 1
If minute >= 60 Then minute=0
GoTo delay
inchr: hour = hour + 1
If hour >= 24 Then hour=0
GoTo delay
delay: For x = 1 To 20 ; Delay 200 ms
DelayMS 10 ;steps of 10 ms to keep interruptions
Next x
actual = 1 ;indicates LCD refresh
GoTo updated
;************** Interrupt Handler to increase counter ***************
Disable ;disables interrupts during the process
interrup:
counting = counting + 1 ;counting interruptions TMR0
TMR0=4 ;subtracting 4 from counter 256 TMR0
If counting < 31 Then reseting ;31 counts (32256ms = 999936uS)
counting = 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
actual = 1 ;LCD update
reseting:
INTCON.2 = 0 ;resets the TMR0 interrupt flag
Resume
End