ASM _RESET_P bcf PORTB,0 ;Force 1-wire I/O bit low bsf STATUS,5 ;Point to Bank1 (direction control) bcf TRISB,0 ;I/O directin=output, 1-wire forced low movlw 167 ;167 counts (500 µs) movwf _cntr ;into count register RESET_P1 decfsz _cntr ;decrement count and skip next if=0 goto RESET_P1 ;else go back and decrement again RESET_P2 bsf TRISB,0 ;I/O direction=input, 1-wire pulled up bcf STATUS,5 ;point back to Bank0 movlw 20 ;20 count (60 µs) movwf _cntr ;into count register RESET_P3 decfsz _cntr ;decrement count and skip next if=0 goto RESET_P3 ;else go back and decrement again clrf _temp ;initialize samples register=0 RESET_P4 movlw 36 ;36 counts (180 µs) movwf _cntr ;into count register RESET_P5 btfss PORTB,0 ;skip next if 1-wire is high incf _temp ;low so increment samples register decfsz _cntr ;decrement count and skip next if=0 goto RESET_P5 ;else go back, sample, decrement again RESET_P6 movlw 80 ;count 80 (240 µs) movf _cntr ;into count register RESET_P7 decfsz _cntr ;decrement count and skip next if=0 goto RESET_P7 ;else go back and decrement again return ;done (temp returns sampled low count) _W_B rrf _temp ;rotate LSBit data into carry btfss STATUS,0 ;skip next if carry (data)=1 goto W_B2 ;else jump to data=0 routine W_B1 call W1S ;call the write-1 time slot goto W_B3 ;go on W_B2 call W0S ;call the write-0 time slot W_B3 decfsz _cntr ;decrement count and skip next if=0 goto _W_B ;else go back and do another bit return ;done sending all bits _R_B call R_S ;call the read time slot movf _rtemp ;get the sampled low count bcf STATUS,0 ;clear carry (read data=0 setup) btfsc STATUS,2 ;skip next if sampled count <>0 bsf STATUS,0 ;set carry (read data=1) rrf _temp ;rotate data into the MSBit decfsz _cntr ;decrement count and skip next if=0 goto _R_B ;else go back and do another bit return ;done sending all bits W1S movlw 1 ;1 count (3 µs) movwf _ctemp ;into count register bcf PORTB,0 ;force 1-wire I/O bit low bsf STATUS,5 ;point to Bank1 (direction control) bcf TRISB,0 ;I/O direction=output, 1-wire forced low W1S1 decfsz _ctemp ;decrement count and skip next if=0 goto W1S1 ;else go back and decrement again bsf TRISB,0 ;I/O direction=input, 1-wire pulled up bcf STATUS,5 ;point to Bank0 movlw 33 ;33 count (99 µs) movwf _ctemp ;into count register W1S2 decfsz _ctemp ;decrement count and skip next if=0 goto W1S2 ;else go back and decrement again return ;done sending bit W0S movlw 30 ;30 count (90 µs) movwf _ctemp ;into count register bcf PORTB,0 ;force 1-wire I/O bit low bsf STATUS,5 ;point to Bank1 (direction control) bcf TRISB,0 ;I/O direction=output, 1-wire forced low WOS1 decfsz _ctemp ;decrement count and skip next if=0 goto WOS1 ;else go back and decrement again bsf TRISB,0 ;I/O direction=input, 1-wire pulled up bcf STATUS,5 ;point to Bank0 movlw 04 ;4 count (12 µs) movwf _ctemp ;into count register WOS2 decfsz _ctemp ;decrement count and skip next if=0 goto WOS2 ;else go back and decrement again return ;done sending bit R_S movlw 1 ;1 count (3 µs) movwf _ctemp ;into count register bcf PORTB,0 ;force 1-wire I/O bit low bsf STATUS,5 ;point to Bank1 (direction control) bcf TRISB,0 ;I/O direction=output, 1-wire forced low R_S1 decfsz _ctemp ;decrement count and skip next if=0 goto R_S1 ;else go back and decrement again bsf TRISB,0 ;I/O direction=input, 1-wire pulled up bcf STATUS,5 ;point to Bank0 movlw 3 ;3 count (15 µs) movwf _ctemp ;into count register clrf _rtemp ;initialize sample count=0 R_S2 btfss PORTB,0 ;skip next if 1-wire input=1 incf _rtemp ;low so increment samples register decfsz _ctemp ;decrement count and skip next if=0 goto R_S2 ;else go back and decrement again R_S3 movlw 16 ;16 count (48 µs) movwf _ctemp ;into count register R_S4 decfsz _ctemp ;decrement count and skip next if=0 goto R_S4 ;else go back and decrement again return ;done receiving bit ENDASM END
Listing 1With these assembly-language routines, you can use a bidirectional bit to talk to any Dallas 1-wire device.