************************ DEFINIRANJE KONSTANT *********************************
                              
PODH    EQU   $2010
PODL    EQU   $2011
VSOTAL  EQU   $2012
VSOTAH  EQU   $2013
VSOTAC  EQU   $2014
STEVEC  EQU   $2015
ENA     EQU   $2016
DESET   EQU   $2017
STO     EQU   $2018
TISOC   EQU   $2019
MEANL   EQU   $2020
MEANH   EQU   $2021
ENAB    EQU   $2022
DESETB  EQU   $2023
STOB    EQU   $2024
TISOCB  EQU   $2025

*************************  RESET ZA ADP ***************************

	ldaa  #%00111010	nastavi DDRD
	staa  _DDRD
	ldaa  #%01111111	nastavi SPI kontr. reg. na HC11
	staa  _SPCR

	jsr   CSYES	
	ldaa  #%11111111  1. byte enic	RESET ADP
	staa  _SPDR
	jsr   ST
	jsr   CSNO
	jsr   CSYES	
	ldaa  #%11111111	2. byte enic
       	staa  _SPDR
	jsr   ST
	jsr   CSNO
	jsr   CSYES	
	ldaa  #%11111111	3. byte enic
       	staa  _SPDR
	jsr   ST
	jsr   CSNO
	jsr   CSYES	
	ldaa  #%11111111	4. byte enic
       	staa  _SPDR   
	jsr   ST
	jsr   CSNO

	jsr   CSYES	
	ldaa  #$00010000	vpis v kom.reg. + naslednji vpis je v nast.reg.
	staa  _SPDR
	jsr   ST
	jsr   CSNO

	jsr   CSYES	
       	ldaa  #$01101000	vpis v nastavitveni reg.
	staa  _SPDR
	jsr   ST
	jsr   CSNO

*************************** NASTAVITEV KONSTANT **********************************
	
	ldaa  #$00	nastavi zacetne vrednosti
	staa  VSOTAC
	staa  VSOTAL
	staa  VSOTAH
	staa  MEANL
	staa  MEANH
	ldaa  #%00111111
	staa  ENAB
	staa  DESETB
	staa  STOB
	staa  TISOCB
	ldaa  #%01000000
	staa  STEVEC
	

***********************************************************************************
************************  Pisanje na LED display  *********************************
***********************************************************************************

LED_0   ldaa  LED0
        staa  _DPRB
        ldaa  #%11110111
        staa  _DPRA
        rts

LED_1   ldaa  LED1
        staa  _DPRB
        ldaa  #%11111011
        staa  _DPRA
        rts

LED_2   ldaa  LED2
        staa  _DPRB
        ldaa  #%11111101
        staa  _DPRA
        rts

LED_3   ldaa  LED3
        staa  _DPRB
        ldaa  #%11111110
        staa  _DPRA
        rts


*********************************************************************************
******************************  GLAVNI PROGRAM  *********************************
*********************************************************************************

MAIN    
	ldaa  ENAB
	staa  LED3
	ldaa  DESETB
	staa  LED2
	ldaa  STOB
	staa  LED1
	ldaa  TISOCB
	staa  LED0
         jsr   BRANJE
	jsr   B2BCD
	jsr   WRITE
	jmp   MAIN

	
*********************************************************************************
************************* PISANJE NA LED DISPLAY ********************************
*********************************************************************************

WRITE	ldaa  ENA	rutina, ki na display poslje podatek
	jsr   CHECK
	staa  ENAB	shrani ENAB
	
	ldaa  DESET	rutina, ki na display poslje podatek
	jsr   CHECK
	staa  DESETB	shrani DESETB
	
	ldaa  STO	rutina, ki na display poslje podatek
	jsr   CHECK
	staa  STOB	shrani STOB

	ldaa  TISOC	rutina, ki na display poslje podatek
	jsr   CHECK
	staa  TISOCB	shrani TISOCB

	rts

CHECK	cmpa  #%00000000	subrutina za ugotavljanje, kateri znak naj prikaze
	bne   S1
	ldaa  #%00111111	prikazi 0
	jmp   S

S1	cmpa  #%00000001
	bne   S2
	ldaa  #%00000110
	jmp   S

S2	cmpa  #%00000010
	bne   S3
	ldaa  #%01011011
	jmp   S

S3	cmpa  #%00000011
	bne   S4
	ldaa  #%01001111
	jmp   S

S4	cmpa  #%00000100
	bne   S5
	ldaa  #%01100110
	jmp   S

S5	cmpa  #%00000101
	bne   S6
	ldaa  #%01101101
	jmp   S

S6	cmpa  #%00000110
	bne   S7
	ldaa  #%01111101
	jmp   S

S7	cmpa  #%00000111
	bne   S8
	ldaa  #000000111
	jmp   S

S8	cmpa  #%00001000
	bne   S9
	ldaa  #%01111111
	jmp   S

S9	cmpa  #%00001001
	bne   S
	ldaa  #%01100111

S	rts

*********************************************************************************
************************* PRETVORBA IZ BIN V BCD ********************************
*********************************************************************************

B2BCD	ldaa   MEANL	**** ENICE ****
	anda   #%00001111	zbrisi zg. stiri bite
	cmpa   #%00001010	primerjaj z 9
	bmi    B1	skoci, ce je manjse od 9
	ldaa   MEANL
	adda   #%00000110	pristej 6
	staa   MEANL
	bcc    B1	skoci, ce ni prenosa
	ldab   MEANH	
	addb   #%00000001
	stab   MEANH
B1	anda   #%00001111
	staa   ENA	shrani pod enice

	ldaa   MEANL	**** DESETICE ****
	anda   #%11110000	zbrisi spodnje 4 bite
	cmpa   #%10100000 primerjaj z "9"
	bmi    B2	skoci, ce je manjse od 9
	adda   #%01100000	pristej "6"
	bcc    B2	skoci, ce ni prenosa
	ldab   MEANH
	addb   #%00000001 pristej 1
	stab   MEANH
B2	lsra 		4 x shiftaj v desno
	lsra
	lsra
	lsra
	staa   DESET

	ldaa   MEANH	**** STOTICE ****
	anda   #%00001111	zbrisi zg. stiri bite
	cmpa   #%00001010	primerjaj z 9
	bmi    B3	skoci, ce je manjse od 9
	ldaa   MEANH
	adda   #%00000110	pristej 6
	staa   MEANH
	anda   #%00001111
B3	staa   STO	shrani pod stotice

	ldaa   MEANH	**** TISOCICE ****
	lsra 		4 x shiftaj v desno
	lsra
	lsra
	lsra
	staa   TISOC
	rts
	
*********************************************************************************
******************************** BRANJE Z ADP ***********************************
*********************************************************************************

BRANJE	jsr   BERI
	ldaa  PODL
	adda  VSOTAL      pristej 1 byte k VSOTAL 
	staa  VSOTAL
	ldaa  VSOTAH
	adca  PODH
	staa  VSOTAH
	bcc   C1		skoci, ce ni prenosa
	ldaa  #%00000001	VSOTIC pristej 1, ker je prislo do prenosa
	adda  VSOTAC
	staa  VSOTAC
C1	ldaa  STEVEC	nalozi in preveri, ce je stevec ze 0
	deca
	staa  STEVEC
	bne   BRANJE		ce stevec ni 0, zakljuci 
	jsr   POVP
	ldaa  #$00	nastavi zacetne vrednosti
	staa  VSOTAC
	staa  VSOTAL
	staa  VSOTAH
	ldaa  #%01000000
	staa  STEVEC	
C4	rts	


POVP 	ldaa  VSOTAL	SUBRUTINA za povprecenje rezultata
	lsra		6 x shiftaj v desno
	lsra
	lsra
	lsra
	lsra
	lsra
	staa  VSOTAL	
	ldaa  VSOTAH	
	asla 		2 x shiftaj v levo
	asla		
	adda  VSOTAL	pristej VSOTAL
	staa  MEANL
	ldaa  VSOTAH
	lsra		6 x shiftaj v desno
	lsra
	lsra
	lsra
	lsra
	lsra
	staa  VSOTAH	
	ldaa  VSOTAC	
	asla 		2 x shiftaj v levo
	asla		
	adda  VSOTAH	pristej VSOTAH
	staa  MEANH
	rts


BERI	jsr   CSYES	subrutina za branje enega podatka iz ADP
	ldaa  #%00001011	vpis v kom.reg. in doloci branje iz kom.reg.
	staa  _SPDR
	jsr   ST
	jsr   CSNO

	ldaa  #%00110000
	staa  _DDRD
	jsr   CSYES
	ldaa  #%00000000 	beri SPDR
	staa  _SPDR
	jsr   ST
	jsr   CSNO
	ldaa  #%00111000
	staa  _DDRD	

	ldaa  _SPDR	
	anda  #%11000000	preveri bita DRDY in ZERO
	beq   BERI	skoci na BERI, ce podatki se niso pripravljeni

	jsr   CSYES
	ldaa  #%00111011	nastavi pod. reg. za branje
	staa  _SPDR
	jsr   ST
	jsr   CSNO

	jsr   CSYES	
       	ldaa  #%00000000  beri SPDR
	staa  _SPDR
	jsr   ST
	jsr   CSNO
	ldaa  _SPDR	preberi zg. byte pod. reg.
	anda  #%01111111  prvi bit postavi na 0 ??????
	staa  PODH

	jsr   CSYES
	ldaa  #%00000000	beri SPDR
	staa  _SPDR 	
	jsr   ST
	jsr   CSNO
	ldaa  _SPDR	preberi sp. byte pod. reg. 
	staa  PODL
	rts


**************************************************************************************
************************************  SUBRUTINE  *************************************
**************************************************************************************

ST       ldaa  _SPSR	subrutina, ki preverja, ce je bilo sporocilo uspesno poslano
	bpl   ST		
	rts

CSYES	ldaa  _PORTD	subrutina, enabla ADP 
	anda  #%11011111
	staa  _PORTD
	rts

CSNO	ldaa  _PORTD	subrutina, ki disabla ADP
	eora  #%00100000
	staa  _PORTD
	rts