Mấy bác giúp em sửa lại phần code DS1307 với, phần nút nhấn mode bị lỗi. Bác nào cao tay sửa lại dùm em
Đây là phần code của em. Bác nào giúp em em Thanks nhiều
$MOD51
time_key BIT P1.4
increment BIT P1.3
decrement BIT P1.2
sec equ 40h
min equ 41h
hour equ 42h
days equ 43h
date equ 44h
month equ 45h
year equ 46h
sig equ 48h
sda equ P1.1
scl equ P1.0
MAIN:
mov sp,#5fh
; acall startc
; mov a,#0d0h
; acall send
; mov a,#08h
; acall send
; acall startc
; mov a,#0d1h
; acall send
; acall recv
; acall stop
;cjne a,#'~',done
sjmp run
done:
mov sec,#00h
mov min,#0h
mov hour,#23h
mov days,#1h
mov date,#12h
mov month,#07h
mov year,#09h
mov 47h,#00h
mov sig,#'~'
mov r6,#08h
mov r0,#40h
mov r1,#00h
acall rtc_ini
run:
acall startc
mov a,#0d0h
acall send
mov a,#00h
acall send
acall startc
mov a,#0d1h
acall send
mov r6,#07h
mov r0,#40h
here:
acall recv
acall delay1ms
djnz r6,here
acall stop
mov 51h,41h
mov 52h,42h
mov 53h,44h
mov 54h,45h
mov 55h,46h
mov 56h,#20h
mov 57h,43h
mov 50h,40h
acall display
jnb time_key, SETTIME
sjmp run
rtc_ini:
acall startc
mov a,#0d0h
acall send
mov a,r1
acall send
mov a,@r0
acall send
inc r0
inc r1
acall stop
djnz r6,rtc_ini
acall stop
ret
SETTIME:
ACALL SETTIMEDATE
SJMP run
delay1ms:
push 03h
mov r3,#10
djnz r3,$
pop 03h
ret
startc:
clr scl
setb sda
nop
nop
nop
setb scl
clr sda
nop
nop
nop
ret
send:
push 07h
push acc
mov r7,#08
back:
clr scl
nop
nop
nop
rlc a
SETB sda
mov sda,c
setb scl
nop
nop
nop
clr scl
nop
nop
nop
djnz r7,back
setb sda
setb scl
nop
nop
nop
clr scl
nop
nop
nop
pop acc
pop 07h
ret
stop:
nop
nop
nop
clr sda
setb scl
nop
setb sda
nop
nop
nop
clr scl
ret
recv:
push 07h
push acc
mov r7,#08
back2:
setb sda
setb scl
nop
nop
nop
mov c,sda
rlc a
clr scl
nop
nop
nop
djnz r7,back2
setb sda
clr scl
nop
nop
nop
clr sda
setb scl
nop
nop
nop
clr scl
nop
nop
nop
mov @r0,a
inc r0
pop acc
pop 07h
ret
dechex: mov b,#0ah
div ab
mov 60h,b
mov b,#10h
mul ab
add a,60h
ret
hexdec: mov b,#10h
div ab
mov 60h,b
mov b,#0ah
mul ab
add a,60h
ret
SETTIMEDATE:
jnb time_key,$
mov a,41h
lcall hexdec
key:
lcall dechex
mov 41h,a
ACALL QPHUT
mov a,41h
lcall hexdec
jb increment,chk1
inc a
cjne a,#60,ok1
mov a,#0
ok1:
jnb increment,$
sjmp key
chk1:
jb decrement,chk2
dec a
cjne a,#0ffh,ok2
mov a,#59
ok2: jnb decrement,$
sjmp key
chk2: jb time_key,key
lcall dechex
mov min,a
acall startc
mov a,#0d0h
acall send
mov a,#01h
acall send
mov a,min
acall send
acall stop
jnb time_key,$
mov a,42h
lcall hexdec
key1:
lcall dechex
mov 42h,a
ACALL QGIO
mov a,42h
lcall hexdec
jb increment,chk3
inc a
cjne a,#24,ok3
mov a,#0
ok3:
jnb increment,$
sjmp key1
chk3: jb decrement,chk4
dec a
cjne a,#0ffh,ok4
mov a,#23
ok4: jnb decrement,$
sjmp key1
chk4: jb time_key,key1
lcall dechex
mov hour,a
acall startc
mov a,#0d0h
acall send
mov a,#02h
acall send
mov a,hour
acall send
acall stop
;****************************************
jnb time_key,$; THU
mov a,43h
lcall hexdec
key3:
lcall dechex
mov 43h,a
ACALL QTHU
mov a,43h
lcall hexdec
jb increment,chk7
inc a
cjne a,#8,ok7
mov a,#1
ok7: jnb increment,$
sjmp key3
chk7: jb decrement,chk8
dec a
cjne a,#0,ok8
mov a,#7
ok8: jnb decrement,$
sjmp key3
chk8: jb time_key,key3
lcall dechex
mov days,a
acall startc
mov a,#0d0h
acall send
mov a,#03h
acall send
mov a,days
acall send
acall stop
;************************************************* ***
jnb time_key,$; NGAY
mov a,44h
lcall hexdec
key4:
lcall dechex
mov 44h,a
acall QNGAY
mov a,44h
lcall hexdec
jb increment,chk9
inc a
cjne a,#32,ok9
mov a,#1
ok9: jnb increment,$
sjmp key4
chk9: jb decrement,chk10
dec a
cjne a,#0,ok10
mov a,#31
ok10: jnb decrement,$
chk10: jb time_key,key4
lcall dechex
mov date,a
acall startc
mov a,#0d0h
acall send
mov a,#04h
acall send
mov a,date
acall send
acall stop
;****************************************
jnb time_key,$; THANG
mov a,45h
lcall hexdec
key5:
lcall dechex
mov 45h,a
acall QTHANG
mov a,45h
lcall hexdec
jb increment,chk11
inc a
cjne a,#13,ok11
mov a,#1
ok11: jnb increment,$
sjmp key5
chk11: jb decrement,chk12
dec a
cjne a,#0,ok12
mov a,#12
ok12: jnb decrement,$
chk12: jb time_key,key5
lcall dechex
mov month,a
acall startc
mov a,#0d0h
acall send
mov a,#05h
acall send
mov a,month
acall send
acall stop
;******************************************
jnb time_key,$; NAM
mov a,46h
lcall hexdec
key6:
lcall dechex
mov 46h,a
acall QNAM
mov a,46h
lcall hexdec
jb increment,chk13
inc a
cjne a,#100,ok13
mov a,#0
ok13:
jnb increment,$
sjmp key6
chk13: jb decrement,chk14
dec a
cjne a,#0ffh,ok14
mov a,#99
ok14: jnb decrement,$
chk14: jb time_key,key6
jnb time_key,$
lcall dechex
mov year,a
acall startc
mov a,#0d0h
acall send
mov a,#06h
acall send
mov a,year
acall send
acall stop
nop
nop
nop
ret;**********************************************
QTHU:
MOV A,43H
ACALL MA7THANH
MOV P0,A
MOV P2,#0EH
ACALL DELAY
MOV P0,#0FFH
RET
QNGAY: ; QUET CAP LED NGAY
MOV A,44H
ACALL MA7THANH
MOV P0,A
MOV P2,#06H
ACALL DELAY
MOV P2,#0FFH
MOV A,44H
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,#07H
ACALL DELAY
MOV P2,#0FFH
RET
QTHANG:
MOV A,45H
ACALL MA7THANH
MOV P0,A
MOV P2,#08H
ACALL DELAY
MOV P2,#0FFH
MOV A,45H
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,#09H
ACALL DELAY
MOV P2,#0FFH
RET
QNAM:
MOV A,46H
ACALL MA7THANH
MOV P0,A
MOV P2,#0aH
ACALL DELAY
MOV P2,#0FFH
MOV A,46H
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,#0bH
ACALL DELAY
MOV P2,#0FFH
RET
QGIO:
MOV A,42H
LCALL MA7THANH
MOV P0,A
MOV P2,#04H
ACALL DELAY
MOV P2,#0FFH
MOV A,42H
SWAP A
LCALL MA7THANH
MOV P0,A
MOV P2,#05H
ACALL DELAY
MOV P2,#0FFH
RET
QPHUT:
MOV A,41H
LCALL MA7THANH
MOV P0,A
MOV P2,#02H
ACALL DELAY
MOV P2,#0FFH
MOV A,41H
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,#03H
ACALL DELAY
MOV P2,#0FFH
RET
display: ; CHUONG TRINH CON QUET TAT CA CAC LED
PUSH 00H ; HIEN THI THOI GIAN VA NGAY THANG NAM
PUSH 01H
PUSH ACC
MOV R0,#50H
MOV R1,#00H
LAP:
MOV A,@R0
ACALL MA7THANH
MOV P0,A
MOV P2,R1
ACALL DELAY
MOV P0,#0FFH
MOV P2,#10H
INC R1
MOV A,@R0
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,R1
ACALL DELAY
MOV P0,#0FFH
MOV P2,#10H
INC R1
INC R0
CJNE R0,#58H,LAP
POP ACC
POP 01H
POP 00H
RET
MA7THANH: ; DOI SO BCD SANG MA 7 DOAN DE DUA RA LED 7 DOAN
MOV DPTR,#TABLE
ANL A,#0FH
MOVC A,@A+DPTR
RET
DELAY:
MOV TMOD,#01H
MOV TH0,#HIGH(-250)
MOV TL0,#LOW(-250)
SETB TR0
JNB TF0,$
CLR TR0
CLR TF0
RET
TABLE: ; BANG MA 7 DOAN
DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
END
Đây là phần code của em. Bác nào giúp em em Thanks nhiều
$MOD51
time_key BIT P1.4
increment BIT P1.3
decrement BIT P1.2
sec equ 40h
min equ 41h
hour equ 42h
days equ 43h
date equ 44h
month equ 45h
year equ 46h
sig equ 48h
sda equ P1.1
scl equ P1.0
MAIN:
mov sp,#5fh
; acall startc
; mov a,#0d0h
; acall send
; mov a,#08h
; acall send
; acall startc
; mov a,#0d1h
; acall send
; acall recv
; acall stop
;cjne a,#'~',done
sjmp run
done:
mov sec,#00h
mov min,#0h
mov hour,#23h
mov days,#1h
mov date,#12h
mov month,#07h
mov year,#09h
mov 47h,#00h
mov sig,#'~'
mov r6,#08h
mov r0,#40h
mov r1,#00h
acall rtc_ini
run:
acall startc
mov a,#0d0h
acall send
mov a,#00h
acall send
acall startc
mov a,#0d1h
acall send
mov r6,#07h
mov r0,#40h
here:
acall recv
acall delay1ms
djnz r6,here
acall stop
mov 51h,41h
mov 52h,42h
mov 53h,44h
mov 54h,45h
mov 55h,46h
mov 56h,#20h
mov 57h,43h
mov 50h,40h
acall display
jnb time_key, SETTIME
sjmp run
rtc_ini:
acall startc
mov a,#0d0h
acall send
mov a,r1
acall send
mov a,@r0
acall send
inc r0
inc r1
acall stop
djnz r6,rtc_ini
acall stop
ret
SETTIME:
ACALL SETTIMEDATE
SJMP run
delay1ms:
push 03h
mov r3,#10
djnz r3,$
pop 03h
ret
startc:
clr scl
setb sda
nop
nop
nop
setb scl
clr sda
nop
nop
nop
ret
send:
push 07h
push acc
mov r7,#08
back:
clr scl
nop
nop
nop
rlc a
SETB sda
mov sda,c
setb scl
nop
nop
nop
clr scl
nop
nop
nop
djnz r7,back
setb sda
setb scl
nop
nop
nop
clr scl
nop
nop
nop
pop acc
pop 07h
ret
stop:
nop
nop
nop
clr sda
setb scl
nop
setb sda
nop
nop
nop
clr scl
ret
recv:
push 07h
push acc
mov r7,#08
back2:
setb sda
setb scl
nop
nop
nop
mov c,sda
rlc a
clr scl
nop
nop
nop
djnz r7,back2
setb sda
clr scl
nop
nop
nop
clr sda
setb scl
nop
nop
nop
clr scl
nop
nop
nop
mov @r0,a
inc r0
pop acc
pop 07h
ret
dechex: mov b,#0ah
div ab
mov 60h,b
mov b,#10h
mul ab
add a,60h
ret
hexdec: mov b,#10h
div ab
mov 60h,b
mov b,#0ah
mul ab
add a,60h
ret
SETTIMEDATE:
jnb time_key,$
mov a,41h
lcall hexdec
key:
lcall dechex
mov 41h,a
ACALL QPHUT
mov a,41h
lcall hexdec
jb increment,chk1
inc a
cjne a,#60,ok1
mov a,#0
ok1:
jnb increment,$
sjmp key
chk1:
jb decrement,chk2
dec a
cjne a,#0ffh,ok2
mov a,#59
ok2: jnb decrement,$
sjmp key
chk2: jb time_key,key
lcall dechex
mov min,a
acall startc
mov a,#0d0h
acall send
mov a,#01h
acall send
mov a,min
acall send
acall stop
jnb time_key,$
mov a,42h
lcall hexdec
key1:
lcall dechex
mov 42h,a
ACALL QGIO
mov a,42h
lcall hexdec
jb increment,chk3
inc a
cjne a,#24,ok3
mov a,#0
ok3:
jnb increment,$
sjmp key1
chk3: jb decrement,chk4
dec a
cjne a,#0ffh,ok4
mov a,#23
ok4: jnb decrement,$
sjmp key1
chk4: jb time_key,key1
lcall dechex
mov hour,a
acall startc
mov a,#0d0h
acall send
mov a,#02h
acall send
mov a,hour
acall send
acall stop
;****************************************
jnb time_key,$; THU
mov a,43h
lcall hexdec
key3:
lcall dechex
mov 43h,a
ACALL QTHU
mov a,43h
lcall hexdec
jb increment,chk7
inc a
cjne a,#8,ok7
mov a,#1
ok7: jnb increment,$
sjmp key3
chk7: jb decrement,chk8
dec a
cjne a,#0,ok8
mov a,#7
ok8: jnb decrement,$
sjmp key3
chk8: jb time_key,key3
lcall dechex
mov days,a
acall startc
mov a,#0d0h
acall send
mov a,#03h
acall send
mov a,days
acall send
acall stop
;************************************************* ***
jnb time_key,$; NGAY
mov a,44h
lcall hexdec
key4:
lcall dechex
mov 44h,a
acall QNGAY
mov a,44h
lcall hexdec
jb increment,chk9
inc a
cjne a,#32,ok9
mov a,#1
ok9: jnb increment,$
sjmp key4
chk9: jb decrement,chk10
dec a
cjne a,#0,ok10
mov a,#31
ok10: jnb decrement,$
chk10: jb time_key,key4
lcall dechex
mov date,a
acall startc
mov a,#0d0h
acall send
mov a,#04h
acall send
mov a,date
acall send
acall stop
;****************************************
jnb time_key,$; THANG
mov a,45h
lcall hexdec
key5:
lcall dechex
mov 45h,a
acall QTHANG
mov a,45h
lcall hexdec
jb increment,chk11
inc a
cjne a,#13,ok11
mov a,#1
ok11: jnb increment,$
sjmp key5
chk11: jb decrement,chk12
dec a
cjne a,#0,ok12
mov a,#12
ok12: jnb decrement,$
chk12: jb time_key,key5
lcall dechex
mov month,a
acall startc
mov a,#0d0h
acall send
mov a,#05h
acall send
mov a,month
acall send
acall stop
;******************************************
jnb time_key,$; NAM
mov a,46h
lcall hexdec
key6:
lcall dechex
mov 46h,a
acall QNAM
mov a,46h
lcall hexdec
jb increment,chk13
inc a
cjne a,#100,ok13
mov a,#0
ok13:
jnb increment,$
sjmp key6
chk13: jb decrement,chk14
dec a
cjne a,#0ffh,ok14
mov a,#99
ok14: jnb decrement,$
chk14: jb time_key,key6
jnb time_key,$
lcall dechex
mov year,a
acall startc
mov a,#0d0h
acall send
mov a,#06h
acall send
mov a,year
acall send
acall stop
nop
nop
nop
ret;**********************************************
QTHU:
MOV A,43H
ACALL MA7THANH
MOV P0,A
MOV P2,#0EH
ACALL DELAY
MOV P0,#0FFH
RET
QNGAY: ; QUET CAP LED NGAY
MOV A,44H
ACALL MA7THANH
MOV P0,A
MOV P2,#06H
ACALL DELAY
MOV P2,#0FFH
MOV A,44H
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,#07H
ACALL DELAY
MOV P2,#0FFH
RET
QTHANG:
MOV A,45H
ACALL MA7THANH
MOV P0,A
MOV P2,#08H
ACALL DELAY
MOV P2,#0FFH
MOV A,45H
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,#09H
ACALL DELAY
MOV P2,#0FFH
RET
QNAM:
MOV A,46H
ACALL MA7THANH
MOV P0,A
MOV P2,#0aH
ACALL DELAY
MOV P2,#0FFH
MOV A,46H
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,#0bH
ACALL DELAY
MOV P2,#0FFH
RET
QGIO:
MOV A,42H
LCALL MA7THANH
MOV P0,A
MOV P2,#04H
ACALL DELAY
MOV P2,#0FFH
MOV A,42H
SWAP A
LCALL MA7THANH
MOV P0,A
MOV P2,#05H
ACALL DELAY
MOV P2,#0FFH
RET
QPHUT:
MOV A,41H
LCALL MA7THANH
MOV P0,A
MOV P2,#02H
ACALL DELAY
MOV P2,#0FFH
MOV A,41H
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,#03H
ACALL DELAY
MOV P2,#0FFH
RET
display: ; CHUONG TRINH CON QUET TAT CA CAC LED
PUSH 00H ; HIEN THI THOI GIAN VA NGAY THANG NAM
PUSH 01H
PUSH ACC
MOV R0,#50H
MOV R1,#00H
LAP:
MOV A,@R0
ACALL MA7THANH
MOV P0,A
MOV P2,R1
ACALL DELAY
MOV P0,#0FFH
MOV P2,#10H
INC R1
MOV A,@R0
SWAP A
ACALL MA7THANH
MOV P0,A
MOV P2,R1
ACALL DELAY
MOV P0,#0FFH
MOV P2,#10H
INC R1
INC R0
CJNE R0,#58H,LAP
POP ACC
POP 01H
POP 00H
RET
MA7THANH: ; DOI SO BCD SANG MA 7 DOAN DE DUA RA LED 7 DOAN
MOV DPTR,#TABLE
ANL A,#0FH
MOVC A,@A+DPTR
RET
DELAY:
MOV TMOD,#01H
MOV TH0,#HIGH(-250)
MOV TL0,#LOW(-250)
SETB TR0
JNB TF0,$
CLR TR0
CLR TF0
RET
TABLE: ; BANG MA 7 DOAN
DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
END
Comment