Include 'M64DEF.INC'
ORG X0002
JMP EXT_INT0_ISR
ORG 0X000C
JMP EXT_INT5_ISR
ORG 0X000E
JMP JMP EXT_INT6_ISR
ORG 0X0010
JMP EXT_INT7_ISR
ORG 0X0014
JMP Timer2_ISR 
ORG 0X0020
JMP TIMER0_ISR

ORG 0X0050
main:
LDI R16, low(RAMEND) 
OUT SPL, R16 
LDI R16, high(RAMEND) 
OUT SPH, R16 
LDI R16, 0x07 ; Prescaler =1024    8.192MHZ/1024=8KHZ     1/8KHZ=125usec      20msec/125usec=160         256-160=96
OUT TCCR0, R16
LDI R16, 96
OUT TCNT0, R16
LDI R16, 0x05 ; Prescaler =1024  
OUT TCCR2, R16
LDI R16, 56 
OUT TCNT2, R16
LDI R16, 0x41 
OUT TIMSK, R16 
LDI R16, 0X00
OUT EICRA,R16
LDI R16,0X0A8
OUT EICRB,R16
LDI R16,0X0E1
OUT EIMSK
LDI R16,0X0FF
OUT DDRA,R16
OUT DDRB,R16
OUT DDRC,R16
OUT DDRD,R16
LDI R16,0X01
OUT PORTD,R16
CLR R17 ; SEC
CLR R18 ; MIN
CLR R19 ; HOUR
CLR R20 ; INT5
CLR R21 ; INT6
CLR R23 ; INT0
CLR R24
CLR R25 
CLR R26
SEI


loop:
OUT PORTA,R17
OUT PORTB,R18
OUT PORTC,R19
OUT PORTD,R26
JMP loop


TIMER0_ISR:
LDI R16, 96
OUT TCNT0, R16
CPI R20,1
BREQ MIN
CPI R21,1
BREQ HOUR
INC R24
CPI R24,50
BRNE NEXT
SEC:
CLR R24
INC R17
CPI R17,60
BRNE NEXT 
CLR R17
MIN:
CLR R20
INC R18
CPI R18,60
BRNE NEXT
CLR R18
HOUR:
CLR R21
INC R19
CPI R19,12
BRNE NEXT
CLR R19
NEXT: RETI


TIMER2_ISR:
LDI R16, 96
OUT TCNT2, R16
CPI R23,1
BRNE NEXT2
INC R25
CPI R25,250
BRNE NEXT2
LDI R26, 0X01
CLR R23
CLR R25
NEXT2:
RETI





EXT_INT5_ISR:
LDI R20,1
RETI


EXT_INT6_ISR
LDI R21,1
RETI


EXT_INT7_ISR:
LDI R26,0X00
RETI

EXT_INT0_ISR
LDI R23,1
RETI





