Chuẩn 1-Wire interface là một chuẩn của Maxim-ic, có khá nhiều linh kiện về giao tiếp cho chuẩn này. Điển hình nhất là đo nhiềt độ dùng Sensor nhiệt DS18B20, hay DS1821. Ưu điểm của chuẩn giao tiếp này là chúng ta chỉ cần một dây để giao tiếp, Giao thức giao tiếp cũng khá đơn giản có lợi cho Master ( đơn giản hơn I2C )
Bài viết tới đây tôi xin giới thiệu cho các bạn về chuẩn giao tiếp này
file:
1w_16f6x.inc ( dung de khai bao bien dau - Marco )
PHP Code:
; *******************************************************
;
; Dallas 1-Wire Support for PIC16F628
;
; Processor has 4MHz clock and 1s per instruction cycle.
;
; *******************************************************
; *******************************************************
; Dallas Semiconductor 1-Wire MACROS
; *******************************************************
WRITE_SENTENCE: MACRO POS,DONG,CHUOI,WRITE,VITRICONTRO
MOVLW POS
CALL DONG
CLRF R1
AGAIN1 MOVF R1,W
CALL CHUOI
ADDLW 0x00
BTFSC STATUS,Z
GOTO NEXT1
CALL WRITE
INCF R1,F
GOTO AGAIN1
MOVLW POS
ADDWF R1,W
MOVWF VITRICONTRO ; Luu con tro cua LCD vao bien
NEXT1
ENDM
OW_HIZ:MACRO
BSF STATUS,RP0 ; Select Bank 1 of data memory
BSF TRISB, DQ ; Make DQ pin High Z
BCF STATUS,RP0 ; Select Bank 0 of data memory
ENDM
; --------------------------------------------------------
OW_LO:MACRO
BCF STATUS,RP0 ; Select Bank 0 of data memory
BCF PORTB, DQ ; Clear the DQ bit
BSF STATUS,RP0 ; Select Bank 1 of data memory
BCF TRISB, DQ ; Make DQ pin an output
BCF STATUS,RP0 ; Select Bank 0 of data memory
ENDM
; --------------------------------------------------------
WAIT:MACRO TIME
;Delay for TIME s.
;time must be in multiples of 5s.
MOVLW (TIME/5)-1 ;1s
MOVWF TMP0 ;1s
CALL WAIT5U ;2s
ENDM
; *******************************************************
; Dallas Semiconductor 1-Wire ROUTINES
; *******************************************************
WAIT5U:
;This takes 5uS to complete
NOP ;1s
NOP ;1s
DECFSZ TMP0,F ;1s or 2s
GOTO WAIT5U ;2s
RETLW 0 ;2s
; --------------------------------------------------------
OW_RESET:
OW_HIZ ; Start with the line high
CLRF PDBYTE ; Clear the PD byte
OW_LO
WAIT .500 ; Drive Low for 500s
OW_HIZ
WAIT .70 ; Release line and wait 70s for PD Pulse
BTFSS PORTB,DQ ; Read for a PD Pulse
INCF PDBYTE,F ; Set PDBYTE to 1 if get a PD Pulse
WAIT .400 ; Wait 400s after PD Pulse
RETLW 0
; --------------------------------------------------------
DSRXBYTE: ; Byte read is stored in IOBYTE
MOVLW .8
MOVWF COUNT ; Set COUNT equal to 8 to count the bits
DSRXLP:
OW_LO
NOP
NOP
NOP
NOP
NOP
NOP ; Bring DQ low for 6s
OW_HIZ
NOP
NOP
NOP
NOP ; Change to HiZ and Wait 4s
MOVF PORTB,W ; Read DQ
ANDLW 1<<DQ ; Mask off the DQ bit
ADDLW .255 ; C=1 if DQ=1: C=0 if DQ=0
RRF IOBYTE,F ; Shift C into IOBYTE
WAIT .50 ; Wait 50s to end of time slot
DECFSZ COUNT,F ; Decrement the bit counter
GOTO DSRXLP
RETLW 0
; --------------------------------------------------------
DSTXBYTE: ; Byte to send starts in W
MOVWF IOBYTE ; We send it from IOBYTE
MOVLW .8
MOVWF COUNT ; Set COUNT equal to 8 to count the bits
DSTXLP:
OW_LO
NOP
NOP
NOP
NOP
NOP ; Drive the line low for 5s
RRF IOBYTE,F ; The data byte
BTFSC STATUS,C ; Check the LSB of IOBYTE for 1 or 0
BSF PORTB,DQ ; Drive the line high if LSB is 1
WAIT .60 ; Continue driving line for 60s
OW_HIZ ; Release the line for pullup
NOP
NOP ; Recovery time of 2s
DECFSZ COUNT,F ; Decrement the bit counter
GOTO DSTXLP
RETLW 0
; --------------------------------------------------------
Về chuẩn giao tiếp các bạn có thể tham khảo các tài liệu dưới đây. Về phương thức giao tiếp tôi dùng luôn code của họ. Với các chuẩn giao tiếp tôi nghĩ chúng ta nên dùng các code có sẵn; nếu tự xây dựng thì sẽ rất mất nhiều thời gian và chắc chắn là sẽ không tối ưu ( bài học sương máu từ I2C )