Thông báo

Collapse
No announcement yet.

Xin đoạn code C chọn chế độ 12h/24h cho DS1307

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

  • Xin đoạn code C chọn chế độ 12h/24h cho DS1307

    Bác nào biết cách làm sao để chọn chế độ 12/24h cho DS1307 chỉ giáo em với, code C cho 8051 hoặc Píc6f877 cũng đc, thanks nhìu

  • #2
    Thanh ghi giờ (HOURS): có thể nói đây là thanh ghi phức tạp nhất trong DS1307. Thanh ghi này có địa chỉ 02H. Trước hết 4-bits thấp của thanh ghi này được dùng cho chữ số hàng đơn vị của giờ. Do DS1307 hỗ trợ 2 loại hệ thống hiển thị giờ (gọi là mode) là 12h (1h đến 12h) và 24h (1h đến 24h) giờ, bit6 (hình 4) xác lập hệ thống giờ. Nếu bit6=0 thì hệ thống 24h được chọn, khi đó 2 bit cao 5 và 4 dùng mã hóa chữ số hàng chục của giá trị giờ. Do giá trị lớn nhất của chữ số hàng chục trong trường hợp này là 2 (=10, nhị phân) nên 2 bit 5 và 4 là đủ để mã hóa. Nếu bit6=1 thì hệ thống 12h được chọn, với trường hợp này chỉ có bit 4 dùng mã hóa chữ số hàng chục của giờ, bit 5 (màu orange trong hình 4) chỉ buổi trong ngày, AM hoặc PM. Bit5 =0 là AM và bit5=1 là PM. Bit 7 luôn bằng 0
    chỉ là đặt chế độ thôi
    Website chính thức đổi địa chỉ website thành
    Mời các bạn ghé thăm !!!

    Comment


    • #3
      Nguyên văn bởi mast090 Xem bài viết
      Thanh ghi giờ (HOURS): có thể nói đây là thanh ghi phức tạp nhất trong DS1307. Thanh ghi này có địa chỉ 02H. Trước hết 4-bits thấp của thanh ghi này được dùng cho chữ số hàng đơn vị của giờ. Do DS1307 hỗ trợ 2 loại hệ thống hiển thị giờ (gọi là mode) là 12h (1h đến 12h) và 24h (1h đến 24h) giờ, bit6 (hình 4) xác lập hệ thống giờ. Nếu bit6=0 thì hệ thống 24h được chọn, khi đó 2 bit cao 5 và 4 dùng mã hóa chữ số hàng chục của giá trị giờ. Do giá trị lớn nhất của chữ số hàng chục trong trường hợp này là 2 (=10, nhị phân) nên 2 bit 5 và 4 là đủ để mã hóa. Nếu bit6=1 thì hệ thống 12h được chọn, với trường hợp này chỉ có bit 4 dùng mã hóa chữ số hàng chục của giờ, bit 5 (màu orange trong hình 4) chỉ buổi trong ngày, AM hoặc PM. Bit5 =0 là AM và bit5=1 là PM. Bit 7 luôn bằng 0
      chỉ là đặt chế độ thôi
      Cái này em đọc qua rồi nhưng mà để viết thành chương trình thì em ko viết dc, bác có đoạn code nào share em ngâm cứu thử cái

      Comment


      • #4
        Nguyên văn bởi mast090 Xem bài viết
        Thanh ghi giờ (HOURS): có thể nói đây là thanh ghi phức tạp nhất trong DS1307. Thanh ghi này có địa chỉ 02H. Trước hết 4-bits thấp của thanh ghi này được dùng cho chữ số hàng đơn vị của giờ. Do DS1307 hỗ trợ 2 loại hệ thống hiển thị giờ (gọi là mode) là 12h (1h đến 12h) và 24h (1h đến 24h) giờ, bit6 (hình 4) xác lập hệ thống giờ. Nếu bit6=0 thì hệ thống 24h được chọn, khi đó 2 bit cao 5 và 4 dùng mã hóa chữ số hàng chục của giá trị giờ. Do giá trị lớn nhất của chữ số hàng chục trong trường hợp này là 2 (=10, nhị phân) nên 2 bit 5 và 4 là đủ để mã hóa. Nếu bit6=1 thì hệ thống 12h được chọn, với trường hợp này chỉ có bit 4 dùng mã hóa chữ số hàng chục của giờ, bit 5 (màu orange trong hình 4) chỉ buổi trong ngày, AM hoặc PM. Bit5 =0 là AM và bit5=1 là PM. Bit 7 luôn bằng 0
        chỉ là đặt chế độ thôi
        Cái này em đọc qua rồi nhưng mà để viết thành chương trình thì em ko viết dc, bác có đoạn code nào share em ngâm cứu thử cái

        Comment


        • #5
          Tham khảo nè, từ ý tưởng ASM-->C

          Email:
          Tel: 0983.497.310

          Comment


          • #6
            t cung lam dong ho lcd roi copy cho cau ca chuong trinh xem sai dc ko !

            $include(reg51.inc)
            E bit p1.0
            RW bit p1.1
            RS bit p1.2
            SCL bit p3.0
            SDA bit p3.1
            Time_Key bit p3.2
            Alarm_Key bit p3.3
            Down bit p3.4
            Up bit p3.5
            Alarm_Port bit p3.7 ;Noi den coi bip
            BF bit p2.7
            Lcd_Port equ p2
            Flag data 20h
            Last_Read bit Flag.0
            Mode_12 bit Flag.1
            Mode_AM bit Flag.2
            Ack bit Flag.3
            _2W_Busy bit Flag.4
            OSC bit Flag.5
            Time_flag bit Flag.6
            Alarm_FLag bit Flag.7
            Alarm_On bit 08h
            Alarm_ring bit 09h
            Byte_Count data 30h
            Second data 28h
            Minute data 29h
            Hour data 2Ah
            Day data 2bh
            Date data 2ch
            Month data 2dh
            Year data 2eh
            Sig data 2Fh
            A_hour data 50h
            A_Minute data 31h
            DS1307W equ 0D0h
            DS1307R equ 0D1h
            End_Sig equ 11010000b ;dau hieu bao ket thuc chuoi lenh
            ;hoac chuoi cac ki tu hien thi
            Mode8Bit equ 38h ;2 hang, ma tran 5x7
            Disp_Off equ 08h ;Tat hien thi, DDR khong bi anh huong
            Disp_On equ 0Ch ;bat hien thi
            Cursor_On equ 0Eh ;Bat hien thi, bat con tro
            Disp_All equ 0Fh ;Bat hien thi, bat con tro, bat nhap nhay
            Clr_Disp equ 01h ;xoa DDR, AC=1, tat hien thi
            Cursor_Home equ 03h ;AC=0
            ;============Thiet lap che do====================
            Inc_Ctrl equ 06h ;Tang AC moi khi co hoat dong doc, ghi toi LCD
            Dec_Ctrl equ 04h ;Giam AC moi khi co hoat dong doc, ghi toi LCD
            L_Ctrl equ 07h ;Dich trai DDR moi khi co hoat dong doc,
            ; ghi toi LCD
            R_Ctrl equ 05h ;Dich phai DDR moi khi co hoat dong doc,
            ; ghi toi LCD
            ;========Dich con tro va hien thi====
            L_Cur equ 10h ;Giam AC, dich con tro sang trai
            R_Cur equ 14h ;Tang AC, dich con tro sang phai
            L_Disp equ 18h ;Dich toan bo DDR, hien thi sang trai,
            ;con tro cung dich theo
            R_Disp equ 1Ch ;Dich toan bo DDR, hien thi
            ; sang phai, con tro cung dich theo
            Org 00h
            jmp Main
            Org 03h
            Setb Time_Flag ;Ngat ngoai 0, chinh gio
            reti
            Org 13h
            jmp EX1ISR
            ;----------CHUONG TRINH CHINH---------------------------
            Org 30h
            Main: ; Chuong trinh chinh
            mov ie,#85h ;Cho phep ngat ngoai 0 va ngat ngoai 1
            mov r2,#01h
            call delayms
            mov dptr,#Ready ;Khoi tao LCD, 8bit, bat hien thi
            call Out_Cmd
            mov dptr,#CGRAM
            call Out_Cg
            mov r2,#15
            mov dptr,#Mes1
            call Out_Str
            call delayms
            mov a,#0C1h
            call Cmd
            mov a,#01h
            call Cmd
            mov dptr,#Mes2
            call Out_Str
            mov a,#0C1h
            call Cmd
            mov dptr,#Mes3
            call Out_Str
            mov r2,#1
            call Delayms
            mov a,#Clr_Disp
            call Cmd
            mov r2,#01h
            clr Time_Flag
            clr Alarm_Flag
            clr Alarm_ON
            clr Alarm_ring
            setb Alarm_Port
            mov a_hour,#00h
            mov a_Minute,#00h
            call Sta_Cond
            mov a,#DS1307W
            call Send
            mov a,#08h
            call send
            call Sto_Cond
            call Sta_cond
            mov a,#DS1307R
            call Send
            call recv
            call Sto_Cond
            cjne a,#'~',done
            sjmp Read_Clock
            done:
            mov a,#01h
            call Cmd
            mov Second,#00h
            mov Minute,#0
            mov Hour,#10h
            mov day,#06h
            mov date,#12h
            mov month,#06h
            mov year,#09h
            mov sig,#'~'
            mov r1,#second
            mov r0,#00h
            kk:
            mov a,#DS1307W
            call Send
            mov a,r0
            mov a,@r1
            call Send
            inc r1
            inc r0
            call Sto_Cond
            cjne r0,#9,kk
            call Sto_Cond
            ;---Doc va hien thi dong ho---------------------------------------------
            Read_Clock:
            mov r1,#28h ;bat dau cua vung nho tam ma doc duoc tu DS1307
            mov byte_count,#00h ;dem so byte
            clr Last_Read
            call Sta_cond
            mov a,#DS1307W
            call Send
            mov a,#00h ;tro den thanh ghi 00h chua DS1307
            call Send
            call Sto_cond
            call Sta_cond
            mov a,#DS1307R
            call Send
            Read_Loop:
            mov a,byte_count ;kiem tra xem phai byte cuoi cung chua
            cjne a,#07h,No_Last
            setb Last_Read
            No_Last:
            call Recv
            call Sto_Cond
            mov @r1,a
            mov a,byte_count
            cjne a,#00h,Not_First ;nhay neu khong phai dang doc
            ;thanh ghi dau tien
            clr OSC
            mov a,@r1
            jnb acc.7,Not_First ;neu bit 7 la 1, co OSC=1
            setb OSC
            clr ACC.7
            mov @r1,a
            Not_First:
            inc r1
            inc byte_count
            mov a,byte_count
            cjne a,#08h,Read_Loop
            call Sto_Cond
            call Disp_RTC
            jnb Time_Flag,Check2
            clr EA
            clr Time_Flag
            call Set_time
            clr Time_Flag
            setb EA
            jmp Read_Clock
            Check2:
            jnb Alarm_Flag,Check3
            clr Alarm_Flag
            setb Alarm_On
            call Set_Alarm
            jnb Alarm_on,Check3
            mov dptr,#ready
            call Out_Cmd
            mov dptr,#D_A_ok
            call Out_STr
            mov a,#0C5h
            call Cmd
            mov a,a_Hour
            call Disp_Val
            mov a,#':'
            call Dta
            mov a,A_Minute
            call Disp_Val
            push 2
            mov r2,#50
            call Delayms
            pop 2
            mov a,#01h
            call Cmd
            setb ea
            jmp Read_Clock
            Check3: ;kiem tra den gio hen thi do chuong
            jnb Alarm_Ring,aa
            call Alarm_Alarm
            aa: jmp Read_Clock
            EX1ISR:
            Setb Alarm_Flag ;Ngat ngoai 1, chinh gio
            jnb Alarm_ring,ii
            setb Alarm_port
            mov a,#01h
            call Cmd
            mov dptr,#D_No_Alarm
            call Out_Str
            mov r2,#50
            call Delayms
            mov a,#01
            call Cmd
            mov r2,#01
            clr Alarm_on
            clr alarm_ring
            clr Alarm_Flag
            ii: reti
            ;----Cai dat thoi gian------------------------------------
            Set_Time:
            call Bip
            jnb Time_Key,$
            mov a,#Clr_Disp
            call Cmd
            mov r2,#01h
            mov dptr,#M_Set_Time
            call Out_Str
            mov r2,#50
            call Delayms
            Set_Hour:
            mov a,#Clr_Disp
            call Cmd
            mov r2,#01h
            mov dptr,#M_Set_Hour
            call Out_Str
            mov a,#0C7h
            call Cmd
            mov a,hour
            anl a,#3Fh ;che do 24h
            mov r4,a
            call Disp_Val
            L_Set_hour1:
            mov a,#0c7h
            call cmd
            mov a,r4 ;lay so gio
            call hex_dec
            jb Up,Ch_Down_31
            call Bip
            call De_bounce
            inc a
            cjne a,#24,Conti_31
            mov a,#00
            Conti_31:
            call Dec_Hex
            call Disp_Val
            mov r4,a
            jmp L_Set_Hour1
            Ch_Down_31: ;kiem tra nhan phim DOWN
            jb Down,Ch_Time_key_31
            call Bip
            call De_bounce
            dec a
            cjne a,#0FFh,Conti_31 ;Tiep tuc giam neu gio chua ve 0
            mov a,#23
            jmp Conti_31
            Ch_Time_Key_31:
            jb Time_Key,Ch_Alarm_Key_31
            call Bip
            call De_bounce
            call dec_hex ;dua tro ve lai ma BCD de dua vao thanh ghi
            mov Hour,a
            jmp Set_Minute
            Ch_Alarm_Key_31:
            jb Alarm_Key,L_Set_Hour1
            call Bip
            call De_Bounce
            call dec_hex ;dua tro ve lai ma BCD de dua vao thanh ghi
            mov Hour,a
            jmp Set_Finish
            ;------------------ Set phut (minute) ---------------------------------------------
            Set_Minute:
            mov dptr,#Clear
            call Out_Cmd
            mov r2,#01h
            mov dptr,#M_Set_Minute
            call Out_Str
            mov a,#0c7h
            call Cmd
            mov a,Minute
            push acc
            call Disp_Val
            pop acc
            call Hex_Dec
            L_Set_Minute:
            push acc
            mov a,#0c7h
            call cmd
            pop acc
            jb up,Ch_Down_8
            call Bip
            call De_bounce
            inc a
            cjne a,#60,Conti_8
            mov a,#00
            Conti_8:
            call Dec_Hex
            call Disp_Val
            call Hex_Dec
            jmp L_Set_Minute
            Ch_Down_8:
            jb Down,Ch_Time_Key_8
            call Bip
            call De_bounce
            dec a
            cjne a,#0FFh,Conti_8
            mov a,#59
            jmp Conti_8 ;vi khi da nhan phim tang hoac giam
            ;thi deu hien thi giong nhau
            Ch_Time_Key_8:
            jb Time_Key,Ch_Alarm_Key_8
            call Bip
            call De_Bounce
            call Dec_Hex
            mov Minute,a
            jmp Set_Second
            Ch_Alarm_Key_8:
            jb Alarm_Key,L_Set_Minute
            call Bip
            call De_bounce
            call Dec_Hex
            mov Minute,a
            jmp Set_Finish
            ;------- Set giay (Second) ----------------------------------------------------
            Set_Second:
            mov dptr,#Clear
            call Out_Cmd
            mov r2,#01h
            mov dptr,#M_Set_Second
            call Out_Str
            mov a,#0c7h
            call Cmd
            mov a,Second
            push acc
            call Disp_Val
            pop acc
            call Hex_Dec
            L_Set_Second:
            push acc
            mov a,#0c7h
            call cmd
            pop acc
            jb up,Ch_Down_9
            call Bip
            call De_bounce
            inc a
            cjne a,#60,Conti_9
            mov a,#00
            Conti_9:
            call Dec_Hex
            call Disp_Val
            call Hex_Dec
            jmp L_Set_Second
            Ch_Down_9:
            jb Down,Ch_Time_Key_9
            call Bip
            call De_bounce
            dec a
            cjne a,#0FFh,Conti_9
            mov a,#59
            jmp Conti_9
            Ch_Time_Key_9:
            jb Time_Key,Ch_Alarm_Key_9
            call Bip
            call De_Bounce
            call Dec_Hex
            mov Second,a
            jmp Set_Day
            Ch_Alarm_Key_9:
            jb Alarm_Key,L_Set_Second
            call Bip
            call De_bounce
            call Dec_Hex
            mov Second,a
            jmp Set_Finish
            ;-------------------------Day (thu)------------------------------------------------
            Set_Day:
            mov dptr,#Clear
            call Out_Cmd
            mov r2,#01h
            mov dptr,#M_Set_Day
            call Out_Str
            mov a,#0C5h
            call Cmd
            mov a,Day
            call Disp_Day
            L_Set_Day:
            Push acc
            mov a,#0C5h
            call Cmd
            pop acc
            jb Up,Ch_Down_4
            call Bip
            call De_bounce
            inc a
            cjne a,#8,Conti_41
            mov a,#01
            Conti_41:
            call Disp_Day
            jmp L_Set_Day
            Ch_Down_4:
            jb Down,Ch_Time_Key_4
            call Bip
            call De_Bounce
            dec a
            jnz Conti_42 ;Neu a #0 thi tiep tuc, con khong thi set a=7
            mov a,#07
            Conti_42:
            call Disp_Day
            jmp L_Set_Day
            Ch_Time_Key_4:
            jb Time_Key,Ch_Alarm_Key_4
            call Bip
            call De_bounce
            call Dec_Hex
            mov Day,a
            jmp Set_date
            Ch_Alarm_Key_4:
            jb Alarm_Key,L_Set_Day
            call Bip
            call De_bounce
            call Dec_Hex
            mov Day,a
            jmp Set_Finish
            ;------- Set ngay (date) ----------------------------------------------------
            Set_Date:
            mov dptr,#Clear
            call Out_Cmd
            mov r2,#01h
            mov dptr,#M_Set_Date
            call Out_Str
            mov a,#0c7h
            call Cmd
            mov a,date
            push acc
            call Disp_Val
            pop acc
            call Hex_Dec
            L_Set_Date:
            push acc
            mov a,#0c7h
            call cmd
            pop acc
            jb up,Ch_Down_5
            call Bip
            call De_bounce
            inc a
            cjne a,#32,Conti_5
            mov a,#1
            Conti_5:
            call Dec_Hex
            call Disp_Val
            call Hex_Dec
            jmp L_Set_Date
            Ch_Down_5:
            jb Down,Ch_Time_Key_5
            call Bip
            call De_bounce
            dec a
            jnz Conti_5
            mov a,#31
            jmp Conti_5 ;vi khi da nhan phim tang hoac giam thi deu
            ;hien thi giong nhau
            Ch_Time_Key_5:
            jb Time_Key,Ch_Alarm_Key_5
            call Bip
            call De_Bounce
            call Dec_hex
            mov Date,a
            jmp Set_month
            Ch_Alarm_Key_5:
            jb Alarm_Key,L_Set_Date
            call Bip
            call De_bounce
            call Dec_hex
            mov Date,a
            jmp Set_Finish
            ;---------------------- Set thang (month)----------------------------------
            Set_Month:
            mov dptr,#Clear
            call Out_Cmd
            mov r2,#01h
            mov dptr,#M_Set_Month
            call Out_Str
            mov a,#0c7h
            call Cmd
            mov a,Month
            push acc
            call Disp_Val
            pop acc
            call Hex_Dec
            L_Set_Month:
            push acc
            mov a,#0c7h
            call cmd
            pop acc
            jb up,Ch_Down_6
            call Bip
            call De_bounce
            inc a
            cjne a,#13,Conti_6
            mov a,#1
            Conti_6:
            call Dec_Hex
            call Disp_Val
            call Hex_Dec
            jmp L_Set_month
            Ch_Down_6:
            jb Down,Ch_Time_Key_6
            call Bip
            call De_bounce
            dec a
            jnz Conti_6
            mov a,#12
            jmp Conti_6 ;vi khi da nhan phim tang hoac giam
            ; thi deu hien thi giong nhau
            Ch_Time_Key_6:
            jb Time_Key,Ch_Alarm_Key_6
            call Bip
            call De_Bounce
            call Dec_Hex
            mov Month,a
            jmp Set_year
            Ch_Alarm_Key_6:
            jb Alarm_Key,L_Set_Month
            call Bip
            call De_bounce
            call Dec_Hex
            mov Month,a
            jmp Set_Finish
            ;----------------------- Set year------------------------------------------
            Set_Year:
            mov dptr,#Clear
            call Out_Cmd
            mov r2,#01h
            mov dptr,#M_Set_Year
            call Out_Str
            mov a,#0c7h
            call Cmd
            mov a,Year
            push acc
            call Disp_Val
            pop acc
            call Hex_Dec
            L_Set_Year:
            push acc
            mov a,#0c7h
            call cmd
            pop acc
            jb up,Ch_Down_7
            call Bip
            call De_bounce
            inc a
            cjne a,#100,Conti_7
            mov a,#0
            Conti_7:
            call Dec_Hex
            call Disp_Val
            call Hex_Dec
            jmp L_Set_Year
            Ch_Down_7:
            jb Down,Ch_Time_Key_7
            call Bip
            call De_bounce
            dec a
            cjne a,#0FFH,Conti_7
            mov a,#99
            jmp Conti_7 ;vi khi da nhan phim tang hoac giam
            ; thi deu hien thi giong nhau
            Ch_Time_Key_7:
            jb Time_Key,Ch_Alarm_Key_7
            call Bip
            call De_Bounce
            call Dec_Hex
            mov Year,a
            jmp Set_Finish
            Ch_Alarm_Key_7:
            jb Alarm_Key,L_Set_Year
            call Bip
            call De_bounce
            call Dec_Hex
            mov Year,a
            jmp Set_Finish
            ;--- khi da thiet lap xong, ghi thoi gian vao cac thanh ghi--
            Set_Finish:
            call Sta_Cond
            mov a,#DS1307W
            call Send
            mov a,#00h
            call Send
            mov r1,#second
            Send_Set:
            mov a,@r1
            call Send
            inc r1
            cjne r1,#2Fh,Send_Set
            call Sto_Cond
            mov dptr,#clear
            call Out_cmd
            mov dptr,#Set_Ok
            call Out_Str
            mov r2,#50
            call delayms
            mov r2,#01
            mov dptr,#clear
            call Out_Cmd
            ret
            ;----------- SET ALARM ---------------------------
            Set_Alarm:
            call Bip
            clr EA ;cam ngat
            clr Alarm_flag
            mov dptr,#Ready
            call Out_Cmd
            mov r2,#01h
            mov dptr,#M_Set_Alarm
            call Out_Str
            mov r2,#50
            call Delayms
            ;------- Set gio hen ----------------------------------------------------
            A_Set_hour:
            mov A,#Clr_Disp
            call Cmd
            mov r2,#01h
            mov dptr,#M_Set_Hour
            call Out_Str
            mov a,#0c7h
            call Cmd
            mov a,A_hour
            call Disp_Val
            call Hex_Dec
            A_L_Set_Hour:
            push acc
            mov a,#0c7h
            call cmd
            pop acc
            jb up,A_Ch_Down_7
            call Bip
            call De_bounce
            inc a
            cjne a,#24,A_Conti_7
            mov a,#00
            A_Conti_7:
            call Dec_Hex
            call Disp_Val
            call Hex_Dec
            jmp A_L_Set_Hour
            A_Ch_Down_7:
            jb Down,A_Ch_Alarm_Key_7
            call Bip
            call De_bounce
            dec a
            cjne a,#0FFh,A_Conti_7
            mov a,#23
            jmp A_Conti_7 ;vi khi da nhan phim tang hoac giam
            ; thi deu hien thi giong nhau
            A_Ch_Alarm_Key_7:
            jb Alarm_Key,A_Ch_Time_Key_7
            call Bip
            call De_Bounce
            call Dec_Hex
            mov A_Hour,a
            jmp A_Set_Minute
            A_Ch_Time_Key_7:
            jb time_Key,A_L_Set_Hour
            call Bip
            call De_bounce
            jmp Cancel
            ;------- Set phut (minute) ----------------------------------------------------
            A_Set_Minute:
            mov dptr,#Ready
            call Out_Cmd
            mov r2,#01h
            mov dptr,#M_Set_Minute
            call Out_Str
            mov a,#0c7h
            call Cmd
            mov a,A_Minute
            call Disp_Val
            call Hex_Dec
            A_L_Set_Minute:
            push acc
            mov a,#0c7h
            call cmd
            pop acc
            jb up,A_Ch_Down_8
            call Bip
            call De_bounce
            inc a
            cjne a,#60,A_Conti_8
            mov a,#00
            A_Conti_8:
            call Dec_Hex
            call Disp_Val
            call Hex_Dec
            jmp A_L_Set_Minute
            A_Ch_Down_8:
            jb Down,A_Ch_Alarm_Key_8
            call Bip
            call De_bounce
            dec a
            cjne a,#0FFh,A_Conti_8
            mov a,#59
            jmp A_Conti_8 ;vi khi da nhan phim tang hoac giam
            ; thi deu hien thi giong nhau
            A_Ch_Alarm_Key_8:
            jb Alarm_Key,A_Ch_Time_Key_8
            call Bip
            call De_Bounce
            call Dec_Hex
            mov A_Minute,a
            jmp return3
            A_Ch_Time_Key_8:
            jb time_Key,A_L_Set_Minute
            call Bip
            call De_bounce
            ;----------------------- Huy bo thiet lap hen gio---------------------------------
            Cancel:
            mov dptr,#ready
            call Out_cmd
            mov dptr,#D_No_Alarm
            call Out_str
            push 2
            mov r2,#50
            call delayms
            pop 2
            mov a,#01h
            call Cmd
            clr Alarm_on
            clr Alarm_ring
            setb ea
            return3:
            ret
            ;-------Chong nay phim-------------------------------------------
            De_bounce:
            push 2
            mov r2,#10
            call delayms
            pop 2
            ret
            ;--------------coi ---------------------------------------------------
            alarm_alarm: ;Alarm Ring
            clr alarm_port
            mov r3,#0ffh
            a_loop:
            mov r4,#0ffh
            djnz r4,$
            djnz r3,a_loop
            setb alarm_port
            mov r3,#0ffh
            a_loop1:
            mov r4,#0ffh
            djnz r4,$
            djnz r3,a_loop1
            clr alarm_port
            mov r3,#0ffh
            a_loop2:
            mov r4,#0ffh
            djnz r4,$
            djnz r3,a_loop2
            setb alarm_port
            mov r3,#0ffh
            a_loop3:
            mov r4,#0ffh
            djnz r4,$
            djnz r3,a_loop3
            clr alarm_port
            mov r3,#0ffh
            a_loop4:
            mov r4,#0ffh
            djnz r4,$
            djnz r3,a_loop4
            setb alarm_port
            mov r3,#0ffh
            a_loop5:
            mov r4,#0ffh
            djnz r4,$
            djnz r3,a_loop5
            clr alarm_port
            mov r3,#0ffh
            a_loop6:
            mov r4,#0ffh
            djnz r4,$
            djnz r3,a_loop6
            setb alarm_port
            ret
            ;-----------Chuyen doi tu hexa sang dec-----------------------
            Hex_Dec:
            mov b,#10h
            div ab
            mov 70h,b
            mov b,#0ah
            mul ab
            add a,70h
            ret
            ;------Hien thi thu trong tuan-----------------
            Disp_Day:
            push acc
            cjne a,#01,D_Mon
            mov dptr,#Sun
            call Out_Str
            jmp exit
            D_Mon:
            cjne a,#02h,D_Tue
            mov dptr,#Mon
            call Out_Str
            jmp exit
            D_Tue:
            cjne a,#03h,D_Wed
            mov dptr,#Tue
            call Out_Str
            jmp exit
            D_Wed:
            cjne a,#04h,D_Thu
            mov dptr,#Wed
            call Out_Str
            jmp exit
            D_Thu:
            cjne a,#05h,D_Fri
            mov dptr,#Thu
            call Out_Str
            jmp exit
            D_Fri:
            cjne a,#06h,D_Sat
            mov dptr,#Fri
            call Out_Str
            jmp exit
            D_Sat:
            mov dptr,#Sat
            call Out_Str
            Exit:
            pop acc
            ret
            ;----Hien thi gia tri thap phan voi so dua vao la ma BCD (lay tu DS1307)-
            Disp_Val:
            push acc
            swap a
            anl a,#0Fh
            add a,#30h ;ki tu so tuong ung voi ma BCD
            call Dta
            pop acc
            push acc
            anl a,#0Fh
            add a,#30h
            call Dta
            pop acc
            ret
            ;------Gui du lieu tu VDK den DS1307 ---------
            Send:
            mov r7,#08 ;dem so bit
            L_Send:
            jnb acc.7,SDA_0 ;chuyen bit 7 cua thanh ghi A vao SDA
            setb SDA
            jmp Cont
            SDA_0:
            Clr SDA
            Cont:
            call SCL_High
            rl A ;chuyen dan bit cao den thap
            clr SCL
            djnz r7,L_Send
            setb SDA ;chot de doc xung bao nhan
            call SCL_High
            clr ACK
            jnb SDA,Ack_0
            setb ACK
            ACK_0:
            call Hold
            clr Scl
            call Hold
            ret
            ;-----Nhan du lieu tu DS1307 -------
            Recv:
            mov r7,#08h
            mov a,#00h
            L_Recv:
            call SCL_High
            mov C,SDA ;dua gia tri doc duoc 0 SDA vao co Carry
            rlc a
            clr SCL
            djnz r7,L_Recv
            jb Last_Read,N_Send_ACK ;neu khong phai la lan doc cuoi cung
            ;thi gui bit bao nhan
            clr SDA
            N_Send_ACK:
            call SCL_High
            clr SCl
            ret
            ;-------Dieu kien bat dau DS1307-------
            Sta_Cond:
            setb _2W_Busy
            clr Ack
            setb SDA
            call SCL_High
            clr SDA
            call Hold
            clr SCL
            ret
            ;-------Dieu kien dung cua DS1307--------
            Sto_Cond:
            clr SDA
            call SCL_High
            setb SDA
            clr _2W_Busy
            ret
            ;-------thiet lap duong clock len bit cao-------------
            SCL_High:
            setb SCL
            jnb SCL,$
            ret
            Out_Str: ;Xuat 1 chuoi ki tu ra man hinh LCD
            mov a,#00h
            movc a,@a+dptr
            cjne a,#End_Sig,Next_Char ;Xuat den khi ket thuc chuoi
            jmp return ;Tro lai chuong trinh
            Next_Char:
            call Dta ;Gui du lieu den LCD
            push 2 ;Cat gia tri R2, so miligiay
            call Delayms
            pop 2
            inc dptr
            jmp Out_Str
            Out_Cmd: ;Xuat 1 chuoi lenh den LCD
            mov a,#00h
            movc a,@a+dptr
            cjne a,#End_Sig,Next_Cmd
            jmp Return
            Next_Cmd:
            call cmd
            inc dptr
            jmp Out_Cmd
            Out_Cg: ;Xay dung ki tu dac biet vao CGRAM
            mov R6,#40h ;bat dau dia chi cua CGRAM tai 00h
            CG:
            mov a,r6
            call cmd
            inc r6
            mov a,#00h
            movc a,@a+dptr
            cjne a,#End_Sig,Next_Cg
            jmp return
            Next_Cg:
            call dta
            inc dptr
            jmp CG
            Cmd: ;Xuat 1 lenh den LCD
            call Check_BF
            clr RS
            clr RW
            jmp Out
            Dta: ;Xuat du lieu den LCD
            call Check_BF
            setb RS
            clr RW
            Out:
            mov Lcd_Port,a
            setb E
            clr E
            ret
            Check_BF: ;Kiem tra co ban BF
            clr RS
            setb RW
            mov Lcd_Port,#0FFh ;Chot de doc co ban
            L_Check:
            clr E
            Setb E
            jb BF,L_Check
            Return:
            ret
            ;-----Delay ms, so ms duoc dua vao R2 o chuong trinh chinh------------
            Delayms:
            push 2 ;Delay 50 ms
            mov Tmod,#01h
            L_Delay:
            mov Th0,#High(50000)
            mov Tl0,#Low(50000)
            setb tr0
            jnb tf0,$
            clr tf0
            djnz r2,L_Delay
            clr tr0
            pop 2
            ret
            ;------Thoi gian giu giua cac hoat dong cua DS1307--------------
            Hold:
            nop
            nop
            nop
            nop
            ret
            ;---Xuat ra man hinh LCD du lieu nhan duoc tu LCD---------
            Disp_RTC:
            mov a,#80h ;dua con tro ve dau hang thu 1
            call Cmd
            mov r1,#2Bh ;thu (day)
            mov a,@r1
            call Disp_Day
            mov a,#88h ;vi tri thu 9, hang 1
            call Cmd
            mov r1,#2Ch ;ngay
            mov a,@r1
            call Disp_Val
            mov a,#'/'
            call Dta
            mov r1,#2Dh ;thang
            mov a,@r1
            call Disp_Val
            mov a,#'/'
            call Dta
            mov a,year
            call Disp_Val
            jnb Alarm_on,D_Next ;hien thi cai chuong neu co hen gio
            mov a,#0C2h
            call Cmd
            mov a,#01h
            call Dta
            D_next:
            mov a,#0C7h ;vi tri thu 8,hang 2
            call Cmd
            mov r1,#2Ah ;gio
            mov a,@r1
            call Disp_Hour
            mov a,#':'
            call Dta
            mov r1,#29h ;phut
            mov a,@r1
            call Disp_Val
            mov a,#':'
            call Dta
            mov r1,#28h ;giay
            mov a,@r1
            call Disp_Val
            jnb Alarm_on,return4
            mov a,A_hour
            cjne a,hour,return4
            mov a,A_minute
            cjne a,Minute,return5
            setb Alarm_ring
            jmp return4
            return5:
            clr Alarm_ring
            return4:
            ret
            ;--------------Hien thi gio-----------------------
            Disp_hour:
            anl a,#3Fh
            call Disp_Val
            Ret
            ;------Chuyen doi tu Dec sang HEXA-----------------------
            Dec_Hex:
            mov b,#0ah
            div ab
            mov 70h,b
            mov b,#10h
            mul ab
            add a,70h
            ret
            ;-----Tao tieng Bip moi khi nhan phim---------------------
            Bip:
            push 2
            mov r2,#01
            clr alarm_port
            call delayms
            setb alarm_port
            pop 2
            ret
            ;-------Chuoi lenh va chuoi hien thi-------------------------
            Ready: db Mode8Bit,Disp_On,Clr_Disp,End_Sig
            Clear: db Clr_Disp,Disp_On,End_Sig
            CGRAM: db 00h,0Eh,15h,1dh,11h,0eh,00,00 ;hinh dong ho
            db 4h,0eh,0eh,0eh,1fh,0h,4h,0h ;hinh cai chuong
            db 0h,1h,3h,16h,1ch,8h,0h,0h ;OK
            db 1h,3h,0fh,0fh,0fh,3h,1h,0h ;SPEAKER OFF
            db 8h,10h,0h,18h,0h,10h,8h,0h ;SPEAKER ON
            db 0ah,1fh,1fh,1fh,0eh,4h,0h,0h ;hinh trai tim
            db 2h,3h,2h,0eh,1eh,0ch,0h,0h,End_Sig ;MUSIC
            Sun: db '<Sun>',End_Sig
            Mon: db '<Mon>',End_Sig
            Tue: db '<Tue>',End_Sig
            Wed: db '<Wed>',End_Sig
            Thu: db '<Thu>',End_Sig
            Fri: db '<Fri>',End_Sig
            Sat: db '<Sat>',End_Sig
            Mes1: db 'DH SP KT HY',End_Sig
            Mes2: db 'GVHD:THAY KHANH',End_Sig
            Mes3: db 'SV :HIEU_TRUONG',End_Sig
            Mes4: db 'DO AN DONG HO THUC',End_Sig
            M_Set_time: db 0,'SET TIME !',End_Sig
            M_Set_Second:
            db 'SECOND =',End_Sig
            M_Set_Minute:
            db 'MINUTE =',End_Sig
            M_Set_Hour:
            db 'HOUR =',End_Sig
            M_Set_Day: db 'DAY =',End_Sig
            M_Set_Date: db 'DATE =',End_Sig
            M_Set_Month:
            db 'MONTH =',End_Sig
            M_Set_Year:
            db 'YEAR =',End_Sig
            M_Set_Alarm:
            db 1,' SET ALARM !',End_Sig
            Set_ok: db 2,' SET TIME',End_sig
            D_No_Alarm: db 2,' ALARM OFF',End_Sig
            D_A_ok: db 2,' ALARM ON :',End_Sig
            End

            Comment


            • #7
              chuong trinh LCD

              t cung lam dong ho lcd roi copy cho cau ca chuong trinh xem sai dc ko !

              $include(reg51.inc)
              E bit p1.0
              RW bit p1.1
              RS bit p1.2
              SCL bit p3.0
              SDA bit p3.1
              Time_Key bit p3.2
              Alarm_Key bit p3.3
              Down bit p3.4
              Up bit p3.5
              Alarm_Port bit p3.7 ;Noi den coi bip
              BF bit p2.7
              Lcd_Port equ p2
              Flag data 20h
              Last_Read bit Flag.0
              Mode_12 bit Flag.1
              Mode_AM bit Flag.2
              Ack bit Flag.3
              _2W_Busy bit Flag.4
              OSC bit Flag.5
              Time_flag bit Flag.6
              Alarm_FLag bit Flag.7
              Alarm_On bit 08h
              Alarm_ring bit 09h
              Byte_Count data 30h
              Second data 28h
              Minute data 29h
              Hour data 2Ah
              Day data 2bh
              Date data 2ch
              Month data 2dh
              Year data 2eh
              Sig data 2Fh
              A_hour data 50h
              A_Minute data 31h
              DS1307W equ 0D0h
              DS1307R equ 0D1h
              End_Sig equ 11010000b ;dau hieu bao ket thuc chuoi lenh
              ;hoac chuoi cac ki tu hien thi
              Mode8Bit equ 38h ;2 hang, ma tran 5x7
              Disp_Off equ 08h ;Tat hien thi, DDR khong bi anh huong
              Disp_On equ 0Ch ;bat hien thi
              Cursor_On equ 0Eh ;Bat hien thi, bat con tro
              Disp_All equ 0Fh ;Bat hien thi, bat con tro, bat nhap nhay
              Clr_Disp equ 01h ;xoa DDR, AC=1, tat hien thi
              Cursor_Home equ 03h ;AC=0
              ;============Thiet lap che do====================
              Inc_Ctrl equ 06h ;Tang AC moi khi co hoat dong doc, ghi toi LCD
              Dec_Ctrl equ 04h ;Giam AC moi khi co hoat dong doc, ghi toi LCD
              L_Ctrl equ 07h ;Dich trai DDR moi khi co hoat dong doc,
              ; ghi toi LCD
              R_Ctrl equ 05h ;Dich phai DDR moi khi co hoat dong doc,
              ; ghi toi LCD
              ;========Dich con tro va hien thi====
              L_Cur equ 10h ;Giam AC, dich con tro sang trai
              R_Cur equ 14h ;Tang AC, dich con tro sang phai
              L_Disp equ 18h ;Dich toan bo DDR, hien thi sang trai,
              ;con tro cung dich theo
              R_Disp equ 1Ch ;Dich toan bo DDR, hien thi
              ; sang phai, con tro cung dich theo
              Org 00h
              jmp Main
              Org 03h
              Setb Time_Flag ;Ngat ngoai 0, chinh gio
              reti
              Org 13h
              jmp EX1ISR
              ;----------CHUONG TRINH CHINH---------------------------
              Org 30h
              Main: ; Chuong trinh chinh
              mov ie,#85h ;Cho phep ngat ngoai 0 va ngat ngoai 1
              mov r2,#01h
              call delayms
              mov dptr,#Ready ;Khoi tao LCD, 8bit, bat hien thi
              call Out_Cmd
              mov dptr,#CGRAM
              call Out_Cg
              mov r2,#15
              mov dptr,#Mes1
              call Out_Str
              call delayms
              mov a,#0C1h
              call Cmd
              mov a,#01h
              call Cmd
              mov dptr,#Mes2
              call Out_Str
              mov a,#0C1h
              call Cmd
              mov dptr,#Mes3
              call Out_Str
              mov r2,#1
              call Delayms
              mov a,#Clr_Disp
              call Cmd
              mov r2,#01h
              clr Time_Flag
              clr Alarm_Flag
              clr Alarm_ON
              clr Alarm_ring
              setb Alarm_Port
              mov a_hour,#00h
              mov a_Minute,#00h
              call Sta_Cond
              mov a,#DS1307W
              call Send
              mov a,#08h
              call send
              call Sto_Cond
              call Sta_cond
              mov a,#DS1307R
              call Send
              call recv
              call Sto_Cond
              cjne a,#'~',done
              sjmp Read_Clock
              done:
              mov a,#01h
              call Cmd
              mov Second,#00h
              mov Minute,#0
              mov Hour,#10h
              mov day,#06h
              mov date,#12h
              mov month,#06h
              mov year,#09h
              mov sig,#'~'
              mov r1,#second
              mov r0,#00h
              kk:
              mov a,#DS1307W
              call Send
              mov a,r0
              mov a,@r1
              call Send
              inc r1
              inc r0
              call Sto_Cond
              cjne r0,#9,kk
              call Sto_Cond
              ;---Doc va hien thi dong ho---------------------------------------------
              Read_Clock:
              mov r1,#28h ;bat dau cua vung nho tam ma doc duoc tu DS1307
              mov byte_count,#00h ;dem so byte
              clr Last_Read
              call Sta_cond
              mov a,#DS1307W
              call Send
              mov a,#00h ;tro den thanh ghi 00h chua DS1307
              call Send
              call Sto_cond
              call Sta_cond
              mov a,#DS1307R
              call Send
              Read_Loop:
              mov a,byte_count ;kiem tra xem phai byte cuoi cung chua
              cjne a,#07h,No_Last
              setb Last_Read
              No_Last:
              call Recv
              call Sto_Cond
              mov @r1,a
              mov a,byte_count
              cjne a,#00h,Not_First ;nhay neu khong phai dang doc
              ;thanh ghi dau tien
              clr OSC
              mov a,@r1
              jnb acc.7,Not_First ;neu bit 7 la 1, co OSC=1
              setb OSC
              clr ACC.7
              mov @r1,a
              Not_First:
              inc r1
              inc byte_count
              mov a,byte_count
              cjne a,#08h,Read_Loop
              call Sto_Cond
              call Disp_RTC
              jnb Time_Flag,Check2
              clr EA
              clr Time_Flag
              call Set_time
              clr Time_Flag
              setb EA
              jmp Read_Clock
              Check2:
              jnb Alarm_Flag,Check3
              clr Alarm_Flag
              setb Alarm_On
              call Set_Alarm
              jnb Alarm_on,Check3
              mov dptr,#ready
              call Out_Cmd
              mov dptr,#D_A_ok
              call Out_STr
              mov a,#0C5h
              call Cmd
              mov a,a_Hour
              call Disp_Val
              mov a,#':'
              call Dta
              mov a,A_Minute
              call Disp_Val
              push 2
              mov r2,#50
              call Delayms
              pop 2
              mov a,#01h
              call Cmd
              setb ea
              jmp Read_Clock
              Check3: ;kiem tra den gio hen thi do chuong
              jnb Alarm_Ring,aa
              call Alarm_Alarm
              aa: jmp Read_Clock
              EX1ISR:
              Setb Alarm_Flag ;Ngat ngoai 1, chinh gio
              jnb Alarm_ring,ii
              setb Alarm_port
              mov a,#01h
              call Cmd
              mov dptr,#D_No_Alarm
              call Out_Str
              mov r2,#50
              call Delayms
              mov a,#01
              call Cmd
              mov r2,#01
              clr Alarm_on
              clr alarm_ring
              clr Alarm_Flag
              ii: reti
              ;----Cai dat thoi gian------------------------------------
              Set_Time:
              call Bip
              jnb Time_Key,$
              mov a,#Clr_Disp
              call Cmd
              mov r2,#01h
              mov dptr,#M_Set_Time
              call Out_Str
              mov r2,#50
              call Delayms
              Set_Hour:
              mov a,#Clr_Disp
              call Cmd
              mov r2,#01h
              mov dptr,#M_Set_Hour
              call Out_Str
              mov a,#0C7h
              call Cmd
              mov a,hour
              anl a,#3Fh ;che do 24h
              mov r4,a
              call Disp_Val
              L_Set_hour1:
              mov a,#0c7h
              call cmd
              mov a,r4 ;lay so gio
              call hex_dec
              jb Up,Ch_Down_31
              call Bip
              call De_bounce
              inc a
              cjne a,#24,Conti_31
              mov a,#00
              Conti_31:
              call Dec_Hex
              call Disp_Val
              mov r4,a
              jmp L_Set_Hour1
              Ch_Down_31: ;kiem tra nhan phim DOWN
              jb Down,Ch_Time_key_31
              call Bip
              call De_bounce
              dec a
              cjne a,#0FFh,Conti_31 ;Tiep tuc giam neu gio chua ve 0
              mov a,#23
              jmp Conti_31
              Ch_Time_Key_31:
              jb Time_Key,Ch_Alarm_Key_31
              call Bip
              call De_bounce
              call dec_hex ;dua tro ve lai ma BCD de dua vao thanh ghi
              mov Hour,a
              jmp Set_Minute
              Ch_Alarm_Key_31:
              jb Alarm_Key,L_Set_Hour1
              call Bip
              call De_Bounce
              call dec_hex ;dua tro ve lai ma BCD de dua vao thanh ghi
              mov Hour,a
              jmp Set_Finish
              ;------------------ Set phut (minute) ---------------------------------------------
              Set_Minute:
              mov dptr,#Clear
              call Out_Cmd
              mov r2,#01h
              mov dptr,#M_Set_Minute
              call Out_Str
              mov a,#0c7h
              call Cmd
              mov a,Minute
              push acc
              call Disp_Val
              pop acc
              call Hex_Dec
              L_Set_Minute:
              push acc
              mov a,#0c7h
              call cmd
              pop acc
              jb up,Ch_Down_8
              call Bip
              call De_bounce
              inc a
              cjne a,#60,Conti_8
              mov a,#00
              Conti_8:
              call Dec_Hex
              call Disp_Val
              call Hex_Dec
              jmp L_Set_Minute
              Ch_Down_8:
              jb Down,Ch_Time_Key_8
              call Bip
              call De_bounce
              dec a
              cjne a,#0FFh,Conti_8
              mov a,#59
              jmp Conti_8 ;vi khi da nhan phim tang hoac giam
              ;thi deu hien thi giong nhau
              Ch_Time_Key_8:
              jb Time_Key,Ch_Alarm_Key_8
              call Bip
              call De_Bounce
              call Dec_Hex
              mov Minute,a
              jmp Set_Second
              Ch_Alarm_Key_8:
              jb Alarm_Key,L_Set_Minute
              call Bip
              call De_bounce
              call Dec_Hex
              mov Minute,a
              jmp Set_Finish
              ;------- Set giay (Second) ----------------------------------------------------
              Set_Second:
              mov dptr,#Clear
              call Out_Cmd
              mov r2,#01h
              mov dptr,#M_Set_Second
              call Out_Str
              mov a,#0c7h
              call Cmd
              mov a,Second
              push acc
              call Disp_Val
              pop acc
              call Hex_Dec
              L_Set_Second:
              push acc
              mov a,#0c7h
              call cmd
              pop acc
              jb up,Ch_Down_9
              call Bip
              call De_bounce
              inc a
              cjne a,#60,Conti_9
              mov a,#00
              Conti_9:
              call Dec_Hex
              call Disp_Val
              call Hex_Dec
              jmp L_Set_Second
              Ch_Down_9:
              jb Down,Ch_Time_Key_9
              call Bip
              call De_bounce
              dec a
              cjne a,#0FFh,Conti_9
              mov a,#59
              jmp Conti_9
              Ch_Time_Key_9:
              jb Time_Key,Ch_Alarm_Key_9
              call Bip
              call De_Bounce
              call Dec_Hex
              mov Second,a
              jmp Set_Day
              Ch_Alarm_Key_9:
              jb Alarm_Key,L_Set_Second
              call Bip
              call De_bounce
              call Dec_Hex
              mov Second,a
              jmp Set_Finish
              ;-------------------------Day (thu)------------------------------------------------
              Set_Day:
              mov dptr,#Clear
              call Out_Cmd
              mov r2,#01h
              mov dptr,#M_Set_Day
              call Out_Str
              mov a,#0C5h
              call Cmd
              mov a,Day
              call Disp_Day
              L_Set_Day:
              Push acc
              mov a,#0C5h
              call Cmd
              pop acc
              jb Up,Ch_Down_4
              call Bip
              call De_bounce
              inc a
              cjne a,#8,Conti_41
              mov a,#01
              Conti_41:
              call Disp_Day
              jmp L_Set_Day
              Ch_Down_4:
              jb Down,Ch_Time_Key_4
              call Bip
              call De_Bounce
              dec a
              jnz Conti_42 ;Neu a #0 thi tiep tuc, con khong thi set a=7
              mov a,#07
              Conti_42:
              call Disp_Day
              jmp L_Set_Day
              Ch_Time_Key_4:
              jb Time_Key,Ch_Alarm_Key_4
              call Bip
              call De_bounce
              call Dec_Hex
              mov Day,a
              jmp Set_date
              Ch_Alarm_Key_4:
              jb Alarm_Key,L_Set_Day
              call Bip
              call De_bounce
              call Dec_Hex
              mov Day,a
              jmp Set_Finish
              ;------- Set ngay (date) ----------------------------------------------------
              Set_Date:
              mov dptr,#Clear
              call Out_Cmd
              mov r2,#01h
              mov dptr,#M_Set_Date
              call Out_Str
              mov a,#0c7h
              call Cmd
              mov a,date
              push acc
              call Disp_Val
              pop acc
              call Hex_Dec
              L_Set_Date:
              push acc
              mov a,#0c7h
              call cmd
              pop acc
              jb up,Ch_Down_5
              call Bip
              call De_bounce
              inc a
              cjne a,#32,Conti_5
              mov a,#1
              Conti_5:
              call Dec_Hex
              call Disp_Val
              call Hex_Dec
              jmp L_Set_Date
              Ch_Down_5:
              jb Down,Ch_Time_Key_5
              call Bip
              call De_bounce
              dec a
              jnz Conti_5
              mov a,#31
              jmp Conti_5 ;vi khi da nhan phim tang hoac giam thi deu
              ;hien thi giong nhau
              Ch_Time_Key_5:
              jb Time_Key,Ch_Alarm_Key_5
              call Bip
              call De_Bounce
              call Dec_hex
              mov Date,a
              jmp Set_month
              Ch_Alarm_Key_5:
              jb Alarm_Key,L_Set_Date
              call Bip
              call De_bounce
              call Dec_hex
              mov Date,a
              jmp Set_Finish
              ;---------------------- Set thang (month)----------------------------------
              Set_Month:
              mov dptr,#Clear
              call Out_Cmd
              mov r2,#01h
              mov dptr,#M_Set_Month
              call Out_Str
              mov a,#0c7h
              call Cmd
              mov a,Month
              push acc
              call Disp_Val
              pop acc
              call Hex_Dec
              L_Set_Month:
              push acc
              mov a,#0c7h
              call cmd
              pop acc
              jb up,Ch_Down_6
              call Bip
              call De_bounce
              inc a
              cjne a,#13,Conti_6
              mov a,#1
              Conti_6:
              call Dec_Hex
              call Disp_Val
              call Hex_Dec
              jmp L_Set_month
              Ch_Down_6:
              jb Down,Ch_Time_Key_6
              call Bip
              call De_bounce
              dec a
              jnz Conti_6
              mov a,#12
              jmp Conti_6 ;vi khi da nhan phim tang hoac giam
              ; thi deu hien thi giong nhau
              Ch_Time_Key_6:
              jb Time_Key,Ch_Alarm_Key_6
              call Bip
              call De_Bounce
              call Dec_Hex
              mov Month,a
              jmp Set_year
              Ch_Alarm_Key_6:
              jb Alarm_Key,L_Set_Month
              call Bip
              call De_bounce
              call Dec_Hex
              mov Month,a
              jmp Set_Finish
              ;----------------------- Set year------------------------------------------
              Set_Year:
              mov dptr,#Clear
              call Out_Cmd
              mov r2,#01h
              mov dptr,#M_Set_Year
              call Out_Str
              mov a,#0c7h
              call Cmd
              mov a,Year
              push acc
              call Disp_Val
              pop acc
              call Hex_Dec
              L_Set_Year:
              push acc
              mov a,#0c7h
              call cmd
              pop acc
              jb up,Ch_Down_7
              call Bip
              call De_bounce
              inc a
              cjne a,#100,Conti_7
              mov a,#0
              Conti_7:
              call Dec_Hex
              call Disp_Val
              call Hex_Dec
              jmp L_Set_Year
              Ch_Down_7:
              jb Down,Ch_Time_Key_7
              call Bip
              call De_bounce
              dec a
              cjne a,#0FFH,Conti_7
              mov a,#99
              jmp Conti_7 ;vi khi da nhan phim tang hoac giam
              ; thi deu hien thi giong nhau
              Ch_Time_Key_7:
              jb Time_Key,Ch_Alarm_Key_7
              call Bip
              call De_Bounce
              call Dec_Hex
              mov Year,a
              jmp Set_Finish
              Ch_Alarm_Key_7:
              jb Alarm_Key,L_Set_Year
              call Bip
              call De_bounce
              call Dec_Hex
              mov Year,a
              jmp Set_Finish
              ;--- khi da thiet lap xong, ghi thoi gian vao cac thanh ghi--
              Set_Finish:
              call Sta_Cond
              mov a,#DS1307W
              call Send
              mov a,#00h
              call Send
              mov r1,#second
              Send_Set:
              mov a,@r1
              call Send
              inc r1
              cjne r1,#2Fh,Send_Set
              call Sto_Cond
              mov dptr,#clear
              call Out_cmd
              mov dptr,#Set_Ok
              call Out_Str
              mov r2,#50
              call delayms
              mov r2,#01
              mov dptr,#clear
              call Out_Cmd
              ret
              ;----------- SET ALARM ---------------------------
              Set_Alarm:
              call Bip
              clr EA ;cam ngat
              clr Alarm_flag
              mov dptr,#Ready
              call Out_Cmd
              mov r2,#01h
              mov dptr,#M_Set_Alarm
              call Out_Str
              mov r2,#50
              call Delayms
              ;------- Set gio hen ----------------------------------------------------
              A_Set_hour:
              mov A,#Clr_Disp
              call Cmd
              mov r2,#01h
              mov dptr,#M_Set_Hour
              call Out_Str
              mov a,#0c7h
              call Cmd
              mov a,A_hour
              call Disp_Val
              call Hex_Dec
              A_L_Set_Hour:
              push acc
              mov a,#0c7h
              call cmd
              pop acc
              jb up,A_Ch_Down_7
              call Bip
              call De_bounce
              inc a
              cjne a,#24,A_Conti_7
              mov a,#00
              A_Conti_7:
              call Dec_Hex
              call Disp_Val
              call Hex_Dec
              jmp A_L_Set_Hour
              A_Ch_Down_7:
              jb Down,A_Ch_Alarm_Key_7
              call Bip
              call De_bounce
              dec a
              cjne a,#0FFh,A_Conti_7
              mov a,#23
              jmp A_Conti_7 ;vi khi da nhan phim tang hoac giam
              ; thi deu hien thi giong nhau
              A_Ch_Alarm_Key_7:
              jb Alarm_Key,A_Ch_Time_Key_7
              call Bip
              call De_Bounce
              call Dec_Hex
              mov A_Hour,a
              jmp A_Set_Minute
              A_Ch_Time_Key_7:
              jb time_Key,A_L_Set_Hour
              call Bip
              call De_bounce
              jmp Cancel
              ;------- Set phut (minute) ----------------------------------------------------
              A_Set_Minute:
              mov dptr,#Ready
              call Out_Cmd
              mov r2,#01h
              mov dptr,#M_Set_Minute
              call Out_Str
              mov a,#0c7h
              call Cmd
              mov a,A_Minute
              call Disp_Val
              call Hex_Dec
              A_L_Set_Minute:
              push acc
              mov a,#0c7h
              call cmd
              pop acc
              jb up,A_Ch_Down_8
              call Bip
              call De_bounce
              inc a
              cjne a,#60,A_Conti_8
              mov a,#00
              A_Conti_8:
              call Dec_Hex
              call Disp_Val
              call Hex_Dec
              jmp A_L_Set_Minute
              A_Ch_Down_8:
              jb Down,A_Ch_Alarm_Key_8
              call Bip
              call De_bounce
              dec a
              cjne a,#0FFh,A_Conti_8
              mov a,#59
              jmp A_Conti_8 ;vi khi da nhan phim tang hoac giam
              ; thi deu hien thi giong nhau
              A_Ch_Alarm_Key_8:
              jb Alarm_Key,A_Ch_Time_Key_8
              call Bip
              call De_Bounce
              call Dec_Hex
              mov A_Minute,a
              jmp return3
              A_Ch_Time_Key_8:
              jb time_Key,A_L_Set_Minute
              call Bip
              call De_bounce
              ;----------------------- Huy bo thiet lap hen gio---------------------------------
              Cancel:
              mov dptr,#ready
              call Out_cmd
              mov dptr,#D_No_Alarm
              call Out_str
              push 2
              mov r2,#50
              call delayms
              pop 2
              mov a,#01h
              call Cmd
              clr Alarm_on
              clr Alarm_ring
              setb ea
              return3:
              ret
              ;-------Chong nay phim-------------------------------------------
              De_bounce:
              push 2
              mov r2,#10
              call delayms
              pop 2
              ret
              ;--------------coi ---------------------------------------------------
              alarm_alarm: ;Alarm Ring
              clr alarm_port
              mov r3,#0ffh
              a_loop:
              mov r4,#0ffh
              djnz r4,$
              djnz r3,a_loop
              setb alarm_port
              mov r3,#0ffh
              a_loop1:
              mov r4,#0ffh
              djnz r4,$
              djnz r3,a_loop1
              clr alarm_port
              mov r3,#0ffh
              a_loop2:
              mov r4,#0ffh
              djnz r4,$
              djnz r3,a_loop2
              setb alarm_port
              mov r3,#0ffh
              a_loop3:
              mov r4,#0ffh
              djnz r4,$
              djnz r3,a_loop3
              clr alarm_port
              mov r3,#0ffh
              a_loop4:
              mov r4,#0ffh
              djnz r4,$
              djnz r3,a_loop4
              setb alarm_port
              mov r3,#0ffh
              a_loop5:
              mov r4,#0ffh
              djnz r4,$
              djnz r3,a_loop5
              clr alarm_port
              mov r3,#0ffh
              a_loop6:
              mov r4,#0ffh
              djnz r4,$
              djnz r3,a_loop6
              setb alarm_port
              ret
              ;-----------Chuyen doi tu hexa sang dec-----------------------
              Hex_Dec:
              mov b,#10h
              div ab
              mov 70h,b
              mov b,#0ah
              mul ab
              add a,70h
              ret
              ;------Hien thi thu trong tuan-----------------
              Disp_Day:
              push acc
              cjne a,#01,D_Mon
              mov dptr,#Sun
              call Out_Str
              jmp exit
              D_Mon:
              cjne a,#02h,D_Tue
              mov dptr,#Mon
              call Out_Str
              jmp exit
              D_Tue:
              cjne a,#03h,D_Wed
              mov dptr,#Tue
              call Out_Str
              jmp exit
              D_Wed:
              cjne a,#04h,D_Thu
              mov dptr,#Wed
              call Out_Str
              jmp exit
              D_Thu:
              cjne a,#05h,D_Fri
              mov dptr,#Thu
              call Out_Str
              jmp exit
              D_Fri:
              cjne a,#06h,D_Sat
              mov dptr,#Fri
              call Out_Str
              jmp exit
              D_Sat:
              mov dptr,#Sat
              call Out_Str
              Exit:
              pop acc
              ret
              ;----Hien thi gia tri thap phan voi so dua vao la ma BCD (lay tu DS1307)-
              Disp_Val:
              push acc
              swap a
              anl a,#0Fh
              add a,#30h ;ki tu so tuong ung voi ma BCD
              call Dta
              pop acc
              push acc
              anl a,#0Fh
              add a,#30h
              call Dta
              pop acc
              ret
              ;------Gui du lieu tu VDK den DS1307 ---------
              Send:
              mov r7,#08 ;dem so bit
              L_Send:
              jnb acc.7,SDA_0 ;chuyen bit 7 cua thanh ghi A vao SDA
              setb SDA
              jmp Cont
              SDA_0:
              Clr SDA
              Cont:
              call SCL_High
              rl A ;chuyen dan bit cao den thap
              clr SCL
              djnz r7,L_Send
              setb SDA ;chot de doc xung bao nhan
              call SCL_High
              clr ACK
              jnb SDA,Ack_0
              setb ACK
              ACK_0:
              call Hold
              clr Scl
              call Hold
              ret
              ;-----Nhan du lieu tu DS1307 -------
              Recv:
              mov r7,#08h
              mov a,#00h
              L_Recv:
              call SCL_High
              mov C,SDA ;dua gia tri doc duoc 0 SDA vao co Carry
              rlc a
              clr SCL
              djnz r7,L_Recv
              jb Last_Read,N_Send_ACK ;neu khong phai la lan doc cuoi cung
              ;thi gui bit bao nhan
              clr SDA
              N_Send_ACK:
              call SCL_High
              clr SCl
              ret
              ;-------Dieu kien bat dau DS1307-------
              Sta_Cond:
              setb _2W_Busy
              clr Ack
              setb SDA
              call SCL_High
              clr SDA
              call Hold
              clr SCL
              ret
              ;-------Dieu kien dung cua DS1307--------
              Sto_Cond:
              clr SDA
              call SCL_High
              setb SDA
              clr _2W_Busy
              ret
              ;-------thiet lap duong clock len bit cao-------------
              SCL_High:
              setb SCL
              jnb SCL,$
              ret
              Out_Str: ;Xuat 1 chuoi ki tu ra man hinh LCD
              mov a,#00h
              movc a,@a+dptr
              cjne a,#End_Sig,Next_Char ;Xuat den khi ket thuc chuoi
              jmp return ;Tro lai chuong trinh
              Next_Char:
              call Dta ;Gui du lieu den LCD
              push 2 ;Cat gia tri R2, so miligiay
              call Delayms
              pop 2
              inc dptr
              jmp Out_Str
              Out_Cmd: ;Xuat 1 chuoi lenh den LCD
              mov a,#00h
              movc a,@a+dptr
              cjne a,#End_Sig,Next_Cmd
              jmp Return
              Next_Cmd:
              call cmd
              inc dptr
              jmp Out_Cmd
              Out_Cg: ;Xay dung ki tu dac biet vao CGRAM
              mov R6,#40h ;bat dau dia chi cua CGRAM tai 00h
              CG:
              mov a,r6
              call cmd
              inc r6
              mov a,#00h
              movc a,@a+dptr
              cjne a,#End_Sig,Next_Cg
              jmp return
              Next_Cg:
              call dta
              inc dptr
              jmp CG
              Cmd: ;Xuat 1 lenh den LCD
              call Check_BF
              clr RS
              clr RW
              jmp Out
              Dta: ;Xuat du lieu den LCD
              call Check_BF
              setb RS
              clr RW
              Out:
              mov Lcd_Port,a
              setb E
              clr E
              ret
              Check_BF: ;Kiem tra co ban BF
              clr RS
              setb RW
              mov Lcd_Port,#0FFh ;Chot de doc co ban
              L_Check:
              clr E
              Setb E
              jb BF,L_Check
              Return:
              ret
              ;-----Delay ms, so ms duoc dua vao R2 o chuong trinh chinh------------
              Delayms:
              push 2 ;Delay 50 ms
              mov Tmod,#01h
              L_Delay:
              mov Th0,#High(50000)
              mov Tl0,#Low(50000)
              setb tr0
              jnb tf0,$
              clr tf0
              djnz r2,L_Delay
              clr tr0
              pop 2
              ret
              ;------Thoi gian giu giua cac hoat dong cua DS1307--------------
              Hold:
              nop
              nop
              nop
              nop
              ret
              ;---Xuat ra man hinh LCD du lieu nhan duoc tu LCD---------
              Disp_RTC:
              mov a,#80h ;dua con tro ve dau hang thu 1
              call Cmd
              mov r1,#2Bh ;thu (day)
              mov a,@r1
              call Disp_Day
              mov a,#88h ;vi tri thu 9, hang 1
              call Cmd
              mov r1,#2Ch ;ngay
              mov a,@r1
              call Disp_Val
              mov a,#'/'
              call Dta
              mov r1,#2Dh ;thang
              mov a,@r1
              call Disp_Val
              mov a,#'/'
              call Dta
              mov a,year
              call Disp_Val
              jnb Alarm_on,D_Next ;hien thi cai chuong neu co hen gio
              mov a,#0C2h
              call Cmd
              mov a,#01h
              call Dta
              D_next:
              mov a,#0C7h ;vi tri thu 8,hang 2
              call Cmd
              mov r1,#2Ah ;gio
              mov a,@r1
              call Disp_Hour
              mov a,#':'
              call Dta
              mov r1,#29h ;phut
              mov a,@r1
              call Disp_Val
              mov a,#':'
              call Dta
              mov r1,#28h ;giay
              mov a,@r1
              call Disp_Val
              jnb Alarm_on,return4
              mov a,A_hour
              cjne a,hour,return4
              mov a,A_minute
              cjne a,Minute,return5
              setb Alarm_ring
              jmp return4
              return5:
              clr Alarm_ring
              return4:
              ret
              ;--------------Hien thi gio-----------------------
              Disp_hour:
              anl a,#3Fh
              call Disp_Val
              Ret
              ;------Chuyen doi tu Dec sang HEXA-----------------------
              Dec_Hex:
              mov b,#0ah
              div ab
              mov 70h,b
              mov b,#10h
              mul ab
              add a,70h
              ret
              ;-----Tao tieng Bip moi khi nhan phim---------------------
              Bip:
              push 2
              mov r2,#01
              clr alarm_port
              call delayms
              setb alarm_port
              pop 2
              ret
              ;-------Chuoi lenh va chuoi hien thi-------------------------
              Ready: db Mode8Bit,Disp_On,Clr_Disp,End_Sig
              Clear: db Clr_Disp,Disp_On,End_Sig
              CGRAM: db 00h,0Eh,15h,1dh,11h,0eh,00,00 ;hinh dong ho
              db 4h,0eh,0eh,0eh,1fh,0h,4h,0h ;hinh cai chuong
              db 0h,1h,3h,16h,1ch,8h,0h,0h ;OK
              db 1h,3h,0fh,0fh,0fh,3h,1h,0h ;SPEAKER OFF
              db 8h,10h,0h,18h,0h,10h,8h,0h ;SPEAKER ON
              db 0ah,1fh,1fh,1fh,0eh,4h,0h,0h ;hinh trai tim
              db 2h,3h,2h,0eh,1eh,0ch,0h,0h,End_Sig ;MUSIC
              Sun: db '<Sun>',End_Sig
              Mon: db '<Mon>',End_Sig
              Tue: db '<Tue>',End_Sig
              Wed: db '<Wed>',End_Sig
              Thu: db '<Thu>',End_Sig
              Fri: db '<Fri>',End_Sig
              Sat: db '<Sat>',End_Sig
              Mes1: db 'DH SP KT HY',End_Sig
              Mes2: db 'GVHD:THAY KHANH',End_Sig
              Mes3: db 'SV :HIEU_TRUONG',End_Sig
              Mes4: db 'DO AN DONG HO THUC',End_Sig
              M_Set_time: db 0,'SET TIME !',End_Sig
              M_Set_Second:
              db 'SECOND =',End_Sig
              M_Set_Minute:
              db 'MINUTE =',End_Sig
              M_Set_Hour:
              db 'HOUR =',End_Sig
              M_Set_Day: db 'DAY =',End_Sig
              M_Set_Date: db 'DATE =',End_Sig
              M_Set_Month:
              db 'MONTH =',End_Sig
              M_Set_Year:
              db 'YEAR =',End_Sig
              M_Set_Alarm:
              db 1,' SET ALARM !',End_Sig
              Set_ok: db 2,' SET TIME',End_sig
              D_No_Alarm: db 2,' ALARM OFF',End_Sig
              D_A_ok: db 2,' ALARM ON :',End_Sig
              End

              Comment


              • #8
                t k bit cach up tai lieu len . neu can j mail cho mik nguyenvanhieu2403@gmail.com

                Comment


                • #9
                  Cám ơn các bác nhiều, code asm em ngẫm mãi vẫn ko thấm dc

                  Comment

                  Về tác giả

                  Collapse

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

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

                  Collapse

                  Đang tải...
                  X