Thông báo

Collapse
No announcement yet.

đồng hồ lcd 16x2 ds1307 ( càn giúp )

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • đồng hồ lcd 16x2 ds1307 ( càn giúp )

    chào các anh , chị trong diễn đàn , em có tìm trên mạng được code , file mô phỏng của mạch đồng hồ số hiển thị lcd 16x2 , nhưng em không biết code (asm ) là dùng phần mếm gì để viết , mong các anh chị xem qua và giúp em với , em xin cảm ơn .
    đây là code :

    $NOMOD51
    $INCLUDE (89C51RX2.INC)
    ;=================KHAI BAO THU VIEN CUA CHIP SU DUNG
    ;================================================= ====
    ;===========KHAI BAO BIT,DATA LINES CUA LCD VA NUT NHAN

    RS BIT P1.0 ;BIT RS CUA LCD
    RW BIT P1.1 ;BIT RW CUA LCD
    E BIT P1.2 ;BIT E CUA LCD
    UP BIT P3.4
    DOWN BIT P3.2
    OK BIT P3.3
    ALARM BIT 00H //CO` BAO' THUC'
    SPEAKER BIT 01H //CO` BAO' SPEAKER
    LOA BIT P1.4
    ALARM_AM_PM BIT 02H
    AM_PM BIT 03H //BIT CO` TRANG THAI AM-PM
    SAVE_TIME_CHANGE BIT 04H //BIT CO` THAY DOI THOI` GIAN
    IS_ALARM BIT 05H
    ALARM_ALL BIT 06H
    RUNG_CHUONG BIT 07H
    ;================================================= ==================================

    SCL EQU P1.6 //CHAN NOI VAO SCL CUA RTC
    SDA EQU P1.7 //CHAN NOI VAO SDA CUA RTC
    ALARM_GOXY EQU R1
    GOTOXY EQU R0 //GIA TRI XAC DINH GIA TRI CAN THAY DOI
    LCD_PORT EQU P0 //LCD DATA LINES
    ;================================================= ==================================
    ;=====BYTE LENH CHO PHEP DOC HOAC GHI VAO RTC

    CONT_BYTE_W EQU 0D0H
    CONT_BYTE_R EQU 0D1H
    NULL DATA 00H
    ;================================================= ===================================
    ;=====ADD_LOW LA THANH GHI CHUA DIA CHI CUA CUA CAC THANH GHI CUA RTC
    ;=====I2CDATA LA BIEN LUU GIA TRI THU DUOC KHI LAY TU RTC

    ADD_LOW EQU 08H
    I2CDATA EQU 09H

    ;=====CAC BIEN LUU GIA TRI CHO TIME
    SEC EQU 0AH
    MIN EQU 0BH
    HOUR EQU 0CH
    DAY EQU 0DH
    DATE EQU 0EH
    MONTH EQU 0FH
    YEAR EQU 10H
    RAND EQU 11H
    ;************************************************* ***********************
    ;======CAC THANH GHI CHUA' GIA TRI CAP NHAT ; *
    ; CHO RTC ; *
    ; *
    TEMP_SEC EQU 12H ; *
    TEMP_MIN EQU 13H ; *
    TEMP_HOUR EQU 14H ; *
    TEMP_DAY EQU 15H ; *
    TEMP_DATE EQU 16H ; *
    TEMP_MONTH EQU 17H ; *
    TEMP_YEAR EQU 18H ; *
    XY EQU 19H //TOA DO CUA THOI GIAN TREN LCD ; *
    TEMP EQU 1AH //BIEN LUU TAM GIA TRI ; *
    ALARM_DAY EQU 1BH
    ALARM_HOUR EQU 1CH
    ALARM_MIN EQU 1DH
    ALARM_XY EQU 1EH

    ;************************************************* ***********************
    ;======================PROGRAM==================== =======================
    ORG 00H
    LJMP MAIN
    ;==============================VECTOR NGAT NGOAI, DUNG DE CAI DAT THOI GIAN
    ORG 13H
    LJMP SETTIME
    MAIN:

    MOV SP,#21H
    MOV ALARM_HOUR,#00H
    MOV ALARM_MIN,#00H
    MOV ALARM_DAY,#00H
    CLR SAVE_TIME_CHANGE
    CLR ALARM
    CLR SPEAKER
    SETB ALARM_ALL
    LCALL LCD_INT
    CALL INTRO

    //NGAT' NGOAI` 0, CHO PHE'P NGAT; CANH.
    SETB EA
    SETB EX1
    SETB IT1

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
    LCALL RTC_INT
    ;
    ;LENH TRONG KHUNG NAY DUOC BAT DE CAP NHAT THOI GIAN CHO RTC BANG PHAN` MEM`;
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;************************************************* *****************************
    ;===========TAO BIEU TUONG DONG HO` LEN LCD
    MOV LCD_PORT,#40H
    LCALL COMMAND
    MOV DPTR,#CLOCK //DIA CHI CHUA BIEU TUONG DONG` HO`
    LCALL LOAD_ICON //CHUONG TRINH CON GHI ICON VAO CGRAM CUA LCD
    MOV DPTR,#ALARM_ON
    LCALL LOAD_ICON
    MOV DPTR,#ALARM_OFF
    LCALL LOAD_ICON //CHUONG TRINH CON GHI ICON VAO CGRAM CUA LCD
    MOV DPTR,#SPEAKER_ON
    LCALL LOAD_ICON
    MOV DPTR,#SPEAKER_OFF
    LCALL LOAD_ICON
    MOV DPTR,#IS_ALARM_ON
    LCALL LOAD_ICON
    ;================================================= ==============================
    CALL I2CRTC
    CALL CLRSCR
    CALL DISPLAY

    ;************************************************* ****************************************
    ;=============================DIEU KHIEN GHI BIEU TUONG AM-PM VAO` LCD
    LOOP_DISPLAY:
    CALL ICON_AM_PM
    CALL ICON_ALARM_SP
    CALL DISPLAY_TIME
    ;==============================GOI CHUONG TRINH CON UPDATE DE CAP NHAT THOI GIAN VAO CAC THANH GHI THOI GIAN
    CALL UPDATE
    JNB ALARM,KHONG_BAO_THUC
    CLR P1.5
    CALL CHECK_ALARM
    JNB IS_ALARM,KHONG_BAO_THUC
    CALL GIO_BAO_THUC
    KHONG_BAO_THUC:
    SJMP LOOP_DISPLAY
    ;================================================= =================DISPLAY
    DISPLAY:
    CALL DISPLAY_TIME
    CALL DISPLAY_DAY
    CALL DISPLAY_DATE
    CALL ICON_AM_PM
    CALL ICON_ALARM_SP
    RET

    ;================================================= =================ICON_ALARM_SP
    ICON_ALARM_SP:
    JNB ALARM,AL_OFF
    AL_ON:
    MOV LCD_PORT,#0C0H
    LCALL COMMAND
    MOV LCD_PORT,#01H
    LCALL WRITELCD
    SJMP SP_ON
    AL_OFF:
    MOV LCD_PORT,#0C0H
    CALL COMMAND
    MOV LCD_PORT,#02H
    CALL WRITELCD
    SP_ON: JNB SPEAKER,SP_OFF
    MOV LCD_PORT,#0C1H
    CALL COMMAND
    MOV LCD_PORT,#03H
    CALL WRITELCD
    JMP EXIT_ICON_AL_SP
    SP_OFF:
    MOV LCD_PORT,#0C1H
    LCALL COMMAND
    MOV LCD_PORT,#04H
    LCALL WRITELCD
    EXIT_ICON_AL_SP:
    RET
    ;================================================= ==========================================
    ICON_AM_PM:
    MOV LCD_PORT,#0CEH
    CALL COMMAND
    JNB AM_PM,AM //KIEM TRA BIT CO` AM-PM, BANG MOT THI
    MOV LCD_PORT,#'P' //
    CALL WRITELCD // GHI PM VAO DO', BAO' HIEU BUOI? CHIEU`
    MOV LCD_PORT,#'M' // BANG 0 THI NHAY DEN GHI VAO AM
    CALL WRITELCD // CHI CAP NHAT THOI GIAN NEU LA PM
    SJMP EXIT_AM_PM
    ;===============================AM
    AM: MOV LCD_PORT,#'A' //
    CALL WRITELCD //GHI KY TU AM VAO LCD NEU LA BUOI? SANG'
    MOV LCD_PORT,#'M' //
    CALL WRITELCD //
    CALL DISPLAY_DAY
    CALL DISPLAY_DATE
    EXIT_AM_PM:

    RET

    ;================================================= ==============================INTRO
    INTRO:
    PUSH DPH
    PUSH DPL
    PUSH ACC
    PUSH 02
    MOV DPTR,#DEL
    MOV LCD_PORT,#0C0H
    CALL COMMAND
    CALL PRINT
    MOV DPTR,#DEL
    MOV LCD_PORT,#080H
    CALL COMMAND
    CALL PRINT
    MOV LCD_PORT,#080H
    CALL COMMAND
    CALL DELAYMS
    MOV DPTR,#DIGITAL
    MOV R2,#16
    L1: CLR A
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    CALL DELAYMS
    INC DPTR
    DJNZ R2,L1
    MOV LCD_PORT,#0C0H
    CALL COMMAND
    MOV DPTR,#MSV
    MOV R2,#16
    L2: CLR A
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    INC DPTR
    CALL DELAYMS
    DJNZ R2,L2
    CALL CLRSCR
    CALL DELAYMS
    MOV DPTR,#DIGITAL
    MOV R2,#16
    L3: CLR A
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    INC DPTR
    DJNZ R2,L3
    MOV LCD_PORT,#0C0H
    CALL COMMAND
    MOV DPTR,#MSV
    MOV R2,#16
    L4: CLR A
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    INC DPTR
    DJNZ R2,L4
    CALL DELAYMS
    CALL CLRSCR
    POP 02
    POP ACC
    POP DPL
    POP DPH
    RET
    ;================================================= ===========LOAD_ICON
    LOAD_ICON: PUSH ACC
    PUSH 02
    MOV R2,#8
    LOOP_ICON:
    CLR A
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    INC DPTR
    DJNZ R2,LOOP_ICON
    POP 02
    POP ACC
    RET
    ;================================================= =========GIO_BAO_THUC
    GIO_BAO_THUC:
    PUSH 05
    PUSH DPH
    PUSH DPL
    CLR EA //CAM' NGAT' TOA`N CU.C
    CLR EX1
    CALL CLRSCR
    MOV LCD_PORT,#82H
    CALL COMMAND
    MOV DPTR,#IS_ALARM_CHAR
    CALL PRINT
    MOV LCD_PORT,#0C7H
    CALL COMMAND
    MOV LCD_PORT,#00H
    CALL WRITELCD
    MOV R5,#60
    KEU_LOA:
    JNB SPEAKER,KHONG_BAT_LOA
    CPL LOA
    KHONG_BAT_LOA:
    CPL RUNG_CHUONG
    JB RUNG_CHUONG,HIEN_THI_RUNG1
    MOV LCD_PORT,#0C7H
    CALL COMMAND
    MOV LCD_PORT,#01H
    CALL WRITELCD
    CALL DELAYMS
    DJNZ R5,KEU_LOA
    SJMP HET_BAO_THUC
    HIEN_THI_RUNG1:
    MOV LCD_PORT,#0C7H
    CALL COMMAND
    MOV LCD_PORT,#05H
    CALL WRITELCD
    JNB OK,HET_BAO_THUC
    CALL DELAYMS
    DJNZ R5,KEU_LOA
    HET_BAO_THUC:
    CALL CLRSCR
    CALL DISPLAY_DAY
    CALL DISPLAY_DATE
    SETB EA
    SETB EX1
    POP DPL
    POP DPH
    POP 05
    RETI
    ;================================================= ====KIEM TRA BAO' THUC'
    CHECK_ALARM:
    PUSH ACC
    JNB ALARM_ALL,BAO_THUC_1_LAN
    BAO_THUC_NHIEU_LAN:
    CALL CHECK_ALARM_TIME
    JMP EXIT_CHECK_ALARM
    BAO_THUC_1_LAN:
    CALL BAO_THUC_DAY

    EXIT_CHECK_ALARM:
    POP ACC

    RET
    ;================================================= ======================================
    ;================================================= ================================BAO THUC DAY
    BAO_THUC_DAY:
    PUSH ACC
    MOV A,ALARM_DAY
    ;========================================KIEM TRA NGAY CHU NHAT
    CJNE A,#01H,NO_DAY_1
    CALL CHECK_ALARM_TIME
    JMP EXIT_BAO_THUC_DAY
    ;========================================KIEM TRA NGAY THU HAI
    NO_DAY_1:
    CJNE A,#02H,NO_DAY_2
    CALL CHECK_ALARM_TIME
    JMP EXIT_BAO_THUC_DAY
    ;========================================KIEM TRA NGAY THU BA
    NO_DAY_2:
    CJNE A,#03H,NO_DAY_3
    CALL CHECK_ALARM_TIME
    JMP EXIT_BAO_THUC_DAY
    ;========================================KIEM TRA NGAY CHU TU
    NO_DAY_3:
    CJNE A,#04H,NO_DAY_4
    CALL CHECK_ALARM_TIME
    JMP EXIT_BAO_THUC_DAY
    ;========================================KIEM TRA NGAY THU NAM
    NO_DAY_4:
    CJNE A,#05H,NO_DAY_5
    CALL CHECK_ALARM_TIME
    JMP EXIT_BAO_THUC_DAY
    ;========================================KIEM TRA NGAY THU SAU
    NO_DAY_5:
    CJNE A,#06H,NO_DAY_6
    CALL CHECK_ALARM_TIME
    JMP EXIT_BAO_THUC_DAY
    ;========================================KIEM TRA NGAY THU 7
    NO_DAY_6:
    CJNE A,#07H,EXIT_BAO_THUC_DAY
    CALL CHECK_ALARM_TIME
    JMP EXIT_BAO_THUC_DAY

    EXIT_BAO_THUC_DAY:
    POP ACC
    RET
    ;================================================= ==============================CHECK ALARM_TIME
    CHECK_ALARM_TIME:
    PUSH ACC
    MOV A,HOUR
    ANL A,#1FH
    CJNE A,ALARM_HOUR,EXIT_CHECK_ALARM_TIME_WITH_NO_ALARM
    SETB IS_ALARM
    MOV A,ALARM_MIN
    CJNE A,MIN,EXIT_CHECK_ALARM_TIME_WITH_NO_ALARM
    SETB IS_ALARM //DUNG' GIO` BAO' THUC'
    CLR A
    MOV C,ALARM_AM_PM
    RLC A
    MOV TEMP,A
    CLR A
    MOV C,AM_PM
    RLC A
    CJNE A,TEMP,EXIT_CHECK_ALARM_TIME_WITH_NO_ALARM
    SETB IS_ALARM
    JMP EXIT_CHECK_ALARM_TIME_WITH_ALARM
    EXIT_CHECK_ALARM_TIME_WITH_NO_ALARM:
    CLR IS_ALARM
    EXIT_CHECK_ALARM_TIME_WITH_ALARM:
    POP ACC
    RET
    ;================================================= ======================CHUONG TRINH CON NGAT' NGOAI` PHUC VU CHO VIEC CAI DAT THOI GIAN
    SETTIME:

    PUSH DPH
    PUSH DPL
    CLR EA //CO' YEU CAU` CAI DAT THOI GIAN, XOA EA CAM' NGAT' TOAN` CUC
    CALL DISPLAY
    MOV LCD_PORT,#80H
    CALL COMMAND
    MOV LCD_PORT,#7FH
    CALL WRITELCD
    MOV XY,#80H
    CALL POINTER
    JNB OK,$ //O TAI DAY NEU BIT OK = 0
    CALL INDEC_HOUR //GOI CHUONG TRINH CON NHAP THOI GIAN
    ;================================================= ======================FINISH
    ;==================================LENH
    MOV LCD_PORT,#0CH
    CALL COMMAND
    CALL CLRSCR

    CALL SAVE_TIME
    CALL DELAYMS
    CALL CLRSCR
    MOV LCD_PORT,#0C2H
    CALL COMMAND
    MOV LCD_PORT,#00H
    CALL WRITELCD
    CALL DISPLAY //LAY' LAI TAT CA THONG TIN BO NHO' DE HIEN THI LAI LEN LCD
    SETB EA
    CLR IE1
    POP DPL
    POP DPH
    EXIT_SETTIME:
    RETI
    ;================================================= ================================================SA VE TIME
    SAVE_TIME:
    PUSH DPH
    PUSH DPL
    MOV LCD_PORT,#0EH
    CALL COMMAND
    MOV DPTR,#SAVE_CAST
    CALL PRINT
    MOV LCD_PORT,#0C5H
    CALL COMMAND
    MOV DPTR,#SAVE_YES
    CALL PRINT
    MOV LCD_PORT,#0CCH
    CALL COMMAND
    MOV DPTR,#SAVE_NO
    CALL PRINT
    MOV LCD_PORT,#0C5H
    CALL COMMAND
    SETB SAVE_TIME_CHANGE
    CHECK_AGAIN:
    JB DOWN,YES_CHANGE
    JNB DOWN,$
    MOV LCD_PORT,#0C5H
    CALL COMMAND
    SETB SAVE_TIME_CHANGE
    SJMP CHECK_CHANGE

    YES_CHANGE: JB UP,CHECK_CHANGE
    JNB UP,$
    MOV LCD_PORT,#0CCH
    CALL COMMAND
    CLR SAVE_TIME_CHANGE
    CHECK_CHANGE:
    JB OK,CHECK_AGAIN
    JNB OK,$
    CALL CLRSCR
    MOV DPTR,#TIME_SAVED
    CALL PRINT
    CALL DELAYMS
    JNB SAVE_TIME_CHANGE,KHONG_DOI
    THAY_DOI:
    CALL RTC_UPDATE
    JMP EXIT_SAVE_TIME
    KHONG_DOI:
    CALL I2CRTC
    EXIT_SAVE_TIME:
    MOV LCD_PORT,#0CH
    CALL COMMAND
    POP DPL
    POP DPH
    CLR SAVE_TIME_CHANGE
    RET
    ;================================================= ===
    INDEC_HOUR: PUSH 00H
    INDEC: MOV XY,#80H
    CALL POINTER
    ;================================================= ==========================KET THUC PHAN HIEN THI CHINH GIO
    ;=============CAI DAT GIO
    MOV GOTOXY,#00H
    IN1:
    CJNE GOTOXY,#00H,IN2
    MOV XY,#080H
    CALL POINTER
    JB OK,IN2
    JNB OK,$
    LJMP EXIT_IN
    IN2:
    JB UP,IN3
    JNB UP,$
    INC GOTOXY
    LJMP IN4
    IN3:
    JB DOWN,IN5
    JNB DOWN,$
    DEC GOTOXY
    LJMP IN4
    ;================================================= ====GIOI HAN CHO TOA DO TREN LCD
    IN4:
    CJNE GOTOXY,#0FFH,IN5
    MOV GOTOXY,#0AH
    LJMP IN5
    IN5:
    CJNE GOTOXY,#01H,IN6
    MOV XY,#082H
    CALL POINTER
    CALL OPTION
    LJMP IN1
    IN6:
    CJNE GOTOXY,#02H,IN7
    MOV XY,#087H
    CALL POINTER
    CALL OPTION
    JMP IN1
    IN7:
    CJNE GOTOXY,#03H,IN8
    MOV XY,#8AH
    CALL POINTER
    CALL OPTION
    JMP IN1

    IN8:
    CJNE GOTOXY,#04H,IN9
    MOV XY,#08FH
    CALL POINTER
    CALL OPTION
    JMP IN1
    IN9:
    CJNE GOTOXY,#05H,IN10
    MOV XY,#0C0H
    CALL POINTER
    CALL OPTION
    JMP IN1

    IN10:
    CJNE GOTOXY,#06H,IN11
    MOV XY,#0C1H
    CALL POINTER
    CALL OPTION
    JMP IN1

    IN11:
    CJNE GOTOXY,#07H,IN12
    MOV XY,#0C5H
    CALL POINTER
    CALL OPTION
    JMP IN1
    IN12:
    CJNE GOTOXY,#08H,IN13
    MOV XY,#0C8H
    CALL POINTER
    CALL OPTION
    JMP IN1

    IN13: CJNE GOTOXY,#0AH,IN14
    MOV XY,#0CFH
    CALL POINTER
    CALL OPTION
    JMP IN1
    IN14: CJNE GOTOXY,#09H,IN15
    MOV XY,#0CBH
    CALL POINTER
    CALL OPTION
    JMP IN1
    IN15:
    CJNE GOTOXY,#0BH,IN16
    MOV GOTOXY,#00H
    IN16:
    JMP IN1
    EXIT_IN:
    POP 00H
    RET
    ;================================================= =============OPTION
    OPTION: PUSH ACC
    PUSH DPH
    PUSH DPL
    PUSH 00H
    PUSH 01H
    MOV DPTR,#LCD_DATA
    ;====================================CHANGE HOUR
    O0: CJNE GOTOXY,#07H,O1
    JB OK,EXIT0
    JNB OK,$
    MOV A,HOUR
    ANL A,#0E0H
    MOV TEMP,A
    MOV A,HOUR
    ANL A,#1FH
    ADD A,#1
    DA A
    CJNE A,#13H,EXIT_O0
    MOV A,#00H
    SJMP EXIT_O0
    EXIT_O0:
    ADD A,TEMP
    MOV HOUR,A
    MOV TEMP_HOUR,A
    MOV C,ACC.4
    CLR A
    RLC A
    MOVC A,@A+DPTR
    MOV LCD_PORT,#0C4H
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,HOUR
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,XY
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    EXIT0:
    LJMP EXIT_O
    ;=====================================CHANGE MIN
    O1:
    CJNE GOTOXY,#08H,O2
    JB OK,EXIT1
    JNB OK,$
    MOV A,MIN
    ADD A,#01H
    DA A
    CJNE A,#60H,EXIT_O1
    MOV A,#00H
    SJMP EXIT_O1
    EXIT_O1:
    MOV MIN,A
    MOV TEMP_MIN,A
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,#0C7H
    CALL COMMAND
    MOV LCD_PORT,A
    LCALL WRITELCD
    MOV A,MIN
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,XY
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    EXIT1:
    JMP EXIT_O
    ;===============================CHANGE AM-PM
    O2:
    CJNE GOTOXY,#0AH,O3
    JB OK,EXIT2
    JNB OK,$
    CPL AM_PM
    MOV C,AM_PM
    MOV A,HOUR
    MOV ACC.5,C
    MOV HOUR,A
    MOV TEMP_HOUR,A
    JB AM_PM,NO_AM
    MOV LCD_PORT,#0CEH
    CALL COMMAND
    MOV DPTR,#AM_ICON
    CALL PRINT
    JMP EXIT2
    NO_AM:
    MOV LCD_PORT,#0CEH
    LCALL COMMAND
    MOV DPTR,#PM_ICON
    CALL PRINT
    EXIT2:
    JMP EXIT_O
    ;===============================CHANGE DAY
    O3:
    CJNE GOTOXY,#01H,O4
    JB OK,EXIT_O3
    JNB OK,$
    MOV A,DAY
    ADD A,#1
    DA A
    CJNE A,#08,NEXT_O3
    MOV A,#01H
    NEXT_O3: MOV DAY,A
    MOV TEMP_DAY,A
    CALL DISPLAY_DAY

    EXIT_O3:
    JMP EXIT_O
    ;===============================CHANGE DATE
    O4: CJNE GOTOXY,#02H,O5
    JB OK,EXIT_O4
    JNB OK,$

    MOV DPTR,#LCD_DATA
    MOV R0,MONTH
    CJNE R0,#02,KHONG_PHAI_THANG2
    MOV A,YEAR
    MOV B,#4
    DIV AB
    MOV A,B
    CJNE A,#00H,KHONG_PHAI_NAM_NHUAN
    MOV A,DATE
    ADD A,#01
    DA A
    CJNE A,#30H,NEXT_MONTH2
    MOV A,#00H
    JMP NEXT_MONTH2
    KHONG_PHAI_NAM_NHUAN:
    MOV A,DATE
    ADD A,#01
    DA A
    CJNE A,#29H,NEXT_MONTH2
    MOV A,#00H
    JMP NEXT_MONTH2
    KHONG_PHAI_THANG2:
    MOV A,MONTH
    MOV B,#2
    DIV AB
    MOV A,B
    CJNE A,#00H,THANG_LE
    MOV A,DATE
    ADD A,#1
    DA A
    CJNE A,#31H,NEXT_MONTH2
    MOV A,#00H
    SJMP NEXT_MONTH2
    THANG_LE:
    MOV A,DATE
    ADD A,#1
    DA A
    CJNE A,#32H,NEXT_MONTH2
    MOV A,#00H
    NEXT_MONTH2:
    MOV DATE,A
    MOV TEMP_DATE,A
    MOV LCD_PORT,#086H
    LCALL COMMAND
    MOV A,DATE
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,DATE
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    JMP EXIT_O4

    EXIT_O4:
    JMP EXIT_O
    ;===============================CHANGE MONTH
    O5:
    CJNE GOTOXY,#03H,O6
    JB OK,EXIT_O5
    JNB OK,$
    MOV A,MONTH
    ANL A,#01FH
    MOV DPTR,#LCD_DATA
    ADD A,#1
    DA A
    CJNE A,#13H,NEXT_O5
    MOV A,#00H
    NEXT_O5:
    MOV MONTH,A
    MOV TEMP_MONTH,A
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,#089H
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,MONTH
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,#08AH
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    EXIT_O5:
    JMP EXIT_O
    ;================================CHANGE YEAR
    O6:
    CJNE GOTOXY,#04H,O7
    JB OK,EXIT_O6
    JNB OK,$
    MOV DPTR,#LCD_DATA
    MOV A,YEAR
    ADD A,#1
    DA A
    CJNE A,#91H,NEXT_O6
    MOV A,#00H

    NEXT_O6:
    MOV YEAR,A
    MOV TEMP_YEAR,A
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,#08EH
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,YEAR
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    EXIT_O6:

    JMP EXIT_O
    ;======================================CHANGE ALARM
    O7: CJNE GOTOXY,#05H,O8
    JB OK,NOSET_ALARM
    JNB OK,$
    SET_ALARM:
    CPL ALARM
    CALL ICON_ALARM_SP
    JNB ALARM,NOSET_ALARM
    CALL SETTING_ALARM
    CALL ICON_ALARM_SP

    NOSET_ALARM:

    JMP EXIT_O
    ;=======================================CHANGE SPEAKER
    O8: CJNE GOTOXY,#06H,O9
    JB OK,NOSET_SP
    JNB OK,$
    SET_SP:
    CPL SPEAKER
    CALL ICON_ALARM_SP
    NOSET_SP:
    JMP EXIT_O
    O9:
    CJNE GOTOXY,#09H,EXIT_O
    JB OK,EXIT_O
    JNB OK,$
    MOV DPTR,#LCD_DATA
    MOV A,SEC
    ADD A,#01H
    DA A
    CJNE A,#60H,O9_E1
    MOV A,#00H
    O9_E1:
    MOV SEC,A
    MOV TEMP_SEC,A
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,#0CAH
    CALL COMMAND
    MOV LCD_PORT,A
    LCALL WRITELCD
    MOV A,SEC
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,XY
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    JMP EXIT_O
    EXIT_O:
    POP 01H
    POP 00H
    POP DPL
    POP DPH
    POP ACC
    RET
    ;================================================= =====================SETTING_ALARM
    SETTING_ALARM:
    PUSH 01H
    PUSH DPH
    PUSH DPL
    PUSH ACC
    CALL CLRSCR
    MOV LCD_PORT,#80H
    CALL COMMAND
    MOV LCD_PORT,#7FH
    CALL WRITELCD
    MOV LCD_PORT,#82H
    CALL COMMAND
    MOV DPTR,#ALARMSET
    CALL PRINT
    MOV DPTR,#ALARM_YES
    CALL PRINT
    MOV LCD_PORT,#8DH
    CALL COMMAND
    CALL ALARM_DAY_SET
    CALL DISPLAY_TIME_ALARM
    CALL ICON_AL_AM_PM
    MOV LCD_PORT,#0C2H
    CALL COMMAND
    MOV LCD_PORT,#00H
    CALL WRITELCD
    MOV LCD_PORT,#80H
    CALL COMMAND

    MOV ALARM_GOXY,#00H
    A_IN1:
    CJNE ALARM_GOXY,#00H,A_IN2
    MOV LCD_PORT,#80H
    CALL COMMAND
    JB OK,A_IN2
    JNB OK,$
    JMP EXIT_A_SET

    A_IN2: JB UP,A_IN3
    JNB UP,$
    INC ALARM_GOXY
    JMP A_IN5

    A_IN3: JB DOWN,A_IN4
    JNB DOWN,$
    DEC ALARM_GOXY
    JMP A_IN4
    A_IN4:
    CJNE ALARM_GOXY,#0FFH,A_IN5
    MOV ALARM_GOXY,#05H
    JMP A_IN6
    A_IN5:
    CJNE ALARM_GOXY,#06H,A_IN6
    MOV LCD_PORT,#80H
    CALL COMMAND
    MOV ALARM_GOXY,#00H
    JMP A_IN1
    ;================================================= ========================ALARM ON/OFF
    A_IN6:
    CJNE ALARM_GOXY,#01H,A_IN7
    MOV LCD_PORT,#089H
    CALL COMMAND
    JB OK,EXIT_A_IN6
    JNB OK,$
    CPL ALARM
    JB ALARM,BAT_BAO_THUC
    TAT_BAO_THUC:
    MOV DPTR,#CANCEL_ALARM
    CALL PRINT
    JMP EXIT_A_IN6
    BAT_BAO_THUC:
    MOV LCD_PORT,#89H
    CALL COMMAND
    MOV DPTR,#ALARM_YES
    CALL PRINT
    EXIT_A_IN6:
    JMP A_IN1
    ;================================================= ==CHON NGAY` BAO' THUC'
    A_IN7:
    CJNE ALARM_GOXY,#02H,A_IN8
    MOV LCD_PORT,#8DH
    CALL COMMAND
    JB OK,EXIT_A_IN7
    JNB OK,$
    MOV A,ALARM_DAY
    ADD A,#1
    MOV ALARM_DAY,A
    IS_SUN: CJNE A,#01,IS_MON
    MOV DPTR,#SUN
    CALL PRINT
    JMP EXIT_A_IN7
    IS_MON: CJNE A,#02,IS_TUE
    MOV DPTR,#MON
    CALL PRINT
    JMP EXIT_A_IN7
    IS_TUE: CJNE A,#03,IS_WED
    MOV DPTR,#TUE
    CALL PRINT
    JMP EXIT_A_IN7
    IS_WED: CJNE A,#04,IS_THU
    MOV DPTR,#WED
    CALL PRINT
    JMP EXIT_A_IN7
    IS_THU: CJNE A,#05,IS_FRI
    MOV DPTR,#THU
    CALL PRINT
    JMP EXIT_A_IN7
    IS_FRI: CJNE A,#06,IS_SAT
    MOV DPTR,#FRI
    CALL PRINT
    JMP EXIT_A_IN7
    IS_SAT: CJNE A,#07,EXIT_A_IN7
    MOV DPTR,#SAT
    CALL PRINT
    MOV ALARM_DAY,#00H
    JMP EXIT_A_IN7

    EXIT_A_IN7:
    JMP A_IN1
    ;================================================= ==CHON GIO` BAO' THUC'
    A_IN8:
    CJNE ALARM_GOXY,#03H,A_IN9
    MOV LCD_PORT,#0C5H
    CALL COMMAND
    JB OK,EXIT_A_IN8
    JNB OK,$
    MOV A,ALARM_HOUR
    MOV DPTR,#LCD_DATA
    MOV A,ALARM_HOUR
    ANL A,#1FH
    ADD A,#1
    DA A
    CJNE A,#13H,E_A_IN8
    MOV A,#00H
    E_A_IN8:
    MOV ALARM_HOUR,A
    MOV C,ACC.4
    CLR A
    RLC A
    MOVC A,@A+DPTR
    MOV LCD_PORT,#0C4H
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,ALARM_HOUR
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,#0C5H
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD
    EXIT_A_IN8:
    JMP A_IN1
    ;================================================= ==CHON PHUT' BAO' THUC'
    A_IN9:
    CJNE ALARM_GOXY,#04H,A_IN10
    MOV LCD_PORT,#0C8H
    CALL COMMAND
    JB OK,EXIT_A_IN9
    JNB OK,$
    MOV DPTR,#LCD_DATA
    MOV A,ALARM_MIN
    ADD A,#01H
    DA A
    CJNE A,#60H,E_A_IN9
    MOV A,#00H
    E_A_IN9:
    MOV ALARM_MIN,A
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,#0C7H
    CALL COMMAND
    MOV LCD_PORT,A
    LCALL WRITELCD
    MOV A,ALARM_MIN
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,0C8H
    CALL COMMAND
    MOV LCD_PORT,A
    CALL WRITELCD

    EXIT_A_IN9:
    JMP A_IN1
    ;================================================= =CHON BUOI? BAO' THUC' AM/PM
    A_IN10: CJNE ALARM_GOXY,#05H,A_IN11
    MOV LCD_PORT,#0CEH
    CALL COMMAND
    JB OK,A_IN11
    JNB OK,$
    CPL ALARM_AM_PM
    CALL ICON_AL_AM_PM
    A_IN11:
    JMP A_IN1


    EXIT_A_SET:
    ;================================================= =======FNISH ALARM
    CALL CLRSCR
    MOV LCD_PORT,#80H
    CALL COMMAND
    MOV DPTR,#ALARM_CHANGE
    CALL PRINT
    CALL DELAYMS
    LCALL CLRSCR
    CALL DISPLAY
    MOV LCD_PORT,#80H
    CALL COMMAND
    MOV LCD_PORT,#7FH
    CALL WRITELCD
    POP ACC
    POP DPL
    POP DPH
    POP 01H
    RET
    ;================================================= ========================

    ALARM_DAY_SET:
    PUSH ACC
    PUSH DPH
    PUSH DPL
    MOV A,ALARM_DAY
    ANL A,#0FH
    CJNE A,#00H,A_DAY_0
    MOV LCD_PORT,#08DH
    CALL COMMAND
    MOV DPTR,#ALL
    CALL PRINT
    SETB ALARM_ALL
    JMP A_DAY


    ;=========================================SUNDAY
    A_DAY_0:
    CJNE A,#01,A_DAY_1
    MOV DPTR,#SUN
    CALL PRINT
    CLR ALARM_ALL
    SJMP A_DAY
    ;=========================================MONDAY
    A_DAY_1:
    CJNE A,#02,A_DAY_2
    MOV DPTR,#MON
    CALL PRINT
    CLR ALARM_ALL
    SJMP A_DAY
    ;=========================================TUEDAY
    A_DAY_2:
    CJNE A,#03,A_DAY_3
    MOV DPTR,#TUE
    CALL PRINT
    CLR ALARM_ALL
    SJMP A_DAY
    ;=========================================WED
    A_DAY_3:
    CJNE A,#04,A_DAY_4
    MOV DPTR,#WED
    CALL PRINT
    CLR ALARM_ALL
    SJMP A_DAY
    ;=========================================THU
    A_DAY_4:
    CJNE A,#05,A_DAY_5
    MOV DPTR,#THU
    CALL PRINT
    CLR ALARM_ALL
    JMP A_DAY
    ;========================================FRI
    A_DAY_5:
    CJNE A,#06,A_DAY_6
    MOV DPTR,#FRI
    CALL PRINT
    CLR ALARM_ALL
    JMP A_DAY
    ;========================================SAT
    A_DAY_6: CJNE A,#07,A_DAY
    MOV DPTR,#SAT
    CALL PRINT
    CLR ALARM_ALL
    A_DAY:
    POP DPL
    POP DPH
    POP ACC
    RET

    ;================================================= =========ICON_AL_AM_PM
    ICON_AL_AM_PM:
    MOV LCD_PORT,#0CEH
    CALL COMMAND
    JNB ALARM_AM_PM,A_AM //KIEM TRA BIT CO` AM-PM, BANG MOT THI
    MOV LCD_PORT,#'P'
    CALL WRITELCD // GHI PM VAO DO', BAO' HIEU BUOI? CHIEU`
    MOV LCD_PORT,#'M' // BANG 0 THI NHAY DEN GHI VAO AM
    CALL WRITELCD // CHI CAP NHAT THOI GIAN NEU LA PM
    SJMP EXIT_AL_AM_PM
    ;===============================AM
    A_AM:
    MOV LCD_PORT,#'A' //
    CALL WRITELCD //GHI KY TU AM VAO LCD NEU LA BUOI? SANG'
    MOV LCD_PORT,#'M' //
    CALL WRITELCD //
    EXIT_AL_AM_PM:

    RET

    ;================================================= =========HIEN THI GIO` DA HEN, DUOC LUU TRONG RTC
    DISPLAY_TIME_ALARM:
    PUSH ACC
    PUSH DPH
    PUSH DPL
    ;================================================= ===LAY' THOI GIAN HEN GIO` TRONG RTC
    ;========================================HOUR
    MOV LCD_PORT,#0C4H
    CALL COMMAND
    MOV DPTR,#LCD_DATA
    MOV A,ALARM_HOUR
    SWAP A
    ANL A,#01H
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,ALARM_HOUR
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    LCALL WRITELCD
    MOV A,#':'
    MOV LCD_PORT,A
    LCALL WRITELCD
    ;======================================UPDATE MIN
    MOV A,ALARM_MIN
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,ALARM_MIN
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    LCALL WRITELCD

    POP DPL
    POP DPH
    POP ACC
    RET
    ;==========================================HIEN THI NGAY TRONG TUAN LEN LCD
    ;
    DISPLAY_DAY:
    PUSH ACC
    PUSH DPH
    PUSH DPL
    MOV LCD_PORT,#82H
    CALL COMMAND
    MOV A,DAY
    ANL A,#0FH
    ;=========================================SUNDAY
    CJNE A,#01,NEXT_DAY_1
    MOV DPTR,#SUN
    CALL PRINT
    SJMP EXIT_DAY
    ;=========================================MONDAY
    NEXT_DAY_1:
    CJNE A,#02,NEXT_DAY_2
    MOV DPTR,#MON
    CALL PRINT
    SJMP EXIT_DAY
    ;=========================================TUEDAY
    NEXT_DAY_2:
    CJNE A,#03,NEXT_DAY_3
    MOV DPTR,#TUE
    CALL PRINT
    SJMP EXIT_DAY
    ;=========================================WED
    NEXT_DAY_3:
    CJNE A,#04,NEXT_DAY_4
    MOV DPTR,#WED
    CALL PRINT
    SJMP EXIT_DAY
    ;=========================================THU
    NEXT_DAY_4:
    CJNE A,#05,NEXT_DAY_5
    MOV DPTR,#THU
    CALL PRINT
    JMP EXIT_DAY
    ;========================================FRI
    NEXT_DAY_5:
    CJNE A,#06,NEXT_DAY_6
    MOV DPTR,#FRI
    LCALL PRINT
    JMP EXIT_DAY
    ;========================================SAT
    NEXT_DAY_6: CJNE A,#07,EXIT_DAY
    MOV DPTR,#SAT
    LCALL PRINT
    JMP EXIT_DAY
    ;=======================================EXIT_DAY
    EXIT_DAY: POP DPL
    POP DPH
    POP ACC
    RET
    ;================================================= ========DISPLAY_DATE
    DISPLAY_DATE:
    PUSH ACC
    PUSH DPH
    PUSH DPL
    MOV DPTR,#LCD_DATA
    MOV LCD_PORT,#086H
    LCALL COMMAND
    ;===============================UPDATE DATE
    MOV A,DATE
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,DATE
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,#'/'
    MOV LCD_PORT,A
    CALL WRITELCD
    ;==============================UPDATE MONTH
    MOV A,MONTH
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,MONTH
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,#'/'
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,#'2'
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,#'0'
    MOV LCD_PORT,A
    CALL WRITELCD
    ;=============================UPDATE YEAR
    MOV A,YEAR
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,YEAR
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    POP DPL
    POP DPH
    POP ACC
    RET
    ;================================================= =============DISPLAY_TIME
    DISPLAY_TIME:

    PUSH ACC
    PUSH DPH
    PUSH DPL
    MOV LCD_PORT,#0C2H
    CALL COMMAND
    MOV LCD_PORT,#00H
    CALL WRITELCD
    MOV DPTR,#LCD_DATA
    MOV LCD_PORT,#0C4H
    CALL COMMAND
    ;========================================UPDATE HOUR
    MOV A,HOUR
    SWAP A
    ANL A,#01H
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,HOUR
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    LCALL WRITELCD
    MOV A,HOUR
    SWAP A
    RRC A
    RRC A
    MOV AM_PM,C
    MOV A,#':'
    MOV LCD_PORT,A
    LCALL WRITELCD
    ;======================================UPDATE MIN
    MOV A,MIN
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    MOV A,MIN
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    LCALL WRITELCD
    MOV A,#':'
    MOV LCD_PORT,A
    LCALL WRITELCD
    ;=====================================UPDATE SEC
    MOV A,SEC
    SWAP A
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    LCALL WRITELCD
    MOV A,SEC
    ANL A,#0FH
    MOVC A,@A+DPTR
    MOV LCD_PORT,A
    CALL WRITELCD
    POP DPL
    POP DPH
    POP ACC
    RET

    ;================================================= ================================================== ====UPDATE
    ;======================SU DUNG PHUONG PHAP KIEM TRA VONG` LIEN TUC DE NHAN BIET SU THAY DOI VE THOI GIAN
    UPDATE: PUSH ACC
    MOV A,SEC
    HERE:
    CALL I2CRTC
    CJNE A,SEC,EXIT_UPDATE
    SJMP HERE
    EXIT_UPDATE:POP ACC
    RET
    ;================================================= ================================================== =====RTC_INT
    ;===================CHUONG TRINH CAP NHAT THOI GIAN BANG PHAN MEM CHO RTC

    RTC_INT:
    PUSH ACC
    CLR SCL
    CLR SDA
    NOP
    SETB SCL
    SETB SDA
    NOP
    ;=============================GIAY
    MOV ADD_LOW,#00H //DIA CHI THANH GHI GIAY
    MOV I2CDATA,#00H //DATA CAN GHI VAO`
    CALL WRITE_BYTE
    ;=============================PHUT
    MOV ADD_LOW,#01H
    MOV I2CDATA,#33H
    CALL WRITE_BYTE
    ;=============================GIO
    MOV ADD_LOW,#02H
    MOV I2CDATA,#01000111b
    CALL WRITE_BYTE
    ;=============================DAY
    ;DAY = 1->7
    MOV ADD_LOW,#03H
    MOV I2CDATA,#04H
    CALL WRITE_BYTE
    ;=============================DATE
    MOV ADD_LOW,#04H
    MOV I2CDATA,#09H
    CALL WRITE_BYTE
    ;============================MONTH
    MOV ADD_LOW,#05H
    MOV I2CDATA,#03H
    CALL WRITE_BYTE
    ;============================YEAR
    MOV ADD_LOW,#06H
    MOV I2CDATA,#11H
    CALL WRITE_BYTE
    POP ACC
    RET
    ;================================================= ==============RTC_UPDATE
    RTC_UPDATE:
    PUSH ACC
    CLR SCL
    CLR SDA
    NOP
    SETB SCL
    SETB SDA
    NOP
    ;=====================================GIAY
    MOV ADD_LOW,#00H
    MOV I2CDATA,TEMP_SEC
    CALL WRITE_BYTE
    ;======================================PHUT
    MOV ADD_LOW,#01H
    MOV I2CDATA,TEMP_MIN
    CALL WRITE_BYTE
    ;======================================GIO`
    MOV ADD_LOW,#02H
    MOV I2CDATA,TEMP_HOUR
    CALL WRITE_BYTE
    ;=====================================DAY
    MOV ADD_LOW,#03H
    MOV I2CDATA,TEMP_DAY
    CALL WRITE_BYTE
    ;======================================DATE
    MOV ADD_LOW,#04H
    MOV I2CDATA,TEMP_DATE
    CALL WRITE_BYTE
    ;======================================MONTH
    MOV ADD_LOW,#05H
    MOV I2CDATA,TEMP_MONTH
    CALL WRITE_BYTE
    ;======================================YEAR
    MOV ADD_LOW,#06H
    MOV I2CDATA,TEMP_YEAR
    CALL WRITE_BYTE
    POP ACC
    RET
    ;================================================= READ DATA FROM RTC
    I2CRTC:
    PUSH ACC
    ;================================================= =READS SECONDS
    MOV ADD_LOW,#00h
    CALL READ_BYTE
    MOV SEC,I2CDATA
    MOV TEMP_SEC,I2CDATA
    CALL I2C_STOP
    ;================================================= =READS MINUTES
    MOV ADD_LOW,#01h
    CALL READ_BYTE
    MOV MIN,I2CDATA
    MOV TEMP_MIN,I2CDATA
    CALL I2C_STOP
    ;================================================= =READS HOURS
    MOV ADD_LOW,#02h
    CALL READ_BYTE
    MOV HOUR,I2CDATA
    MOV TEMP_HOUR,I2CDATA
    LCALL I2C_STOP
    ;================================================= =READS DAYS
    MOV ADD_LOW,#03h
    CALL READ_BYTE
    MOV DAY,I2CDATA
    MOV TEMP_DAY,I2CDATA
    CALL I2C_STOP
    ;================================================= =READS DATE
    MOV ADD_LOW,#04h
    CALL READ_BYTE
    MOV DATE,I2CDATA
    MOV TEMP_DATE,I2CDATA
    CALL I2C_STOP
    ;================================================= =READS MONTHS
    MOV ADD_LOW,#05h
    CALL READ_BYTE
    MOV MONTH,I2CDATA
    MOV TEMP_MONTH,I2CDATA
    CALL I2C_STOP
    ;================================================= =READS YEARS
    MOV ADD_LOW,#06H
    CALL READ_BYTE
    MOV YEAR,I2CDATA
    MOV TEMP_YEAR,I2CDATA
    CALL I2C_STOP
    POP ACC
    RET

    ;================================================= =================================
    ;=====STOP I2C

    I2C_STOP:
    CLR SDA
    SETB SCL
    NOP
    SETB SDA
    RET

    ;================================================= =================================

    ;************************************************* ****
    WRITE_BYTE:
    CLR SDA ;BIT START CHO I2C
    CLR SCL
    MOV A,#CONT_BYTE_W ;GUI BYTE DIEU KHIEN
    CALL LOOP_BYTE
    SETB SDA
    SETB SCL
    JB SDA,WRITE_BYTE ;HOI VONG` DE KIEM TRA CO` BAN
    CLR SCL
    MOV A,ADD_LOW ;GUI DIA CHI VAO RTC QUA I2C
    CALL LOOP_BYTE ;CHUONG TRINH CON GUI DIA CHI BANG I2C
    SETB SDA
    SETB SCL
    JB SDA,WRITE_BYTE ;HOI VONG DE KIEM TRA CO BAN
    CLR SCL
    MOV A,I2CDATA ;GUI DATA VAO DIA CHI TRONG RTC
    CALL LOOP_BYTE
    SETB SDA
    SETB SCL
    JB SDA,WRITE_BYTE ;LAP LAI NEU CO` BAN CHUA DUOC XOA'
    CLR SDA
    CLR SCL
    SETB SCL ;STOP BIT
    SETB SDA
    RET

    ;************************************************* *****CHUONG TRINH CON DOC BYTE TU RTC KIEU I2C
    READ_BYTE:

    CLR SDA ;START BIT
    CLR SCL
    MOV A,#CONT_BYTE_W ;GUI BYTE DIEU KHIEN
    CALL LOOP_BYTE
    SETB SDA
    SETB SCL
    JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN
    CLR SCL
    MOV A,ADD_LOW ;GUI DIA CHI
    CALL LOOP_BYTE
    SETB SDA
    SETB SCL
    JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN SDA CHUA DC XOA'
    CLR SCL

    SETB SCL
    SETB SDA
    CLR SDA ;STOP BIT
    CLR SCL
    MOV A,#CONT_BYTE_R ;GUI BYTE DIEU KHIEN
    CALL LOOP_BYTE
    SETB SDA
    SETB SCL
    JB SDA,READ_BYTE ;LAP LAI NEU CO` BAN CHUA DUOC XOA'
    CLR SCL
    CALL LOOP_READ
    SETB SDA
    SETB SCL
    CLR SCL

    SETB SCL ;STOP BIT
    SETB SDA
    RET

    ;************************************************* ******************************
    ;* GHI VAO RTC THEO PHUONG THUC I2C *
    ;* INPUT: ACC *
    ;************************************************* ******************************
    LOOP_BYTE: PUSH 02H
    MOV R2,#08H
    LOOP_SEND: RLC A
    MOV SDA,C
    SETB SCL
    CLR SCL
    DJNZ R2,LOOP_SEND
    POP 02H
    RET

    ;************************************************* ******************************
    ;* DOC TU RTC THEO PHUONG THUC I2C *
    ;* OUTPUT: ACC *
    ;************************************************* ******************************
    LOOP_READ: PUSH 02H
    MOV R2,#08H
    LOOP_READ1: SETB SCL
    MOV C,SDA
    CLR SCL
    RLC A
    DJNZ R2,LOOP_READ1
    MOV I2CDATA,A
    POP 02H
    RET
    ;================================================= ==========================KHAI BAO' LCD

    LCD_INT:
    MOV lcd_port,#38H ;KHOI TAO LCD 2 DONG MATRIX 5X8
    CALL COMMAND ;CHUONG TRINH CON GHI LENH
    MOV lcd_port,#0cH ;BAT HIEN THI, TAT' CON TRO
    CALL COMMAND
    MOV lcd_port,#01H ;XOA MAN HINH LCD
    ACALL COMMAND
    MOV lcd_port,#80H ;DAT VI TRI AC CUA LCD = 0
    ACALL COMMAND
    RET
    ;=====================HAM GHI LENH VAO LCD========================
    COMMAND:
    CLR RS
    CLR RW
    SETB E
    ACALL DELAY
    CLR E
    RET
    ;======================HAM GHI DATA VAO LCD========================
    WRITELCD:
    SETB RS
    CLR RW
    SETB E
    ACALL DELAY
    CLR E
    RET
    ;======================DELAY====================== =====
    ;DELAY SHORT TIME
    DELAY: PUSH 06
    PUSH 07
    MOV R7,#30
    RELOAD_R6: MOV R6,#30
    DJNZ R6,$
    DJNZ R7,RELOAD_R6
    POP 07
    POP 06
    RET
    ;=======================DELAY LONG TIME USE TIMER 0
    DELAYMS:
    PUSH 02H
    MOV TMOD,#01H
    MOV R2,#100
    RELOAD_TIMER:
    MOV TH0,#HIGH(-5000)
    MOV TL0,#LOW(-5000)
    SETB TR0
    JNB TF0,$
    CLR TR0
    CLR TF0
    DJNZ R2,RELOAD_TIMER
    POP 02
    RET
    ;=======================PRINT===================
    PRINT: PUSH ACC
    LOOPPRINT: CLR A
    MOVC A,@A+DPTR
    CJNE A,#NULL,PRINTLCD
    MOV LCD_PORT,#' '
    SJMP EXITPRINT
    ;==================CHUONG TRINH IN CHUOI RA LCD================================
    PRINTLCD:
    MOV lcd_port,A ;DUA RA P0
    CALL WRITELCD ;GHI VAO LCD
    INC DPTR
    SJMP LOOPPRINT
    EXITPRINT: POP ACC
    RET ;TRO VE TU HAM PRINT
    ;===================CLEAR SCREEN============================================ ====
    CLRSCR: MOV lcd_port,#01H ;LENH XOA MAN HINH LCD
    CALL COMMAND ; GOI CHUONG TRINH CON LENH
    MOV lcd_port,#02H ;TRO VE DAU DONG
    CALL COMMAND
    RET
    ;====================POINTER====================== =============================
    POINTER: MOV LCD_PORT,#0EH
    CALL COMMAND
    MOV LCD_PORT,XY
    CALL COMMAND
    RET
    ;;=================CHAR ARRAY============================================= =====
    CHAR1: DB 7EH,'Chu Manh Cuong',7FH,NULL
    CHAR2: DB 7EH,' TN VDK 8051 ',7FH,NULL
    CHAR3: DB 7EH,' DH CNTP HCM ',7FH,NULL
    TIME_SAVE: DB 'CHANGE TIME: '
    YES: DB 'YES'
    NO: DB 'NO'
    DONE: DB 'Done',NULL
    SUN: DB 'Sun',NULL
    MON: DB 'Mon',NULL
    TUE: DB 'Tue',NULL
    WED: DB 'Wed',NULL
    THU: DB 'Thu',NULL
    FRI: DB 'Fri',NULL
    SAT: DB 'Sat',NULL
    AM_ICON: DB 'AM',NULL
    PM_ICON: DB 'PM',NULL
    CLOCK: DB 00H,0EH,1BH,1BH,19H,1FH,0EH,00H
    ALARM_ON: DB 4H,0EH,0EH,0EH,1FH,0H,4H,0H
    ALARM_OFF: DB 5H,0EH,0EH,0EH,1FH,10H,4H,0H
    SPEAKER_ON: DB 5H,0CH,1CH,1DH,1CH,0CH,5H,0H
    SPEAKER_OFF: DB 5H,0EH,1CH,1CH,1CH,0CH,14H,0H
    IS_ALARM_ON: DB 04H,0EH,0EH,0EH,01FH,00H,015H,00H
    DEL: DB 0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,0FFH, 0FFH,0FFH,0FFH,0FFH,0FFH,0FFH,NULL
    ALL: DB 'ALL',NULL
    MSV: DB ' By: Manh Cuong '
    DIGITAL: DB ' DIGITAL CLOCK '
    ALARMSET: DB 'ALARM: ',NULL
    CANCEL_ALARM: DB 'OFF',NULL
    ALARM_YES: DB 'ON ',NULL
    LCD_DATA: DB '0123456789'
    SAVE_CAST: DB 'CHANGE TIME....?',NULL
    SAVE_YES: DB 'YES',NULL
    SAVE_NO: DB 'NO',NULL
    ALARM_CHANGE: DB 'SAVING ALARM...',NULL
    TIME_SAVED: DB 'DONE.....',NULL
    IS_ALARM_CHAR: DB '...ALARM...',NULL
    END

  • #2
    ban gui mach mo phong len dc hok?

    Comment


    • #3
      code gi dai he vay troi

      Comment

      Về tác giả

      Collapse

      nguoidentukd Tìm hiểu thêm về nguoidentukd

      Bài viết mới nhất

      Collapse

      Đang tải...
      X