Chương2 :Các chương trình phục vụ cho RTC SEND_START (Gửi điều kiện START) SEND_STOP (Gửi điều kiện STOP) SCL_HIGH (đưa SCL lên mức cao) SEND_BYTE (gửi byte vào RTC) READ_BYTE (đọc byte từ RTC) READ_CLOCK (đọc từ RTC) SET_CLOCK (cài đặt RTC) READ_RAM (đọc 56 byte từ RTC) SET_RAM (ghi 56 byte vào RTC) 10 OSC_CONTROL RTC) (Điều khiển dao động 11 SQW_CONTROL_1HZ (xuất xung 1Hz chân RTC) 12 SQW_CONTROL_4KHZ(xuất xung 4.096KHz chân RTC) 13 SQW_CONTROL_8KHZ(xuất xung 8.192Hz chân RTC) 14 SQW_CONTROL_32KHZ(xuất xung 32.768Hz chân RTC) 15 ALARM_TEST ;START CONDITION (kiểm tra báo thức) SEND_START: CLR NO_START JB _2W_BUSY,SEND_START_ERROR SETB _2W_BUSY CLR ACK JNB SCL,SEND_START_ERROR JNB SDA,SEND_START_ERROR SETB SDA LCALL SCL_HIGH LCALL DELAY CLR SDA LCALL DELAY; CLR SCL RET SEND_START_ERROR: SETB NO_START RET ;=============================================== ;STOP CONDITION SEND_STOP: CLR SDA LCALL SCL_HIGH LCALL DELAY SETB SDA NOP JNB SDA,$ CLR _2W_BUSY RET ;================================================ ;SEND BYTE ;DUA GIA TRI TRONG THANH GHI A VAO DS1307 SEND_BYTE: MOV BITCOUNT,#8H SEND_BYTE_LOOP: JNB ACC.7,LEVEL_LOW SETB SDA AJMP SEND_BITS LEVEL_LOW: CLR SEND_BITS: SDA ; NOP ; NOP ; NOP LCALL SCL_HIGH LCALL DELAY CLR SCL;KET THUC XUNG NHIP RL A DJNZ BITCOUNT,SEND_BYTE_LOOP SETB SDA;CAU HINH DE NHAN XUNG ACK NOP LCALL ; NOP ; NOP ; SCL_HIGH NOP CLR ACK JNB SDA,SEND_BYTE_COMPLISH SETB ACK SEND_BYTE_COMPLISH: LCALL DELAY CLR SCL LCALL DELAY RET ;=============================================\ ;DOC GIA TRI VAO A READ_BYTE: MOV BITCOUNT,#8H MOV A,#00H SETB SDA READ_BITS: LCALL ; NOP ; NOP ; SCL_HIGH NOP MOV RLC A;DUA SDA VAO THANH GHI A CLR ; C,SDA SCL NOP NOP ; NOP DJNZ BITCOUNT,READ_BITS JB LAST_READ,NOT_ACK;NEU LA LAN DOC CUOI CUNG THI KHONG GUI ACK CLR SDA;GUI ACK BIT NOT_ACK: LCALL SCL_HIGH CLR SCL RET; SCL_HIGH: SETB SCL JNB SCL,$ NOP RET READ_CLOCK: MOV R0,#25H MOV BYTECOUNT,#00 CLR LAST_READ LCALL SEND_START MOV A,#0D0H;WRITE COMMAND LCALL SEND_BYTE MOV A,#00H;DAT DIA CHI LAM VIEC LCALL SEND_BYTE LCALL SEND_STOP LCALL SEND_START MOV A,#0D1H;READ LCALL SEND_BYTE READ_LOOP: MOV A,BYTECOUNT CJNE A,#07H,NOTLAST SETB LAST_READ NOTLAST: LCALL READ_BYTE MOV @R0,A INC R0 INC BYTECOUNT MOV A,BYTECOUNT CJNE A,#00H,NOT_FIRST CLR OSC MOV A,@R0 JNB ACC.7,NO_OSC SETB OSC CLR ACC.7 MOV @R0,A NO_OSC: NOT_FIRST: CJNE A,#8H,READ_LOOP RET SQW_CONTROL_1HZ: LCALL SEND_START MOV A,#0D0H LCALL SEND_BYTE MOV A,#07H;DAT DIA CHI LAM VIEC LCALL SEND_BYTE MOV A,#80H JNB SQW,TURN_OFF_SQW1 MOV A,#90H TURN_OFF_SQW1: LCALL SEND_BYTE LCALL SEND_STOP RET SQW_CONTROL_4KHZ: LCALL SEND_START MOV A,#0D0H LCALL SEND_BYTE MOV A,#07H;DAT DIA CHI LAM VIEC LCALL SEND_BYTE MOV A,#81H JNB SQW,TURN_OFF_SQW2 MOV A,#91H TURN_OFF_SQW2: LCALL SEND_BYTE LCALL SEND_STOP RET SQW_CONTROL_8KHZ: LCALL SEND_START MOV A,#0D0H LCALL SEND_BYTE MOV A,#07H;DAT DIA CHI LAM VIEC LCALL SEND_BYTE MOV A,#82H JNB SQW,TURN_OFF_SQW3 MOV A,#92H TURN_OFF_SQW3: LCALL SEND_BYTE LCALL SEND_STOP RET SQW_CONTROL_32KHZ: LCALL SEND_START MOV A,#0D0H LCALL SEND_BYTE MOV A,#07H;DAT DIA CHI LAM VIEC LCALL SEND_BYTE MOV A,#83H JNB SQW,TURN_OFF_SQW4 MOV A,#93H TURN_OFF_SQW4: LCALL SEND_BYTE LCALL SEND_STOP RET ;*********************************** READ_RAM: MOV R0,#30H MOV BYTECOUNT,#00H CLR LAST_READ LCALL SEND_START MOV A,#0D0H;WRITE COMMAND LCALL SEND_BYTE MOV A,#08H LCALL SEND_BYTE;DAT DIA CHI LAM VIEC LCALL SEND_STOP LCALL SEND_START MOV A,#0D1H;READ COMMAND LCALL SEND_BYTE READ_RAM_LOOP1: MOV CJNE CUOI CUNG SETB A,BYTECOUNT A,#55,NOT_LAST2;KHONG PHAI LAN DOC LAST_READ NOT_LAST2: LCALL MOV @R0,A INC R0 INC BYTECOUNT MOV A,BYTECOUNT CJNE A,#56,READ_RAM_LOOP1 LCALL RET READ_BYTE SEND_STOP SET_RAM: MOV R0,#30H MOV BYTECOUNT,#00H LCALL SEND_START MOV A,#0D0H;WRITE COMMAND LCALL SEND_BYTE MOV A,#08H LCALL SEND_BYTE;DIA CHI LAM VIEC SET_RAM_LOOP: MOV A,@R0 LCALL SEND_BYTE INC R0 INC BYTECOUNT MOV A,BYTECOUNT CJNE A,#38H,SET_RAM_LOOP LCALL SEND_STOP RET OSC_CONTROL: LCALL SEND_START MOV A,#0D0H;WRITE COMMAND LCALL SEND_BYTE MOV A,#00H LCALL SEND_BYTE LCALL SEND_STOP LCALL SEND_START MOV A,#0D1H;READ COMMAND LCALL SEND_BYTE SETB LAST_READ LCALL READ_BYTE PUSH ACC;CAT SECOND TRONG REAL TIMER LCALL SEND_STOP LCALL SEND_START MOV A,#0D0H;WRITE COMMAND LCALL SEND_BYTE MOV A,#00H LCALL SEND_BYTE JB OSC,OSC_ON POP ACC SETB ACC.7;OSC DISABLE LJMP SET_OSC OSC_ON: POP ACC CLR ACC.7 SET_OSC: LCALL SEND_BYTE LCALL SEND_STOP RET ALARM_TEST: JNB ALARM,EXIT_ALARM MOV A,MINUTE CJNE A,MINUTE_ALARM,EXIT_ALARM MOV A,HOUR CJNE A,HOUR_ALARM,EXIT_ALARM CLR P3.6;BAO THUC EXIT_ALARM: SETB RET P3.6;TAT BAO THUC .. .SEND_START: CLR NO_START JB _2W_BUSY ,SEND_START_ ERROR SETB _2W_BUSY CLR ACK JNB SCL ,SEND_START_ ERROR JNB SDA ,SEND_START_ ERROR SETB SDA LCALL SCL_HIGH... BYTECOUNT,#00 CLR LAST_READ LCALL SEND_START MOV A,#0D0H;WRITE COMMAND LCALL SEND_BYTE MOV A,#00H;DAT DIA CHI LAM VIEC LCALL SEND_BYTE LCALL SEND_STOP LCALL SEND_START MOV A,#0D1H;READ LCALL... BYTECOUNT,#00H CLR LAST_READ LCALL SEND_START MOV A,#0D0H;WRITE COMMAND LCALL SEND_BYTE MOV A,#08H LCALL SEND_BYTE;DAT DIA CHI LAM VIEC LCALL SEND_STOP LCALL SEND_START MOV A,#0D1H;READ COMMAND