'RELOJ CALENDARIO CON PIC 16F84a ,LCD Y DS 1307
'CONFIGURACION OSCILADOR
@ device xt_osc ; oscilador externo XT
define osc 4 ; especifica que se va a utilizar uno de 4 Mhz
'CONFIFURACION LCD
Define LCD_DREG PORTA
Define LCD_DBIT 0
Define LCD_RSREG PORTA
Define LCD_RSBIT 6
Define LCD_EREG PORTA
Define LCD_EBIT 7
Define LCB_BITS 4
Define LCD_LINES 2
Define LCD_COMMANDUS 2000
Define LCD_DATAUS 50
'CONFIGURACION DS1307
SQWpin var PORTB.0
SCLpin var PORTB.1
SDApin var PORTB.2
'CONFIGURACION DE PINES
DecButton var PORTB.4 ' Presionar para decrementar
SetButton var PORTB.5 ' Presionar para memorizar
IncButton var PORTB.6 ' Presionar para incrementar
'CPNFIGURACION MESES,DIAS
Data @0,74,97,110,70,101,98,77,97,114,65,112,114
' Jan Feb Mar Apr
Data 77,97,121,74,117,110,74,117,108,65,117,103
' May Jun Jul Aug
Data 83,101,112,79,99,116,78,111,118,68,101,99
' Sep Oct Nov Dec
Data 84,117,101,87,101,100,84,104,117,70,114,105
' Tue Wed Thu Fri
Data 83,97,116,83,117,110,77,111,110
' Sat Sun Mon
'CREACION DE VARIABLES
CounterA var byte
CounterB var byte
CounterC var byte
CounterD var byte
RTCSec var byte
RTCMin var byte
RTCHour var byte
RTCWDay var byte
RTCDay var byte
RTCMonth var byte
RTCYear var byte
RTCCtrl var byte
SetTime var byte
SetSec var byte
SetMin var byte
SetHour var byte
SetDay var byte
SetMonth var byte
SetYear var byte
TimeOut var word
'CONTANTES DEL PROGRAMA
ButtonRepeat con 200
'inicio del programa
goto JumpStart
ConvertBCD:
CounterB=CounterA DIG 1
CounterB=CounterB<<4
CounterB=CounterB+CounterA DIG 0
Return
DisplayMonth:
CounterB=CounterB*3-3
DisplaySequence:
For CounterA=CounterB to CounterB+2
Read CounterA,CounterD
LCDOut CounterD
Next CounterA
Return
'SUBRUTINA PARA NUMERO DE DIAS Y MESES
FindDays:
LookUp SetMonth-1,[31,28,31,30,31,30,31,31,30,31,30,31],CounterA
If SetMonth=2 then
If (SetYear&$03)=0 then CounterA=29
endif
Return
'SUBRUTINA RETARDO DE BOTONES
SetButtonRelease:
LCDOut $FE,1
While SetButton=0:Wend
Pause 250
Return
JumpStart:
CMCON=%00000111
TRISA=%00000000
TRISB=%11111111
OPTION_REG.7=0
Pause 200
ReDisplay:
LCDOut $FE,1
ReDisplayLoop:
'RUTINA PARA LEER INTEGRADO DS1307
I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]
If RTCSec.7=1 then goto SetUpPreset
If SetButton=0 then
Gosub SetButtonRelease
goto Setup
endif
'RUTINA PARA MOSTRAR VALOR EN LCD
LCDOut $FE,$80
If RTCHour.6=1 then
CounterA=(RTCHour>>4)&$01
else
CounterA=(RTCHour>>4)&$03
endif
CounterA=CounterA*10+(RTCHour&$0F)
If RTCHour.6=1 then
LCDOut #CounterA
else
LCDOut #CounterA Dig 1,#CounterA Dig 0
endif
LCDOut ":",#(RTCMin>>4)&$0F,#RTCMin&$0F,":"
LCDOut #(RTCSec>>4)&$0F,#RTCSec&$0F," "
IF RTCHour.6=1 then
If RTCHour.5=1 then
LCDOut "PM"
else
LCDOut "AM"
endif
endif
LCDOut " ",$FE,$C0
CounterB=RTCWDay*3+33
Gosub DisplaySequence
LCDOut " ",#(RTCDay>>4)&$0F,#RTCDay&$0F," "
CounterB=((RTCMonth>>4)&$0F)*10+(RTCMonth&$0F)
Gosub DisplayMonth
LCDOut " 20",#(RTCYear>>4)&$0F,#RTCYear&$0F,$FE,$80
Pause 250 ' Repeat about 4 times/sec
Goto ReDisplayLoop
SetupPreset:
RTCSec=$00 '
RTCMin=$15
RTCHour=$13
RTCWDay=$01
RTCDay=$12
RTCMonth=$06
RTCYear=$02
RTCCtrl=$10
Setup:
SetTime=RTCHour.6
If SetTime=1 then
SetHour=(RTCHour>>4)&$01
else
SetHour=(RTCHour>>4)&$03
endif
SetHour=SetHour*10+(RTCHour&$0F)
If SetTime=1 then
If RTCHour.5=1 then
If SetHour<12 then SetHour=SetHour+12
else
If SetHour=12 then SetHour=0
endif
endif
SetMin=((RTCMin>>4)&$0F)*10+(RTCMin&$0F)
SetSec=((RTCSec>>4)&$0F)*10+(RTCSec&$0F)
SetYear=((RTCYear>>4)&$0F)*10+(RTCYear&$0F)
SetMonth=((RTCMonth>>4)&$0F)*10+(RTCMonth&$0F)
SetDay=((RTCDay>>4)&$0F)*10+(RTCDay&$0F)
CounterC=0
TimeOut=0
SetupLoop:
LCDOut $FE,1,"Set "
If CounterC=0 then
LCDOut "Mode"
endif
If CounterC=1 then
LCDOut "Hours"
endif
If CounterC=2 then
LCDOut "Minutes"
endif
If CounterC=3 then
LCDOut "Seconds"
endif
If CounterC=4 then
LCDOut "Year : 20"
endif
If CounterC=5 then
LCDOut "Month"
endif
If CounterC=6 then
LCDOut "Day"
endif
If CounterC<>4 then LCDOut " :"
SetupDisplayLoop:
If CounterC=0 then
LCDOut $FE,$8B
If SetTime=0 then
LCDOut "24HR"
else
LCDOut "12HR"
endif
LCDOut $FE,$8B
endif
If CounterC=1 then
CounterA=SetHour
If SetTime=1 then
LCDOut $FE,$8E
If CounterA<12 then
LCDOut "AM"
else
LCDOut "PM"
endif
If CounterA=0 then CounterA=12
If CounterA>12 then CounterA=CounterA-12
endif
LCDOut $FE,$8C,#CounterA
If CounterA<10 then LCDOut " "
LCDOut $FE,$8C
endif
If CounterC=2 then
LCDOut $FE,$8E,#SetMin
If SetMin<10 then LCDOut " "
LCDOut $FE,$8E
endif
If CounterC=3 then
LCDOut $FE,$8E,#SetSec
If SetSec<10 then LCDOut " "
LCDOut $FE,$8E
endif
If CounterC=4 then
LCDOut $FE,$8D,#SetYear DIG 1,#SetYear Dig 0
LCDOut $FE,$8D
endif
If CounterC=5 then
LCDOut $FE,$8C
CounterB=SetMonth
Gosub DisplayMonth
LCDOut $FE,$8C
endif
If CounterC=6 then
LCDOut $FE,$8A,#SetDay
If SetDay<10 then LCDOut " "
LCDOut $FE,$8A
endif
SetupEntryLoop:
If DecButton=0 then
If CounterC=0 then
If SetTime=0 then
SetTime=1
else
SetTime=0
endif
endif
If CounterC=1 then
If SetHour=0 then
SetHour=23
else
SetHour=SetHour-1
endif
endif
If CounterC=2 then
If SetMin=0 then
SetMin=59
else
SetMin=SetMin-1
endif
endif
If CounterC=3 then
If SetSec=0 then
SetSec=59
else
SetSec=SetSec-1
endif
endif
If CounterC=4 then
If SetYear=0 then
SetYear=99
else
SetYear=SetYear-1
endif
endif
If CounterC=5 then
If SetMonth=1 then
SetMonth=12
else
SetMonth=SetMonth-1
endif
Gosub FindDays
If SetDay>CounterA then SetDay=CounterA
endif
If CounterC=6 then
Gosub FindDays
If SetDay=1 then
SetDay=CounterA
else
SetDay=SetDay-1
endif
endif
Pause ButtonRepeat
TimeOut=0
Goto SetUpDisplayLoop
endif
If IncButton=0 then
If CounterC=0 then
If SetTime=1 then
SetTime=0
else
SetTime=1
endif
endif
If CounterC=1 then
If SetHour=23 then
SetHour=0
else
SetHour=SetHour+1
endif
endif
If CounterC=2 then
If SetMin=59 then
SetMin=0
else
SetMin=SetMin+1
endif
endif
If CounterC=3 then
If SetSec=59 then
SetSec=0
else
SetSec=SetSec+1
endif
endif
If CounterC=4 then
If SetYear=99 then
SetYear=0
else
SetYear=SetYear+1
endif
endif
If CounterC=5 then
If SetMonth=12 then
SetMonth=1
else
SetMonth=SetMonth+1
endif
Gosub FindDays
If SetDay>CounterA then SetDay=CounterA
endif
If CounterC=6 then
Gosub FindDays
If SetDay=>CounterA then
SetDay=1
else
SetDay=SetDay+1
endif
endif
Pause ButtonRepeat
TimeOut=0
Goto SetupDisplayLoop
endif
If SetButton=0 then
CounterC=CounterC+1
TimeOut=0
If CounterC>6 then
LCDOut $FE,1,"Memorizar"
CounterA=SetHour
If SetTime=1 then
If CounterA>12 then CounterA=CounterA-12
If CounterA=0 then CounterA=12
endif
Gosub ConvertBCD
RTCHour=CounterB
If SetTime=1 then
RTCHour.6=1
If SetHour=>12 then RTCHour.5=1
endif
CounterA=SetMin
Gosub ConvertBCD
RTCMin=CounterB
'
CounterA=SetSec
Gosub ConvertBCD
RTCSec=CounterB
CounterA=SetYear
Gosub ConvertBCD
RTCYear=CounterB
CounterA=SetMonth
Gosub ConvertBCD
RTCMonth=CounterB
CounterA=SetDay
Gosub ConvertBCD
RTCDay=CounterB
CounterA=SetYear+4
CounterB=SetMonth
If SetMonth<3 then
CounterA=CounterA-1
CounterB=CounterB+12
endif
CounterD=(SetDay+(153*CounterB-457)/5+365*CounterA+CounterA/4-CounterA/100+CounterA/400+2) MOD 7
RTCWDay=CounterD+1
I2CWrite SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]
Pause 1000
Gosub SetButtonRelease
Goto ReDisplay
endif
Gosub SetButtonRelease
Goto SetupLoop
endif
Pause 1
TimeOut=TimeOut+1
If TimeOut>20000 then goto ReDisplay
Goto SetupEntryLoop
End