;####################    LCD   ##########################


.nolist						;librerias
.include "m8def.inc"
.list
.listmac


.def tmp=r19


; Define the pin numbers
.equ   E       = PB1    ;LCD e pin connected to PC1
.equ   RW      = PB2    ;LCD r/w pin connected to PC2
.equ   RS      = PB3    ;LCD rs pin connected to PC3
  



.org 0x000 rjmp RESET
.org 0x001 rjmp EX_INT0
.org 0x002 //rjmp EX_INT1
.org 0x003 //rjmp TIMER2_COMP
.org 0x004 //rjmp TIMER2_OVF
.org 0x005 //rjmp TIMER1_CAPT
.org 0x006 //rjmp TIMER1_COMPA
.org 0x007 //rjmp TIMER1_COMPB
.org 0x008 //rjmp TIMER1_OVF
.org 0x009 //rjmp TIMER0_OVF  
.org 0x00A //rjmp SPI_STC
.org 0x00B //rjmp USART_RXC
.org 0x00C //rjmp USART_UDRE
.org 0x00D //rjmp USART_TXC
.org 0x00E //rjmp AD_C
.org 0x00F //rjmp EE_RDY
.org 0x010 //rjmp ANA_COMP
.org 0x011 //rrjmp TWI_
.org 0x012 //rjmp SPM_RDY

.org 0x013
 

RESET:

cli
	ldi r16, low(ramend)	;
	ldi r17, high(ramend)	;
	out spl, r16			;
	out sph, r17
				
	ldi r16, 0xFF     ;All pins are outputs 
	out DDRC, r16
	ldi r16, 0x00	  ;Setup port pins 	;
	out PORTC, r16    ;Pull all pins low 


 ldi tmp, (1 << INT1)| (1 << INT0)
 out GICR,tmp

 ldi tmp, (1<<ISC11)| (0<<ISC10) | (1<<ISC01)| (0<<ISC00)
 out MCUCR,tmp

	rcall INIT_LCD
sei

LOOP:					
	rjmp LOOP

EX_INT0:

    rcall DS18B20_LCD

reti

DS18B20_LCD:
	ldi ZL, 0x90


	ldi tmp, 0b10000000		;Set DDRAM adres
	rcall SEND_ADR
	rcall DELAY
	Line1:
		ldi tmp, 'T'
		rcall SEND_DATA
		rcall WAIT

		brne line1
		ldi tmp, 'E'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'M'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'P'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'E'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'R'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'A'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'T'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'U'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'R'
		rcall SEND_DATA
		rcall WAIT
	
		brne line1
		ldi tmp, 'E'
		rcall SEND_DATA
		rcall WAIT
		
	
				
	;	ldi tmp, 0b11000000		;Set DDRAM adres 01
	;	rcall SEND_ADR
	;	rcall DELAY
	;	ldi i, 0x0A

	;Line2:
	;	ld tmp, Z+
	;	rcall SEND_DATA
	;	rcall WAIT
	;	dec i
	;	brne line2
	ret	

SEND_ADR:
	cbi portC, RS
	cbi portC, RW
	sbi portC, E
	out portD, tmp
	rcall DELAY
	cbi portC, E
	ret

SEND_DATA:
	sbi portC, RS
	sbi portC, E
	out portD, tmp
	rcall DELAY
	cbi portC, E
	cbi portC, RS
	ret


WAIT:
	ldi tmp, (0<<DDB7)
	out ddrD, tmp
	cbi portC, RS
	sbi portC, RW
	sbi portC, E

	WAIT_IN:
		in tmp, pinB
		sbrc tmp, DDB7
		rjmp WAIT_IN
		cbi portC, E
		cbi portC, RS
		cbi portC, RW
		ldi tmp, 0xFF
		out DDRD, tmp
		ret



DELAY:
	ldi r16, $03
	
	PETLA_1:
		ldi r17, $0F
	PETLA_2:
		ldi r18, $0F
	PETLA_3:
		dec r18
		brne PETLA_3
		dec r17
		brne PETLA_2
		dec r16
		brne PETLA_1
		ret


INIT_LCD:

 
	ldi tmp, 0b00111000	;Function set: two lines, znaki 5x8
	rcall SEND_ADR
	rcall DELAY 


	ldi tmp, 0b00001100	;Display on/off - Display OFF
	rcall SEND_ADR
	rcall DELAY

	ldi tmp, 0x01		;Display CLR
	rcall SEND_ADR
	rcall DELAY 

	ldi tmp, 0x07		;Entry mode set
	rcall SEND_ADR
	rcall DELAY

	ldi tmp, 0x02		;Home
	rcall SEND_ADR
	rcall DELAY

	ret
