Code:
' 80 year countdown clock, thanks to Bill Legge for the MAX7221 code
@__CONFIG _CONFIG1H, _IESO_ON_1H & _FSCM_OFF_1H & _RC_OSC_1H
@__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _BORV_27_2L
@__CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32K_2H
@__CONFIG _CONFIG3H, _MCLRE_OFF_3H
@__CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_ON_4L & _STVR_ON_4L
@__CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
@__CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@__CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
@__CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
@__CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
@__CONFIG _CONFIG7H, _EBTRB_OFF_7H
include "modedefs.bas"
define OSC 4
PORTA = %00000000
PORTB = %00000000
TRISA = %00000000
TRISB = %00000000
OSCCON = %01100000 '4MHZ clock
ADCON0 = %00000000
ADCON1 = %11111111
INTCON = %11000000
T1CON = %00001101
PIE1.0 = 1
preset con 45543
CS_Max1 VAR PORTB.4 ' Chip select for MAX7221 IC1
CS_Max2 VAR PORTB.3 ' Chip select for MAX7221 IC2
CS_DS VAR PORTB.2 ' Chip select for DS1302 IC
clockpin VAR PORTB.0 ' Clock for SPI communication
datapin VAR PORTB.1 ' Serial Data Out for SPI communication
servo var PORTB.5
rtcyear VAR BYTE ' Allocate variables
rtcday VAR BYTE
rtcmonth VAR BYTE
rtcdate VAR BYTE
rtchr VAR BYTE
rtcmin VAR BYTE
rtcsec VAR BYTE
rtccontrol VAR BYTE
command var byte
counter var word
I var byte
temp var byte
temp2 var word
temp3 var long
temp4 var word
old_second var byte
servoCounter var word
flag1 var bit
flag2 var bit
clear
'GoSub settime
pause 100
init:
HIGH CS_Max1
HIGH CS_Max2
pause 100
low CS_Max2
shiftout datapin,clockpin,1,[1,%01001110] ' "S"
high CS_Max2
low CS_Max2
shiftout datapin,clockpin,1,[2,%01001111] ' "E"
high CS_Max2
low CS_Max2
shiftout datapin,clockpin,1,[3,%01011011] ' "C"
high CS_Max2
low CS_Max2
shiftout datapin,clockpin,1,[4,%00000000] ' blank digit
high CS_Max2
LOW CS_Max1
shiftout datapin,clockpin,1,[$09,%11111111] ' Decode register - all 8 digits
HIGH CS_Max1
LOW CS_Max2
shiftout datapin,clockpin,1,[$09,%11110000] ' Decode register - decode only 4 digits
HIGH CS_Max2
LOW CS_Max1
LOW CS_Max2
shiftout datapin,clockpin,1,[$0A, $0F] ' Intensity register - 32/32 brightness
HIGH CS_Max1
HIGH CS_Max2
low CS_Max1
low CS_Max2
shiftout datapin,clockpin,1,[$0B, $07] ' Scan limit register - display all digits
high CS_Max1
high CS_Max2 ' otherwise not equally bright
LOW CS_Max1
LOW CS_Max2
shiftout datapin,clockpin,1,[$0F, $00] ' test mode off
HIGH CS_Max1
HIGH CS_Max2
LOW CS_Max1
LOW CS_Max2
shiftout datapin,clockpin,1,[$0C, $01] ' normal operation
HIGH CS_Max1
HIGH CS_Max2
on interrupt goto myint
loop1
gosub calculate
pauseus 1000
goto loop1
calculate:
gosub gettime
if rtcsec <> old_second then
temp3 = 0
for counter = 1 to rtcmonth
lookup2 counter,[31,28,31,30,31,30,31,31,30,31,30,31],temp2
temp3 = temp3 + temp2
next
temp3 = ((60 - rtcyear) * 365) - temp3 'aantal dagen
temp3 = ((temp3 * 24)-rtchr) 'aantal uren
temp3 = (temp3 * 60) - rtcmin 'aantal minuten
temp3 = (temp3 * 60) - rtcsec 'aantal seconden
old_second = rtcsec
gosub display
endif
return
disable
gettime:
CS_DS = 1
ShiftOut datapin, clockpin, LSBFIRST, [$BF]
ShiftIn datapin, clockpin, LSBPRE, [rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, rtccontrol]
CS_DS = 0
temp = (rtcsec/16) * 10 + (rtcsec//16)
rtcsec = temp
temp = (rtcmin/16) * 10 + (rtcmin//16)
rtcmin = temp
temp = (rtchr/16) * 10 + (rtchr//16)
rtchr = temp
temp = (rtcyear/16) * 10 + (rtcyear//16)
rtcyear = temp
temp = (rtcday/16) * 10 + (rtcday//16)
rtcday = temp
temp = (rtcmonth/16) * 10 + (rtcmonth//16)
rtcmonth = temp
temp = (rtcdate/16) * 10 + (rtcdate//16)
rtcdate = temp
Return
display:
for I=0 to 3
low CS_Max2
shiftout datapin,clockpin,1,[I+5,temp3 dig I]
high CS_Max2
next I
for I=0 to 7 ' higher part of display
low CS_Max1
shiftout datapin,clockpin,1,[I+1,temp3 dig (I+2)]
high CS_Max1
next I
return
settime:
rtcyear = $11
rtcday = $01
rtcmonth = $4
rtcdate = $5
rtchr = $11
rtcmin = $07
rtcsec = 0
CS_DS = 1
ShiftOut datapin, clockpin, LSBFIRST, [$8E, 0]
CS_DS = 0
CS_DS = 1
ShiftOut datapin, clockpin, LSBFIRST, [$BE, rtcsec, rtcmin, rtchr, rtcdate, rtcmonth, rtcday, rtcyear, 0]
CS_DS = 0
Return
myint:
if PIR1.0 = 1 then
PIR1.0 = 0
servo = 1
pauseus 1000 + servoCounter
servo = 0
temp4 = preset
TMR1H = temp4.highbyte
TMR1L = temp4.lowbyte
flag1 = 0
endif
if flag2 = 0 then
if servoCounter >= 1000 then
flag2 = 1
goto myint
endif
servoCounter = servoCounter + 5
else
if servoCounter <= 0 then
flag2 = 0
goto myint
endif
servoCounter = servoCounter - 5
endif
resume
end
Bookmarks