Tuesday, May 31, 2016

8051 Program - day of week calculation

;Day Of Week calculation
;(P)2008 by Jan Waclawek
;tested with MetaLink assembler
;first, a test program going through all dates; the utility itself is below
day equ 30h
month equ 31h
year equ 32h
dow equ 33h
TestDow:
mov dow,#2
mov year,#0
mov month,#3
sjmp TestDowX2
TestDowX1:
mov month,#1
TestDowX2:
mov day,#1
TestDowX3:
mov r5,day
mov r6,month
mov r7,year
call DayOfWeek
cjne a,dow,Fail
inc dow
mov a,dow
cjne a,#7,TestDowX4
mov dow,#0
TestDowX4:
inc day
mov a,month
add a,#TestDowTab-TestDowX5-1
movc a,@a+pc
TestDowX5:
cjne a,day,TestDowX6
sjmp TestDowX3
TestDowX6:
jnc TestDowX3
mov a,month
cjne a,#2,TestDowX7
mov a,day
cjne a,#29,TestDowX7
mov a,year
anl a,#3
jz TestDowX3
TestDowX7:
inc month
mov a,month
cjne a,#12+1,TestDowX2
inc year
mov a,year
cjne a,#100,TestDowX1
sjmp Pass
TestDowTab:
db 31,28,31,30,31,30,31,31,30,31,30,31
Fail:
ljmp Fail
Pass:
sjmp Pass
;calculates day of week
;good since 01.03.2000 until 31.12.2099
;input: r7 - year (00-99 for 2000-2099)
; r6 - month (01-12)
; r5 - day (01-31, as appropriate for given month)
;no sanity check performed
;returns in acc 00-06 for Monday-Sunday
;uses b
DayOfWeek:
cjne r6,#3,DayOfWeekX1
DayOfWeekX1:
mov a,r7
subb a,#0
mov b,a
rr a
rr a
anl a,#03Fh
clr c
subb a,b
mov b,a
mov a,r7
rl a
add a,b
mov b,a
mov a,r6
add a,#DayOfWeekTab-DayOfWeekX2-1
movc a,@a+pc
DayOfWeekX2:
add a,r5
add a,b
;add a,#4
mov b,#7
div ab
mov a,b
ret
DayOfWeekTab:
;db 0,3,4,0,2,5,0,3,6,1,4,6
db 4,0,1,4,6,2,4,0,3,5,1,3
end



Related topics:
8051 Program - i2c subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines   |   8051 Program - memory subroutines   |   8051 Projects   |   8051 Question Bank

List of topics: 8051

No comments:

Post a Comment