device = 18F43K22
clock = 10
config FOSC = HSMP // HS oscillator (medium power 4-16 MHz)
config PLLCFG = OFF // Oscillator used directly
config PRICLKEN = ON // Primary clock is always enabled
config FCMEN = OFF // Fail-Safe Clock Monitor disabled
config IESO = OFF // Oscillator Switchover mode disabled
config PWRTEN = ON // Power up timer enabled
config BOREN = ON // Brown-out Reset enabled and controlled by software (SBOREN is enabled)
config BORV = 285 // VBOR set to 2.85 V nominal
config WDTEN = OFF // Watch dog timer is always disabled. SWDTEN has no effect
config WDTPS = 128 // 1:128
config CCP2MX = PORTC1 // CCP2 input/output is multiplexed with RC1
config PBADEN = OFF // PORTB<5:0> pins are configured as digital I/O on Reset
config CCP3MX = PORTB5 // P3A/CCP3 input/output is multiplexed with RB5
config HFOFST = ON // HFINTOSC output and ready status are not delayed by the oscillator stable status
config T3CMX = PORTC0 // T3CKI is on RC0
config P2BMX = PORTD2 // P2B is on RD2
config MCLRE = EXTMCLR // MCLR pin enabled, RE3 input pin disabled
config STVREN = ON // Stack full/underflow will cause Reset
config LVP = ON // Single-Supply ICSP enabled if MCLRE is also 1
config XINST = OFF // Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
config DEBUG = OFF // Disabled
config CP0 = OFF // Block 0 (000200-000FFFh) not code-protected
config CP1 = OFF // Block 1 (001000-001FFFh) not code-protected
config CPB = OFF // Boot block (000000-0001FFh) not code-protected
config CPD = OFF // Data EEPROM not code-protected
config WRT0 = OFF // Block 0 (000200-000FFFh) not write-protected
config WRT1 = OFF // Block 1 (001000-001FFFh) not write-protected
config WRTC = OFF // Configuration registers (300000-3000FFh) not write-protected
config WRTB = OFF // Boot Block (000000-0001FFh) not write-protected
config WRTD = OFF // Data EEPROM not write-protected
config EBTR0 = OFF // Block 0 (000200-000FFFh) not protected from table reads executed in other blocks
config EBTR1 = OFF // Block 1 (001000-001FFFh) not protected from table reads executed in other blocks
config EBTRB = OFF // Boot Block (000000-0001FFh) not protected from table reads executed in other blocks
// for setalldigital
Include "setdigitalio.bas"
// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
Include "lcd.bas"
Include "convert.bas"
//testing for input
Dim sw1 As PORTC.4
Dim sw2 As PORTC.6
Dim led1 As PORTC.0
Dim led2 As PORTC.1
Dim Int_Counter As Word
Dim update As Boolean
Dim secs,mins,hrs As Byte
// TMR2 setup for 1ms ticks
const TICKS_PER_SEC = 1000
dim TMR2_IF as PIR1.1
dim TMR2_IE as PIE1.1
Interrupt RTC()
Dec(Int_Counter)
If Int_Counter = 0 Then
Int_Counter = TICKS_PER_SEC ' each interrupt = 1ms x 1000 int's = 1 second
update = true ' update LCD output
End If
TMR2_IF = 0 ' clear interrupt flag
End Interrupt
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
//routine for settime current time
///xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
If sw1 = 0 Then //advance hrs by 1
hrs = hrs + 1
End If
If sw2 = 0 Then //advance mins by 5
mins = mins + 5
End If
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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()
secs = 0
mins = 0
hrs = 0
Int_Counter = TICKS_PER_SEC
update = false
LCD.Command(130)
LCD.Write("24-HOUR CLOCK")
LCD.Command(196)
LCD.Write("00:00:00")
' Timer2 Registers:
TMR2 = 0
' Prescaler=1:1; TMR2 PostScaler=1:10; PR2=249 - Freq = 1.00kHz - Period = 1,000,000 ns (1ms)
PR2 = 249 ' PR2 (Timer2 Match value)(1 cycle adjusted)
'T2CON | = 72 ' bits 6-3 Post scaler 1:1 thru 1:16
'T2CON.TOUTPS3 = 1
'T2CON.TOUTPS2 = 0
'T2CON.TOUTPS1 = 0
'T2CON.TOUTPS0 = 1
'T2CON.TMR2ON = 1 ' Timer2 on bit: 1=Timer2 is on;
'T2CON.T2CKPS1 = 0 ' bits 1-0 Prescaler Rate Select bits
'T2CON.T2CKPS0 = 0
T2CON = %01001100
TMR2_IF = 0 ' clear TMR2 interrupt flag
TMR2_IE = 1 ' enable TMR2 interrupt
Enable(RTC) ' enable jump to RTC ISR
End Sub
// main code
setalldigital()
Input (sw1)
Output (led1)
Initialize()
While true
If update Then
Clock24() ' update 24H Clock output
End If
End while