;***************************************************************************
;*
;* demo program with cound up printing to three 7-segment dispays
;*
;***************************************************************************

	.include "1200def.inc"
	
	.def	7int		=r0		;integer 8-bit word to print
	.def	7dec		=r1		;decimal 4-bit word to print
	.def	7bin		=r2		;byte to print
	.def	7temp		=r3		;count general register
	.def	dtemp		=r4		;count general register
	.def	7segout	=r5		;7 segment digit byte output

	
	
	clr	7dec
	clr	7int
	call	7seginit
loopa:
	call	7segprint
	inc	7dec
	cpi	7dec,$0A
	brlo	loopb				;if less then branch
	clr	7dec
	inc	7int
	cpi	7int,$64
	brlo  loopb
	clr	7int
loopb:	
	ldi	dtemp,30			;delay time in ms
	call	delay
	jmp	loopa
	




;***************************************************************************
;*
;* main routine for word conversion and print to three 7segment displays
;* with two integer and one decimal numbers ex. - 99.9 -
;* 
;***************************************************************************


7seginit:
	ldi 	7temp,$FF			;init port B
	out	DDRB,7temp			;as output
	ldi	7temp,$70			;init port D
	out	DDRD,7temp			;as I/O (port 4,5,6 as output all other inputs)
	ret
7segprint:
	mov	7bin,7dec
	cbr	7bin,b11110000		;get the decimal 4-bit 
	call  bin2deg
	brcs	noprint			;error
	call	degto7seg
	sbr	7segout,b10000000		;set decimal point on
	ldi	7temp,b01000000		
	out	PORTD,7temp			;set on decimal column at display
	out	PORTB,7segout		;print
	mov	7bin,7int
	call	bin2deg
	brcs	noprint			;error
	cbr	7bin,b11110000		;get the integer 4-bit LSB
	call	degto7seg			;convert it to 7-segment word
	cbr	7segout,b10000000		;set decimal point off
	ldi	7temp,b00100000
	out	PORTD,7temp			;set on single integer column at display
	out	PORTB,7segout		;print
	mov	7bin,7int
	swap	7bin
	cbr	7bin,b11110000		;get the integer 4-bit MSB
	call	degto7seg			;convert it to 7-segment word
	ldi	7temp,b00010000
	out	PORTD,7temp			;set on decade integer column at display
	out	PORTB,7segout		;print
complete:	
	clc					;succefull
	ret
noprint:
	;******* here put a error display flashing routine *******
	ret

;***************************************************************************
;*
;* Binary to Decimal conversion
;*
;* This subroutine converts a 8-bit Binary number to Decimal number
;*
;* Input at 7bin................Output at 7bin	
;*
;***************************************************************************

bin2deg:
	mov	dtemp,7bin
	swap	dtemp
	cbr	dtemp,b11110000		;get high 4 bits
	cpi	dtemp,$0A
	brsh	exitloopc			;exit if overflow
	mov 	7temp,7bin
	cbr	7temp,b11110000		;get low 4 bits
	cpi	7temp,$0A
	brlo	loopc
	subi	7temp,$09
	inc	dtemp
	cpi	dtemp,$0A
	brsh	exitloopc			;exit if overflow
loopc:
	swap	dtemp				;restore place of 4 high bits
	add	dtemp,7temp			;make the conversion
	mov	7bin,dtemp			;put it in the correct resistor
	ret
exitloopc:
	sec
	ret		

;***************************************************************************
;*
;* Decade to 7-segment conversion
;*
;* This subroutine converts a 4-bit Binary number to a 7-segmend word
;*
;* Input at 7bin......................Output at 7segout
;*
;***************************************************************************

degto7seg:
	tst	7bin			;compare with "0"
	ldi	7segout,b01111110
	breq	7segend
	ldi	7temp,1
	cpi	7bin,7temp		;compare with "1"
	ldi	7segout,b00110000
	breq	7segend
	inc	7temp
	cpi	7bin,7temp		;compare with "2"
	ldi	7segout,b01101101
	breq	7segend
	inc	7temp
	cpi	7bin,7temp		;compare with "3"
	ldi	7segout,b01111001
	breq	7segend
	inc	7temp
	cpi	7bin,7temp		;compare with "4"
	ldi	7segout,b00110011
	breq	7segend
	inc	7temp
	cpi	7bin,7temp		;compare with "5"
	ldi	7segout,b01011011
	breq	7segend
	inc	7temp
	cpi	7bin,7temp		;compare with "6"
	ldi	7segout,b00011111
	breq	7segend
	inc	7temp
	cpi	7bin,7temp		;compare with "7"
	ldi	7segout,b01110000
	breq	7segend
	inc	7temp
	cpi	7bin,7temp		;co "2"
	ldi	7segout,b01101101
	breq	7segend
	inc	7temp
	cpi	7bin,7temp		;compare with "3"
	ldi	7segout,b01111001
	breq	7sege
­ 