; By Eberhard Haug (c) 4.8.2017 - www.LED-Treiber.de

; Please note: use this software at your own risk - no liability assumed

;=======================================================================

; HBCD hex counter, up to 5 ASCII BCD digits in 5 separate registers

; stop at a given value, the demo includes the digit print out

; (similar to MAX7219_COUNT_BCD)

;

MAX7219_COUNT_HBCD:

    push    AKKU                                ; save counter registers

    push    AKKU2

    push    AKKU3

    push    AKKU4

    push    AKKU5

    ldi     AKKU5,'0'                           ; reset counter

    ldi     AKKU4,'0'

    ldi     AKKU3,'0'

    ldi     AKKU2,'0'

    ldi     AKKU,'0'

    ldi     MAX7219_ADDR, 1                     ; right most digit

    mov     MAX7219_DATA, AKKU

    rcall   MAX7219_PRINT_CHAR

    rcall   Delay1ms

    REPEAT.m                                    ; medium range is required

        REPEAT.m                                ; medium range is required

            REPEAT

                REPEAT

                    REPEAT

                        inc     AKKU

                        IF AKKU == #'9'+1

                            ldi     AKKU,'A'    ; next is ASCII A

                        ELSEIF AKKU == #'F'+1

                            ldi     AKKU,'0'    ; carry to next digit

                        ENDI

                        ldi     MAX7219_ADDR, 1

                        mov     MAX7219_DATA, AKKU

                        rcall   MAX7219_PRINT_CHAR

                        IF AKKU <> #'0'         ; otherwise delay is

                                                ; handled by next digit

                            rcall   Delay1ms       

                        ENDI

                    UNTIL AKKU == #'0'

                    inc     AKKU2               ; next digit

                    IF AKKU2 == #'9'+1

                        ldi     AKKU2,'A'

                    ELSEIF AKKU2 == #'F'+1

                        ldi     AKKU2,'0'

                    ENDI

                    ldi     MAX7219_ADDR, 2

                    mov     MAX7219_DATA, AKKU2

                    rcall   MAX7219_PRINT_CHAR

                    IF AKKU2 <> #'0'

                        rcall   Delay1ms

                    ENDI

                UNTIL AKKU2 == #'0'

                inc     AKKU3                   ; next digit

                IF AKKU3 == #'9'+1

                    ldi     AKKU3,'A'

                ELSEIF AKKU3 == #'F'+1

                    ldi     AKKU3,'0'

                ENDI

                ldi     MAX7219_ADDR, 3

                mov     MAX7219_DATA, AKKU3

                ori     MAX7219_DATA, 0x80      ; set DP

                rcall   MAX7219_PRINT_CHAR

                IF AKKU3 <> #'0'

                    rcall   Delay1ms

                ENDI

            UNTIL AKKU3 == #'0'

            inc     AKKU4                       ; next digit

            IF AKKU4 == #'9'+1

                ldi     AKKU4,'A'

            ELSEIF AKKU4 == #'F'+1

                ldi     AKKU4,'0'

            ENDI

            ldi     MAX7219_ADDR, 4

            mov     MAX7219_DATA, AKKU4

            rcall   MAX7219_PRINT_CHAR

            IF AKKU4 <> #'0'

                rcall   Delay1ms

            ENDI

        UNTIL AKKU4 == #'0'

        inc     AKKU5                           ; next digit

        IF AKKU5 == #'9'+1

            ldi     AKKU5,'A'

        ELSEIF AKKU5 == #'F'+1

            ldi     AKKU5,'0'

        ENDI

        ldi     MAX7219_ADDR, 5

        mov     MAX7219_DATA, AKKU5

        ori     MAX7219_DATA, 0x80              ; set another DP

        rcall   MAX7219_PRINT_CHAR

        IF AKKU5 <> #'0'

            rcall   Delay1ms

        ENDI

        EXITIF AKKU5 == #'1'            ; exit the demo at 0x01_00_00

    UNTIL AKKU5 == #'0'                 ; all 5 digits done!

    pop     AKKU5

    pop     AKKU4

    pop     AKKU3

    pop     AKKU2

    pop     AKKU

    ret