[ บทความ : ตัวอย่างสำหรับ CP-SB31 V2 ตอนที่ 6 ] ทดสอบติดต่อกับ RTC ด้วย CP-SB31V2 |
ตัวอย่างซอร์สโค้ดสำหรับติดต่อกับ ระบบ RTC ที่ใช้ไอซีรุ่น DS1202 กับบอร์ด CP-SB31V2 ที่ติดตั้ง ET-DEBUG31 เป็นดังนี้
;/*******************************/; ;/* Demo Program For RTC DS1202 */; ;/* Hardware : ET-CPSB31 V2.0 */; ;/* Complier : Cross-32 V2.0 */; ;/*******************************/; ; ;/***************************************/; ;/* Equate List Port I/O ET-CPSB31 V2.0 */; ;/***************************************/; ; ADC_CS EQU P1.4 ; Chips Select LTC1298 RTC_RESET EQU P1.5 ; Chips Select DS1202 RTC_DATA EQU P1.6 ; Data I/O DS1202 RTC_CLOCK EQU P1.7 ; Clock DS1202 ; SECOND EQU 00H ; Second 00-59 MINUTE EQU 01H ; Minute 00-59 HOUR EQU 02H ; Hour 01-12 or 00-23 DATE EQU 03H ; Data 01-28/29 or 01-30 or 01-31 MONTH EQU 04H ; Month 01-12 DAY EQU 05H ; Day 01-07 YEAR EQU 06H ; Year 00-99 CTRL_RTC EQU 07H ; Write Protect Enable/Disable ORG 2200H ; Strat Program For "ET-DEBUG31" MAIN: SETB RTC_CLOCK ; Initial Port RTC-DS1202 SETB RTC_DATA CLR RTC_RESET SETB ADC_CS ; Disable CS LTC1298 ; INIT_SER: MOV A,#0FDH ; Set baud rate 9600 MOV TH1,A MOV TL1,A MOV TMOD,#00100000B ; Timer1 Mode 1 MOV SCON,#01010000B ; Serial mode 1 CLR ES ; Disable serial interupt CLR ET1 ; Disable timer1 interupt SETB TR1 ; Set timer1 control ; LCALL PRINT_SER DB 0CH,"Hardware ET-CPSB31 V2.0",0DH,0AH DB "Test RTC DS1202",0DH,0AH,0DH,0AH DB "Hr:Mn:Sec",0DH,0AH,00H ; LCALL DISABLE ; Write Protect OFF MOV A,#SECOND LCALL READ_RTC CLR ACC.7 ; Disable Clock Halt Flag MOV B,A MOV A,#SECOND LCALL WRITE_RTC LCALL ENABLE ; Write Protect ON ; LOOP: MOV A,#HOUR LCALL READ_RTC PUSH ACC SWAP A ANL A,#0FH ADD A,#30H ; Convert High-Nibble to ASCII LCALL TX_BYTE POP ACC ANL A,#0FH ADD A,#30H ; Convert Low-Nibble to ASCII LCALL TX_BYTE MOV A,#':' LCALL TX_BYTE MOV A,#MINUTE LCALL READ_RTC PUSH ACC SWAP A ANL A,#0FH ADD A,#30H ; Convert High-Nibble to ASCII LCALL TX_BYTE POP ACC ANL A,#0FH ADD A,#30H ; Convert Low-Nibble to ASCII LCALL TX_BYTE MOV A,#':' LCALL TX_BYTE MOV A,#SECOND LCALL READ_RTC PUSH ACC SWAP A ANL A,#0FH ADD A,#30H ; Convert High-Nibble to ASCII LCALL TX_BYTE POP ACC ANL A,#0FH ADD A,#30H ; Convert Low-Nibble to ASCII LCALL TX_BYTE MOV A,#0DH LCALL TX_BYTE SJMP LOOP ;/************************************/; ;/* Print Data data to Serial Port */; ;/* to display buffer. */; ;/* Usage : LCALL PRINT_SER */; ;/* : DFB "xxxx",00 */; ;/* Register : ACC */; ;/* Note : last byte must be 00 */; ;/************************************/; ; PRINT_SER: POP DPH POP DPL PRINT1: CLR A MOVC A,@A+DPTR CJNE A,#00H,PRINT2 SJMP PRINT3 PRINT2: LCALL TX_BYTE INC DPTR SJMP PRINT1 PRINT3: PUSH DPL PUSH DPH RET ;/*************************/; ;/* Send 1-Byte to RS-232 */; ;/* Input : ACC */; ;/* Output : Serial port */; ;/*************************/; ; TX_BYTE: PUSH IE CLR TI MOV SBUF,A JNB TI,$ CLR TI POP IE RET ;/****************************/; ;/* Read Ram From RTC-DS1202 */; ;/* Input : ACC = Address */; ;/* Output : ACC = Data */; ;/* Reg. : ACC,B,R2 */; ;/****************************/; ; READ_RAM: LCALL START_RST ; Start Communication MOV R2,#8 RL A ; Shift A0 to A1 SETB ACC.7 ; Command Status SETB ACC.6 ; Ram Accress SETB ACC.0 ; Read Command CLR CY READ_RAM1: LCALL CLOCK_LO ; Falling Clock RRC A ; Send D0..D7 (Read-Command) MOV RTC_DATA,C ; Prepare Port NOP ; Wait Bus Ready NOP NOP NOP LCALL CLOCK_HI ; Rising Clock DJNZ R2,READ_RAM1 ; Repeat Send Read-Command ; MOV B,#0 MOV R2,#8 READ_RAM2: SETB RTC_DATA ; Initial for Input NOP NOP LCALL CLOCK_LO ; Read data D0..D7 (Data) NOP ; Wait Data From RTC NOP NOP NOP NOP NOP NOP NOP MOV C,RTC_DATA ; Read data out bit MOV A,B RRC A ; Read D0..D7 MOV B,A ; Backup Data LCALL CLOCK_HI ; Rising Clock DJNZ R2,READ_RAM2 MOV A,B ; Data Output 1-Byte LCALL STOP_RST ; Disable Communication RET ;/***************************/; ;/* Write Ram to RTC-DS1202 */; ;/* Input : ACC = Address */; ;/* : B = Data */; ;/* Reg. : ACC,B,R2 */; ;/***************************/; ; WRITE_RAM: LCALL START_RST ; Enable Communication MOV R2,#8 RL A ; Shift A0 to A1 SETB ACC.7 ; Command Status SETB ACC.6 ; Ram Accress CLR ACC.0 ; Write Command CLR CY WRITE_RAM1: LCALL CLOCK_LO ; Falling Clock RRC A ; Send D0..D7 (Write-Command) MOV RTC_DATA,C NOP ; Wait Bus Ready NOP NOP NOP LCALL CLOCK_HI ; Rising Clock DJNZ R2,WRITE_RAM1 ; Repeat write Command ; MOV A,B ; Get Data For Send MOV R2,#8 CLR CY WRITE_RAM2: LCALL CLOCK_LO ; Falling Clock RRC A ; Write D0..D7 MOV RTC_DATA,C NOP ; Wait Bus Ready NOP NOP NOP LCALL CLOCK_HI DJNZ R2,WRITE_RAM2 ; Repeat write Data LCALL STOP_RST ; Disable Communication RET ;/****************************/; ;/* Read RTC From RTC-DS1202 */; ;/* Input : ACC = Address */; ;/* Output : ACC = Data */; ;/* Reg. : ACC,B,R2 */; ;/****************************/; ; READ_RTC: LCALL START_RST ; Start Communication MOV R2,#8 RL A ; Shift A0 to A1 SETB ACC.7 ; Command Status CLR ACC.6 ; Ram Accress SETB ACC.0 ; Read Command CLR CY READ_RTC1: LCALL CLOCK_LO ; Falling Clock RRC A ; Send D0..D7 (Read-Command) MOV RTC_DATA,C ; Prepare Port NOP ; Wait Bus Ready NOP NOP NOP LCALL CLOCK_HI ; Clock DJNZ R2,READ_RTC1 ; Repeat write Command ; MOV B,#0 MOV R2,#8 READ_RTC2: SETB RTC_DATA ; Initial for Input NOP NOP SETB RTC_DATA ; Initial for Input LCALL CLOCK_LO ; Read data D0..D7 (Data) NOP ; Wait Data From RTC NOP NOP NOP NOP NOP NOP NOP MOV C,RTC_DATA ; Read data out bit MOV A,B RRC A ; Read D0..D7 MOV B,A ; Backup Data LCALL CLOCK_HI ; Rising Clock DJNZ R2,READ_RTC2 MOV A,B ; Data Output 1-Byte LCALL STOP_RST ; Disable Communication RET ;/***************************/; ;/* Write RTC to RTC-DS1202 */; ;/* Input : ACC = Address */; ;/* : B = Data */; ;/* Reg. : ACC,B,R2 */; ;/***************************/; ; WRITE_RTC: LCALL START_RST ; Enable Communication MOV R2,#8 RL A ; Shift A0 to A1 SETB ACC.7 ; Command Status CLR ACC.6 ; Ram Accress CLR ACC.0 ; Write Command CLR CY WRITE_RTC1: LCALL CLOCK_LO ; Falling Clock RRC A ; Write D0..D7 (Command) MOV RTC_DATA,C NOP ; Wait Bus Ready NOP NOP NOP LCALL CLOCK_HI ; Rising Clock DJNZ R2,WRITE_RTC1 ; Repeat write Command ; MOV A,B ; Get Data MOV R2,#8 CLR CY WRITE_RTC2: LCALL CLOCK_LO RRC A ; Write D0..D7 MOV RTC_DATA,C NOP ; Wait Bus Ready NOP NOP NOP LCALL CLOCK_HI ; Rising Clock DJNZ R2,WRITE_RTC2 ; Repeat write Data LCALL STOP_RST ; Disable Communication RET ;/*************************/; ;/* Write Protect Disable */; ;/* Now You Can Write RTC */; ;/* Reg. : ACC,B,R2 */; ;/*************************/; ; DISABLE: MOV A,#CTRL_RTC MOV B,#00H ; Disable Write-Protect LCALL WRITE_RTC RET ;/************************/; ;/* Write Protect Enable */; ;/* You Can't Write RTC */; ;/* Reg. : ACC,B,R2 */; ;/************************/; ; ENABLE: MOV A,#CTRL_RTC MOV B,#80H ; Enable Write-Protect LCALL WRITE_RTC RET ;/*****************/; ;/* RTC Clock */; ;/* Falling Edge */; ;/*****************/; ; CLOCK_LO: CLR RTC_CLOCK ; Falling Clock NOP NOP NOP NOP RET ;/***************/; ;/* RTC Clock */; ;/* Rising Edge */; ;/***************/; ; CLOCK_HI: SETB RTC_CLOCK ; Rising Clock NOP NOP NOP NOP RET ;/*************************/; ;/* Initial Reset & Clock */; ;/* For Start Communicate */; ;/*************************/; ; START_RST: CLR RTC_RESET CLR RTC_CLOCK NOP NOP NOP NOP SETB RTC_RESET ; Enable Communication NOP NOP NOP NOP RET ;/*************************/; ;/* Initial Reset & Clock */; ;/* For Stop Communicate */; ;/*************************/; ; STOP_RST: SETB RTC_CLOCK NOP NOP NOP NOP CLR RTC_RESET ; Disable Communication NOP NOP NOP NOP RET ENDสามารถ download ไฟล์ตัวอย่างของบอร์ด พร้อม assembler ได้เลยครับ