;
; asm_2313.asm
;
; Created: 15.12.2017 21:57:17
; Author : Leon
;


; Replace with your application code

.device ATTiny2313A

.def	temp = r16
.def	ds_counter	=R17
.def  	lbyte		=R19
.def	hbyte		=R20
.def	ds_command	=R21

.def	wr_counter	=R28







			ldi temp,1<<acd ;отключение компаратора
			out acsr,temp

			

			;ldi temp,0x2A
			;out OSCCAL,temp

		ldi temp,0x7f		;Инициализация портов ввода/вывода
		out DDRB, temp
		ldi temp,0xff
		out PORTB, temp
		
		out DDRD, temp
		out PORTD, temp
		cli

		
		rcall 	ds_reset
		
		

		ldi 	ds_command, 0xCC		; пропуск идентификации
		rcall 	ds_write

		ldi 	temp, 2
		rcall ds_delay

		ldi 	ds_command, 0x44 		;  преобразование
		rcall 	ds_write
		





		ldi 	temp, 255
		rcall 	ds_longdelay			; ожидание 240мс
		ldi 	temp, 255
		rcall 	ds_longdelay			
		ldi 	temp, 255
		rcall 	ds_longdelay			

		ldi 	temp, 255
		rcall 	ds_longdelay			

		
		rcall 	ds_reset 				; вызов обнуления линии
	
		ldi 	ds_command, 0xCC
		rcall 	ds_write

		ldi 	ds_command, 0xBE 		;чтение блокнота, получение результата
		rcall 	ds_write

		ldi		temp, 5
		rcall	ds_delay

		rcall 	ds_read					; принять 


		SBRC hbyte,7
		rjmp set_otr
		rcall pologit
		rjmp set_r1
set_otr:
		rcall otricat
		
		
set_r1:
		ldi r31,4
		mov r30,r2
		lpm r2,z
		mov r30,r3
		lpm r3,z
		
			
		cbi	DDRB,7
		clr temp
		out PORTB,temp
		

start:
		ldi r29,$3F
		out portd,r29
		out portb,r1
		ldi r29,$3C
		out portd,r29
		ldi temp, 10
		rcall 	ds_longdelay
		ldi r29,$3F
		out portd,r29
		
		out portb,r2
		
		
		ldi r29,$33
		out portd,r29
		ldi 	temp,10
		rcall 	ds_longdelay
		ldi r29,$3F
		out portd,r29
		
		out portb,r3
		

		ldi r29,$F
		out portd,r29
		ldi 	temp,10
		rcall 	ds_longdelay


    rjmp start

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

pologit:
	andi lbyte,$F0
	andi hbyte,$F
	add lbyte,hbyte
	swap lbyte
	clr r1
	clr r2
	cpi lbyte,100
	brlo polog_1
	ldi temp, 6
	mov r1,temp
	subi lbyte,100
polog_1:
	cpi lbyte,10
	brlo polog_2
	inc r2
	subi lbyte,10
	rjmp polog_1
polog_2:
	mov r3,lbyte	
	ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

otricat:
	ldi temp,$40
	mov r1,temp
	com lbyte
	com hbyte
	ldi temp,1
	add lbyte,temp
	BRCC otric_1
	inc hbyte
otric_1:
	andi lbyte,$F0
	andi hbyte,$F
	add lbyte,hbyte
	swap lbyte
	clr r2
otric_2:
	cpi lbyte,10
	brlo otric_3
	inc r2
	subi lbyte,10
	rjmp otric_2
otric_3:
	mov r3,lbyte
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	ds_reset:

		cbi	 	PORTB,	PB7
		sbi		DDRB,7

		ldi 	temp,255			
		rcall 	ds_delay
		ldi 	temp,255				
		rcall 	ds_delay
		ldi 	temp,255				
		rcall 	ds_delay
		
		cbi		DDRB,7

		
		
sss:
		in		temp,PINB
		SBRC	temp,7
		rjmp sss

		

sss1:
		in		temp,PINB
		SBRS	temp,7
		rjmp sss1

		

		
		ldi 	temp, 255
		rcall 	ds_longdelay
		ldi 	temp, 255
		rcall 	ds_longdelay
		ldi 	temp, 255
		ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	ds_delay:


		dec 	temp
		brne	ds_delay
		ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ds_write:
		
		ldi 	r31,8
	Label1:
		lsr 	ds_command			
		brcs 	one_wr				
		rjmp 	zero_wr

	
	one_wr:

		cbi 	PORTB, PB7
		sbi		DDRB,7
				
		ldi 	temp,18			
		rcall 	ds_delay 
		
		cbi		DDRB,7

		ldi 	temp,54 			
		rcall 	ds_delay	
		rjmp 	Label2

	zero_wr:
		
		cbi 	PORTB, PB7
		sbi		DDRB,7
			
		ldi 	temp, 72			; 60мкс
		rcall 	ds_delay 
			
		cbi		DDRB,7

	Label2:
	ldi 	temp, 1				
	rcall 	ds_delay
	dec 	r31
	brne 	Label1				
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ds_longdelay:

		mov 	ds_counter, temp		
		Label:
		ldi 	temp,0xff
		rcall 	ds_delay
		ldi 	temp,0xff
		rcall 	ds_delay
		ldi 	temp,0xff
		rcall 	ds_delay
		ldi 	temp,0xff
		rcall 	ds_delay
		ldi 	temp,0xff
		rcall 	ds_delay
		ldi 	temp,43
		rcall 	ds_delay
		dec 	ds_counter
		brne 	Label
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ds_read:

	ldi wr_counter, 8
	ldi lbyte, 0x00

	; new section
	
r_byte:
	cbi PORTB,PB7
	sbi		DDRB,7

	ldi temp,18
	rcall ds_delay				
	
	cbi		DDRB,7

	ldi temp, 18
	rcall ds_delay				

	SBIC PINB,PB7            ;прорустить если бит очищен
	rjmp  set_1

	CLC
	rjmp end_bitl
set_1:
	
	SEC
end_bitl:
	ror lbyte
	ldi temp, 45				; 60мкс
	rcall ds_delay

	dec wr_counter
	brne r_byte

	ldi wr_counter, 8
	ldi hbyte, 0x00

	
	
h_byte:
	cbi PORTB,PB7
	sbi		DDRB,7

	ldi temp,18
	rcall ds_delay				; синхроимпульс 6mks   ---2,7мкс
	
	cbi		DDRB,7

	ldi temp, 18
	rcall ds_delay				; проверка через 8мкс

	SBIC PINB,PB7            ;прорустить если бит очищен
	rjmp  set_h1

	CLC
	rjmp end_bith
set_h1:
	
	SEC
end_bith:
	ROR hbyte
	ldi temp, 45				; 60мкс
	rcall ds_delay

	dec wr_counter
	brne h_byte

	ret


.org 0x200
.db $3F,6,$5b,$4F,$66,$6D,$7D,7,$7F,$6F