.include "m8def.inc"
.list					
.def temp=r16

;________Прерывания____________		
		
		.cseg 			; Выбор сегмента программного кода
		.org	0		; Установка текущего адреса на ноль

		rjmp	init	; Переход на начало программы
		.org	0x008
		rjmp    start1
		rjmp    start	; Прерывание по переполнению T0

init:
		cli
;________Вершина стека__________

		ldi     temp, high(ramend)
		out     SPH, temp
		ldi     temp, low(ramend)
		out		SPL, temp

;________Инициализация компаратора

		ldi 	temp, 0x80		; Выключение компаратора
		out		ACSR, temp

;________Инициализация портов ВВ
	
		ldi     temp, 0b11111111
		out     DDRB, temp
		out     DDRD, temp

;________Инициализация таймера

		ldi		temp, (1<<TOIE0|1<<TOIE1)
		out		timsk, temp
		ldi		temp, (1<<CS00)
		out		tccr0, temp
		ldi		temp, (1<<CS10)
		out		tccr1b, temp

;________Программа

		ldi     ZL, low(sinus*2)
		ldi     ZH, high(sinus*2)
		ldi		r21, 16

sram:
		lpm		temp, Z
		st		Z+, temp
		dec		r21
		brne	sram

		ldi     ZL, low(sinus*2)
		ldi     ZH, high(sinus*2)

		mov		YH, ZH
		mov		YL, ZL

		ldi		temp, 0xE6
		out		TCNT0, temp

		ldi		temp, 0xCF
		out		TCNT1H, temp
		ldi		temp, 0x2C
		out		TCNT1L, temp
		sei

wait:	rjmp wait

start:
		ldi		temp, 0xE6
		out		TCNT0, temp
		cpi		r17, 0
		breq	pv
		cpi		r17, 1
		breq	pu
		cpi		r17, 2
		breq	ou
		cpi		r17, 3
		breq	ov

pv:
		lpm		temp, Z+
		out		PORTB, temp
		inc		r18
		cpi		r18, 15
		brne	end
		ldi		r17, 1
end:
		reti
		
pu:
		lpm		temp, Z
		dec		ZL
		out		PORTB, temp
		dec		r18
		cpi		r18, 0
		brne	end
		ldi		r17, 2
		rjmp	end
		
ou:
		lpm		temp, Z+
		com		temp
		out		PORTB, temp
		inc		r18
		cpi		r18, 15
		brne	end
		ldi		r17, 3
		rjmp	end

ov:
		lpm		temp, Z
		dec		ZL
		com		temp
		out		PORTB, temp
		dec		r18
		cpi		r18, 0
		brne	end
		ldi		r17, 0
		rjmp	end

start1:

		push	ZH
		push	ZL
		ldi		temp, 0xCF
		out		TCNT1H, temp
		ldi		temp, 0x2C
		out		TCNT1L, temp
		cpi		r19, 0
		breq	pv1
		cpi		r19, 1
		breq	pu1
		cpi		r19, 2
		breq	ou1
		cpi		r19, 3
		breq	ov1

pv1:
		ld		temp, Y+
		out		PORTD, temp
		inc		r20
		cpi		r20, 15
		brne	end1
		ldi		r19, 1
end1:
		pop		ZL
		pop		ZH
		reti
		
pu1:
		ld		temp, Y
		dec		YL
		out		PORTD, temp
		dec		r20
		cpi		r20, 0
		brne	end1
		ldi		r19, 2
		rjmp	end1
		
ou1:
		ld		temp, Y+
		com		temp
		out		PORTD, temp
		inc		r20
		cpi		r20, 15
		brne	end1
		ldi		r19, 3
		rjmp	end1

ov1:
		ld		temp, Y
		dec		YL
		com		temp
		out		PORTD, temp
		dec		r20
		cpi		r20, 0
		brne	end1
		ldi		r19, 0
		rjmp	end1


sinus:
.db 127, 139, 152, 164, 176, 187, 198, 208, 217, 225
.db 233, 239, 244, 249, 252, 253.






