symbol DigitBit0 = 0
symbol DigitBit1 = 1
symbol DigitBit2 = 2
symbol DigitBit3 = 5
symbol DigitBit4 = 6
symbol DigitBit5 = 7
symbol RE1 = 0
symbol RE2 = 1
symbol SSW = 2
symbol VATTERY = 4
symbol VatLow = 1
symbol VatNormal = 0
symbol VL = 7
symbol Century = 7
symbol HIGH = 1
symbol LOW = 0
symbol YMD = 1
symbol HMS = 0
const dptnB = %00000000 ' Blank
const dptn0 = %11101011 ' Number 0
const dptn1 = %10001000 ' Number 1
const dptn2 = %00111011 ' Number 2
const dptn3 = %10111010 ' Number 3
const dptn4 = %11011000 ' Number 4
const dptn5 = %11110010 ' Number 5
const dptn6 = %11110011 ' Number 6
const dptn7 = %11101000 ' Number 7
const dptn8 = %11111011 ' Number 8
const dptn9 = %11111010 ' Number 9
dim TargetDigit as byte
dim DispS as byte
dim DispM as byte
dim DispH as byte
dim DispS1 as byte
dim DispS10 as byte
dim DispM1 as byte
dim DispM10 as byte
dim DispH1 as byte
dim DispH10 as byte
dim Control1 as byte
dim Control2 as byte
dim Seconds as byte
dim Minutes as byte
dim Hours as byte
dim Days as byte
dim Weekdays as byte
dim Months as byte
dim Years as byte
dim VLbit as byte
dim RE1W as byte
dim DispCont as byte
sub procedure wait1ms
Delay_ms(1)
end sub
sub procedure ReadRTC
I2C_Start 'Send I2C Start Condition
I2C_Wr(%10100010) 'Send RTC slave address with write mode
I2C_Wr(%00000000) 'Send RTC read address
I2C_Repeated_Start 'Send I2C Restart Condition
I2C_Wr(%10100011) 'Send RTC slave address with read mode
Control1 = I2C_Rd(1) 'Read RTC
Control2 = I2C_Rd(1)
Seconds = I2C_Rd(1)
Minutes = I2C_Rd(1)
Hours = I2C_Rd(1)
Days = I2C_Rd(1)
Weekdays = I2C_Rd(1)
Months = I2C_Rd(1)
Years = I2C_Rd(0)
I2C_Stop
Control2.6 = 0
if Seconds.VL = 1 then
VLbit = 1
else
VLbit = 0
end if
Seconds.VL = 0
Minutes.7 = 0
Hours.6 = 0
Hours.7 = 0
Days.6 = 0
Days.7 = 0
Months.5 = 0
Months.6 = 0
Months.century = 0
Seconds = Bcd2Dec(Seconds)
Minutes = Bcd2Dec(Minutes)
Hours = Bcd2Dec(Hours)
Days = Bcd2Dec(Days)
Months = Bcd2Dec(Months)
Years = Bcd2Dec(Years)
while PORTA.VATTERY = VatLow
wait1ms
wend
end sub
sub procedure StartRTC
I2C_Start 'Send I2C Start Condition
I2C_Wr(%10100010) 'Send RTC slave address with write mode
I2C_Wr(%00000000) 'Send RTC write address
I2C_Wr(%00000000) 'Write RTC (Control1)
I2C_Stop
while PORTA.VATTERY = VatLow
wait1ms
wend
end sub
sub function led7seg_decode( dim DispD as byte ) as byte
select case DispD
case 0 result = dptn0
case 1 result = dptn1
case 2 result = dptn2
case 3 result = dptn3
case 4 result = dptn4
case 5 result = dptn5
case 6 result = dptn6
case 7 result = dptn7
case 8 result = dptn8
case 9 result = dptn9
case 10 result = dptnB
case else result = dptnB
end select
end sub
sub procedure DISP7SEG
DispH10 = DispH div 10
DispH1 = DispH - (DispH10*10)
DispM10 = DispM div 10
DispM1 = DispM - (DispM10*10)
DispS10 = DispS div 10
DispS1 = DispS - (DispS10*10)
select case TargetDigit
case 0
PORTC.DigitBit5 = 0
PORTC.DigitBit0 = 1
PORTB = led7seg_decode(DispS1)
TargetDigit = 1
case 1
PORTC.DigitBit0 = 0
PORTC.DigitBit1 = 1
PORTB = led7seg_decode(DispS10)
TargetDigit = 2
case 2
PORTC.DigitBit1 = 0
PORTC.DigitBit2 = 1
PORTB = led7seg_decode(DispM1)
TargetDigit = 3
case 3
PORTC.DigitBit2 = 0
PORTC.DigitBit3 = 1
PORTB = led7seg_decode(DispM10)
TargetDigit = 4
case 4
PORTC.DigitBit3 = 0
PORTC.DigitBit4 = 1
PORTB = led7seg_decode(DispH1)
TargetDigit = 5
case 5
PORTC.DigitBit4 = 0
PORTC.DigitBit5 = 1
PORTB = led7seg_decode(DispH10)
TargetDigit = 0
end select
wait1ms
end sub
if PORTA.SSW = LOW then
do
wait1ms
loop until PORTA.SSW = LOW
wait1ms
goto InitDateTime
end if
DispTime:
ReadRTC
if VLbit = 1 then
goto InitDateTime
else
if PORTA.SSW = LOW then
goto SetDate
end if
if (PORTA.RE1 = LOW) and (RE1W = HIGH) then
if PORTA.RE2 = HIGH then
DispCont = YMD
else
DispCont = HMS
end if
end if
if DispCont = HMS then
DispH = Hours
DispM = Minutes
DispS = Seconds
else
DispH = Years
DispM = months
DispS = Days
end if
RE1W = PORTA.RE1
DISP7SEG
end if
goto DispTime
SetDate:
DISP7SEG
if PORTA.SSW = LOW then
goto SetDate
end if
DISP7SEG
DispH = Years
DispM = Months
DispS = Days
SetDate010:
do 'Wait RE1 HIGH
while PORTA.RE1 = LOW
if PORTA.SSW = LOW then
goto SetDate020
end if
DISP7SEG
wend
Disp7SEG
loop until PORTA.RE1 = HIGH
do 'Wait RE1 LOW
while PORTA.RE1 = HIGH
if PORTA.SSW = LOW then
goto SetDate020
end if
DISP7SEG
wend
DISP7SEG
loop until PORTA.RE1 = LOW
select case PORTA.RE2
case HIGH 'Inc. Date
select case DispM
case 01
if DispS = 31 then
DispS = 01
DispM = 02
else
DispS = DispS + 1
end if
case 02
if (DispH mod 4) = 0 then
if DispS = 29 then
DispS = 01
DispM = 03
else
DispS = DispS + 1
end if
else
if DispS = 28 then
DispS = 01
DispM = 03
else
DispS = DispS + 1
end if
end if
case 03
if DispS = 31 then
DispS = 01
DispM = 04
else
DispS = DispS + 1
end if
case 04
if DispS = 30 then
DispS = 01
DispM = 05
else
DispS = DispS + 1
end if
case 05
if DispS = 31 then
DispS = 01
DispM = 06
else
DispS = DispS + 1
end if
case 06
if DispS = 30 then
DispS = 01
DispM = 07
else
DispS = DispS + 1
end if
case 07
if DispS = 31 then
DispS = 01
DispM = 08
else
DispS = DispS + 1
end if
case 08
if DispS = 31 then
DispS = 01
DispM = 09
else
DispS = DispS + 1
end if
case 09
if DispS = 30 then
DispS = 01
DispM = 10
else
DispS = DispS + 1
end if
case 10
if DispS = 31 then
DispS = 01
DispM = 11
else
DispS = DispS + 1
end if
case 11
if DispS = 30 then
DispS = 01
DispM = 12
else
DispS = DispS + 1
end if
case 12
if DispS = 31 then
DispS = 01
DispM = 01
if DispH = 99 then
DispH = 00
else
DispH = DispH + 1
end if
else
DispS = DispS + 1
end if
end select
case LOW 'Dec. Date
if DispS = 01 then
select case DispM
case 01 DispS = 31 '12
case 02 DispS = 31 ' 1
case 03
if (DispH mod 4) = 0 then
DispS = 29 ' 2
else
DispS = 28 ' 2
end if
case 04 DispS = 31 ' 3
case 05 DispS = 30 ' 4
case 06 DispS = 31 ' 5
case 07 DispS = 30 ' 6
case 08 DispS = 31 ' 7
case 09 DispS = 31 ' 8
case 10 DispS = 30 ' 9
case 11 DispS = 31 '10
case 12 DispS = 30 '11
end select
if DispM = 1 then
DispM = 12
if DispH = 0 then
DispH = 99
else
DispH = DispH - 1
end if
else
DispM = DispM - 1
end if
else
DispS = DispS - 1
end if
end select
goto SetDate010
SetDate020:
DISP7SEG
if PORTA.SSW = LOW then
goto SetDate020
end if
Years = DispH
Months = DispM
Days = DispS
SetTime:
DISP7SEG
DispH = Hours
DispM = Minutes
DispS = 00
SetTime010:
do 'Wait RE1 HIGH
while PORTA.RE1 = LOW
if PORTA.SSW = LOW then
goto SetTime020
end if
DISP7SEG
wend
DISP7SEG
loop until PORTA.RE1 = HIGH
do 'Wait RE1 LOW
while PORTA.RE1 = HIGH
if PORTA.SSW = LOW then
goto SetTime020
end if
DISP7SEG
wend
DISP7SEG
loop until PORTA.RE1 = LOW
select case PORTA.RE2
case HIGH
DispM = DispM + 1
if DispM = 60 then
DispM = 0
DispH = DispH + 1
if DispH = 24 then
DispH = 0
end if
end if
case LOW
if DispM = 0 then
DispM = 59
if DispH = 0 then
DispH = 23
else
DispH = DispH - 1
end if
else
DispM = DispM - 1
end if
end select
goto SetTime010
SetTime020:
DISP7SEG
if PORTA.SSW = LOW then
goto SetTime020
end if
Hours = DispH
Minutes = DispM
Seconds = DispS