📄 1wire.asm
字号:
;###########################;# 1wire bus communication #;###########################;.nolist.include "param.inc".list; ------ 1-Wire Dallas Interface Support ------.ifge CPUFRQ - 250000 .equ delay, (CPUCLOCK * 10)/104.else .equ delay, (CPUCLOCK * 10)/52.endif .equ DS1Wire, 0x90
;P1.0 .text .using 0; ------ DS1Reset ------.global DS1Reset;; return Carry if 1-Wire bus not present;DS1Reset: CLR DS1Wire ; Start the reset pulse MOV B,#delayLP1: NOP NOP.ifge CPUFRQ - 250000 nop nop nop nop.endif DJNZ B,LP1 ; Wait 480 us with Data low CLR EA ; Interrupt disable SETB DS1Wire ; Release the reset pulse ; Wait for pullup go high MOV B,#delay ; watchdog SETB C ; DS1820 not present flagWL1: JB DS1Wire,GH1 ; Exit loop if line high.ifge CPUFRQ - 250000 nop nop nop nop.endif DJNZ B,WL1 ; Hang around if line is low SETB EA ; Interrupt enable RET ; Return if data line is shorted to LowGH1: SETB EA ; Interrupt enable MOV B,#delay ; Presence scanner counterPR1: ANL C,DS1Wire ; Catch presence pulse.ifge CPUFRQ - 250000 nop nop nop nop.endif DJNZ B,PR1 ; Scan presence pulse RET; ------ DS1Rd / DS1Wr ------.global DS1Rd.global DS1Wr;; Shift in/out a byte to the 1Wire, last significant bit first.;DS1Rd: MOV A,#0xFFDS1Wr: MOV B,#8 ; Bit counterB81: CLR EA ; Interrupt disable CLR DS1Wire ; Start the time slot. NOP ;1 Delay NOP ;2 NOP ;3 RRC A ;4 MOV data bit to carry MOV DS1Wire,C ;5 Send out the data bit NOP ;6 Delay NOP ;7 NOP ;8 NOP ;9 NOP ;10 NOP ;11 MOV C,DS1Wire ;12 Sample data input (~ 9.8 us / 14.74 MHz) ; (~ 6.5 us / 22.11 MHz) SETB EA ;13 Interrupt enable PUSH B ;15 Save B register.ifge CPUFRQ - 250000 MOV B,#(delay - 15);17 Wait for end of time slot.else MOV B,#(delay/2 - 15);17 Wait for end of time slot.endif DJNZ B,. ; POP B ; Restore B register SETB DS1Wire ; Terminate time slot NOP ; DJNZ B,B81 ; Bit counter RRC A ; Save MSB bit RET ; Return to caller. ;;END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -