Device = 18F4321
Clock = 8
// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
Include "lcd.bas"
Include "convert.bas"
{
For One Second Update:
8MHz Fosc = 2MHz internal clock = 0.5us per cycle (timer count)
Use 8-bit Timer2, No Prescaler
Set PR2 = 250; Timer2 resets on match every 250 counts = 125us
Each Timer2 reset requires 1 cycle compensation... so set PR2 = 249
8000 interrupts x 125us each = 1 second
}
Dim Int_Counter As Word
Dim update As Boolean
Dim secs,mins,hrs As Byte
dim Hours as byte
//testing for input
Dim sw1 As PORTC.4
Dim led1 As PORTC.0
Interrupt RTC()
Dec(Int_Counter)
If Int_Counter = 0 Then
Int_Counter = 8000 ' each interrupt = 125us x 8000 int's = 1 second
update = true ' update LCD output
End If
PIR1.1 = 0 ' clear interrupt flag
End Interrupt
sub set-clock()
If sw1 = 0 Then // switch for setting time
hrs = hours + 5
led1 = 1
DelayMS(2000)
Else
hrs = 0
End If
end sub
Sub Clock24()
Dim clk As String
Inc(secs)
If secs = 60 Then ' check each tally for rollover
secs = 0
Inc(mins)
If mins = 60 Then
mins = 0
Inc(hrs)
If hrs = 24 Then
hrs = 0
End If
End If
End If
clk = DecToStr(hrs,2) ' output to LCD
LCD.WriteAt(2,5,clk)
clk = DecToStr(mins,2)
LCD.WriteAt(2,8,clk)
clk = DecToStr(secs,2)
LCD.WriteAt(2,11,clk)
update = false
End Sub
Sub Initialize()
ADCON1 = 15
secs = 0
mins = 0
hours = 0
If sw1 = 0 Then // switch for setting time
hrs = hours + 5
led1 = 1
DelayMS(2000)
//Else
hrs = 0
End If
hrs = hours
Int_Counter = 8000
update = false
LCD.Command(130)
LCD.Write("24-HOUR CLOCK")
LCD.Command(196)
LCD.Write("00:00:00")
INTCON = 192 ' enable GIE & PEIE
T2CON = 0 ' no prescaler or postscaler: timer2 OFF
TMR2 = 0 ' clear TMR2
PR2 = 249 ' set match value
PIE1.1 = 1 ' enable interrupt
PIR1.1 = 0 ' clear interrupt flag
T2CON.2 = 1 ' Timer2 ON
Enable(RTC)
Input (sw1) ' enable jump to RTC ISR
End Sub
Input (sw1)
Initialize
While 1=1
set-clock()
If update = true Then
Clock24() ' update 24H Clock output
End If
Wend