;	Hlavicka
; write_both
; write_left
; write_right
; write_left_with_read
; write_right_with_read
; write_both_with_read
; select_line_both
; select_line_left
; select_line_right
; select_X_both
; select_X_left
; select_X_right
; Draw_main_screen
; Draw_right_menu
; X_line
; Clear_display
;;; Clear_main_screen
; Show_Dec_number_0_to_255
; Show_Dec_number_0_to_255_without_zeros
; Show_digit
; Show_tens_and_units
; Show_tens_and_units_without_zero
; Show_tens_and_units_without_zeros_all
; wait_for_LCD
; signal_enable


; *-----------------------------------------*

write_left:
	RCALL wait_for_LCD	; pockame, az bude LCD pripraveno cist data

;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp	; nastavime data_port na vystupni
; Data vystupni - nastavi wait_for_LCD pri zapnuti LCD
	OUT data_port,data	; a na data_port nahrajeme informace
 ; ulozena v "data"

	SBI control_port_LCD,RS	; RS=1 - data
	CBI control_port_LCD,RW	; RW=0 - zapis
	SBI control_port_LCD,CS1	; CS1=1 - leva pulka displeje
	CBI control_port_LCD,CS2	; CS2=0 - prava polovina vypla
	RCALL signal_enable	; "Enter"

	RET
; *-----------------------------------------*
write_right:
	RCALL wait_for_LCD

;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp	; nastavime data_port na vystupni
	OUT data_port,data	; a na data_port nahrajeme informace
 ; ulozena v "data"

	SBI control_port_LCD,RS
	CBI control_port_LCD,RW
	CBI control_port_LCD,CS1
	SBI control_port_LCD,CS2
	RCALL signal_enable

	RET
; *-----------------------------------------*
write_both:
	RCALL wait_for_LCD

;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp
	OUT data_port,data
	
	SBI control_port_LCD,RS
	CBI control_port_LCD,RW
	SBI control_port_LCD,CS1
	SBI control_port_LCD,CS2
	RCALL signal_enable

	RET
; *-----------------------------------------*
write_left_with_read:	; precte si data na LCD a zapise dalsi data tak,
  ; aby zachoval puvodni data a pridal nova - musime zadat adresu, ze ktere
  ; budeme cist a zapisovat (na jiny zpusob jsem zatim neprisel) - ulozit do
  ; promenne number2
  ; Postup: Zvolit rezim cteni, zvolit Xovou souradnici, rezim cteni a cist :)
  ; data - informace, ktera ma byt pridana
  ; number2 - Xova souradnice - 0~63

	SBI control_port_LCD,CS1
	CBI control_port_LCD,CS2
  ; Nastavili jsme aktivni levy radic
	RCALL wait_for_LCD

  ; Nastavime Xovou souradnici
	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp	; Nastavime data_port jako vystupni
  ; Nemusime, jiz wait_for_LCD to nastavil
	LDI tmp,0b01000000
	ADD tmp,number2		; timto se vypocte Xova souradnice
	OUT data_port,tmp	; a odesleme informace na LCD
	RCALL signal_enable	

	RCALL wait_for_LCD


; Nyni nastavime displej na cteni dat
	LDI tmp,$00	; nastavit datovou sbernici na vstupni
	OUT data_port,tmp	; vymazeme data na portu (jinak tam zustanou
  ; posledni zapsana data - nezadouci)
	OUT IO_data_LCD,tmp	; nastavime port na vstupni

	SBI control_port_LCD,RS
	SBI control_port_LCD,RW
  ; Nastavili jsme cteni dat z displeje
	RCALL signal_enable_for_read	; tzv. "dummy read". je nutny
	RCALL signal_enable_for_read	; a "skutecny" read..

	IN tmp,data_port_in	; a nacteme data do tmp
	OR data,tmp	; vytvorime vysledny bajt
  ; pockame az bude LCD pripraven a zaroven nastavime port na vystupni
	RCALL wait_for_LCD


  ; nastavi se Xova souradnice (z mista, ze ktereho jsme cetli zase budeme
  ; zapisovat -> nutno znova definovat Xovou souradnici, protoze radic
  ; si Xovou souradnici po kazdem cteni/zapisu posune o 1 smerem doprava
	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp	; Nastavime data_port jako vystupni
  ; Opet zbytecne, nastavil wait_for_LCD 
	LDI tmp,0b01000000
	ADD tmp,number2		; timto se vypocte Xova souradnice
	OUT data_port,tmp	; a odesleme informace na LCD
	RCALL signal_enable	

  ; a zapisi se data
	RCALL write_left

	RET
; *-----------------------------------------*
write_right_with_read:
  ; data - informace, ktera ma byt pridana
  ; number2 - Xova souradnice - 0~63
	CBI control_port_LCD,CS1
	SBI control_port_LCD,CS2
  ; Nastavili jsme aktivni pravy radic
	RCALl wait_for_LCD

   ; Nastavit Xovou souradnici
	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	LDI tmp,0b01000000
	ADD tmp,number2
	OUT data_port,tmp
	RCALL signal_enable

	RCALL wait_for_LCD

  ; Nastavit opet cteni dat z LCD
	LDI tmp,$00
	OUT data_port,tmp
	OUT IO_data_LCD,tmp

	SBI control_port_LCD,RS
	SBI control_port_LCD,RW
	RCALL signal_enable_for_read
	RCALL signal_enable_for_read

	IN tmp,data_port_in
	OR data,tmp
	RCALL wait_for_LCD

  ; Nastavit zase Xovou souradnici (LCD si posunul X o 1)
	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	LDI tmp,0b01000000
	ADD tmp,number2
	OUT data_port,tmp
	RCALL signal_enable

  ; A zapsat na LCD
	RCALL write_right

	RET
; *-----------------------------------------*
write_both_with_read:
  ; data - informace, ktera ma byt pridana
  ; number2 - Xova souradnice - 0~63
	SBI control_port_LCD,CS1
	SBI control_port_LCD,CS2
  ; Nastavili jsme aktivni oba radice
	RCALl wait_for_LCD

  ; Nastavit Xovou souradnici
	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	LDI tmp,0b01000000
	ADD tmp,number2
	OUT data_port,tmp
	RCALL signal_enable

	RCALL wait_for_LCD

  ; Nastavit opet cteni dat z LCD
	LDI tmp,$00
	OUT data_port,tmp
	OUT IO_data_LCD,tmp

	SBI control_port_LCD,RS
	SBI control_port_LCD,RW
	RCALL signal_enable_for_read
	RCALL signal_enable_for_read

	IN tmp,data_port_in
	OR data,tmp
	RCALL wait_for_LCD

  ; Nastavit zase Xovou souradnici (LCD si posunul X o 1)
	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	LDI tmp,0b01000000
	ADD tmp,number2
	OUT data_port,tmp
	RCALL signal_enable

  ; A zapsat na LCD
	RCALL write_both

	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
select_line_both:
; podprogram slouzi k "aktivaci" urciteho radku na obou radicich
	RCALL wait_for_LCD

;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp	; nastavime data_port na vystupni (pro vsechny pripady)

	SBI control_port_LCD,CS1	; Plati pro 1. radic (leva polovina)
	SBI control_port_LCD,CS2	; Plati i pro 2. radic (prava polovina)
	CBI control_port_LCD,RS	; RS=0 - instrukce
	CBI control_port_LCD,RW	; RW=0 - zapis
	LDI tmp,0b10111000	; ulozime do tmp kod pro nastaveni 0.teho radku
	ADD tmp,data	; pricteme k 0 pocet pozadovanych radku a vysledek
  ; ulozime do tmp - obsah v "data" zustava nezmenen
	OUT data_port,tmp	; nahrajeme na sbernici
	RCALL signal_enable	; "Enter"

	RET
; *-----------------------------------------*
select_line_left:
	RCALL wait_for_LCD
;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp

	SBI control_port_LCD,CS1
	CBI control_port_LCD,CS2
	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	LDI tmp,0b10111000
	ADD tmp,data

	OUT data_port,tmp
	RCALL signal_enable

	RET
; *-----------------------------------------*
select_line_right:
	RCALL wait_for_LCD
;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp

	CBI control_port_LCD,CS1
	SBI control_port_LCD,CS2
	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	LDI tmp,0b10111000
	ADD tmp,data

	OUT data_port,tmp
	RCALL signal_enable

	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
select_X_both:
	RCALL wait_for_LCD
; podprogram slouzi k posouvani se v X souradnicich
;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp	; viz. vyse

	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	SBI control_port_LCD,CS1
	SBI control_port_LCD,CS2
	LDI tmp,0b01000000
	ADD tmp,data
; V podstate ten samy princip jako vyse. Akorat ze kod je jiny
	OUT data_port,tmp
	RCALL signal_enable	; uplne to same ;-)

	RET
; *-----------------------------------------*
select_X_left:
	RCALL wait_for_LCD
;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp

	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	SBI control_port_LCD,CS1
	CBI control_port_LCD,CS2
	LDI tmp,0b01000000
	ADD tmp,data

	OUT data_port,tmp
	RCALL signal_enable

	RET
; *-----------------------------------------*
select_X_right:
	RCALL wait_for_LCD
;	LDI tmp,$FF
;	OUT IO_data_LCD,tmp

	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	CBI control_port_LCD,CS1
	SBI control_port_LCD,CS2
	LDI tmp,0b01000000
	ADD tmp,data

	OUT data_port,tmp
	RCALL signal_enable

	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
rotate_Y_left:
  ; Umozni nastavit vertikalni pozici (data[0~63])
	RCALL wait_for_LCD

	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	SBI control_port_LCD,CS1
	CBI control_port_LCD,CS2

	LDI tmp,0b11000000
	ADD tmp,data

	OUT data_port,tmp
	RCALL signal_enable

	RET
; *-----------------------------------------*
rotate_Y_right:
  ; Umozni nastavit vertikalni pozici (data[0~63])
	RCALL wait_for_LCD

	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	CBI control_port_LCD,CS1
	SBI control_port_LCD,CS2

	LDI tmp,0b11000000
	ADD tmp,data

	OUT data_port,tmp
	RCALL signal_enable

	RET
; *-----------------------------------------*
rotate_Y_both:
  ; Umozni nastavit vertikalni pozici (data[0~63])
	RCALL wait_for_LCD

	CBI control_port_LCD,RS
	CBI control_port_LCD,RW
	SBI control_port_LCD,CS1
	SBI control_port_LCD,CS2

	LDI tmp,0b11000000
	ADD tmp,data

	OUT data_port,tmp
	RCALL signal_enable

	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
Draw_main_screen:
; prida pomocne tecky (musi byt nejdrive, jinak by premazaly uz vykreslena
; data

; pomocne tecky 0. a 2. radek
	CLR CNT		; bude urcovat radek
	LDI time1,0b00000100	; pouzijeme promennou time1 na vykreslovani
; pomocnych tecek - tuto promennou vyuzivaji pouze podprogramy wait_* a
; tady podprogramy wait_* nepouzivame -> promenna nebude prepsana
Draw_dots_top:	; vykreslime tecky na horni polovine displeje
	MOV data,CNT
	RCALL select_line_both
	LDI CNT2,9	; urcuje X pozici na leve strane
	LDI CNT3,5	; urcuje X pozici na prave strane
Draw_dots_top_left:
	MOV data,CNT2
	RCALL select_X_left
	MOV data,time1
	RCALL write_left
	LDI tmp,10
	ADD CNT2,tmp
	CPI CNT2,69
	BRNE Draw_dots_top_left
Draw_dots_top_right:
	MOV data,CNT3
	RCALL select_X_right
	MOV data,time1
	RCALL write_right
	LDI tmp,10
	ADD CNT3,tmp
	CPI CNT3,35
	BRNE Draw_dots_top_right

	LSL time1
	LSL time1
	INC CNT
	CPI CNT,3
	BRNE Draw_dots_top

; ted vykreslime tecky na spodni casti displeje
	LDI CNT,5
	LDI time1,0b00000100
Draw_dots_bottom:
	MOV data,CNT
	RCALL select_line_both
	LDI CNT2,9
	LDI CNT3,5
Draw_dots_bottom_left:
	MOV data,CNT2
	RCALL select_X_left
	MOV data,time1
	RCALL write_left
	LDI tmp,10
	ADD CNT2,tmp
	CPI CNT2,69
	BRNE Draw_dots_bottom_left
Draw_dots_bottom_right:
	MOV data,CNT3
	RCALL select_X_right
	MOV data,time1
	RCALL write_right
	LDI tmp,10
	ADD CNT3,tmp
	CPI CNT3,35
	BRNE Draw_dots_bottom_right

	LSL time1
	LSL time1
	INC CNT
	CPI CNT,8
	BRNE Draw_dots_bottom

	LDI data,4
	RCALL select_line_both
	CLR data
	RCALL select_X_both
	CLR CNT
X_dash_line_L: ; Skoro prez cely displej
 ; Aby neprekreslovalo leve menu (treba po vykresleni signalu se musi
 ; chvilku pockat, aby lidske oko postrehlo signal zobrazeny na LCD
 ; a potom se musi zobrazeny signal smazat. A to je problem, protoze se
 ; smazou i ty pomocne tecky atd... takze tento cely podprogram
 ;  ( Draw_main_screen ) bude mit za ukol zobrazit jen svoji cast na
 ; displeji a ne jinou (aby ji neprepsal)

	LDI data,0b00000001
	RCALL write_left
	INC CNT
	INC CNT
	MOV data,CNT
	RCALL select_X_left
	CPI CNT,64
	BRNE X_dash_line_L

	CLR CNT
X_dash_line_R:
	LDI data,0b00000001
	RCALL write_right
	INC CNT
	INC CNT
	MOV data,CNT
	RCALL select_X_right
	CPI CNT,36
	BRNE X_dash_line_R

; nakresli - - - - - - - - - - - - - - - - - - - - - - - - - -
  ; prida pomocne rysky v Xove ose - museji se vykreslit ted
	LDI CNT2,9	; pocatecni hodnota X na leve strane
	LDI CNT3,5	; pocatecni hodnota X na prave strane
	LDI data,3
	RCALL select_line_both	; vybereme 3. radek
Draw_x_cross_left_3_line:
	MOV data,CNT2
	RCALL select_X_left
	LDI data,0b10000000
	RCALL write_left
	LDI tmp,10
	ADD CNT2,tmp
	CPI CNT2,69
	BRNE Draw_x_cross_left_3_line

Draw_x_cross_right_3_line:
	MOV data,CNT3
	RCALL select_X_right
	LDI data,0b10000000
	RCALL write_right
	LDI tmp,10
	ADD CNT3,tmp
	CPI CNT3,35
	BRNE Draw_x_cross_right_3_line

	LDI CNT2,9
	LDI CNT3,5
	LDI data,4
	RCALL select_line_both
Draw_x_cross_left_4_line:
	MOV data,CNT2
	RCALL select_X_left
	LDI data,0b00000011
	RCALL write_left
	LDI tmp,10
	ADD CNT2,tmp
	CPI CNT2,69
	BRNE Draw_x_cross_left_4_line

Draw_x_cross_right_4_line:
	MOV data,CNT3
	RCALL select_X_right
	LDI data,0b00000011
	RCALL write_right
	LDI tmp,10
	ADD CNT3,tmp
	CPI CNT3,35
	BRNE Draw_x_cross_right_4_line


	CLR CNT
Y_dash_line: ; je v leve polovine displeje
	MOV data,CNT
	RCALL select_line_left
	LDI data,49
	RCALL select_X_left
	LDI data,$AA	; 1010 1010
	RCALL write_left
	INC CNT
	CPI CNT,8
	BRNE Y_dash_line


; Dekorace
; Udela v prostred tecku
	LDI data,4
	RCALL select_line_left
	LDI data,49
	RCALL select_X_left
	LDI data,0b10101011
	RCALL write_left
  ; Prida pomocne rysky v Yove ose
	CLR CNT	; pocita radky
	LDI CNT2,0b00000100	; prvni a posledni bod v krizku
	LDI CNT3,0b10101110	; stred krizku
Draw_y_cross_0_2:
	MOV data,CNT
	RCALL Pre_set		; Ulehci to praci a pamet programu
	MOV data,CNT2
	RCALL write_left
	MOV data,CNT3
	RCALL write_left
	MOV data,CNT2
	RCALL write_left
	ROL CNT2
	ROL CNT2
	ROL CNT3
	ROL CNT3
	INC CNT3
	INC CNT
	CPI CNT,3
	BRNE Draw_y_cross_0_2

	LDI CNT,5	; pocet radku
	LDI CNT2,0b00000100	; prvni a posledni bod v krizku
	LDI CNT3,0b10101110	; stred krizku
Draw_y_cross_5_7:
	MOV data,CNT
	RCALL Pre_set		; Ulehci to praci a pamet programu
	MOV data,CNT2
	RCALL write_left
	MOV data,CNT3
	RCALL write_left
	MOV data,CNT2
	RCALL write_left
	ROL CNT2
	ROL CNT2
	ROL CNT3
	ROL CNT3
	INC CNT3
	INC CNT
	CPI CNT,8
	BRNE Draw_y_cross_5_7


	RET
; *-----------------------------------------*
Pre_set:
	RCALL select_line_left
	LDI data,48
	RCALL select_X_left
	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
Draw_right_menu:
	CLR CNT
left_side_of_menu:	; >|<   |
	LDI data,0
	RCALL select_line_right ; prubezne meni radek


	LDI data,35	; nastavime pevne sloupec
	RCALL select_X_right


	LDI data,$FF ; 0b11111111
	RCALL write_right ; a zapiseme plnou caru


	RCALL clear_right_menu_line	; Smaze aktualni radek


	LDI data,1
	RCALL select_line_right
	LDI data,35
	RCALL select_X_right
	LDI data,$FF
	RCALL write_right
	RCALL clear_right_menu_line

  ; Ted prijde trochu zmena
	LDI data,2
	RCALL select_line_right
	LDI data,35
	RCALL select_X_right
	LDI data,0b10101111
	RCALL write_right
	RCALL clear_right_menu_line

  ; A nasledujici radky jsou prazdne
	LDI data,3
	RCALL select_line_right
	LDI data,36
	RCALL select_X_right
	RCALL clear_right_menu_line

	LDI data,4
	RCALL select_line_right
	LDI data,36
	RCALL select_X_right
	RCALL clear_right_menu_line

	LDI data,5
	RCALL select_line_right
	LDI data,36
	RCALL select_X_right
	RCALL clear_right_menu_line

	LDI data,6
	RCALL select_line_right
	LDI data,36
	RCALL select_X_right
	RCALL clear_right_menu_line

	LDI data,7
	RCALL select_line_right
	LDI data,38	; Kvuli dekoracim - default 36
	RCALL select_X_right
	RCALL clear_right_menu_line

; right_side
right_side_of_menu:	; |   >|<
	LDI data,1	; opet kvuli dekoracim (default: 0)
	RCALL select_line_right
	LDI data,63
	RCALL select_X_right
	LDI data,$FF
	RCALL write_right

	LDI data,2
	RCALL select_line_right
	LDI data,63
	RCALL select_X_right
	LDI data,0b10101111
	RCALL write_right


; top side
	CLR data
	RCALL select_line_right
	LDI data,36
	MOV CNT,data
	RCALL select_X_right
	LDI data,0b00000001

top_side_of_menu:	; ^^^^^^

	RCALL write_right
	INC CNT
	CPI CNT,61	; dekorace (default: 63)
	BRNE top_side_of_menu


	LDI data,7
	RCALL select_line_right
	LDI CNT,38	; dekorace (default: 36)
	MOV data,CNT
	RCALL select_X_right
;bottom_side_of_menu:	; _______
;	LDI data,0b10000000
;	RCALL write_right
;	INC CNT
;	CPI CNT,63
;	BRNE bottom_side_of_menu

; DEKORACE
  ; Levy horni roh
	LDI data,61
	RCALL select_X_right
	CLR data
	RCALL select_line_right
	LDI data,0b00000010
	RCALL write_right
	LDI data,0b00000100
	RCALL write_right
	LDI data,0b11111000
	RCALL write_right
  ; pravy spodni roh
;	LDI data,7
;	RCALL select_line_right
;	LDI data,35
;	RCALL select_X_right
;	LDI data,0b00011111
;	RCALL write_right
;	LDI data,0b00100000
;	RCALL write_right
;	LDI data,0b01000000
;	RCALL write_right

  ; A nakonec vykreslit informace o casove zakladne
;	LDS number,TimeBaseSRAM	; do "number" si ulozime informace o casovce
  ; a pomoci toho, rozhodneme, jake cislo se ma zobrazit

  ; 128 odpovida 0.1us
	

	RET

; *-----------------------------------------*
clear_right_menu_line:
  ; a ted musime "vycistit" zbytek okna - aby pri znovuvykresleni nezustaly
  ; na displeji puvodni data. Tento podprogram jen smaze dany radek
	CLR data	; nyni se budou zapisovat pouze 0b00000000 -> nic
	LDI CNT2,36	; zacne od 36. pixelu prave poloviny
clear_right_menu_cycle:
	RCALL write_right
	INC CNT2
	CPI CNT2,63	; az se dostane ke konci -> pokracuj na INC CNT
	BRNE clear_right_menu_cycle
	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
X_line:		; bude vykreslovat v lajne, to, co bude ulozeno v "data"
; Pouizva promenne data, CNT
	LDI CNT,$00
X_line_1:
	RCALL write_both
	INC CNT
	CPI CNT,64
	BRNE X_line_1

	RET
; *-----------------------------------------*
Clear_display:
; Pouziva promenne: data,CNT (v dusledku)
; Zacne se od Nejnizsiho radku a pokracuje se az k prvnimu. Predpoklad je, ze
  ; pokud "uzivatel" smaze data na LCD, chce zapisovat na prvni radek
	LDI data,7	; v data je ted ulozeno 7
	RCALL select_line_both
	CLR data
	RCALL X_line

	LDI data,6
	RCALL select_line_both
	CLR data
	RCALL X_line

	LDI data,5
	RCALL select_line_both
	CLR data
	RCALL X_line

	LDI data,4
	RCALL select_line_both
	CLR data
	RCALL X_line

	LDI data,3
	RCALL select_line_both
	CLR data
	RCALL X_line

	LDI data,2
	RCALL select_line_both
	CLR data
	RCALL X_line

	LDI data,1
	RCALL select_line_both
	CLR data
	RCALL X_line

	LDI data,0
	RCALL select_line_both	; Timto se nastavi radek na prvni
;	CLR data	; Jiz nastaveno
	RCALL X_line

	RCALL select_X_both	; a nastavi se Xova souradnice na prvni pixel

	RET
; *-----------------------------------------*
X_line_R:		; bude vykreslovat v lajne, to, co bude ulozeno v
  ; "data", ale jen na pravem radici
; Pouiva promenne data, CNT
	LDI CNT,$00
X_line_R_1:
	RCALL write_right
	INC CNT
	CPI CNT,64
	BRNE X_line_R_1

	RET
; *-----------------------------------------*
Clear_right_half_of_LCD:
; Pouziva promenne: data,CNT (v dusledku)
	CLR data	; v data je ted ulozeno 0000 0000
	RCALL select_line_right
	RCALL X_line_R

	LDI data,1
	RCALL select_line_right
	CLR data
	RCALL X_line_R

	LDI data,2
	RCALL select_line_right
	CLR data
	RCALL X_line_R

	LDI data,3
	RCALL select_line_right
	CLR data
	RCALL X_line_R

	LDI data,4
	RCALL select_line_right
	CLR data
	RCALL X_line_R

	LDI data,5
	RCALL select_line_right
	CLR data
	RCALL X_line_R

	LDI data,6
	RCALL select_line_right
	CLR data
	RCALL X_line_R

	LDI data,7
	RCALL select_line_right
	CLR data
	RCALL X_line_R

	RET
; *-----------------------------------------*
; - behem vyvoje zjisteno, ze neni potreba
;Clear_main_screen:
;	CLR CNT2	; pocita stranky (radky - 0~7)
;
;Clear_both_side:
;	CLR CNT		; pocita Xove souradnice
;	MOV data,CNT2
;	RCALL select_line_both	; vybereme patricny radek
;	MOV data,CNT	; nebo CLR data
;	RCALL select_X_both	; a pak nastavime Xovou souradnici
;Clear_both_side_line:
;	RCALL write_both	; zapiseme $00 na displej (nic :) )
;	INC CNT		; indikuje posuv v Xove ose
;	CPI CNT,35	; pokud dosahneme na ose X souradnice 34, pak je
;  ; potreba preskocit na dalsi radek a gumovat dalsi radek
;	BRNE Clear_both_side_line
;	
;	INC CNT2	; posun o jeden radek (jen promenna - zvyseni o 1)
;	CPI CNT2,8	; 8. radek neni na LCD -> skok
;	BRNE Clear_both_side
;
;	DEC CNT2
;Clear_left_side:
;	LDI CNT,35
;	MOV data,CNT2
;	RCALL select_line_left
;	MOV data,CNT
;	RCALL select_X_left
;Clear_left_side_line:
;	CLR data
;	RCALL write_left
;	INC CNT
;	CPI CNT,64
;	BRNE Clear_left_side_line
;
;	DEC CNT2
;	CPI CNT2,$FF
;	BRNE Clear_left_side
;
;	RET


; Musi to byt definovane nejdrive tady. V math.asm uz to je jen pro
  ; vetsi prehlednost
.DEF units = R0
.DEF tens = R1
.DEF hundreds = R2

Show_Dec_number_0_to_255:
  ; "Tupe" zobrazi stovky, jednotky a desitky
	MOV number,hundreds	; do number si nahrajeme stovky
  ; Musime totiz zacit od nejvetsich radu
	RCALL Show_digit
	MOV number,tens
	RCALL Show_digit
	MOV number,units
	RCALL Show_digit

	RET
; *-----------------------------------------*
Show_Dec_number_0_to_255_without_zeros:
  ; to same jako vyse, ale pokud jsou na miste desitek, nebo stovek nuly, tak
  ; se nezobrazi -> muze to vypadat mene strojove
	MOV number,hundreds
	CPI number,0	; test jestli nejsou stovky v nule...
	BREQ Show_Dec_number_0_to_255_without_zeros_tens	; pokud ano,
  ; tak skok na dane navesti (tzn. cislo se nevypise)
	RCALL Show_digit	; v opacnem pripade se vypise cislo
  ; a pokud vypiseme stovky, tak se musi vypsat i desitky, a to i v pripade,
  ; ze budou desitky rovny nule
	MOV number,tens
	RCALL Show_digit	; vypise desitky
	RJMP Show_Dec_number_0_to_255_without_zeros_units	; a hura
  ; vypsat jednotky
Show_Dec_number_0_to_255_without_zeros_tens:
	MOV number,tens
	CPI number,0
	BREQ Show_Dec_number_0_to_255_without_zeros_units
	RCALL Show_digit
Show_Dec_number_0_to_255_without_zeros_units:
	MOV number,units
	RCALL Show_digit	; jednotky se vypisi vzdy, aby bylo pripadne
  ; videt, ze pocet je nula. Prazdne policko by bylo asi matouci

	RET
; *-----------------------------------------*
Show_digit:	; pracuje s registrem "number" a "tmp"
	LDS tmp,Font_micro_SRAM	; Do tmp se ulozi informace o velikosti pisma
	CPI tmp,0
	BRNE Show_digit_u_0	; V pripade, ze neni promenna nastavena na 0 ->
  ; -> velikost pisma bude mala
	RJMP Show_digit_normal

  ; V opacnem pripade bude velikost pisma mala
Show_digit_u_0:
	CPI number,0
	BRNE Show_digit_u_1
	LDI ZH,HIGH(2*Num_u0)
	LDI ZL,LOW(2*Num_u0)
	CALL write_char_from_flash
	RET
Show_digit_u_1:
	CPI number,1
	BRNE Show_digit_u_2
	LDI ZH,HIGH(2*Num_u1)
	LDI ZL,LOW(2*Num_u1)
	CALL write_char_from_flash
	RET
Show_digit_u_2:
	CPI number,2
	BRNE Show_digit_u_3
	LDI ZH,HIGH(2*Num_u2)
	LDI ZL,LOW(2*Num_u2)
	CALL write_char_from_flash
	RET
Show_digit_u_3:
	CPI number,3
	BRNE Show_digit_u_4
	LDI ZH,HIGH(2*Num_u3)
	LDI ZL,LOW(2*Num_u3)
	CALL write_char_from_flash
	RET
Show_digit_u_4:
	CPI number,4
	BRNE Show_digit_u_5
	LDI ZH,HIGH(2*Num_u4)
	LDI ZL,LOW(2*Num_u4)
	CALL write_char_from_flash
	RET
Show_digit_u_5:
	CPI number,5
	BRNE Show_digit_u_6
	LDI ZH,HIGH(2*Num_u5)
	LDI ZL,LOW(2*Num_u5)
	CALL write_char_from_flash
	RET
Show_digit_u_6:
	CPI number,6
	BRNE Show_digit_u_7
	LDI ZH,HIGH(2*Num_u6)
	LDI ZL,LOW(2*Num_u6)
	CALL write_char_from_flash
	RET
Show_digit_u_7:
	CPI number,7
	BRNE Show_digit_u_8
	LDI ZH,HIGH(2*Num_u7)
	LDI ZL,LOW(2*Num_u7)
	CALL write_char_from_flash
	RET
Show_digit_u_8:
	CPI number,8
	BRNE Show_digit_u_9
	LDI ZH,HIGH(2*Num_u8)
	LDI ZL,LOW(2*Num_u8)
	CALL write_char_from_flash
	RET
Show_digit_u_9:
	CPI number,9
	BRNE Show_digit_u_A
	LDI ZH,HIGH(2*Num_u9)
	LDI ZL,LOW(2*Num_u9)
	CALL write_char_from_flash
	RET
Show_digit_u_A:
	RJMP Show_digit_error	; dodelat A,B,C....



Show_digit_normal:
	CPI number,0	; porovnani s cislem 0
	BRNE Show_digit_1	; pokud to neni cislo nula, pokracuje se dal
	LDI ZH,HIGH(2*Num_0)	; Nahrajeme do Z-pointeru pocatek tabulky se
  ; znaky
	LDI ZL,LOW(2*Num_0)	; to same, ale pro spodnich 8 bitu
	CALL write_char_from_flash	; pokud je to nula, tak se nula
  ; vykresli a RET
	RET
Show_digit_1:
	CPI number,1
	BRNE Show_digit_2
	LDI ZH,HIGH(2*Num_1)
	LDI ZL,LOW(2*Num_1)
	CALL write_char_from_flash
	RET
Show_digit_2:
	CPI number,2
	BRNE Show_digit_3
	LDI ZH,HIGH(2*Num_2)
	LDI ZL,LOW(2*Num_2)
	CALL write_char_from_flash
	RET
Show_digit_3:
	CPI number,3
	BRNE Show_digit_4
	LDI ZH,HIGH(2*Num_3)
	LDI ZL,LOW(2*Num_3)
	CALL write_char_from_flash
	RET
Show_digit_4:
	CPI number,4
	BRNE Show_digit_5
	LDI ZH,HIGH(2*Num_4)
	LDI ZL,LOW(2*Num_4)
	CALL write_char_from_flash
	RET
Show_digit_5:
	CPI number,5
	BRNE Show_digit_6
	LDI ZH,HIGH(2*Num_5)
	LDI ZL,LOW(2*Num_5)
	CALL write_char_from_flash
	RET
Show_digit_6:
	CPI number,6
	BRNE Show_digit_7
	LDI ZH,HIGH(2*Num_6)
	LDI ZL,LOW(2*Num_6)
	CALL write_char_from_flash
	RET
Show_digit_7:
	CPI number,7
	BRNE Show_digit_8
	LDI ZH,HIGH(2*Num_7)
	LDI ZL,LOW(2*Num_7)
	CALL write_char_from_flash
	RET
Show_digit_8:
	CPI number,8
	BRNE Show_digit_9
	LDI ZH,HIGH(2*Num_8)
	LDI ZL,LOW(2*Num_8)
	CALL write_char_from_flash
	RET
Show_digit_9:
	CPI number,9
	BRNE Show_digit_A
	LDI ZH,HIGH(2*Num_9)
	LDI ZL,LOW(2*Num_9)
	CALL write_char_from_flash
	RET
Show_digit_A:	; pocitame s pripadem, ze budeme chtit vypsat cislo
  ; v sestnackove soustave
	CPI number,$0A
	BRNE Show_digit_B
	LDI ZH,HIGH(2*Big_A)
	LDI ZL,LOW(2*Big_A)
	CALL write_char_from_flash
	RET
Show_digit_B:
	CPI number,$0B
	BRNE Show_digit_C
	LDI ZH,HIGH(2*Big_B)
	LDI ZL,LOW(2*Big_B)
	CALL write_char_from_flash
	RET
Show_digit_C:
	CPI number,$0C
	BRNE Show_digit_D
	LDI ZH,HIGH(2*Big_C)
	LDI ZL,LOW(2*Big_C)
	CALL write_char_from_flash
	RET
Show_digit_D:
	CPI number,$0D
	BRNE Show_digit_E
	LDI ZH,HIGH(2*Big_D)
	LDI ZL,LOW(2*Big_D)
	CALL write_char_from_flash
	RET
Show_digit_E:
	CPI number,$0E
	BRNE Show_digit_F
	LDI ZH,HIGH(2*Big_E)
	LDI ZL,LOW(2*Big_E)
	CALL write_char_from_flash
	RET
Show_digit_F:
	CPI number,$0F
	BRNE Show_digit_error
	LDI ZH,HIGH(2*Big_F)
	LDI ZL,LOW(2*Big_F)
	CALL write_char_from_flash
	RET
Show_digit_error:
  ; A pokud to nebude ani jedna z moznosti -> chyba -> otaznik
	LDI ZH,HIGH(2*Symbol_quest)
	LDI ZL,LOW(2*Symbol_quest)
	CALL write_char_from_flash

	RET
; *-----------------------------------------*
Show_tens_and_units:
	CALL Packed_BCD2Dec
	MOV number,tens
	RCALL Show_digit	; zobrazime desitky (vyssi bity)
Show_tens_and_units_Units:
	MOV number,units
	RCALL Show_digit	; jednotky (nizsi bity)

	RET
; *-----------------------------------------*
Show_tens_and_units_without_zero:
  ; zobrazi jednotky a desitky. Pokud budou desitky 0, pak se nevypisi

	CALL Packed_BCD2Dec
	MOV number,tens
	CPI number,0	; porovnani s cislem 0
	BREQ Show_tens_and_units_Units	; pokud bude 0, pak se
  ; nema vypsat -> preskoci se nasledujici prikaz a zacnou se zkoumat jednotky
	RCALL Show_digit

Show_tens_and_units_without_zero_units:
	RJMP Show_tens_and_units_Units	; pro usporu kodu. Museji se provest
  ; ty same prikazy a je zbytecne to znova vypisovat. Zabira to akorat
  ; zbytecne misto v pameti

; *-----------------------------------------*
Show_tens_and_units_without_zeros_all:
  ; zobrazi jednotky a desitky. Pokud jsou desitky v 0, tak je nezobrazi. To
  ; same ale plati i pro jednotky
	CALL Packed_BCD2Dec
	MOV number,tens
	CPI number,0
	BREQ Show_tens_and_units_without_zeros_all_Units
	RCALL Show_digit

Show_tens_and_units_without_zeros_all_Units:
	MOV number,units
	CPI number,0
	BREQ Show_tens_and_units_without_zeros_all_end
	RCALL Show_digit
Show_tens_and_units_without_zeros_all_end:
	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
wait_for_LCD:
	LDI tmp,$00	; nastavit datovou sbernici na vstupni
	OUT data_port,tmp	; vymazeme data na portu
	OUT IO_data_LCD,tmp	; nastavime port na vstupni

	CBI control_port_LCD,RS	; nastavime na cteni stavu displeje
	SBI control_port_LCD,RW	; soucast nastavovani :)
wait_for_LCD_1:
	RCALL signal_enable	; pred kazdym ctenim MUSIME poslat signal
  ; ENABLE! V dobu, kdy je LCD zaneprazdnen, nereaguje na signal ENABLE!
	SBIC data_port_in,7	; pokud busy =0 -> navrat z podprogramu
  ; priznak busy je na MSB -> 7.
	RJMP wait_for_LCD_1

	LDI tmp,$FF
	OUT IO_data_LCD,tmp	; nastavime datovou sbernici na vystupni
  ; protoze v drtive vetsine na displej zapisujeme a cteme jen minimalne

	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
signal_enable:
 ; E - ___|^^^^^|___
; E - Tmin - 450ns ! (podle datasheetu) - ale zalezi asi na konkretnim
; displeji - lepsi dat o malinko delsi impulz -> bude spolehliveji zobrazovat
	SBI control_port_LCD,E
;	nop	; upravit podle krystalu
; nyni pro 4Mhz int. oscilator (experimentalne)
	nop
	nop
	nop
; sem tom staci pro 4Mhz
	nop
	nop
;	nop
  ; Pro 8Mhz je nutnych nop vice (experimentalne urceno)

	CBI control_port_LCD,E
  ; a pak pockat na LCD - wait_for_LCD - musi byt spusten az potom, protoze
  ; vola funkci signal_enable a tim by se to zacyklilo
	RET
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*
signal_enable_for_read:
  ; Cteni z LCD trva o dost dyl, takze by bylo zbytecne se zdrzovat pri zapisu
  ; dat na LCD. takze pokud budeme cist z LCD, tak musi byt delsi i signal
  ; enable a vubec veskere procedury jsou delsi
	SBI control_port_LCD,E
	nop
	nop
	nop
  ; sem to staci pro 4Mhz (experimentalne urceno)
	nop
	nop
  ; toto staci pro 8Mhz (experimentalne urceno)

	nop	; plus jeden rezervni, kdyby mela nejaka mega ne dobre
  ; zkalibrovany vnitrni oscilator

	CBI control_port_LCD,E
  ; a pak pockat na LCD - wait_for_LCD - musi byt spusten az potom, protoze
  ; vola funkci signal_enable a tim by se to zacyklilo


  ; po "enable" impulzu bychom meli mit data na sbernici do 350ns. Ale
  ; vzhledem k tomu, ze vystupy jsou velice mekky zdroj a mezi vodici je jista
  ; kapacita, tak musime pockat o neco dele
	RCALL wait_2us
	nop
	nop
	nop
  ; sem to funguje pro 4Mhz bez problemu

	RCALL wait_1us	; tento podprogram je pristupny pouze u zpozdeni
  ; pro taktovani 8Mhz. Lze nahradit 8x nop
	nop
	nop
	nop
	nop
	nop
	nop
  ; toto se musi pridat, pokud budeme taktovat na 8Mhz.


	nop	; plus jeden rezervni, kdyby byla navrzena DPS nevhodnym
  ; zpusobem, nebo byla moc velka kapacita mezi vodici atd....

	RET	
; *-----------------------------------------*
; |                                         |
; *-----------------------------------------*