
	;================= DISPLAY/SET PROGRAMM ===========================
PGSET:	
	;------------ Set previous TIME_L/TIME_H,Points --------------
	rcall	RD_EE		;READ EEPROM
	sbrc	EETMP_H,3	;IF PGMxx "Active"
	rjmp	pgs7		;Then go over to Edit

	dec	PGM_ADDR	;PGM_ADDR=PGM_ADDR-1
	sbrs	PGM_ADDR,7	;IF PGM_ADDR=0 Then GO OVER
	rcall	RD_EE		;READ EEPROM
	inc	PGM_ADDR	;PGM_ADDR=PGM_ADDR+1
pgs7:	;-------------------------------------------------------------
	rcall	SETTIME		;Set TIME 
	tst	r18		;IF "CLEAR FLAG">0
	brne	pgs6		;then	GO WRITE "PGM Passive"
	;------------ Set Points ------------------------
	ldi	XL,bind0	;Set "10xHOURS" position
	ldi	r18,0x88	;P7=>Const

PGS3:	rcall	DISPTIME	;Display Time	
	;------------ First SYM control ----------------------
	lds	r16,bind0		;IF FIRST DIGIT=" "or" ."
	andi	r16,0xFB		;THEN 0=> BIND0
	brne	PGS5
	ldi	r16,0xDB		;"0"=> first position
	sts	bind0,r16		;"0"=> first position
	sts	bind1,r16		;"0"=> first position
PGS5:	st	X,r31			;Flashing position
	;-------------- Wait Key Pressed -------------------
	clr	KeyD		;
PGS4:	tst 	KeyD		;IF NO KEY pressed
	breq	pgs4		;then Wait
	;---------------------------------------------------
	;------------ Pressed Key Analys -------------------
	cpi	KeyD,0x04	;KeyD=K2 "Set"(Next)
	breq	PGS04		;goto SUB 

	andi	KeyD,0x03	;KeyD=K0 or K1 ("+" or "-")
	brne	PGS01		;goto SUB 

	rjmp	PGS3		;LOOP
	;---------------------------------------------------

	;======= DISPLAY/SET Key handlers ================== 
PGS01:	eor	Points,r18	;Set Points
	rjmp	PGS3	
	;------------------------------------
PGS04:	inc	XL		;Next point position
	lsr	r18		;Next Const
	brcs	PGSE		;IF END Then GO OUT
	rjmp	PGS3	
	;===================================================


	;---------- Finalasing PGM SET ------------------
PGSE:	andi	Points,0xF0	;Mask	HIGH nibble Points
	ori	Points,0x08	;Set Flag "PGM Active"
	or	TIME_H,Points	;Include Points to PGM_DATA
	;------------------------------------------------

	;--------- WRITE EEPROM -------------------------	
pgs6:	push	PGM_ADDR	;Store PGM ADDR

	lsl	PGM_ADDR	;PGM ADDR=PGM ADDR*2
	rcall	WR_EE0		;Write 1 byte
	inc	PGM_ADDR	;PGM_ADDR=PGM_ADDR+1
	mov	TIME_L,TIME_H	;Move Data
	rcall	WR_EE0		;Write 1 byte

	pop	PGM_ADDR	;Restore PGM ADDR
	;------------------------------------------------

	rjmp	pgmE
;===========================================================================



;================= S U B R O U T I N E S ===================================

;================ Write Byte Data in EEPROM  ===============================
	;-------(EEADDR=(R17)PGM_ADDR,Write data in TIME_L)---------------
WR_EE0:	cli
wree1:	sbic	EECR,EEWE	;
	rjmp	wree1		;Wait EEPROM Ready
	out	EEAR,PGM_ADDR	;PGM ADDR=>EEAR
	out	EEDR,TIME_L	;TIME_L=>EEDR
	sbi	EECR,EEMWE	;Set MASTER Write Enable
	sbi	EECR,EEWE	;Set Write Enable
	sei
	RET
;==========================================================================


;================ READ EEPROM (+ Correction) ===============================
;-------(EEADDR(R17)=PGM_ADDR,Corr DATA=>TIME_L/TIME_H,Points) -------------
RD_EE:	;---------------- SYNCHRONIZATION -----------------------
	rcall	SYNCHRO		;Sinchronization with INTERRUPTS	
	;---------- Read 2 byte=>EETMP_L/EETMP_H --------------
	rcall	RD_EE0			;Read 2*Data EEPROM
	;--------------- Set Time ------------------------------
	mov	TIME_L,EETMP_L		;PGM DATA=>TIME_L (LOW BYTE)
	mov	r16,EETMP_H		;PGM DATA=>temp   (HIGH BYTE)
	andi	r16,0x07		;Mask High nibble
	mov	TIME_H,r16		;PGM DATA=>TIME_H (HIGH BYTE)
	;--------------- Set Points ----------------------------
	mov	r16,EETMP_H	;PGM DATA=>temp   (HIGH BYTE)
	andi	r16,0xF0	;Mask High nibble
	mov	Points,r16	;Mov points
	swap	r16		;Swap nibbles
	or	Points,r16	;Mov points
	RET
;============================================================================


;============== Read 2*Data EEPROM (NOT Corrected) ==========================
;---------(EEADDR(R17)=PGM_ADDR,LOW=>EETMP_L, HIGH=>EETMP_H)---------------
RD_EE0:	push	PGM_ADDR	;Store PGM ADDR
	lsl	PGM_ADDR	;PGM ADDR=PGM ADDR*2
	out	EEAR,PGM_ADDR	;PGM ADDR=>EEAR
	sbi	EECR,EERE	;Set Read Enable 
	in	EETMP_L,EEDR	;PGM DATA=>EETMP_L (LOW BYTE)
	sbi	EEAR,0		;PGM ADDR=PGM ADDR+1
	sbi	EECR,EERE	;Set Read Enable
	in	EETMP_H,EEDR	;PGM DATA=>EETMP_H (HIGH BYTE)
	pop	PGM_ADDR	;Restore PGM ADDR
	RET
;==========================================================================

