;29.09.2023 test09
;
;zum testen einer subtractions-routine
;..................................
; PIC: 18F14K22//Assembler:MPASM V5.43//IDE: MPLABX v5.20//PICkit3
;
;mclre enabled = normale io-nutzung somit disabled = a3 nur fr programming
;	
;
#include <p18F14K22.inc>

    ;Config settings
    CONFIG IESO = OFF, PLLEN = OFF, FOSC = IRC, FCMEN = OFF, PCLKEN = OFF
    CONFIG BOREN = OFF, BORV = 19, PWRTEN = OFF, WDTEN = OFF
    CONFIG MCLRE = ON, HFOFST = OFF, DEBUG = OFF, STVREN = ON
    CONFIG XINST = OFF, BBSIZ = OFF, LVP = OFF
    CONFIG CP0 = OFF, CP1 = OFF
    CONFIG CPD = OFF, CPB = OFF
    CONFIG WRT0 = OFF, WRT1 = OFF
    CONFIG WRTB = OFF, WRTC = OFF, WRTD = OFF
    CONFIG EBTR0 = OFF, EBTR1 = OFF
    CONFIG EBTRB = OFF

    errorlevel -302      ;surpress the 'not in bank0' warning

GPR_VAR         UDATA
adclow          RES     1
adchigh         RES     1
eeplow          RES     1
eephigh         RES     1
sub             RES     1
;.....................................................................
     Org 0x0000                 ;kein reset vector
     bra    START

MAIN_PROG   CODE

START:                          ;Setup main init
     movlw      b'00000010'     ;set cpu clock speed of 31KHz
     movwf      OSCCON          ;move contents of working register into OSCCON
     clrf       OSCTUNE         ;
          
     clrf       LATA
     clrf       TRISA           ;a3 ist nur als eingang implementiert
     clrf       ANSEL            ;dig buffer enabled
     movlw      b'11111111'     ;alle pull-ups-r's enabled
     movwf      WPUA            ;
     clrf       LATB            ;
     clrf       TRISB           ;
     clrf       LATC            ;
     clrf       TRISC           ;
;------------------------------------------------------------------------------
MainLoop:                ;beginn hauptprogramm
    nop
main01:
    movlw   d'140'       ;fr TEST-ROUTINE hier die 8-bit-werte fr adc eingeben 
    movwf   adclow
    movlw   d'00'        ; 2 empfohlene beispiele: erst dez 140, danach dez 160
    movwf   adchigh    
main04:                
    movlw   d'17'        ;eep ist in fast allen fllen grer,
    movwf   eeplow	 ;deshalb eep minus adc
    clrf    eephigh      ;
    nop
    call    subtrahier   ;EEP MINUS ADC
    nop
    bnn     main05       ;ergebnis ist zero oder positiv: jetzt anzeigen 
    bra     hoeher       ;negativ : adc ist grer als eeprom: stufe hher
;---------------
main05:                  ;ergebnis subtract ist positiv
    nop                  ; 
    bra     MainLoop     ;ein durchlauf fertig
hoeher:                  ;ergebnis subtract ist negativ
    nop
    nop
    bra     main04       ;zum erneuten einpassen
;------------------
                             ;eingnge: adc in adclow u adchigh
subtrahier:                  ;          eeprom in eeplow u eephigh
                             ;eep ist in fast allen fllen grer als adc,
sub1:                        ;deshalb eep minus adc
    movff   eephigh,sub      ;eephigh soll nicht berschrieben werden: sichern
    movf    adclow,0,1       ;adclow in WREG
    subwf   eeplow,0,1       ;eep minus adc (WREG),ergebnis in WREG
    bnn     sub2             ;wenn positiv nach sub2
    decf    eephigh          ;ist negativ: fr nchsten substract negativer
                             ;bertrag in eephigh
sub2:
    movf    adchigh,0,1      ;adchigh in WREG
    subwf   eephigh,0,1      ;eeprom minus adc (WREG),ergebnis in WREG
    movff   sub,eephigh      ;eephigh zurck sichern
                             
    return               
;--------------------

    end
 ;--------------------------------
