.include "m8def.inc"

.equ OE 		=0b00001000 	; bit 3 Port D
.equ RS 		=0b00000100 	; bit 2 Port D

.equ temp_H		=0x60			;младший адрес принятой информации
.equ temp_L		=0x61			;метка <LF>
.equ abs_temp	=0x62
.equ polov		=0x63
.equ disp_stat 	=0x64
.equ navod		=0x65
.equ action		=0x66
.equ blim		=0x67

.equ HU			=0x68
.equ MU			=0x69
.equ HU1		=0x6A
.equ MU1		=0x6B
.equ HU2		=0x6C
.equ MU2		=0x6D
.equ UST		=0x6E

.equ hour_H		=0x70
.equ hour_L		=0x71
.equ min_H		=0x72
.equ min_L		=0x73
.equ sec_H		=0x74
.equ sec_L		=0x75

.equ hour_H1	=0x76
.equ hour_L1	=0x77
.equ min_H1		=0x78
.equ min_L1		=0x79

.equ hour_H2	=0x7A
.equ hour_L2	=0x7B
.equ min_H2		=0x7C
.equ min_L2		=0x7D
.equ temp_1		=0x7E
.equ temp_2		=0x7F


.set DQ =2

.def  	DELAY3	=r7  ;
.def  	DELAY1 	=r8  ; To generate long delays
.def  	DELAY2 	=r9  ;
.def  	temp   	=r16  ; temporary working register
.def	temp2	=r17
.def  	param  	=r18  ; Parameter transfer register
.def	wreg	=R19		;General use working register
.def	timeout	=R20		;Timeout value passed to subroutine
.def	ioreg	=R21		;Register for reading/writing DS1820
.def	loopcnt	=R22		;Loop counter for serial read/write

.def	fbin	=r16		;8-bit binary value
.def	tBCDL	=r16		;BCD result MSD
.def	tBCDH	=r17		;BCD result LSD

;***** Port pin definitions
.equ	dsio	=2		;I/O pin connected to PB3
.equ	T0PRE	=0b00000010	;Timer 0 prescaler 4MHz / 8 = 2uS

tim0_ovf:					;вектор переполнение таймера счетчика 0
		set			;Set T flag
		ldi	wreg,0		;Timer 0 off
		out	TCCR0,wreg	;Stop timer
		reti			;Done, return
        reti   

temp_conv:
		rcall	resetds		;Reset DS1820 device
		brts	present		;Device found
		rjmp ex_temp_conv
present:
		ldi	ioreg,0xCC	;Skip ROM command
		rcall	writeds
		ldi	ioreg,0x44	;Convert T command
		rcall	writeds
twait:
		rcall	readds		;Check for conversion complete
		tst	ioreg		;Returns all zeros while busy
		breq	twait
		rcall	resetds		;Reset again
		ldi	ioreg,0xCC	;Skip ROM command
		rcall	writeds
		ldi	ioreg,0xBE	;Read scratchpad command
		rcall	writeds
		rcall	readds		;Read temperature LSB
		sts	temp_L,ioreg	;Store reading
		rcall	readds		;Read temperature MSB
		sts	temp_H,ioreg	;Store reading
		rcall	resetds		;Don't read the rest
		ldi	loopcnt,255
pause:
		ldi	timeout,6
		rcall	delay
		dec	loopcnt
		brne	pause
ex_temp_conv:	
		ret

;***** Write a byte to the DS1820
writeds:
		ldi	loopcnt,8	;Send 8 bits to device
wrloop:
		sbi	DDRB,dsio	;Make I/O pin an output
		cbi	PORTB,dsio	;Pull I/O pin low
		ldi	timeout,-1	;Make timer 0 count 2uS
		rcall	delay
		ror	ioreg		;Get bit into carry
		brcc	wrzero		;If clear, leave line low
		sbi	PORTB,dsio	;Otherwise pull line high
wrzero:
		ldi	timeout,-30	;Make timer 0 count 30 * 2uS
		rcall	delay
		sbi	PORTB,dsio	;Pull line back high
		ldi	timeout,-1	;Make timer 0 count 2uS
		rcall	delay
		dec	loopcnt		;Decrement bit counter
		brne	wrloop		;Loop for all 8 bits
		ret

;***** Read a byte from the DS1820
readds:
		ldi	loopcnt,8	;Get 8 bits from device
		ldi	ioreg,0		;Clear holding register
rdloop:
		sbi	DDRB,dsio	;Make I/O pin an output
		cbi	PORTB,dsio	;Pull I/O pin low
		ldi	timeout,-1	;Make timer 0 count 2uS
		rcall	delay
		cbi	DDRB,dsio	;Make I/O pin an input, will pull up high
		ldi	timeout,-2	;Make timer 0 count 2 * 2uS
		rcall	delay		;Give pin time to go high if needed
		clc			;clear carry, assume bit is a zero
		sbic	PINB,dsio	;Check I/O pin
		sec			;Set carry if pin was high
		ror	ioreg		;Get bit into register
		ldi	timeout,-29	;Make timer 0 count 29 * 2uS
		rcall	delay		;Fill out time slot
		dec	loopcnt		;Decrement bit counter
		brne	rdloop		;Loop for all 8 bits
		ret

;***** Reset DS1820 and read presence pulse, return result in T
resetds:
		sbi	DDRB,dsio	;Make I/O pin an output
		cbi	PORTB,dsio	;Pull I/O pin low
		ldi	timeout,6	;Make timer 0 count 250 * 2uS
		rcall	delay
		sbi	PORTB,dsio	;Pull I/O pin high
		cbi	DDRB,dsio	;Make I/O pin an input
		ldi	timeout,-45	;Make timer 0 count 45 * 2uS
		rcall	delay
		clt			;Assume device not present
		sbis	PINB,dsio	;Check for presence pulse
		set			;Indicate result in T
		ldi	timeout,6	;Make timer 0 count 250 * 2uS
		rcall	delay
		ret

;***** Delay n*2 microseconds using timer 0, delay time passed in timeout
delay:
	in	wreg,SREG	;Save status register
	push	wreg
	out	TCNT0,timeout
	clt			;Clear T
	ldi	wreg,T0PRE	;Timer 0 prescaler
	out	TCCR0,wreg	;Run timer
dwait:
	brtc	dwait		;Wait for timer 0 interrupt to set T
	pop	wreg		;Restore status register
	out	SREG,wreg
	ret
