⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1820search.asm

📁 DS1820 1-wire temp sensor AVR asm Program
💻 ASM
字号:
;************************************************
;* DS1820 subroutines for AVR's			*
;*						*
;* MicroLan Search Algorithm			*
;* Requires DS1820sub.asm			*
;*						*
;* Copyright 2001-2003 Wayne Peacock		*
;* Wayne Peacock 10th March 2001		*
;* Version 1.00 				*
;************************************************
;* See Schematic for Hardware connections	*
;* 						*
;* Disclaimer:					*
;* The Author takes no responsibility for the 	*
;* use of this code. Use at your own risk!	*
;* 						*
;* This code or part there of is licensed only 	*
;* for private use and NOT for commercial use.	*
;* 						*
;* Please contact the author 'Wayne Peacock' 	*
;* <wpeacock@senet.com.au> before using the code*
;* or part there of in a commercial project.	*
;************************************************

.def	ldsearch	= r19	; Last disagreement hold variable
.def	lastdis		= r20	; Last disagreement
.def	ec		= r21	; Eight Counter (Bit's in Byte)
.def	bitnum		= r22	; Bit number counter for ROM search
.def	serial		= r23	; Number of devices found
.def	tbyte		= r24	; Temp Byte

;************************************************
;* DS1820 Search 				*
;* 0 to 8 devices only				*
;*						*
;* Returns: 	temp				*
;* 		If 0 then OK			*
;*		If 1 then error			*
;*		If 2 then more than 8 devices	*
;************************************************

Search:
	; Set serial to 0 (Number of devices found)
	clr	serial
	
	ldi	YH,high(DS1820_0)
	ldi	YL,low(DS1820_0)	;init Y-pointer	
	
	; Point Rom Search algorithm to top.
	ldi	lastdis, 0x41		; Last Disagreement
	
	; Move Last disagreement to Last Disagreement of Last Search
	mov	ldsearch, lastdis

nextsch:
	
	rcall	DS1820_reset		; Reset Devices on wire
        bld	temp,WIRE
        sbrc	temp,WIRE
        rjmp	nodow			; No device on wire
	
	ldi	bitnum, 0x40		; Set Bitnum to 64 to read 64 bits
	ldi	ec, 0x08		; Set Byte counter to 8
	
	; Move Last disagreement to Last Disagreement of Last Search
	mov	ldsearch, lastdis
		
	ldi	data, 0xF0		; Search ROM Code
	rcall	DS1820_Write		; Write Search Rom to bus
	
nextbit:
	clr	data
	rcall	DS1820_read_bit		; Read bit
	rcall	DS1820_read_bit		; Read Compliment of bit
	
	; if 3 - no devices on bus - end search
	cpi	data, 0x03	
	breq	nodow			; No devices in search
	
	; if 1 - only 0's on bus therefore write zero
	cpi	data, 0x01
	brne	check1
	clr	data	
	rjmp	writebit		; Write zero to bus

	; if 2 - only 1's on bus therefore write one
check1:
	cpi	data, 0x02
	brne	check10
	ldi	data, 0x80
	rjmp	writebit		; Write one to bus
		
	
check10:
	; if 0 - both 0's and 1's on bus
	cp	ldsearch, bitnum	; Compare location with last disagreement
	brne	lwhere			; If not equal look some more 
	
	; We've been on this path and sent a 0
	; Now lets send a 1
	
	ldi	data, 0x80
	;mov	lastdis, bitnum		; Set up last disagreement
	rjmp	writebit		; Write one to bus
	
lwhere:	
	brcs	slastbit		; Stay on previous path
	; Now lets send a 0

	ldi	data, 0x00
	mov	lastdis, bitnum		; Set up last disagreement
	rjmp	writebit		; Write zero to bus

nodow:
	; Search failed !	
	sts	DScount, serial		; Save number of devices
	ldi	temp, 0x01		; ERROR RETURN CODE!
 	ret
 	
; **** END HERE **** Search Failed 
	
slastbit:	
	; Get Last Path!!	
        
        subi	r28, 0x08		; Dec 8 from pointer
        ld	data ,Y			; Get last byte 
	ldi	temp, 0x08
        add	r28, temp		; Put pointer back!
	mov	temp, ec		; Pointer to bit location 
	dec	temp       

glast:
        rol	data
        dec	temp
	brne	glast
	andi	data, 0x80		; Only want one bit
	; if zero save discrepancy
	brne	writebit
	mov	lastdis, bitnum		; Set up last disagreement			
			
writebit:
	rcall	DS1820_Write_bit	; Write value to bus
        lsr	tbyte			; Ready for next bit	
	or	tbyte, data		; Write bit		
	dec	bitnum			; Goto next bit
	
	; if ec equals zero - save tbyte and start newbyte
	dec	ec
	brne	nextbit			; Repeat
	st	Y+,tbyte		; store data to SRAM
	ldi	ec, 0x08		; Reset ec
	clr	tbyte
	
	; if bitcount zero break
	cpi	bitnum , 0
	brne	nextbit
	; End of Rom Search
	
	inc	serial			; One more device found
	
	; Check if more devices are on wire
	cp	lastdis, ldsearch	
	brne	moredows		; More Devices on wire
	
	; No more devices present
	sts	DScount, serial		; Save number of devices
	ldi	temp, 0x00		; Search OK!
	
	ret				; Found all devices

; **** END HERE **** Search Passed

	
moredows:	
	; Repeat to find more devices
	cpi	serial, 0x06		; only find 6 devices
        breq	endit
	rjmp	nextsch
      
	
endit:
	; More than 8 devices or something wrong
	sts	DScount, serial		; Save number of devices
	ldi	temp, 0x02		; MORE DEVICES ON BUS
	ret
	
; **** END HERE **** More than 8 devices on bus!




;************************************************
;* DS1820 Write Bit	(4 Mhz)			*
;*						*
;* bit 0 of Data				*
;************************************************

DS1820_Write_bit:

	sbi	WIREDIR, WIRE		; [1] Bus low ( 1us to 15us )
	nop
	nop
	nop
	nop
	nop
	rol	data			; [1]
	brcc	DS1820_wb0		; [1] Write 0

DS1820_wb1:
	cbi	WIREDIR, WIRE		; Data = high

DS1820_wb0:
	ror	data		
					; Data alread low
	ldi	count1, 13 		; Just wait >60us
	rcall	delayX5us				
					
	cbi	WIREDIR, WIRE		; Bus high
	ldi	count1, 1		; Recovery time >1us
	rcall	delayX5us		; 5us delay!
	
	ret						

;************************************************
;* DS1820 Read Bit	(4 Mhz)			*
;*						*
;* Output:	bit 0 of data			*
;************************************************

DS1820_read_bit:

	sbi	WIREDIR, WIRE		; [1] Bus low ( 1us to 15us )
	ldi	count1, 1		; Recovery time >1us
	rcall	delayX5us		; 5us delay!
	cbi	WIREDIR, WIRE		; Bus high
	ldi	count1, 1		; Recovery time >1us
	rcall	delayX5us		; 5us delay!
	; Get Data Now
	lsl	data
	sbic	WIREIN, WIRE		; check bit
	sbr	data, 0x01
					; Data alread inactive
	ldi	count1, 13		; Just wait >60us
	rcall	delayX5us				
									
	ret	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -