;***********************************************
;*** 8 bit LCD interface for the AVR
;*** Version 1.0
;*** Date 25/5/1999
;*** Author : Stelios Koroneos - 
;*** E-mail k-tel@geocities.com
;*** Web page : http://come.to/Stelios_Cellar
;*********************************************
;*** This is a small program implementing an 8 bit
;*** LCD interface for the Avr 90s2313.
;*** It works on any 16x2 LCD that is based on the Hitachi
;*** HD44780 lcd controler and can be easily modified to work
;*** with other LCD's
;***
;*** Connections
;*** LCD CONTROLS
;*** PortB D0----> Lcd D0
;*** PortB D1----> Lcd D1
;*** PortB D2----> Lcd D2
;*** PortB D3----> Lcd D3
;*** PortB D4----> Lcd D4
;*** PortB D5----> Lcd D5
;*** PortB D6----> Lcd D6
;*** PortB D7----> Lcd D7

;*** PortD D4----> Lcd RS
;*** PortD D5----> Lcd WR
;*** PortD D6----> Lcd E


;*** A number of routines and macros are implemented which
;*** can be easily re-used
;*** LCD_Init - Initilize the LCD and set cursor at the beggining of first line 
;*** LCD_Command - Send a byte command to the LCD
;*** LCD_Write - Write a char at current possition of the cursor
;*** LCD_GotoXY - Move cursor to a specific position
;*** LCD_EPulse - Togle the E line High and then Low
;*** Delay40us - Delay 40 us 
;*** Delay100us - Delay 100 us
;*** Delay4ms - Delay 4ms
;*** NOTE : ALL DELAYS ARE CALCULATED FOR A 4MhZ CRYSTAL

;*** MACROS
;*** LCD_CLS - Clear LCD move cursor at beginning of first line
;*** LCD_Home - Move cursor to left edge of the display
;*** LCD_ON - Turn display on
;*** LCD_OFF - Turn display off
;*** LCD_CursorOn - Cursor will be displayed
;*** LCD_CursorOFF - No cursor will be displayed
;*** LCD_BlinkON - The chararcter at the cursor position will blink
;*** LCD_BlinkOFF - The chararcter at the cursor position will not blink
;*** LCD_ShiftCursorL - Shift cursor one position to the left
;*** LCD_ShiftCursorR - Shift cursor one position to the right
;*** LCD_ShiftDisplayL - Shift BOTH LINES 1 char to the Left
;*** LCD_ShiftDisplayR - Shift BOTH LINES 1 char to the Right



.nolist                       ;Suppress listing of include file
.include "2313def.inc"        ;Set 2313's data for the compiler

.list


;***** Global register variables


.def	edge	=r6
.def	tc_over	=r7

.def	temp=r16		;General use working register
.def	Datar=r16
.def	cursor_x=r17
.def	cursor_y=r18
.def	LCD_XPos=r19
.def	LCD_Ypos=r20
.def	CommandReg=r21
.def	DataReg	=r22
.def	lcdnull=r28


;---------------------------------- EEprom -----------------------------------

.def	EEaddr	=r29
.def	EEdata	=r0		;result data byte

;------------------------ 24-bit Binary to BCD conversion --------------------

.def	ttBCD0	=r8		;high BCD value digit 2
.def	ttBCD1	=r9		;high BCD value digit 4
.def	ttBCD2	=r10		;high BCD value digit 6
.def	ttBCD3	=r11		;high BCD value digit 8

.def	tBCD0	=r12		;low BCD value digit 1
.def	tBCD1	=r13		;low BCD value digit 3
.def	tBCD2	=r14		;low BCD value digit 5
.def	tBCD3	=r15		;low BCD value digit 7

.def	fbinL	=r23		;binary value Low byte
.def	fbinM	=r24		;binary value Middle byte
.def	fbinH	=r25		;binary value High byte
.def	cnt24a	=r28		;loop counter

;--------------------------- divide 16bit/16bit number -----------------------

.def	drem16uL=r14
.def	drem16uH=r15

.def	dres16uL=r16
.def	dres16uH=r17
.def	dd16uL	=r16
.def	dd16uH	=r17
.def	dv16uL	=r18
.def	dv16uH	=r19













;.EQU 	T0_RELOAD	 = 194
;.EQU 	RS232BSIZE	 = 32
;
;.DSEG
;
; Variables Used
;_isrflags:      .byte	1
;_sysflags:      .byte	1
;_systic:        .byte	2
;_incount:       .byte	1
;_rxRS232pos:    .byte	2
;age:            .byte	2
;days:           .byte	2
;tmp:            .byte	2
;Name:           .byte	32
;_rxRS232Buf:    .byte	RS232BSIZE
;
;store direct at 'age' address the value of zl
;	sts 	age,zl
;	sts 	age+1,zh
;
;load direct zl with value from 'age' adress
;	lds 	zl,age
;	lds 	zh,age+1
;
;load zl with value from 'Name' adress
;	ldi 	zl,low(Name)
;	ldi 	zh,high(Name)
;
;
;load r20,r21 with values from address 10
;	ldi 	r20, low(10)
;	ldi 	r21, high(10)



;Subroutine 'bin2BCD24' Register Variables

.equ	AtBCD0	=12		;address of tBCD0
.equ	AtBCD3	=15		;address of tBCD1

;PortB D0-D7 goes to D0-D7 in the LCD
;PortD D3 is RW
;PortD D4 is RS
;PortD D5 is E 
;PortD D6 is Pulse Input (ICP)

.equ  	RxD	=PD0        	;RS232 in
.equ  	TxD	=PD1        	;RS232 out
.equ  	BL	=PD2        	;backlight control line
.equ	lcdrw 	=PD3		;LCD RS pin connected to PD3
.equ	lcdrs 	=PD4		;LCD R/W pin connected to PD4
.equ	lcde	=PD5		;LCD E pin connected to PD5
.equ	trigger	=PD6		;Frequency measure input  

.equ	LineLength=20
.equ	NoOfLines=2

;*** All baud rates are for a 10Mhz crystal 


.equ	B4800	=129		; 0,16% Error
.equ	B9600	=64		; 0,16% Error
.equ	B19200	=32		; 1,36% Error

;*** Set your UART Baud rate

.equ  BAUD	=B9600


;**********************************
;Accepts one parameter which is the address to jump
;Executes and inderect jump in order to support
;all AVR's, since 90s1200/2313 do not support
;direct jump
;The Z register points to the address

.Macro   GOTO                 ;GOTO macro

         ldi ZL,Low(@0 )      ;Load the low byte of the address
         ldi ZH,High(@0)      ;Load the high byte
         ijmp                 ;Jump

.Endmacro

.macro   LCD_Cls
         
	  ldi CommandReg,0b00000001
         rcall LCD_Command 
         rcall delay4ms
         ldi LCD_Xpos,0
         ldi LCD_Ypos,1 

.endmacro

.macro   LCD_Home
         
	  ldi CommandReg,0b00000010
         rcall LCD_Command 
         rcall delay4ms
         ldi LCD_Xpos,0
         ldi LCD_Ypos,1
.endmacro

.macro   LCD_On
         
         ldi CommandReg,0b00001100
         rcall LCD_Command 
         rcall delay40us
.endmacro

.macro   LCD_Off
         
  	  ldi CommandReg,0b00001000
  	  rcall LCD_Command 
         rcall delay40us
.endmacro

.macro   LCD_CursorOn
         
         ldi CommandReg,0b00001110
         rcall LCD_Command 
         rcall delay40us
.endmacro

.macro   LCD_CursorOff
         
	  ldi 	CommandReg,0b00001100
         rcall LCD_Command 
         rcall delay40us
.endmacro

.macro   LCD_BlinkOn
         
	  ldi CommandReg,0b00001111
         rcall LCD_Command 
         rcall delay40us
.endmacro

.macro   LCD_BlinkOff
         
	  ldi CommandReg,0b00001110
         rcall LCD_Command 
         rcall delay40us
.endmacro

.macro   LCD_ShiftCursorL
         
	  ldi CommandReg,0b00010000
         rcall LCD_Command 
         rcall delay40us 
.endmacro

.macro   LCD_ShiftCursorR
         
	  ldi CommandReg,0b00010100
         rcall LCD_Command 
         rcall delay40us 
.endmacro

.macro   LCD_ShiftDisplayL
         ldi CommandReg,0b00011000
         rcall LCD_Command 
         rcall delay40us 
.endmacro

.macro   LCD_ShiftDisplayR
         
	  ldi CommandReg,0b00011100
         rcall LCD_Command 
         rcall delay40us 
.endmacro

.cseg

;INTERRUPT JUMP TABLE

.org 0

	rjmp 	reset 		
	rjmp	ScanKey			;IRQ0 handle		
	reti				;IRQ1 handle		
	reti ;rjmp	ISP_Handle		;Timer1 capture handle		
	reti          			;Timer1 compare handle		
	reti ;rjmp	TC1_OVR			;Timer1 overflow		
	reti		          	;Timer0 overflow		
	reti          			;SPI		
	reti          			;UART RX complete		
	reti          			;UART UDR Empty		
	reti          			;UART TX complete		
	reti          			;Analog comp 


;                     ------------------------------------- 
;-------------------- | Main program entry point on reset | -----------------
;                     |    AVR and LCD Initialization     |  
;                     -------------------------------------

reset:   
	cli				;Global Interrupt Disable

	ldi temp,RAMEND		;Init Stack Pointer
	out SPL,temp

	ldi temp,0b10000000
	out ACSR,temp			;Comparator Disabled to save power 

	ldi temp,0b00000010		;Interrupt on falling edge of INT0 
	out MCUCR,temp

	ldi temp,0b01000000		;External Interrupt0 Enable
	out GIMSK,temp

	;ldi temp,0b00000000
	;out GIMSK,temp		;disable external interrupt
	
	clr temp			;PortB Low
	out PORTB,temp

	ldi temp,0xFF			;Make port B pin all outputs
	out DDRB,temp

	clr temp			;PortD Low
	out PORTD,temp

	ldi temp,0b01111110		;Make port D pin all outputs
	out DDRD,temp			;Except ICP, TxD pins

	clr temp
	out TCCR1A,temp		;disconnect TC1 from pin OC1
	
	ldi temp,0b01000001		;CK (0,25us), rising edge transfer 
	out TCCR1B,temp		;at capture register
	
	clr temp			;reset TC1
	out TCNT1L,temp 		
	out TCNT1H,temp		

	ldi temp,0b10001000
	out TIMSK,temp			;enable TC1 capture, overflow interrupts

	rcall Lcd_Init			;Init the LCD

	LCD_CursorOff

	ldi cursor_x,0	
	ldi cursor_y,1			;Move cursor to 1st line
	rcall LCD_Gotoxy

	ldi EEaddr,InitString1	;Get the address of the first line to display 
	rcall Display_Init_Message	;Show the message

	ldi cursor_x,0	
	ldi cursor_y,2			;Move cursor to 2nd line
	rcall LCD_Gotoxy

	ldi EEaddr,InitString2
	rcall Display_Init_Message

	rcall LongDelay		; Wait 1 sec
	rcall LongDelay		; Wait 1 sec

	Lcd_CLS				;Clear the display


;                             -------------------
;---------------------------- | UART Initialize |----------------------------
;                             -------------------
 
Uart_Init:
	
	ldi Temp,BAUD
	out UBRR,Temp

	ldi Temp,0b00011000
	out UCR,Temp

	ldi cursor_x,0			;Move cursor to 2nd line
	ldi cursor_y,1
	rcall LCD_Gotoxy
	
	ldi EEaddr,MenuString1	;Get the address of the first line to display 
	rcall Display_Init_Message	;Show the message

	ldi cursor_x,0			;Move cursor to 2nd line
	ldi cursor_y,2
	rcall LCD_Gotoxy

	ldi EEaddr,MenuString2
	rcall Display_Init_Message
	
	clr edge 
	clr tc_over

	sei				;globally enable interrupts

MainLoop:	
	
	

;* This subroutine divides the two 16-bit numbers 
;* "dd16uH:dd16uL" (dividend) and "dv16uH:dv16uL" (divisor). 
;* The result is placed in "dres16uH:dres16uL" and the remainder in
;* "drem16uH:drem16uL".	
	

;	mov	dd16uL,XL
;	mov	dd16uH,XH
;	ldi	dv16uL,4
;	ldi	dv16uH,0
;	rcall	div16u

;	tst	tc_over
;	breq	No_TC_Over
;	ldi	YH,HIGH(16384)	
;	ldi	YL,LOW(16384)
	
;	add	dres16uL,YL 		;Add low bytes
;	adc	dres16uH,YH		;Add high bytes with carry

;No_TC_Over:
	






	
	ldi cursor_x,7			;Move cursor to 8th row
	ldi cursor_y,1			;Move cursor to 1st line
	rcall LCD_Gotoxy
	
	ldi fbinH,0x00
	ldi fbinM,0xFF
	ldi fbinL,0x0a
	rcall bin2BCD24
	
	mov ttBCD0,tBCD0
	mov ttBCD1,tBCD1
	mov ttBCD2,tBCD2
	mov ttBCD3,tBCD3

	ldi temp,4			;Loop counter
BCD_Loop1:
	lsr ttBCD0			;move BCD data one nibble to the left
	lsl tBCD0
	lsr ttBCD1
	lsl tBCD1
	lsr ttBCD2
	lsl tBCD2
	lsr ttBCD3
	lsl tBCD3
	dec temp
	brne BCD_Loop1
	swap tBCD0
	swap tBCD1
	swap tBCD2
	swap tBCD3

	ldi temp,0x2F			;val(chr$"_") - 30h
	
	tst ttBCD3
	brne LCD_NoNull
	add ttBCD3,temp
	
	tst tBCD3
	brne LCD_NoNull
	add tBCD3,temp
	
	tst ttBCD2
	brne LCD_NoNull
	add ttBCD2,temp
	
	tst tBCD2
	brne LCD_NoNull
	add tBCD2,temp
	
	tst ttBCD1
	brne LCD_NoNull
	add ttBCD1,temp
	
	tst tBCD1
	brne LCD_NoNull
	add tBCD1,temp
	
	tst ttBCD0
	brne LCD_NoNull
	add ttBCD0,temp
	
	tst tBCD0
	brne LCD_NoNull
	add tBCD0,temp

LCD_NoNull:	
	ldi temp,0x30

	mov DataReg,ttBCD3
	add DataReg,temp
	rcall LCD_WriteQuick

	mov DataReg,tBCD3
	add DataReg,temp
	rcall LCD_WriteQuick

	mov DataReg,ttBCD2
	add DataReg,temp
	rcall LCD_WriteQuick

	mov DataReg,tBCD2
	add DataReg,temp
	rcall LCD_WriteQuick

	mov DataReg,ttBCD1
	add DataReg,temp
	rcall LCD_WriteQuick

	mov DataReg,tBCD1
	add DataReg,temp
	rcall LCD_WriteQuick

	mov DataReg,ttBCD0
	add DataReg,temp
	rcall LCD_WriteQuick

	mov DataReg,tBCD0
	add DataReg,temp
	rcall LCD_WriteQuick

	ldi cursor_x,11		;Move cursor to 2nd line
	ldi cursor_y,2
	rcall LCD_Gotoxy
	tst edge
	ldi EEaddr,Trigger_Up		;Get the address of the trigger line to display 
	brne Trigger_Message
	ldi EEaddr,Trigger_Dw		;Get the address of the trigger line to display 
Trigger_Message:
	rcall Display_Init_Message	;Show the message

	rjmp	MainLoop



     	
;	in temp,pind			;Read the switches
;	out portb,			;output the switch valu

ScanKey:
	tst edge
	brne xxx
	inc edge
	reti
xxx:
	clr edge
	reti
	
	
	ldi EEaddr,Trigger_Up	;Get the address of the first line to display 
	rcall Display_Init_Message	;Show the message




ISP_Handle:
	in	XL,ICR1L		; Load the Lo Byte of ICR1
	in	XH,ICR1H		; Load the Hi Byte of ICR1
	clr	tc_over			; reset T/C1 overflow counter
	out	TCNT1H,tc_over		; reset the TC1
	out	TCNT1L,tc_over
	reti

TC1_OVR:
	inc	tc_over			; increment T/C1 overflow counter
	reti




Number_convert:


Check_BackSpace:
;         cpi txbyte,8         ;Check for BackSpace (CTRL-H ASCII 8)
;         brne Check_VerticalTab
         cpi LCD_Xpos,0
         brne Dec_Xpos
         ldi Datareg,$20 
         rcall LCD_Write
         dec LCD_XPos
         mov cursor_x,LCD_Xpos
         mov cursor_y,LCD_Ypos
         rcall LCD_GotoXY 
         ;goto Uart_loop
Dec_Xpos:
         dec Lcd_Xpos
         mov cursor_x,LCD_Xpos
         mov cursor_y,LCD_Ypos
         rcall LCD_GotoXY
         ldi DataReg,$20
         rcall LCD_Write
         dec Lcd_Xpos
         mov cursor_x,LCD_Xpos
         mov cursor_y,LCD_Ypos
         rcall LCD_GotoXY
         ;goto Uart_Loop


WriteLCD:
;         mov Datareg,Txbyte
         rcall LCD_Write 

; Theses were used for debug perposes
; rcall Uart_Putchar ;echoing back the received char and "$" 
; ldi txbyte,$24
; rcall UART_Putchar

Noexit:  
;         goto uart_loop



;                          ----------------------
;------------------------- | LCD Initialization | --------------------------
;                          ----------------------

Lcd_Init:
	rcall 	LongDelay		;Wait approx 1 sec
	rcall 	delay4ms
	rcall 	delay4ms 
	rcall 	delay4ms
	rcall 	delay4ms
	rcall 	delay4ms		;Wait 15ms

	cbi 	PortD,lcdrs		;Clear RS
	cbi 	PortD,lcdrw		;Clear RW
	ldi 	temp,$38
	out 	PortB,temp
	rcall 	LCD_EPulse

	rcall 	Delay4ms		;Wait 4ms
	out 	PortB,temp
	rcall 	LCD_EPulse

	rcall 	Delay100us		;Wait 100us
	out 	PortB,temp
	rcall 	LCD_EPulse

	rcall 	Delay100us		;Wait 100us
	ldi 	temp,$04		;entry mode set
	out 	PortB,temp
	rcall 	LCD_EPulse

	rcall 	Delay4ms		;Wait 4ms
	ldi 	temp,$0e		;display on
	out 	PortB,temp
	rcall 	LCD_EPulse

	rcall 	Delay4ms		;Wait 4ms
	ldi 	temp,$01		;Clear Display
	out 	PortB,temp
	rcall 	LCD_EPulse

	rcall 	Delay4ms		;Wait 4ms

	ldi 	LCD_Xpos,0		;Init the positioning values 
	ldi 	LCD_Ypos,1
	ret


;                     ----------------------------------
;-------------------- | Send a byte command to the LCD | --------------------
;                     ----------------------------------

LCD_Command:
	cbi 	PortD,lcdrs
	cbi 	PortD,lcdrw
	out 	PortB,CommandReg
	rcall 	LCD_EPULSE
	rcall 	Delay40us
	ret 


;            ---------------------------------------------------
;----------- | Write a char at current possition of the cursor | ------------
;            ---------------------------------------------------

LCD_Write:
	cpi 	LCD_Xpos,LineLength	;Check if we are at the end of Xpos
	
	brne 	LCD_WriteQuick  	;if not display the character

	ldi 	cursor_x,0           	;Point at the first row

	cpi 	LCD_Ypos,NoOfLines 	;check if we are at the last line of the lcd
	brne 	Lcd_Write_NextLine 	;If we are not move to the next line
	
	ldi 	cursor_y,1 
	rcall 	LCD_Gotoxy
	
	rjmp 	LCD_WriteQuick 

Lcd_Write_NextLine:
	inc 	Lcd_Ypos
	mov 	cursor_y,Lcd_Ypos
	rcall 	LCD_Gotoxy

LCD_WriteQuick:
	sbi 	PortD,lcdrs
	out 	PortB,DataReg
	rcall 	LCD_EPULSE
	rcall 	Delay40us
	inc 	LCD_Xpos

Lcd_Write_Exit:
	ret 


;                         -----------------------------
;------------------------ | Diplay Initialize Message | ---------------------
;                         -----------------------------


Display_Init_Message:
	out EEAR,EEaddr		;Adress to read from Eeprom
Display_Init_Loop:
	rcall EERead   	  	;get EEPROM data
	mov Temp,EEdata
	cpi Temp,0			;Check if we are at the end of string
	breq Display_Init_Message_End
	mov DataReg,EEdata
	rcall LCD_Write
	rjmp Display_Init_Loop 
Display_Init_Message_End:
	ret

;        -------------------------------------------------------------
;------- | This subroutine increments the address stored in EEAR     | ------
;        | and reads the EEPROM into the register variable "EEdrd_s".|
;        -------------------------------------------------------------

EERead:
	
	sbi EECR,EERE			;set EEPROM Read
	in EEdata,EEDR			;get data
	in EEaddr,EEAR			;get address
	inc EEaddr			;increment address
	out EEAR,EEaddr		;store it 
	ret


;       -------------------------------------------------------------
;------ | Move cursor to a specific position                        | ------
;       | cursor_x contains the column (x), cursor_y contains the Line (y) |
;       -------------------------------------------------------------

LCD_GotoXY:
	ldi CommandReg,0b10000000	;Base address of first line
	cpi cursor_y,1			;Check to see which line
	breq Calc_Xpos
	ldi CommandReg,0b11000000	;Base address of second line
Calc_Xpos:
	or CommandReg,cursor_x
	rcall LCD_Command    		;Set DDRam Add
	rcall Delay40us
	mov LCD_Xpos,cursor_x		;update the x and y possition
	mov LCD_Ypos,cursor_y		;of the cursor
	ret


;                  --------------------------------------
;----------------- | Togle the E line High and then Low | -------------------
;                  --------------------------------------

LCD_EPulse:
	sbi PortD,lcde			;E high
	nop
	nop
	nop
	nop
	cbi PortD,lcde			;E Low
	ret

;                      -----------------------------------
;--------------------- | Send a char using the UART port | -------------------
;                      -----------------------------------

UART_Putchar:
	sbis Usr,UDRE			;wait till the last byte was transmited
	rjmp UART_Putchar
	out udr,Temp			;Now its ok to send
	ret


;                      ----------------------------------
;--------------------- | Get a char using the UART port | --------------------
;                      |   TxByte contains the char     |
;                      ----------------------------------

UART_Getchar:
	sbis Usr,RXC      	   	;wait till a character arrives
	rjmp UART_Getchar
	in Temp,UDR			;Now its ok to read
	ret


;*********************************************************************

Delay40us:
	ldi r30,$A0
Loop3:
	dec r30
	brne loop3 
	ret

;*********************************************************************

Delay100us:
	ldi r30,$02
Loop:   
	ldi r29,$AA
Loop4:	
	dec r29
	brne Loop4 
	dec r30
	brne Loop
	ret

;*********************************************************************

Delay4ms:
	ldi r29,$46
Loop1:   
	ldi r30,$00
Loop2:   
	dec r30
	brne loop2 
	dec r29
	brne loop1
	ret

;*********************************************************************

LongDelay:
	ldi r31,255			; Delay for about 1 sec for the LCD to start
Longdelay_Loop:
	rcall delay4ms
	dec r31
	brne LongDelay_Loop
	ret

;***************************************************************************
;* "div16u" - 16/16 Bit Unsigned Division
;*
;* This subroutine divides the two 16-bit numbers 
;* "dd16uH:dd16uL" (dividend) and "dv16uH:dv16uL" (divisor). 
;* The result is placed in "dres16uH:dres16uL" and the remainder in
;* "drem16uH:drem16uL".
;*  
;* Number of words    : 19
;* Number of cycles   : 235/251 (Min/Max)
;* Low registers used : 2 (drem16uL,drem16uH)
;* High registers used: 5 (dres16uL/dd16uL,dres16uH/dd16uH,dv16uL,dv16uH,
;*			      temp)
;***************************************************************************

div16u:
	clr	drem16uL		;clear remainder Low byte
	sub	drem16uH,drem16uH	;clear remainder High byte and carry
	ldi	temp,17			;init loop counter
d16u_1:
	rol	dd16uL			;shift left dividend
	rol	dd16uH
	dec	temp			;decrement counter
	brne	d16u_2			;if done
	ret				;return
d16u_2:
	rol	drem16uL		;shift dividend into remainder
	rol	drem16uH
	sub	drem16uL,dv16uL	;remainder = remainder - divisor
	sbc	drem16uH,dv16uH
	brcc	d16u_3			;if result negative
	add	drem16uL,dv16uL	;restore remainder
	adc	drem16uH,dv16uH
	clc				;clear carry to be shifted into result
	rjmp	d16u_1			;else
d16u_3:
	sec				;set carry to be shifted into result
	rjmp	d16u_1

;***************************************************************************
;* "bin2BCD24" - 24-bit Binary to BCD conversion
;*
;* This subroutine converts a 24-bit number (fbinH:fbinM:fbinL) to a 7-digit 
;* packed BCD number represented by 4 bytes (tBCD3:tBCD2:tBCD1:tBCD0).
;* MSD of the 7-digit number is placed in the lowermost nibble of tBCD3.
;*  
;* Number of words		:25
;* Number of cycles		:751/768 (Min/Max)
;* Low registers used		:3 (tBCD0,tBCD1,tBCD2,tBCD3) 
;* High registers used  	:4 (fbinL,fbinM,fbinH,cnt24a,temp)	
;* Pointers used		:Z
;***************************************************************************

bin2BCD24:
	ldi	cnt24a,24	;Init loop counter	
	clr	tBCD3
	clr	tBCD2		;clear result (3 bytes)
	clr	tBCD1		
	clr	tBCD0		
	clr	ZH		;clear ZH (not needed for AT90Sxx0x)
bBCDx_1:
	lsl	fbinL		;shift input value
	rol	fbinM
	rol	fbinH		;through all bytes
	rol	tBCD0
	rol	tBCD1
	rol	tBCD2
	rol	tBCD3
	dec	cnt24a		;decrement loop counter
	brne	bBCDx_2		;if counter not zero
	ret			;return
bBCDx_2:
	ldi	r30,AtBCD3+1	;Z points to result MSB + 1
bBCDx_3:
	ld	temp,-Z		;get (Z) with pre-decrement
	subi	temp,-$03	;add 0x03
	sbrc	temp,3		;if bit 3 not clear
	st	Z,temp		;store back
	ld	temp,Z		;get (Z)
	subi	temp,-$30	;add 0x30
	sbrc	temp,7		;if bit 7 not clear
	st	Z,temp		;store back
	cpi	ZL,AtBCD0	;done all three?
	brne	bBCDx_3		;loop again if not
	rjmp	bBCDx_1		


;                              ---------------
;----------------------------- | EEPROM Data | ------------------------------
;                              ---------------

.eseg

EmptyPos:
	
.db 00					;leave eeprom location 00 empty due to bug
					;in AVR that randomly erases the contens
InitString1:

.db "FREQUENCY METER V1.0",00	;What the LCD will display on power up

InitString2:
	
.db " (c) Kostas Elenis  ",00 	;Strings are 0 ended

MenuString1:
	
.db "FREQ :          Hz  ",00 	;Strings are 0 ended

MenuString2:
	
.db "Trigger at: "

Trigger_Up:

.db "RISING  ",00 			;Strings are 0 ended

Trigger_Dw:

.db "FALLING ",00			;Strings are 0 ended
