Xin chào các bác !
Em đang làm project : " Đồng hồ số hiển thị gian + nhiệt độ + báo thức " dùng RTC DS1307 và LM35.
Hiện tại em đã hoàn thành code của chương trình này bằng ASM.
Chương trình có các chức năng cơ bản như sau:
1. Hiển thị thời gian, ngày tháng năm theo lịch dương ( Bằng cách quét LED 7 thanh)
2. Hiển thị nhiệt độ ( trong phần này có phần xử lý số liệu trích mẫu nhiệt độ để đưa ra kết quả)
3. Cài đặt thời gian và báo thức bằng phím bấm
Mới học vi điều khiển được một thời gian, chưa có nhiều kinh nghiệm nên có nhiều sai sót.
Mong các bác " check " lỗi giúp em với ạ.
Link Code+Simulation : RTC_LM_FIXED_N_5_9.rar
P/S: Khi làm project này em có tham khảo code đồng hồ số của bác PTCOM ở topic http://www.dientuvietnam.net/forums/...19/index2.html
Em đang làm project : " Đồng hồ số hiển thị gian + nhiệt độ + báo thức " dùng RTC DS1307 và LM35.
Hiện tại em đã hoàn thành code của chương trình này bằng ASM.
Chương trình có các chức năng cơ bản như sau:
1. Hiển thị thời gian, ngày tháng năm theo lịch dương ( Bằng cách quét LED 7 thanh)
2. Hiển thị nhiệt độ ( trong phần này có phần xử lý số liệu trích mẫu nhiệt độ để đưa ra kết quả)
3. Cài đặt thời gian và báo thức bằng phím bấm
Code:
; KHAI BAO BIEN BAN DAU ;==================================== ;==== DINH NGHIA CAC THANH GHI ====== ;==================================== FLAG_MODE DATA 30H FLAG_SET DATA 31H FLAG_ALARM DATA 32H PERCENT_SEC DATA 33H VARI_JUMP DATA 34H PULSE_JUMP DATA 35H TEMP DATA 36H SEC DATA 37H MIN DATA 38H HOUR DATA 39H DAY DATA 3AH DATE DATA 3BH MONTH DATA 3CH YEAR DATA 3DH MIN_ALARM DATA 3EH HOUR_ALARM DATA 3FH D0_SEC DATA 40H; Don vi cua giay D1_SEC DATA 41H; Hang chuc cua giay D0_MIN DATA 42H D1_MIN DATA 43H D0_HOUR DATA 44H D1_HOUR DATA 45H D0_DATE DATA 46H D1_DATE DATA 47H D0_MONTH DATA 48H D1_MONTH DATA 49H D0_YEAR DATA 4AH D1_YEAR DATA 4BH D0_TEMP DATA 4CH D1_TEMP DATA 4DH D2_TEMP DATA 4EH D0_MIN_ALARM DATA 4FH D1_MIN_ALARM DATA 50H D0_HOUR_ALARM DATA 51H D1_HOUR_ALARM DATA 52H ;==================================== ;======= DINH NGHIA CAC BIT ========= ;==================================== CS BIT P2.4 READ BIT P2.5 WRITE BIT P2.6 IR BIT P3.1 REST_LED BIT P3.0 SW_1 BIT P3.2 SW_2 BIT P3.3 SW_3 BIT P3.4 ALARM_BUZZ BIT P3.5 SCL BIT P3.6 SDA BIT P3.7 ;=================================== ;====== DINH NGHIA CA HANG SO======= ;=================================== LED_DATA EQU P0 BYTE_W EQU 11010000B; BYTE 0D0H DE THONG BAO THAO TAC GHI BYTE_R EQU 11010001B; BYTE 0D01 DE THONG BAO THAO TAC DOC ADD_LOW EQU 53H DATA_DS EQU 54H ;======================================== ORG 0000H LJMP INITIAL_ALL ;======================================== ORG 000BH LJMP INTERRUPT_TIMER ;======================================== ORG 0030H INITIAL_ALL: ;================== KICH HOAT CHUONG TRINH NGAT MOV IE,#10001010B ;================= KICH HOAT TIMER MOV TMOD,#11H MOV TL0,#00H MOV TH0,#0DCH SETB TR0 ;================ MOV PERCENT_SEC ,#0 MOV VARI_JUMP ,#0 MOV PULSE_JUMP ,#0 MOV TEMP ,#0 MOV SEC ,#0 MOV MIN ,#0 MOV HOUR ,#0 MOV DAY ,#0 MOV DATE ,#0 MOV MONTH ,#0 MOV YEAR ,#0 MOV FLAG_SET ,#0 CLR ALARM_BUZZ READ_INTIAL_ALARM: ;============ Read FLAG_ALARM tu DS1307 MOV ADD_LOW,#08H LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV FLAG_ALARM,A LCALL I2C_STOP ;=========== Read MIN_ALARM tu DS1307 MOV ADD_LOW,#09H LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV MIN_ALARM,A LCALL I2C_STOP ;=========== Read HOUR_ALARM tu DS1307 MOV ADD_LOW,#0AH LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV HOUR_ALARM,A LCALL I2C_STOP ;=========================================== MAIN: ;=========================================== Chuong Trinh tai day MOV A,FLAG_MODE CJNE A,#0,UPDATE_TIME_ALARM LCALL READ_DATA_DS1307 LCALL CHECK_ALARM_BUZZ LCALL DISPLAY_LED LCALL READ_DATA_ADC0804 LCALL DISPLAY_LED UPDATE_TIME_ALARM: ;================= Flag MODE khac khong thi dang o che do cai dat gio va ba'o thu'c LCALL SCAN_KEY SJMP MAIN ;========================================== 1. CHUONG TRINH DOC DU LIEU TU RTC DS1307 ================ READ_DATA_DS1307: ;================================================= =READS SECONDS MOV ADD_LOW,#00H LCALL READ_BYTE MOV A,DATA_DS LCALL BCD_HEX MOV SEC,A LCALL I2C_STOP ;================================================= =READS MINUTES MOV ADD_LOW,#01H LCALL READ_BYTE MOV A,DATA_DS LCALL BCD_HEX MOV MIN,A LCALL I2C_STOP ;================================================= =READS HOURS MOV ADD_LOW,#02H LCALL READ_BYTE MOV A,DATA_DS LCALL BCD_HEX MOV HOUR,A LCALL I2C_STOP ;================================================= = READ DAY MOV ADD_LOW,#03H LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV DAY,A LCALL I2C_STOP ;================================================= = READ DATE MOV ADD_LOW,#04H LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV DATE,A LCALL I2C_STOP ;================================================= = READ MONTH MOV ADD_LOW,#05H LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV MONTH,A LCALL I2C_STOP ;================================================= = READ YEAR MOV ADD_LOW,#06H LCALL READ_BYTE MOV A,DATA_DS CALL BCD_HEX MOV YEAR,A LCALL I2C_STOP RET ;========================================================================= ;=================== CHUONG TRINH CON GIAO TIEP I2C VOI DS1307 =========== ;===================== 1.1 CHUONG TRINH CON TAO " STOP CONDITIONAL " TRONG I2C ============= I2C_STOP: CLR SDA SETB SCL NOP SETB SDA RET ;==================== 1.2 CHUONG TRINH CON DE GHI DATA TRONG I2C ============== WRITE_BYTE: CLR SDA ;START BIT CLR SCL MOV A,#BYTE_W ;SEND CONTROL BYTE LCALL LOOP_WRITE_BYTE SETB SDA SETB SCL JB SDA,WRITE_BYTE ;LOOP UNTIL BUSY CLR SCL MOV A,ADD_LOW ;SEND ADDRESS LOW LCALL LOOP_WRITE_BYTE SETB SDA SETB SCL JB SDA,WRITE_BYTE ;LOOP UNTIL BUSY CLR SCL MOV A,DATA_DS ;SEND DATA LCALL LOOP_WRITE_BYTE SETB SDA SETB SCL JB SDA,WRITE_BYTE ;LOOP UNTIL BUSY CLR SDA CLR SCL SETB SCL ;STOP BIT SETB SDA RET ;========================= CHUONG TRINH CON DE GUI 1 BYTE TRONG I2C ======================= LOOP_WRITE_BYTE: PUSH 02H MOV R2,#08H LOOP_SEND: RLC A MOV SDA,C SETB SCL CLR SCL DJNZ R2,LOOP_SEND POP 02H RET ;======================== 1.3 CHUONG TRINH CON DE DOC DATA TRONG I2C ============================ READ_BYTE: CLR SDA ;START BIT CLR SCL MOV A,#BYTE_W ;SEND CONTROL BYTE LCALL LOOP_WRITE_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;LOOP UNTIL BUSY CLR SCL MOV A,ADD_LOW ;SEND ADDRESS LOW LCALL LOOP_WRITE_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;LOOP UNTIL BUSY CLR SCL SETB SCL SETB SDA CLR SDA ;START BIT CLR SCL MOV A,#BYTE_R ;SEND CONTROL BYTE LCALL LOOP_WRITE_BYTE SETB SDA SETB SCL JB SDA,READ_BYTE ;LOOP UNTIL BUSY CLR SCL LCALL LOOP_READ SETB SDA SETB SCL CLR SCL SETB SCL ;STOP BIT SETB SDA RET ;========================= CHUONG TRINH CON DE DOC 1 BYTE TRONG I2C ======================= LOOP_READ: PUSH 02H MOV R2,#08H LOOP_READ1: SETB SCL MOV C,SDA CLR SCL RLC A DJNZ R2,LOOP_READ1 MOV DATA_DS,A POP 02H RET ;===================================== 2. Chuong trinh kiem tra bao thuc =================== CHECK_ALARM_BUZZ: MOV A,FLAG_ALARM CJNE A,#0,CHECK_HOUR_ALARM_BUZZ LJMP ALARM_NOT_BUZZ CHECK_HOUR_ALARM_BUZZ: MOV A,HOUR_ALARM CJNE A,HOUR,EXIT_CHECK_ALARM_BUZZ CHECK_MIN_ALARM_BUZZ: MOV A,MIN_ALARM CJNE A,MIN,EXIT_CHECK_ALARM_BUZZ SETB ALARM_BUZZ SJMP EXIT_CHECK_ALARM_BUZZ ALARM_NOT_BUZZ: CLR ALARM_BUZZ EXIT_CHECK_ALARM_BUZZ: RET ;======================== 3.1 CHUONG TRINH DOC DU LIEU TU ADC 0804 ============================== READ_DATA_ADC0804: LCALL NHAPSOLIEU LCALL XULYSOLIEU ;======================== CHUONG TRINH NAY SU DUNG RAM TU VUNG 58H DEN 6FH DE TINH TOAN ========= ;=============================3.1.1 NHAP 8 MAU SO LIEU VAO RAM=================================== NHAPSOLIEU: MOV R0,#58H LOOPNSL: ACALL ADC MOV @R0,A INC R0 CJNE R0,#60H,LOOPNSL RET ;================================ CHUONG TRINH CON DOC DU LIEU TU ADC0804============== ADC: ACALL CONVERT ACALL READADC RET ;================================= CONVERT: SETB IR CLR CS CLR WRITE NOP SETB WRITE SETB CS JB IR,$ RET ;================================= READADC: CLR CS CLR READ SETB READ MOV A,P1 CLR READ SETB CS RET ;======================== 3.2 CHUONG TRINH XU LIEU DU LIEU TREN 8051 ============================== XULYSOLIEU: LCALL TINHTBC LCALL XUATSOLIEU RET ;=======================3.2.1 TINH TRUNG BINH CONG ============================== TINHTBC: ACALL CHIATUNGMAU ACALL TINHTHUONGVAPHANDU RET ;======================= CHIA TUNG MAU SO LIEU CHO 8 =================== CHIATUNGMAU: MOV A,#58H MOV R0,#60H ; BAT DAU NGAN NHO CHUA THUONG MOV R1,#68H ; BAT DAU NGAN NHO CHUA SO DU LOOPPC: MOV R7,A MOV B,R0 MOV R0,A MOV A,@R0 MOV R0,B MOV B,#8 DIV AB MOV @R0,A MOV A,B MOV @R1,A MOV A,R7 INC A INC R0 INC R1 CJNE R0,#68H,LOOPPC RET ;===================TINH THUONG VA PHAN DU THUC SU PHEP CHIA TRUNG BINH CONG ============== TINHTHUONGVAPHANDU: ACALL TINHPHANDUCUATRUNGBINHCONG8SO ACALL TINHTHUONGCUATRUNGBINHCONG8SO RET ;========================================== TINHPHANDUCUATRUNGBINHCONG8SO: MOV R1,#68H MOV A,#0 MOV R5,#0 MOV R7,#8 LOOPTPD: MOV R6,A MOV A,@R1 ADD A,R5 MOV R5,A MOV A,R6 INC R1 CJNE R1,#70H,LOOPTPD MOV A,R5 MOV B,#8 DIV AB MOV R3,A; PHAN THEM VAO THUONG THUC SU CUA TRUNG BINH CONG MOV R4,B; PHAN DU THUC SU CUA TRUNG BINH CONG RET ;======================================== TINHTHUONGCUATRUNGBINHCONG8SO: MOV R0,#60H MOV R7,#0 LOOPTT: MOV A,@R0 ADD A,R7 MOV R7,A INC R0 CJNE R0,#68H,LOOPTT MOV A,R7 ADD A,R3 MOV R3,A RET ;=================== 3.2.2 XUAT SO LIEU ===================== XUATSOLIEU: LCALL SAUDAUPHAY LCALL HANGTRAM LCALL HANGCHUCVADONVI RET ;==================================== SAUDAUPHAY: MOV A,R4 MOV B,#10 MUL AB MOV B,#8 DIV AB MOV D0_TEMP,A MOV R5,B RET HANGTRAM: MOV A,R3 MOV B,#100 DIV AB MOV R1,A MOV R2,B RET HANGCHUCVADONVI: MOV A,R2 MOV B,#10 DIV AB MOV D2_TEMP,A MOV D1_TEMP,B RET ;================================================================== ;==================== 4.CHUONG TRINH HIEN THI ===================== DISPLAY_LED: MOV A,FLAG_MODE CHECK_FLAG_MODE_DISPLAY_0: ;========================== Kiem tra xem flag_mode dang o che do nao CJNE A,#0,CHECK_FLAG_MODE_DISPLAY_1 LCALL DISPLAY_NORMAL LJMP EXIT_DISPLAY_LED CHECK_FLAG_MODE_DISPLAY_1: CJNE A,#1,CHECK_FLAG_MODE_DISPLAY_2 LJMP DISPLAY_BLINK_TIME CHECK_FLAG_MODE_DISPLAY_2: LJMP DISPLAY_BLINK_ALARM ;======================================== MODE DISPLAY TIME DISPLAY_BLINK_TIME: ;========================== Che do hien thi thoi gian co nha'y hoac khong nha'y MOV A,VARI_JUMP CJNE A,#0,CHECK_DISPLAY_BLINK_TIME LCALL DISPLAY_NORMAL AJMP EXIT_DISPLAY_LED CHECK_DISPLAY_BLINK_TIME: CALL BLINK_TIME JMP DISPLAY_BLINK_TIME EXIT_DISPLAY_LED : RET ;==================================== MODE DISPLAY ALARM DISPLAY_BLINK_ALARM: MOV A,VARI_JUMP CJNE A,#0,CHECK_DISPLAY_BLINK_ALARM LJMP DISPLAY_ALARM_NORMAL CHECK_DISPLAY_BLINK_ALARM: LCALL BLINK_ALARM LJMP DISPLAY_BLINK_ALARM ;========================================= CHUONG TRINH CON NHAY MODE TIME============= DISPLAY_NORMAL: ;=================== Hien thi thoi gian khong nha'y LCALL DISPLAY_TEMP LCALL DISPLAY_SEC LCALL DISPLAY_MIN LCALL DISPLAY_HOUR LCALL DISPLAY_DAY LCALL DISPLAY_DATE LCALL DISPLAY_MONTH LCALL DISPLAY_YEAR RET ;================================= BLINK_TIME: ;================== Hien thi thoi gian co nha'y bang cach kiem tra gia tri flag_set MOV A,FLAG_SET ;================== NHAY 2 LED PHUT ======= CJNE A,#1,CHECK_FLAG_BLINK_1 LCALL DISPLAY_SEC ;===== MOV P2,#5 LCALL DISPLAY_DOT_LED MOV P2,#6 LCALL DISPLAY_DOT_LED ;===== LCALL DISPLAY_HOUR LCALL DISPLAY_DAY LCALL DISPLAY_DATE LCALL DISPLAY_MONTH LCALL DISPLAY_YEAR LCALL DISPLAY_TEMP ;================== NHAY 2 LED GIO ======= CHECK_FLAG_BLINK_1 : MOV A,FLAG_SET CJNE A,#2,CHECK_FLAG_BLINK_2 LCALL DISPLAY_SEC LCALL DISPLAY_MIN ;===== MOV P2,#7 LCALL DISPLAY_DOT_LED ;===== LCALL DISPLAY_DAY LCALL DISPLAY_DATE LCALL DISPLAY_MONTH LCALL DISPLAY_YEAR LCALL DISPLAY_TEMP ;================== NHAY LED THU ======= CHECK_FLAG_BLINK_2: MOV A,FLAG_SET CJNE A,#3,CHECK_FLAG_BLINK_3 LCALL DISPLAY_SEC LCALL DISPLAY_MIN LCALL DISPLAY_HOUR LCALL DISPLAY_DATE LCALL DISPLAY_MONTH LCALL DISPLAY_YEAR LCALL DISPLAY_TEMP ;================== NHAY 2 LED NGAY ======= CHECK_FLAG_BLINK_3: MOV A,FLAG_SET CJNE A,#4,CHECK_FLAG_BLINK_4 LCALL DISPLAY_SEC LCALL DISPLAY_MIN LCALL DISPLAY_HOUR LCALL DISPLAY_DAY ;===== MOV P2,#13 LCALL DISPLAY_DOT_LED ;===== LCALL DISPLAY_MONTH LCALL DISPLAY_YEAR LCALL DISPLAY_TEMP ;================== NHAY 2 LED THANG ======= CHECK_FLAG_BLINK_4: MOV A,FLAG_SET CJNE A,#5,CHECK_FLAG_BLINK_5 LCALL DISPLAY_SEC LCALL DISPLAY_MIN LCALL DISPLAY_HOUR LCALL DISPLAY_DAY LCALL DISPLAY_DATE ;===== MOV P2,#11 LCALL DISPLAY_DOT_LED ;===== LCALL DISPLAY_YEAR LCALL DISPLAY_TEMP ;================== NHAY 2 LED NAM ======= CHECK_FLAG_BLINK_5: MOV A,FLAG_SET CJNE A,#6,EXIT_BLINK LCALL DISPLAY_SEC LCALL DISPLAY_MIN LCALL DISPLAY_HOUR LCALL DISPLAY_DAY LCALL DISPLAY_DATE LCALL DISPLAY_MONTH LCALL DISPLAY_TEMP EXIT_BLINK: RET ;================================= CHUONG TRINH CON HIEN THI CAC LED ============================= DISPLAY_SEC: MOV A,D0_SEC MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#3 SETB REST_LED LCALL DL CLR REST_LED ;========== MOV A,D1_SEC MOV DPTR,#MALED_NGUOC_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#4 SETB REST_LED LCALL DL CLR REST_LED RET ;================================= DISPLAY_MIN: MOV A,D0_MIN MOV DPTR,#MALED_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#5 SETB REST_LED LCALL DL CLR REST_LED ;============ MOV A,D1_MIN MOV DPTR,#MALED_NGUOC_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#6 SETB REST_LED LCALL DL CLR REST_LED RET ;================================= DISPLAY_HOUR: MOV A,D0_HOUR MOV DPTR,#MALED_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#7 SETB REST_LED LCALL DL CLR REST_LED ;============ MOV A,D1_HOUR MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#8 SETB REST_LED LCALL DL CLR REST_LED RET ;================================ DISPLAY_DAY: MOV A,DAY MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#15 SETB REST_LED LCALL DL CLR REST_LED RET ;================================= DISPLAY_DATE: MOV A,D0_DATE MOV DPTR,#MALED_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#13 SETB REST_LED LCALL DL CLR REST_LED ;============ MOV A,D1_DATE MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#14 SETB REST_LED LCALL DL CLR REST_LED RET ;================================= DISPLAY_MONTH: MOV A,D0_MONTH MOV DPTR,#MALED_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#11 SETB REST_LED LCALL DL CLR REST_LED ;============ MOV A,D1_MONTH MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#12 SETB REST_LED LCALL DL CLR REST_LED RET ;================================= DISPLAY_YEAR: MOV A,D0_YEAR MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#9 SETB REST_LED LCALL DL CLR REST_LED ;============ MOV A,D1_YEAR MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#10 SETB REST_LED LCALL DL CLR REST_LED RET DISPLAY_TEMP: MOV A,D0_TEMP MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#0 SETB REST_LED LCALL DL CLR REST_LED ;============ MOV A,D1_TEMP MOV DPTR,#MALED_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#1 SETB REST_LED LCALL DL CLR REST_LED ;============= MOV A,D2_TEMP MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#2 SETB REST_LED LCALL DL CLR REST_LED RET ;====================================== CHUONG TRINH CON MODE ALARM =========================== ;================== BLINK_ALARM: ;================================= MOV A,FLAG_SET CJNE A,#0,CHECK_FLAG_BLINK_ALARM_0 DISPLAY_MODE_ALARM: LCALL DISPLAY_ALARM_AL LCALL DISPLAY_MIN_ALARM LCALL DISPLAY_HOUR_ALARM LCALL DISPLAY_TEMP ;================== NHAY 2 LED PHUT ======= CHECK_FLAG_BLINK_ALARM_0: MOV A,FLAG_SET CJNE A,#1,CHECK_FLAG_BLINK_ALARM_1 LCALL DISPLAY_ALARM_AL LCALL DISPLAY_ALARM_ON_OFF ;===== MOV P2,#6 LCALL DISPLAY_DOT_LED ;===== LCALL DISPLAY_HOUR_ALARM LCALL DISPLAY_TEMP ;================== NHAY 2 LED GIO ======= CHECK_FLAG_BLINK_ALARM_1 : MOV A,FLAG_SET CJNE A,#2,EXIT_DISPLAY_FLAG_ALARM LCALL DISPLAY_ALARM_AL LCALL DISPLAY_ALARM_ON_OFF LCALL DISPLAY_MIN_ALARM ;===== MOV P2,#7 LCALL DISPLAY_DOT_LED ;===== LCALL DISPLAY_TEMP EXIT_DISPLAY_FLAG_ALARM: RET ;================================= DISPLAY_ALARM_NORMAL: ;===================== Hien thi o che do Alarm khong nha'y LED LCALL DISPLAY_ALARM_AL LCALL DISPLAY_ALARM_ON_OFF LCALL DISPLAY_MIN_ALARM LCALL DISPLAY_HOUR_ALARM LCALL DISPLAY_TEMP RET ;============================== DISPLAY_ALARM_AL: ;======================== TAO CHU A================ MOV LED_DATA,#88H MOV P2,#14 SETB REST_LED LCALL DL CLR REST_LED ;======================= TAO CHU L================= MOV LED_DATA,#0C7H MOV P2,#13 SETB REST_LED LCALL DL CLR REST_LED RET ;=========================== DISPLAY_ALARM_ON_OFF: ;============== Flag_alarm = 0 thi hien OFF, Flag_alarm=1 thi hien ON MOV A,FLAG_ALARM CJNE A,#0,DISPLAY_ALARM_ON DISPLAY_ALARM_OFF: ;======= TAO CHU 0====== MOV LED_DATA,#0C0H MOV P2,#12 SETB REST_LED LCALL DL CLR REST_LED ;======== TAO CHU F ===== MOV LED_DATA,#8EH MOV P2,#11 SETB REST_LED LCALL DL CLR REST_LED ;======= TAO CHU F ====== MOV LED_DATA,#8EH MOV P2,#10 SETB REST_LED LCALL DL CLR REST_LED RET ;===================== DISPLAY_ALARM_ON: ;===================== TAO CHU 0=================== MOV LED_DATA,#0C0H MOV P2,#12 SETB REST_LED LCALL DL CLR REST_LED ;=================== TAO CHU N===================== MOV LED_DATA,#0C8H MOV P2,#11 SETB REST_LED LCALL DL CLR REST_LED RET ;===================== DISPLAY_MIN_ALARM: MOV A,D0_MIN_ALARM MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#5 SETB REST_LED LCALL DL CLR REST_LED ;============ MOV A,D1_MIN_ALARM MOV DPTR,#MALED_NGUOC_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#6 SETB REST_LED LCALL DL CLR REST_LED RET ;========================== DISPLAY_HOUR_ALARM: MOV A,D0_HOUR_ALARM MOV DPTR,#MALED_DP MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#7 SETB REST_LED LCALL DL CLR REST_LED ;============ MOV A,D1_HOUR_ALARM MOV DPTR,#MALED_NORMAL MOVC A,@A+DPTR MOV LED_DATA,A MOV P2,#8 SETB REST_LED LCALL DL CLR REST_LED RET ;================ DISPLAY_DOT_LED: MOV LED_DATA,#07FH SETB REST_LED LCALL DL CLR REST_LED RET ;================================= CHUONG TRINH CON DELAY TRONG KHAU HIEN THI ================ DL: MOV R7,#200 DJNZ R7,$ RET ;=============================== CHUONG TRINH CON CONVERT CAC KIEU DU LIEU ============= BCD_HEX: MOV B,#10H DIV AB MOV TEMP,B ;CAT HANG DON VI MOV B,#10 MUL AB ADD A,TEMP RET ;========================================== HEX_BCD: ;========================================== MOV B,#10 DIV AB MOV TEMP,B ;CAT HANG DON VI MOV B,#10H MUL AB ADD A,TEMP RET ;=============================================================== ;============================= CHUONG TRINH NGAT TIMER========== INTERRUPT_TIMER : INC PULSE_JUMP INC PERCENT_SEC ;DAT TIMER CHAY 1/100 GIAY MOV TL0,#0H MOV TH0,#0DCH SETB TR0 ;=============== PUSH ACC PUSH PSW ;THANH GHI TRANG TH?I CHUONG TR?NH PUSH B ;========== MOV A,PULSE_JUMP ;TAO XUNG NHAP NHAY = 1/4 GIAY CJNE A,#25,INTERRUPT_TIMER_1 MOV PULSE_JUMP,#0 INC VARI_JUMP MOV A,VARI_JUMP CJNE A,#3,INTERRUPT_TIMER_1 MOV VARI_JUMP,#0 ;========= INTERRUPT_TIMER_1: MOV A,PERCENT_SEC ;KIEM TRA BIEN PHAN_TRAM_GIAY - THOAT KHOI NGAT TIME0 NEU KHONG = CJNE A,#100,EXIT_INTERRUPT_TIMER MOV PERCENT_SEC,#0 ;NEU = 100 TH? SET BIEN NAY = 0 ;========= EXIT_INTERRUPT_TIMER: LCALL TACHSO POP B POP PSW POP ACC RETI ;===================================================================== ;===================================================================== TACHSO: ;============ TACH GIAY ========== MOV A,SEC ;LAN LUOT CHIA CAC BIEN: GIAY, PHUT, GIO CHO 10 MOV B,#10 ;DE TACH PHAN DON VI VA HANG CHUC RA, DE CAT RIENG VAO CAC BIEN TUONG UNG. DIV AB ;PHAN NGUYEN TRONG A, PHAN DU TRONG B MOV D1_SEC,A ;LUU LAI HANG CHUC GIAY MOV D0_SEC,B ;LUU LAI DON VI GIAY ;========== TACH PHUT ========== MOV A,MIN MOV B,#10 DIV AB MOV D1_MIN,A MOV D0_MIN,B ;========== TACH PHUT ========== MOV A,HOUR MOV B,#10 DIV AB MOV D1_HOUR,A MOV D0_HOUR,B ;========== TACH NGAY ========== MOV A,DATE MOV B,#10 DIV AB MOV D1_DATE,A MOV D0_DATE,B ;========== TACH THANG ========== MOV A,MONTH MOV B,#10 DIV AB MOV D1_MONTH,A MOV D0_MONTH,B ;========== TACH NAM ========== MOV A,YEAR MOV B,#10 DIV AB MOV D1_YEAR,A MOV D0_YEAR,B ;========== TACH PHUT CUA ALARM ========== MOV A,MIN_ALARM MOV B,#10 DIV AB MOV D1_MIN_ALARM,A MOV D0_MIN_ALARM,B ;========== TACH GIO CUA ALARM ========== MOV A,HOUR_ALARM MOV B,#10 DIV AB MOV D1_HOUR_ALARM,A MOV D0_HOUR_ALARM,B RET ;==================================================================================================== ;=======================================5. Chuong trinh kiem tra phim bam============================ SCAN_KEY: ;================================================== SW1: JB SW_1,SW2 INC FLAG_MODE MOV A,FLAG_MODE CJNE A,#1,CHECK_MODE MOV FLAG_SET,#1 SJMP MODE_DONE CHECK_MODE: CJNE A,#2,CHECK_MODE_OUT LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LCALL UPDATE_TIME INTIAL_MODE_ALARM: MOV FLAG_SET,#0 SJMP MODE_DONE CHECK_MODE_OUT: CJNE A,#3,MODE_DONE MOV FLAG_MODE,#0 LCALL UPDATE_ALARM LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY MODE_DONE: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION SJMP SW2 ;============================= SW2: JB SW_2,SW3 MOV A,FLAG_MODE CJNE A,#1,MODE_ALARM ;================= MODE_TIME: INC FLAG_SET MOV A,FLAG_SET CJNE A,#7,MODE_TIME_DONE MOV FLAG_SET,#1 MODE_TIME_DONE: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP SW3 ;================== MODE_ALARM: INC FLAG_SET MOV A,FLAG_SET CJNE A,#3,MODE_ALARM_DONE MOV FLAG_SET,#0 MODE_ALARM_DONE: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP SW3 ;================== SW3: JB SW_3,NOKEY MOV A,FLAG_MODE ; CHECK MODE CJNE A,#1,MODE_ALARM_INC ;================================ MODE_TIME_INC: MOV A,FLAG_SET ; CHECK DATA SET CHECK_MODE_TIME_INC: ;================== CHECK_MODE_TIME_INC_1: CJNE A,#1,CHECK_MODE_TIME_INC_2 LJMP TIME_INC_MIN CHECK_MODE_TIME_INC_2: CJNE A,#2,CHECK_MODE_TIME_INC_3 LJMP TIME_INC_HOUR CHECK_MODE_TIME_INC_3: CJNE A,#3,CHECK_MODE_TIME_INC_4 LJMP TIME_INC_DAY CHECK_MODE_TIME_INC_4: CJNE A,#1,CHECK_MODE_TIME_INC_5 LJMP TIME_INC_DATE CHECK_MODE_TIME_INC_5: CJNE A,#1,CHECK_MODE_TIME_INC_6 LJMP TIME_INC_MONTH CHECK_MODE_TIME_INC_6: LJMP TIME_INC_YEAR ;============================================ NOKEY: LCALL DISPLAY_LED RET ;======================== MODE TANG ALARM MODE_ALARM_INC: MOV A,FLAG_SET CHECK_MODE_ALARM_INC: CHECK_MODE_ALARM_INC_0: CJNE A,#0,CHECK_MODE_ALARM_INC_1 LJMP ALARM_INC_FLAG CHECK_MODE_ALARM_INC_1: CJNE A,#1,CHECK_MODE_ALARM_INC_2 LJMP ALARM_INC_MIN CHECK_MODE_ALARM_INC_2: LJMP ALARM_INC_HOUR ;============================== CHUONG TRINH TANG CAC DATA CUA TIME ======================== TIME_INC_MIN: INC MIN MOV A,MIN CJNE A,#60,DONE_TIME_INC_MIN MOV MIN,#0 DONE_TIME_INC_MIN: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;================ TIME_INC_HOUR: INC HOUR MOV A,HOUR CJNE A,#24,DONE_TIME_INC_HOUR MOV HOUR,#0 DONE_TIME_INC_HOUR: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;=============== TIME_INC_DAY: INC DAY MOV A,DAY CJNE A,#8,DONE_TIME_INC_DAY MOV DAY,#1 DONE_TIME_INC_DAY: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;=============== TIME_INC_DATE: INC DATE MOV A,DATE CJNE A,#32,DONE_TIME_INC_DATE MOV DATE,#1 DONE_TIME_INC_DATE: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;=============== TIME_INC_MONTH: INC MONTH MOV A,MONTH CJNE A,#13,DONE_TIME_INC_MONTH MOV MONTH,#1 DONE_TIME_INC_MONTH: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;=============== TIME_INC_YEAR: INC YEAR MOV A,YEAR CJNE A,#99,DONE_TIME_INC_YEAR MOV YEAR,#0 DONE_TIME_INC_YEAR: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;========================================= TANG CAC GIA TRI CUA ALARM============================ ALARM_INC_FLAG : INC FLAG_ALARM MOV A,FLAG_ALARM CJNE A,#2,DONE_ALARM_INC_FLAG MOV FLAG_ALARM,#0 DONE_ALARM_INC_FLAG: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;============= ALARM_INC_MIN: INC MIN_ALARM MOV A,MIN_ALARM CJNE A,#60,DONE_ALARM_INC_MIN MOV MIN_ALARM,#0 DONE_ALARM_INC_MIN: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;============= ALARM_INC_HOUR: INC HOUR_ALARM MOV A,HOUR_ALARM CJNE A,#24,DONE_ALARM_INC_HOUR MOV HOUR_ALARM,#0 DONE_ALARM_INC_HOUR: LCALL DELAY_VIBRATION LCALL DELAY_VIBRATION LJMP NOKEY ;============================ CHUONG TRINH CAP NHAT THOI GIAN DA SET VAO DS1307 ================== UPDATE_TIME: MOV DATA_DS,00H MOV ADD_LOW,#00H LCALL WRITE_BYTE ;========== MOV A,MIN LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#01H LCALL WRITE_BYTE ;========== MOV A,HOUR LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#02H LCALL WRITE_BYTE ;========== MOV A,DAY LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#03H LCALL WRITE_BYTE ;========== MOV A,DATE LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#04H LCALL WRITE_BYTE ;========== MOV A,MONTH LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#05H LCALL WRITE_BYTE ;========== MOV A,YEAR LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#06H LCALL WRITE_BYTE RET ;========================== UPDATE_ALARM: MOV A,FLAG_ALARM LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#08H LCALL WRITE_BYTE ;========== MOV A,MIN_ALARM LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#09H LCALL WRITE_BYTE ;========== MOV A,HOUR_ALARM LCALL HEX_BCD MOV DATA_DS,A MOV ADD_LOW,#0AH LCALL WRITE_BYTE RET ;========================== DELAY_VIBRATION: PUSH 00H MOV R0,#20 LOOP_DELAY_VIBRATION: LCALL DISPLAY_LED DJNZ R0,LOOP_DELAY_VIBRATION POP 00H RET ;================================ MALED_NORMAL : DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H MALED_DP : DB 40H,79H,24H,30H,19H,12H,02H,78H,00H,10H MALED_NGUOC_DP: DB 40H,4FH,24H,06H,0BH,12H,10H,47H,00H,02H ;================================= END
Mong các bác " check " lỗi giúp em với ạ.
Link Code+Simulation : RTC_LM_FIXED_N_5_9.rar
P/S: Khi làm project này em có tham khảo code đồng hồ số của bác PTCOM ở topic http://www.dientuvietnam.net/forums/...19/index2.html
Comment