Mạch Khóa Số Điện Tử Mạch Đếm Sản Phẩm Mạch Đèn giao thông Ngã Tư Mạch Trái Tim Final Mạch Trái Tim Final Mạch Trái Tim I Love U

December 22, 2011

Giao tiếp I2C và DS1307


Phần này mình xin giới thiệu về mạch giao tiếp và 1 chuơng trình asm giao tiếp DS1307 ở trang 8051projects.info
Lưu ý code chỉ mang tính tham khảo để các bạn hiểu rõ hơn cách giao tiếp với DS1307.
Code




;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


; PORT DECLERATION
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
SDA EQU P3.5  ;SDA=PIN5
SCL EQU P3.4 ;SCL=PIN6

DS1307W   EQU    0D0H     ; SLAVE ADDRESS 1101 000 + 0 TO WRITE
DS1307R   EQU    0D1H     ; SLAVE ADDRESS 1101 000 + 1 TO READ



; **********************************************************
; Main Program
; **********************************************************
CALL OSC_CONTROL ;Initilize the RTC
REPEAT: CALL READ_CLOCK ;Read Clock
AJMP REPEAT

; **********************************************************
; SUB SETS THE DS1307 OSCILLATOR
; **********************************************************

OSC_CONTROL:
ACALL       SEND_START ; GENERATE START CONDITION
MOV         A,#DS1307W ; 1101 0000 ADDRESS + WRITE-BIT
ACALL       SEND_BYTE  ; SEND BYTE TO 1307
MOV         A,#00H     ; ADDRESS BYTE TO REGISTER 00H
ACALL       SEND_BYTE  ; SECONDS REGISTER, ALWAYS LEAVE
SETB        LASTREAD   ; REG 00H-BIT #7 = 0 (LOW)
ACALL       SEND_STOP  ; IF REG 00H-BIT #7 = 1 CLOCK
ACALL       SEND_START ; OSCILLATOR IS OFF.
MOV         A,#DS1307R ; 1101 0001 ADDRESS + READ-BIT
ACALL       SEND_BYTE  ;
ACALL       READ_BYTE  ; READ A BYTE FROM THE 1307
CLR         ACC.7      ; CLEAR REG 00H-BIT #7 TO ENABLE
OSC_SET:                        ; OSCILLATOR.
PUSH        ACC        ; SAVE ON STACK
ACALL       SEND_STOP  ;
ACALL       SEND_START ;
MOV         A,#DS1307W ; SETUP TO WRITE
ACALL       SEND_BYTE  ;
MOV         A,#00H     ; REGISTER 00H ADDRESS
ACALL       SEND_BYTE  ;
POP         ACC        ; GET DATA TO START OSCILLATOR
ACALL       SEND_BYTE  ; SEND IT
ACALL       SEND_STOP
RET
; **********************************************************
; THIS SUB CONTROLS THE SQW OUTPUT 1HZ
; **********************************************************
SQW_CONTROL_1HZ:
LCALL SEND_START ; SEND START CONDITION
MOV A,#DS1307W ; SET POINTER TO REG 07H ON
; DS1307
LCALL SEND_BYTE
MOV A,#07H
LCALL SEND_BYTE
MOV A,#90H ; SQW/OUT ON AT 1HZ
JNB SQW,SQW_SET ; JUMP IF SQW BIT IS ACTIVE
MOV A,#80H ; TURN SQW/OUT OFF – OFF HIGH
SQW_SET:
LCALL SEND_BYTE
LCALL SEND_STOP
RET
; **********************************************************
; THIS SUB READS ONE BYTE OF DATA FROM THE DS1307
; **********************************************************

READ_BYTE:
MOV         BITCNT,#08H; SET COUNTER FOR 8-BITS DATA
MOV         A,#00H
SETB        SDA        ; SET SDA HIGH TO ENSURE LINE
; FREE
READ_BITS:
SCL_HIGH               ; TRANSITION SCL LOW-TO-HIGH
MOV         C,SDA      ; MOVE DATA BIT INTO CARRY
RLC         A          ; ROTATE CARRY-BIT INTO ACC.0
CLR         SCL        ; TRANSITION SCL HIGH-TO-LOW
DJNZ        BITCNT,READ_BITS
; LOOP FOR 8-BITS
JB          LASTREAD,ACKN
; CHECK TO SEE IF THIS IS
; THE LAST READ
CLR         SDA        ; IF NOT LAST READ SEND ACK-BIT

ACKN:
SCL_HIGH               ; PULSE SCL TO TRANSMIT ACKNOWLEDGE
CLR         SCL        ; OR NOT ACKNOWLEDGE BIT
RET

; **********************************************************
; SUB SENDS START CONDITION
; **********************************************************

SEND_START:
SETB        _2W_BUSY   ; INDICATE THAT 2-WIRE
CLR         ACKS        ; OPERATION IS IN PROGRESS
CLR         BUS_FLT    ; CLEAR STATUS FLAGS
JNB         SCL,FAULT
JNB         SDA,FAULT
SETB        SDA        ; BEGIN START CODITION
SCL_HIGH
CLR         SDA
ACALL       DEELAY
CLR         SCL
RET
FAULT:
SETB        BUS_FLT
RET

; **********************************************************
; SUB SENDS STOP CONDITION
; **********************************************************
SEND_STOP:
CLR         SDA
SCL_HIGH
SETB        SDA
CLR         _2W_BUSY
RET
; **********************************************************
; SUB DELAYS THE BUS
; **********************************************************
DEELAY:
NOP                    ; DELAY FOR BUS TIMING
RET
; **********************************************************
; THIS SUB SENDS 1 BYTE OF DATA TO THE DS1307
; CALL THIS FOR EACH REGISTER SECONDS TO YEAR
; ACC MUST CONTAIN DATA TO BE SENT TO CLOCK
; **********************************************************
SEND_BYTE:
MOV         BITCNT,#08H; SET COUNTER FOR 8-BITS
SB_LOOP:
JNB         ACC.7,NOTONE; CHECK TO SEE IF BIT-7 OF
SETB        SDA        ; ACC IS A 1, AND SET SDA HIGH
JMP         ONE
NOTONE:
CLR         SDA        ; CLR SDA LOW
ONE:
SCL_HIGH               ; TRANSITION SCL LOW-TO-HIGH
RL          A          ; ROTATE ACC LEFT 1-BIT
CLR         SCL        ; TRANSITION SCL LOW-TO-HIGH
DJNZ        BITCNT,SB_LOOP; LOOP FOR 8-BITS
SETB        SDA        ; SET SDA HIGH TO LOOK FOR
SCL_HIGH               ; ACKNOWLEDGE PULSE
CLR         ACKS
JNB         SDA,SB_EX  ; CHECK FOR ACK OR NOT ACK
SETB        ACKS        ; SET ACKNOWLEDGE FLAG FOR
; NOT ACK
SB_EX:
CALL       DEELAY      ; DELAY FOR AN OPERATION
CLR         SCL        ; TRANSITION SCL HIGH-TO-LOW
CALL       DEELAY      ; DELAY FOR AN OPERATION
RET
; **********************************************************
; SUB READS THE CLOCK AND WRITES IT TO THE SCRATCHPAD MEMORY
; ON RETURN FROM HERE DATE & TIME DATA WILL BE STORED IN THE
; DATE & TIME REGISTERS FROM 24H (SECS) TO 2AH (YEAR)
; ALARM SETTINGS IN REGISTERS 2CH(HRS) AND 2DH(MINUTES).
; **********************************************************
READ_CLOCK:
MOV         R1,#SECS   ; SECONDS STORAGE LOCATION
MOV         BYTECNT,#00H
CLR         LASTREAD
CALL       SEND_START
MOV         A,#DS1307W
CALL       SEND_BYTE
MOV         A,#00H
CALL       SEND_BYTE
CALL       SEND_STOP
CALL       SEND_START
MOV         A,#DS1307R
CALL       SEND_BYTE

READ_LOOP:
MOV         A,BYTECNT
CJNE        A,#09H,NOT_LAST
SETB        LASTREAD

NOT_LAST:
CALL       READ_BYTE
MOV         @R1,A
MOV         A,BYTECNT
CJNE        A,#00H,NOT_FIRST
MOV         A,@R1
CLR         ACC.7      ; ENSURE OSC BIT=0 (ENABLED)
MOV         @R1,A
NOT_FIRST:
INC         R1
INC         BYTECNT
MOV         A,BYTECNT
CJNE        A,#0AH,READ_LOOP
CALL       SEND_STOP
RET
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
; STORE THE TIME TO RTC CHIP
;(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
STORE_RTC:
LCALL SEND_START ; SEND 2WIRE START CONDITION
MOV A,#DS1307W ; LOAD DS1307 WRITE COMMAND
LCALL SEND_BYTE ; SEND WRITE COMMAND
MOV A,#01H ; SET DS1307 DATA POINTER TO BEGINNING
LCALL SEND_BYTE ; OF 00H
MOV A,MINS ; Send min
LCALL SEND_BYTE
MOV A,HRS ;send hr
SETB ACC.6        ;12 HR MODE
JNB AMS,YUH
CLR ACC.5 ;AM/PM 1=PM,0=AM
AJMP YUH1
YUH: SETB ACC.5
YUH1: LCALL SEND_BYTE
MOV A,DAY ; Send Day
LCALL SEND_BYTE
MOV A,DATE1 ; Send date
LCALL SEND_BYTE
MOV A,MONTH ; Send month
LCALL SEND_BYTE
MOV A,YEAR ; Send yr
LCALL SEND_BYTE
LCALL SEND_STOP ; SEND 2WIRE STOP CONTION
RET
;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$




Theo ktmk.byethost8.com

0 comments:

Post a Comment

Bạn đọc hãy giúp chúng tôi xây dựng cộng đồng bằng cách để lại bình luận, chúng tôi luôn đón nhận mọi ý kiến của các bạn:
» Bình luận nghiêm túc và không chứa các liên kết quảng cáo.
» Vui lòng không Spam nhận xét với mọi hình thức.
» Rất mong bạn đề tên cho nhận xét của chính mình - Bằng cách chọn vào Tên/URL và điền tên bạn vào (Phần URL có thể bỏ trống ).

- Bạn có thể chèn Link nhạc (NCT), video(Youtube),Hình ảnh vào comment bằng cú pháp:
+ [youtube] Link video Youtube [/youtube].
+ [img] Link ảnh( định dạng PNG, JPG,GIF) [/img]
+Chèn link liên kết: <a href="LINK" rel="nofollow">Name</a>
-Bạn copy mã bên cạnh biểu tượng chèn vào nhận xét để bày tỏ cảm xúc!!

:) :( :)) :(( =)) =D> :D :P