Mạch đồng hồ 595 ngôn ngữ ASM

thang37

Học sinh phổ thông
#1
hi,chào mọi người!
mới tập code nên code còn lủng củng lắm nhưng cũng muốn góp chút kiến thức cho những bạn đang học ASM .

code này e tham khảo trên các diễn đàn,chủ yếu là phần I2C.

Mã:
SCL        BIT P1.0
SDA        BIT P1.1
DA_TA    BIT P3.0
SH        BIT P3.1
ST        BIT P3.2
MODE    BIT P3.3
UP        BIT P3.4
DOWN    BIT P3.5
 
LED1    EQU    60H
LED2    EQU    61H
LED3    EQU    62H
LED4    EQU 63H
 
TEMP    EQU    37H
CDOI1    EQU    38H
CDOI2    EQU    39H
F_MODE    EQU    40H
BIEN_GIO EQU 41H
BIEN_PHUT EQU 42H
F_SEND_TIME    BIT    00H
F_SEND_DATE    BIT    01H
F_SEND        BIT    02H
SECONDS        DATA    30H
MINUTES        DATA    31H
HOUR        DATA    32H
DAY            DATA    33H
DATE        DATA    34H
MONTH        DATA    35H
YEAR        DATA    36H
    ORG 00H
    CLR SH
    CLR ST
    LJMP MAIN
MAIN:
    MOV DPTR,#DU_LIEU
    CALL    I2C_INIT
    CALL    DS1307_INIT
    ;CALL    DS1307_OUTPUT
    ;MOV    SECONDS,#31H
    ;MOV    MINUTES,#31H
    ;MOV    HOUR,#10H
LOOP_MAIN:
    CALL DOC_GIO
    CALL SERIAL
    setb p3.7
    ;CALL SCAN_PHIM
    CALL DELAY1S
    clr p3.7
    call delay1s
    JMP LOOP_MAIN
;****************************************
SCAN_PHIM:
    JNB MODE,THIET_LAP_MODE
    RET
;****************************************
DELAY1S:
    PUSH ACC
    PUSH 00
    MOV TMOD,#01
    MOV R0,#60
LOOPDELAY1S:
    MOV TH0,#HIGH(-10000)
    MOV TL0,#LOW(-10000)
    SETB TR0
    JNB MODE,THIET_LAP_MODE
    JNB TF0,$
    CLR TF0
    CLR TR0
    DJNZ R0,LOOPDELAY1S
    POP 00
    POP ACC
    RET
;*****************************************
THIET_LAP_MODE:
    CLR P3.7
    CLR TF0
    CLR TR0
    JNB MODE,$
    INC F_MODE
    MOV A,F_MODE
    CJNE A,#1,NEXT
    JMP CHINH_GIO
NEXT:
    CJNE A,#2,NEXT1
    JMP CHINH_PHUT
NEXT1:
    CJNE A,#3,NEXT2
    MOV F_MODE,#0
    JMP SCAN_PHIM
NEXT2:
    RET
;******************************************
DELAY300MS:
    PUSH 00
    PUSH 01
    MOV R0,#255
LL:
    MOV R1,#255
    DJNZ R1,$
    DJNZ R0,LL
    POP 01
    POP 00
    RET
;******************************************
CHINH_GIO:
KT_UP:
    CALL HIEN_THI_GIO
    JNB MODE,THIET_LAP_MODE
    JNB UP,TANG_GIO
    JNB DOWN,GIAM_GIO
    CALL SERIAL
    CALL DELAY300MS
    MOV LED3,#10
    MOV LED4,#10
    CALL SERIAL
    CALL DELAY300MS
    JMP KT_UP
;******************************************
TANG_GIO:
    JNB UP,$
    MOV A,HOUR
    CJNE A,#23H,SKIP
    MOV HOUR,#0
    JMP SKIP1
SKIP:
    ADD A,#1
    DA A
    MOV HOUR,A
SKIP1:
    MOV BIEN_GIO,A
    CALL HIEN_THI_GIO
    JMP KT_UP
GIAM_GIO:
    JNB DOWN,$
    MOV A,HOUR
    CJNE A,#00H,SKIP3
    MOV HOUR,#23H
    JMP SKIP4
SKIP3:
    ADD A,#99H
    DA A
    MOV HOUR,A
SKIP4:
    MOV BIEN_GIO,A
    CALL HIEN_THI_GIO
    JMP KT_UP
;******************************************
CHINH_PHUT:
KT_DOWN:
    CALL HIEN_THI_PHUT
    JNB MODE,LUU_THOAT_PHUT
    JNB UP,TANG_PHUT
    JNB DOWN,GIAM_PHUT
    CALL SERIAL
    CALL DELAY300MS
    MOV LED1,#10
    MOV LED2,#10
    CALL SERIAL
    CALL DELAY300MS
    JMP KT_DOWN
;******************************************
LUU_THOAT_PHUT:
    MOV HOUR,BIEN_GIO
    MOV MINUTES,BIEN_PHUT
    MOV SECONDS,#00H
    CALL DS1307_WRITE_TIME
    JMP THIET_LAP_MODE
TANG_PHUT:
    JNB UP,$
    MOV A,MINUTES
    CJNE A,#59H,SKIPM
    MOV MINUTES,#0
    JMP SKIPM1
SKIPM:
    ADD A,#1
    DA A
    MOV MINUTES,A
SKIPM1:
    MOV BIEN_PHUT,A
    CALL HIEN_THI_PHUT
    JMP KT_DOWN
GIAM_PHUT:
    JNB DOWN,$
    MOV A,MINUTES
    CJNE A,#00H,SKIPM3
    MOV MINUTES,#59H
    JMP SKIPM4
SKIPM3:
    ADD A,#99H
    DA A
    MOV MINUTES,A
SKIPM4:
    MOV BIEN_PHUT,A
    CALL HIEN_THI_PHUT
    JMP KT_DOWN
;***************************************
DOC_GIO:
    CALL    DS1307_READ_TIME
    MOV TEMP,MINUTES
    CALL HIEN_THI_GIO
    CALL HIEN_THI_PHUT
    RET
;****************************************
HIEN_THI_PHUT:
    MOV TEMP,MINUTES
    CALL BCD2HEX
    MOV LED1,CDOI1
    MOV LED2,CDOI2
    RET
;****************************************
HIEN_THI_GIO:
    ANL HOUR,#3FH
    MOV TEMP,HOUR
    CALL BCD2HEX
    MOV LED3,CDOI1
    MOV LED4,CDOI2
    RET
;****************************************
BCD2HEX:
    MOV CDOI1,TEMP
    MOV CDOI2,TEMP
    ANL CDOI1,#0FH
    ANL CDOI2,#0F0H
    MOV A,CDOI2
    SWAP A
    MOV CDOI2,A
    RET
;**************************************** 
SERIAL:
        PUSH ACC
        MOV A,LED1
        MOVC A,@a+dptr
        CALL DICH
        MOV    A,LED2
        MOVC A,@A+DPTR
        CALL DICH
        MOV A,LED3
        MOVC A,@A+DPTR
        CALL DICH
        MOV A,LED4
        MOVC A,@A+DPTR
        CALL DICH
        SETB ST
        CLR ST
        ;CALL DELAY
        POP ACC
        RET
;**************************************************
DICH:
        MOV R5,#8
LOOP:
        CLR C
        RLC A
        MOV DA_TA,C
        SETB SH
        NOP
        CLR SH
        DJNZ R5,LOOP
        RET
DS1307_INIT:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307
    LCALL    I2C_WRITE    ;
    ;JC    $-5
    MOV    A,#00H        ;DIA CHI THANH GHI 0
    LCALL    I2C_WRITE
    MOV    A,#00H        ;ENABLE    THE oscillator (CH bit = 0)
    LCALL    I2C_WRITE
    CALL    I2C_STOP
    RET
DS1307_OUTPUT:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307
    CALL    I2C_WRITE    ;
    MOV    A,#07H        ;DIA CHI THANH GHI 0
    CALL    I2C_WRITE
    MOV    A,#10H        ;ENABLE    THE OUTPUT oscillator F=1HZ
    CALL    I2C_WRITE
    CALL    I2C_STOP
    RET
DS1307_WRITE_TIME:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307
    CALL    I2C_WRITE    ;
    MOV    A,#00H        ;DIA CHI THANH GHI 0
    CALL    I2C_WRITE
    MOV    A,SECONDS    ;
    CALL    I2C_WRITE
    MOV    A,MINUTES    ;
    CALL    I2C_WRITE
    MOV    A,HOUR        ;
    CALL    I2C_WRITE
    CALL    I2C_STOP
    RET
DS1307_WRITE_DATE:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307
    CALL    I2C_WRITE    ;
    MOV    A,#03H        ;DIA CHI THANH GHI 3
    CALL    I2C_WRITE
    MOV    A,DAY        ;THU
    CALL    I2C_WRITE
    MOV    A,DATE        ;NGAY
    CALL    I2C_WRITE
    MOV    A,MONTH        ;THANG
    CALL    I2C_WRITE
    MOV    A,YEAR        ;NAM
    CALL    I2C_WRITE
    CALL    I2C_STOP
    RET
DS1307_READ_TIME:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307 VA WRITE
    CALL    I2C_WRITE    ;
    MOV    A,#00H        ;DIA CHI THANH GHI 0
    CALL    I2C_WRITE
    CALL    I2C_RESTART
    MOV    A,#0D1H        ;DIA CHI DS1307 VA READ
    CALL    I2C_WRITE
    CALL    I2C_READ_ACK
    MOV    SECONDS,A
    CALL    I2C_READ_ACK
    MOV    MINUTES,A
    CALL    I2C_READ_NACK
    MOV    HOUR,A
    CALL    I2C_STOP
    RET
DS1307_READ_DATE:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307 VA WRITE
    CALL    I2C_WRITE    ;
    MOV    A,#03H        ;DIA CHI THANH GHI 3
    CALL    I2C_WRITE
    CALL    I2C_RESTART
    MOV    A,#0D1H        ;DIA CHI DS1307 VA READ
    CALL    I2C_WRITE
    CALL    I2C_READ_ACK
    MOV    DAY,A
    CALL    I2C_READ_ACK
    MOV    DATE,A
    CALL    I2C_READ_ACK
    MOV    MONTH,A
    CALL    I2C_READ_NACK
    MOV    YEAR,A
    CALL    I2C_STOP
    RET
I2C_WRITE:
    PUSH    07H
    MOV    R7,#8
  ___LOOP_I2C_WRITE:
    CLR    SCL
    RLC    A
    MOV    SDA,C
    NOP
    NOP
    SETB    SCL
    NOP
    NOP
    DJNZ    R7,___LOOP_I2C_WRITE
    CLR    SCL
    NOP
    NOP
    NOP
    NOP
    SETB    SDA        ;CAU HINH NGO VAO SDA DOC ACK
    SETB    SCL
    NOP
    MOV    C,SDA        ;KIEM TRA CO "C" DE XAC DINH LOI
    NOP
    NOP
    CLR    SCL
    POP    07H
    RET
I2C_READ_ACK:
    PUSH    07H
    MOV    R7,#8
    SETB    SDA        ;CAU HINH NGO VAO SDA
___LOOP_I2C_READ_ACK:
    SETB    SCL
    NOP
    MOV    C,SDA
    RLC    A        ;BYTE DOC VE LUU TRONG THANH GHI "A"
    NOP
    CLR    SCL
    NOP
    NOP
    NOP
    DJNZ    R7,___LOOP_I2C_READ_ACK
    CLR    SDA
    NOP
    SETB    SCL
    NOP
    NOP
    NOP
    NOP
    CLR    SCL
    POP    07H
    RET
I2C_READ_NACK:
    PUSH    07H
    MOV    R7,#8
    SETB    SDA        ;CAU HINH NGO VAO SDA
___LOOP_I2C_READ_NACK:
    SETB    SCL
    NOP
    MOV    C,SDA
    RLC    A        ;BYTE DOC VE LUU TRONG THANH GHI "A"
    NOP
    CLR    SCL
    NOP
    NOP
    NOP
    DJNZ    R7,___LOOP_I2C_READ_NACK
    SETB    SDA
    NOP
    SETB    SCL
    NOP
    NOP
    NOP
    NOP
    CLR    SCL
    POP    07H
    RET
I2C_INIT:
    SETB    SCL
    SETB    SDA
    RET
I2C_START:
    SETB    SDA
    SETB    SCL
    NOP        ;DELAY 4.7US
    NOP        ;Bus Free Time Between a STOP and START Condition
    NOP
    NOP
    NOP
    CLR    SDA
    NOP        ;DELAY 4US
    NOP        ;Hold Time (Repeated) START Condition
    NOP
    NOP
    CLR    SCL
    RET
I2C_STOP:
    CLR    SDA
    SETB    SCL
    NOP        ;DELAY    4.7US
    NOP        ;Setup Time for STOP Condition
    NOP
    NOP
    NOP
    SETB    SDA
    RET
I2C_RESTART:
    SETB    SDA
    SETB    SCL
    NOP        ;DELAY 4.7US
    NOP        ;Setup Time for a Repeated START Condition
    NOP
    NOP
    NOP
    CLR    SDA
    NOP        ;DELAY 4US
    NOP        ;Hold Time (Repeated) START Condition
    NOP
    NOP
    CLR    SCL
    NOP
    NOP
    RET
SCL_HIGH:
    NOP        ;DELAY 4US
    NOP        ;HIGH Period of SCL Clock
    SETB    SCL
    NOP
    NOP
    RET
SCL_LOW:
    NOP        ;DELAY 4.7US
    NOP        ;LOW Period of SCL Clock
    CLR    SCL
    NOP
    NOP
    NOP
    RET
DU_LIEU:
    DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
    DB 0FFH
END
Em chưa làm mạch thật đc,mạch này sau khi chỉnh phút xong sẽ tự động ghi ngầm vào thanh ghi giây giá trị 00.

Các bác góp ý dùm e nhé! Còn phần hẹn giờ nữa e sẽ ngâm cứu sau.
 

Đính kèm

congminhfs

Super Moderator
#2
chào bạn ..góp ý thế này nhé
1.cố giắng làm luôn 2 led giây
2.xung 1Hz ds1307 có sẵng sao không dùng mà phải viết code
3.phím bấm nếu ráp mạch thật nó nhảy labada vì không chống rung phím
4.code trình bày đọc thật vất vã....8-X
 

NgoHungCuong

http://ngohungcuong.com
#3
chào bạn ..góp ý thế này nhé
1.cố giắng làm luôn 2 led giây
2.xung 1Hz ds1307 có sẵng sao không dùng mà phải viết code
3.phím bấm nếu ráp mạch thật nó nhảy labada vì không chống rung phím
4.code trình bày đọc thật vất vã....8-X
Giờ em rất ít khi dùng asm, ngay cả viết bootloader mà chỗ nào dùng C được là em dùng, chỉ chỗ nào bắt buộc dùng ASM em mới phải dùng ASM thôi, :)).
 

thang37

Học sinh phổ thông
#4
chào bạn ..góp ý thế này nhé
1.cố giắng làm luôn 2 led giây
2.xung 1Hz ds1307 có sẵng sao không dùng mà phải viết code
3.phím bấm nếu ráp mạch thật nó nhảy labada vì không chống rung phím
4.code trình bày đọc thật vất vã....8-X
cảm ơn a!
1.e tiết kiệm phần cứng với ko cần thiết lắm ạ,theo ý đồ của e :D
2.e cũng định vậy nhưng sợ tốn thêm con tran kéo dòng cho led,nếu làm led s e sẽ dùng chân 7 của DS cho khỏe.
3.đúng là thiếu chống rung nhưng theo nghĩ có lệnh jnb mode,$ cũng đc rồi,nếu e làm mạch thật mà bị rung thì gọi chương trình delay300ms có sẵn là đc,hi.(mấy cái delay chỉ đúng tương đối thôi,e còn gà mờ cái tính toán lắm)
4.mới học nên trình bày cũng ko hay lắm,thank anh.
 

vantruong97

Học sinh phổ thông
#5
hi,chào mọi người!
mới tập code nên code còn lủng củng lắm nhưng cũng muốn góp chút kiến thức cho những bạn đang học ASM .

code này e tham khảo trên các diễn đàn,chủ yếu là phần I2C.

Mã:
SCL        BIT P1.0
SDA        BIT P1.1
DA_TA    BIT P3.0
SH        BIT P3.1
ST        BIT P3.2
MODE    BIT P3.3
UP        BIT P3.4
DOWN    BIT P3.5
 
LED1    EQU    60H
LED2    EQU    61H
LED3    EQU    62H
LED4    EQU 63H
 
TEMP    EQU    37H
CDOI1    EQU    38H
CDOI2    EQU    39H
F_MODE    EQU    40H
BIEN_GIO EQU 41H
BIEN_PHUT EQU 42H
F_SEND_TIME    BIT    00H
F_SEND_DATE    BIT    01H
F_SEND        BIT    02H
SECONDS        DATA    30H
MINUTES        DATA    31H
HOUR        DATA    32H
DAY            DATA    33H
DATE        DATA    34H
MONTH        DATA    35H
YEAR        DATA    36H
    ORG 00H
    CLR SH
    CLR ST
    LJMP MAIN
MAIN:
    MOV DPTR,#DU_LIEU
    CALL    I2C_INIT
    CALL    DS1307_INIT
    ;CALL    DS1307_OUTPUT
    ;MOV    SECONDS,#31H
    ;MOV    MINUTES,#31H
    ;MOV    HOUR,#10H
LOOP_MAIN:
    CALL DOC_GIO
    CALL SERIAL
    setb p3.7
    ;CALL SCAN_PHIM
    CALL DELAY1S
    clr p3.7
    call delay1s
    JMP LOOP_MAIN
;****************************************
SCAN_PHIM:
    JNB MODE,THIET_LAP_MODE
    RET
;****************************************
DELAY1S:
    PUSH ACC
    PUSH 00
    MOV TMOD,#01
    MOV R0,#60
LOOPDELAY1S:
    MOV TH0,#HIGH(-10000)
    MOV TL0,#LOW(-10000)
    SETB TR0
    JNB MODE,THIET_LAP_MODE
    JNB TF0,$
    CLR TF0
    CLR TR0
    DJNZ R0,LOOPDELAY1S
    POP 00
    POP ACC
    RET
;*****************************************
THIET_LAP_MODE:
    CLR P3.7
    CLR TF0
    CLR TR0
    JNB MODE,$
    INC F_MODE
    MOV A,F_MODE
    CJNE A,#1,NEXT
    JMP CHINH_GIO
NEXT:
    CJNE A,#2,NEXT1
    JMP CHINH_PHUT
NEXT1:
    CJNE A,#3,NEXT2
    MOV F_MODE,#0
    JMP SCAN_PHIM
NEXT2:
    RET
;******************************************
DELAY300MS:
    PUSH 00
    PUSH 01
    MOV R0,#255
LL:
    MOV R1,#255
    DJNZ R1,$
    DJNZ R0,LL
    POP 01
    POP 00
    RET
;******************************************
CHINH_GIO:
KT_UP:
    CALL HIEN_THI_GIO
    JNB MODE,THIET_LAP_MODE
    JNB UP,TANG_GIO
    JNB DOWN,GIAM_GIO
    CALL SERIAL
    CALL DELAY300MS
    MOV LED3,#10
    MOV LED4,#10
    CALL SERIAL
    CALL DELAY300MS
    JMP KT_UP
;******************************************
TANG_GIO:
    JNB UP,$
    MOV A,HOUR
    CJNE A,#23H,SKIP
    MOV HOUR,#0
    JMP SKIP1
SKIP:
    ADD A,#1
    DA A
    MOV HOUR,A
SKIP1:
    MOV BIEN_GIO,A
    CALL HIEN_THI_GIO
    JMP KT_UP
GIAM_GIO:
    JNB DOWN,$
    MOV A,HOUR
    CJNE A,#00H,SKIP3
    MOV HOUR,#23H
    JMP SKIP4
SKIP3:
    ADD A,#99H
    DA A
    MOV HOUR,A
SKIP4:
    MOV BIEN_GIO,A
    CALL HIEN_THI_GIO
    JMP KT_UP
;******************************************
CHINH_PHUT:
KT_DOWN:
    CALL HIEN_THI_PHUT
    JNB MODE,LUU_THOAT_PHUT
    JNB UP,TANG_PHUT
    JNB DOWN,GIAM_PHUT
    CALL SERIAL
    CALL DELAY300MS
    MOV LED1,#10
    MOV LED2,#10
    CALL SERIAL
    CALL DELAY300MS
    JMP KT_DOWN
;******************************************
LUU_THOAT_PHUT:
    MOV HOUR,BIEN_GIO
    MOV MINUTES,BIEN_PHUT
    MOV SECONDS,#00H
    CALL DS1307_WRITE_TIME
    JMP THIET_LAP_MODE
TANG_PHUT:
    JNB UP,$
    MOV A,MINUTES
    CJNE A,#59H,SKIPM
    MOV MINUTES,#0
    JMP SKIPM1
SKIPM:
    ADD A,#1
    DA A
    MOV MINUTES,A
SKIPM1:
    MOV BIEN_PHUT,A
    CALL HIEN_THI_PHUT
    JMP KT_DOWN
GIAM_PHUT:
    JNB DOWN,$
    MOV A,MINUTES
    CJNE A,#00H,SKIPM3
    MOV MINUTES,#59H
    JMP SKIPM4
SKIPM3:
    ADD A,#99H
    DA A
    MOV MINUTES,A
SKIPM4:
    MOV BIEN_PHUT,A
    CALL HIEN_THI_PHUT
    JMP KT_DOWN
;***************************************
DOC_GIO:
    CALL    DS1307_READ_TIME
    MOV TEMP,MINUTES
    CALL HIEN_THI_GIO
    CALL HIEN_THI_PHUT
    RET
;****************************************
HIEN_THI_PHUT:
    MOV TEMP,MINUTES
    CALL BCD2HEX
    MOV LED1,CDOI1
    MOV LED2,CDOI2
    RET
;****************************************
HIEN_THI_GIO:
    ANL HOUR,#3FH
    MOV TEMP,HOUR
    CALL BCD2HEX
    MOV LED3,CDOI1
    MOV LED4,CDOI2
    RET
;****************************************
BCD2HEX:
    MOV CDOI1,TEMP
    MOV CDOI2,TEMP
    ANL CDOI1,#0FH
    ANL CDOI2,#0F0H
    MOV A,CDOI2
    SWAP A
    MOV CDOI2,A
    RET
;****************************************
SERIAL:
        PUSH ACC
        MOV A,LED1
        MOVC A,@a+dptr
        CALL DICH
        MOV    A,LED2
        MOVC A,@A+DPTR
        CALL DICH
        MOV A,LED3
        MOVC A,@A+DPTR
        CALL DICH
        MOV A,LED4
        MOVC A,@A+DPTR
        CALL DICH
        SETB ST
        CLR ST
        ;CALL DELAY
        POP ACC
        RET
;**************************************************
DICH:
        MOV R5,#8
LOOP:
        CLR C
        RLC A
        MOV DA_TA,C
        SETB SH
        NOP
        CLR SH
        DJNZ R5,LOOP
        RET
DS1307_INIT:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307
    LCALL    I2C_WRITE    ;
    ;JC    $-5
    MOV    A,#00H        ;DIA CHI THANH GHI 0
    LCALL    I2C_WRITE
    MOV    A,#00H        ;ENABLE    THE oscillator (CH bit = 0)
    LCALL    I2C_WRITE
    CALL    I2C_STOP
    RET
DS1307_OUTPUT:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307
    CALL    I2C_WRITE    ;
    MOV    A,#07H        ;DIA CHI THANH GHI 0
    CALL    I2C_WRITE
    MOV    A,#10H        ;ENABLE    THE OUTPUT oscillator F=1HZ
    CALL    I2C_WRITE
    CALL    I2C_STOP
    RET
DS1307_WRITE_TIME:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307
    CALL    I2C_WRITE    ;
    MOV    A,#00H        ;DIA CHI THANH GHI 0
    CALL    I2C_WRITE
    MOV    A,SECONDS    ;
    CALL    I2C_WRITE
    MOV    A,MINUTES    ;
    CALL    I2C_WRITE
    MOV    A,HOUR        ;
    CALL    I2C_WRITE
    CALL    I2C_STOP
    RET
DS1307_WRITE_DATE:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307
    CALL    I2C_WRITE    ;
    MOV    A,#03H        ;DIA CHI THANH GHI 3
    CALL    I2C_WRITE
    MOV    A,DAY        ;THU
    CALL    I2C_WRITE
    MOV    A,DATE        ;NGAY
    CALL    I2C_WRITE
    MOV    A,MONTH        ;THANG
    CALL    I2C_WRITE
    MOV    A,YEAR        ;NAM
    CALL    I2C_WRITE
    CALL    I2C_STOP
    RET
DS1307_READ_TIME:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307 VA WRITE
    CALL    I2C_WRITE    ;
    MOV    A,#00H        ;DIA CHI THANH GHI 0
    CALL    I2C_WRITE
    CALL    I2C_RESTART
    MOV    A,#0D1H        ;DIA CHI DS1307 VA READ
    CALL    I2C_WRITE
    CALL    I2C_READ_ACK
    MOV    SECONDS,A
    CALL    I2C_READ_ACK
    MOV    MINUTES,A
    CALL    I2C_READ_NACK
    MOV    HOUR,A
    CALL    I2C_STOP
    RET
DS1307_READ_DATE:
    CALL    I2C_START
    MOV    A,#0D0H        ;DIA CHI DS1307 VA WRITE
    CALL    I2C_WRITE    ;
    MOV    A,#03H        ;DIA CHI THANH GHI 3
    CALL    I2C_WRITE
    CALL    I2C_RESTART
    MOV    A,#0D1H        ;DIA CHI DS1307 VA READ
    CALL    I2C_WRITE
    CALL    I2C_READ_ACK
    MOV    DAY,A
    CALL    I2C_READ_ACK
    MOV    DATE,A
    CALL    I2C_READ_ACK
    MOV    MONTH,A
    CALL    I2C_READ_NACK
    MOV    YEAR,A
    CALL    I2C_STOP
    RET
I2C_WRITE:
    PUSH    07H
    MOV    R7,#8
  ___LOOP_I2C_WRITE:
    CLR    SCL
    RLC    A
    MOV    SDA,C
    NOP
    NOP
    SETB    SCL
    NOP
    NOP
    DJNZ    R7,___LOOP_I2C_WRITE
    CLR    SCL
    NOP
    NOP
    NOP
    NOP
    SETB    SDA        ;CAU HINH NGO VAO SDA DOC ACK
    SETB    SCL
    NOP
    MOV    C,SDA        ;KIEM TRA CO "C" DE XAC DINH LOI
    NOP
    NOP
    CLR    SCL
    POP    07H
    RET
I2C_READ_ACK:
    PUSH    07H
    MOV    R7,#8
    SETB    SDA        ;CAU HINH NGO VAO SDA
___LOOP_I2C_READ_ACK:
    SETB    SCL
    NOP
    MOV    C,SDA
    RLC    A        ;BYTE DOC VE LUU TRONG THANH GHI "A"
    NOP
    CLR    SCL
    NOP
    NOP
    NOP
    DJNZ    R7,___LOOP_I2C_READ_ACK
    CLR    SDA
    NOP
    SETB    SCL
    NOP
    NOP
    NOP
    NOP
    CLR    SCL
    POP    07H
    RET
I2C_READ_NACK:
    PUSH    07H
    MOV    R7,#8
    SETB    SDA        ;CAU HINH NGO VAO SDA
___LOOP_I2C_READ_NACK:
    SETB    SCL
    NOP
    MOV    C,SDA
    RLC    A        ;BYTE DOC VE LUU TRONG THANH GHI "A"
    NOP
    CLR    SCL
    NOP
    NOP
    NOP
    DJNZ    R7,___LOOP_I2C_READ_NACK
    SETB    SDA
    NOP
    SETB    SCL
    NOP
    NOP
    NOP
    NOP
    CLR    SCL
    POP    07H
    RET
I2C_INIT:
    SETB    SCL
    SETB    SDA
    RET
I2C_START:
    SETB    SDA
    SETB    SCL
    NOP        ;DELAY 4.7US
    NOP        ;Bus Free Time Between a STOP and START Condition
    NOP
    NOP
    NOP
    CLR    SDA
    NOP        ;DELAY 4US
    NOP        ;Hold Time (Repeated) START Condition
    NOP
    NOP
    CLR    SCL
    RET
I2C_STOP:
    CLR    SDA
    SETB    SCL
    NOP        ;DELAY    4.7US
    NOP        ;Setup Time for STOP Condition
    NOP
    NOP
    NOP
    SETB    SDA
    RET
I2C_RESTART:
    SETB    SDA
    SETB    SCL
    NOP        ;DELAY 4.7US
    NOP        ;Setup Time for a Repeated START Condition
    NOP
    NOP
    NOP
    CLR    SDA
    NOP        ;DELAY 4US
    NOP        ;Hold Time (Repeated) START Condition
    NOP
    NOP
    CLR    SCL
    NOP
    NOP
    RET
SCL_HIGH:
    NOP        ;DELAY 4US
    NOP        ;HIGH Period of SCL Clock
    SETB    SCL
    NOP
    NOP
    RET
SCL_LOW:
    NOP        ;DELAY 4.7US
    NOP        ;LOW Period of SCL Clock
    CLR    SCL
    NOP
    NOP
    NOP
    RET
DU_LIEU:
    DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
    DB 0FFH
END
Em chưa làm mạch thật đc,mạch này sau khi chỉnh phút xong sẽ tự động ghi ngầm vào thanh ghi giây giá trị 00.

Các bác góp ý dùm e nhé! Còn phần hẹn giờ nữa e sẽ ngâm cứu sau.
cho mình xin ảnh mạch được không b.mình k mở đc file kia
 

Quảng cáo Google