 ;*********  THIS SOURCE CODE IS MADED BY @BUCO ************
 ;
 ;**** "MALI PROGRAMER"*********"LITTLE PROGRAMMER"****************
 ;*** ATMEGA128 163 AVR90s SERIE & PIC16 SERIE  ******
 ;*** ISP SERIAL PROGRAMMER *************
 ; **  THIS PROGRAMMER IS MADED AS SIMPLE NEED FOR ONE SIMPLE PROGRAMMER
 ;** FOR ATMEGA128 MCU , HAVE FULLY SUPORT ISP MODE FOR MEGA SERIE 128W AND 64W
 ;**  AVR SERIE AND PIC16 SERIE AS I SAID ALSO  HAVE ALGO FOR PROGRAMMING
 ;**I2C SERIAL EEPROMS AS YOU CAN SEE******
 ;** I HOPE THAT THIS SOURCE CODE WILL HELP TO ALL PEOPLE WHO GOT WISHES TO WORK
 ;** OWEN PROGRAMMER BUT DON'T KNOW HOW TO START TO WROTE ALGO FOR PROGRAMMING SOEME MOST
 ;** USED MCUs AND SERIAL EEPROMS ********************
 ;** I'LL BE GLAD IF THIS SOURCE CODE CAN HELP TO ANYONE IN FUTURE LEARNING !!!!!!!
 ; ** FELL FREE TO UPDATE AND TO CHANGE WHATEWER YOU WANT COSE THIS IS HERE FOR YOU !!!!!
 ;** I HOPE THAT YOU WILL SEND TO ME YOUR UPINIONS AND SUGESTION COSE I THINK THAT THIS CAN BE
 ;** INTERESTING START FOR SOEME FUTURE  SERIE OF "LITTLE" PROGRAMMERS !!!!!
 ;** SOFTWARE IS TESTED (JUST NOT AS I WROTE ON THE END I2C EEPROM PART I JUST ADD ALGO BUT
 ;** DIDNT HAVE TIME TO TEST IT ..) AND WORKED ! WHITH THIS CODE BECOME VB SCRIPT(FOR WINEXPLORER)
 ;**AND TWO BOOTLOADER FILES(atm128 ,atm163)  AND SCHEMATIC DIAGRAM !!!!!!
; ******* Bubble Software *******
;
         LIST   P=PIC16F84
;
         __CONFIG 0x3FF9
;
         __IDLOCS H'FFFF'
;
;
; RAM Page 0 Registers
;
INDF         EQU 0x0000
TMR0         EQU 0x0001
PCL          EQU 0x0002
STATUS       EQU 0x0003
FSR          EQU 0x0004
PORTA        EQU 0x0005
PORTB        EQU 0x0006
EEDATA       EQU 0x0008
EEADR        EQU 0x0009
PCLATH       EQU 0x000A
INTCON       EQU 0x000B
;
; RAM Page 1 Registers
;
OPTION_REG   EQU 0x0081
TRISA        EQU 0x0085
TRISB        EQU 0x0086
EECON1       EQU 0x0088
EECON2       EQU 0x0089
;
; STATUS bits
;
C            EQU 0x00
DC           EQU 0x01
Z            EQU 0x02
PD           EQU 0x03
TO           EQU 0x04
RP0          EQU 0x05
RP1          EQU 0x06
IRP          EQU 0x07
;
; INTCON bits
;
RBIF         EQU 0x00
INTF         EQU 0x01
T0IF         EQU 0x02
RBIE         EQU 0x03
INTE         EQU 0x04
T0IE         EQU 0x05
EEIE         EQU 0x06
GIE          EQU 0x07
;
; OPTION_REG bits
;
PS0          EQU 0x00
PS1          EQU 0x01
PS2          EQU 0x02
PSA          EQU 0x03
T0SE         EQU 0x04
T0CS         EQU 0x05
INTEDG       EQU 0x06
RBPU         EQU 0x07
;
; EECON1 bits
;
RD           EQU 0x00
WR           EQU 0x01
WREN         EQU 0x02
WRERR        EQU 0x03
EEIF         EQU 0x04
;
LOWADR      EQU  0X0F         ; REGISTRY DEFINITION 
HIADR       EQU  0X10
SHIADR      EQU  0X11
MFD         EQU  0X13         ; MANFACTURER DATA
DEVD        EQU  0X14         ; DEVICE CODE
DEVCFG      EQU  0X15         ; SIGN BYTE ZA ATMELE
SHIADC      EQU  0X16         ; SHI ADR  IZ DBASE
HIADC       EQU  0X17         ; HI ADR  IZ DBASE
EPADC       EQU  0X18         ; INT EEPROM ADRES IZ DBASE (ZA MCU-e)
PODATAK     EQU  0X19         ; PODAK ZA UPIS ILI PROCITANI IZ IC-a (DATA)
LEN         EQU  0X12
INTLEN      EQU  0X0E         ; INTERNI LEN BITAN ZA MCU-e// INTERN LEN
INTCMD      EQU  0X0D         ; INTERNA NAREDBA  ZA MCU-e// INTERN COMMAND
CMD         EQU  0X0C         ; MAIN COMMAND
MARKER      EQU  0X22
DELB        EQU  0X23
PIC1        EQU  0X25
PIC2        EQU  0X26
PIC3        EQU  0X27
PIC4        EQU  0X28
PIC5        EQU  0X29
LOOP        EQU  0X20
LOOP1       EQU  0X2A
LOOP2       EQU  0X2B
LOOP3       EQU  0X2C
;TDO         EQU  PORTB,4h    ;
;TDI         EQU  PORTB,5h
;TMS         EQU  PORTB,3h
;TCK         EQU  PORTB,2h
;
;
            ORG 0000h
;
ad0000      GOTO INIHW                 ; 
ad0001      ADDLW 0x01
            BTFSS STATUS,Z
ad0003      GOTO ad0001
            RETURN
            ;
IZLAZNI     BSF STATUS,RP0         ; OUT
            BCF PORTB,7h           ; IZLAZNI
            BCF STATUS,RP0
            RETURN
            ;
PINP       BSF   STATUS,RP0      ; DATA IN PORT
           BSF   TRISB,4h        ; PODATAK U PORT B4
           BCF   STATUS,RP0
           RETURN
           ;
POUT       BSF  STATUS,RP0      ; DATA OUT PORT
           BCF  TRISB,4h        ; PODATAK IZ PORTA B4
           BCF  STATUS,RP0
           RETURN
           ;
ad00D7      MOVWF PODATAK     ; UPISI 
ad00D9      MOVLW 0x08        ; DATA SEND
            MOVWF LOOP
            MOVLW 0xF2             ; B2(9600) , EC (28800) 1/4 odnos  F2 1/6(38400)
            CALL ad0001
            NOP                    ; 28800
ad00DD      BTFSS PORTB,7h
            GOTO ad00DD
            BSF STATUS,RP0
            BCF TRISB,7h           ; IZLAZNA
            BCF STATUS,RP0
            BCF PORTB,7h
            MOVLW 0xF9             ; DB(9600) , F6(28800)  F9 38400
            CALL ad0001
ad00E5      RRF PODATAK,F
            BTFSC STATUS,C
            BSF PORTB,7h
            BTFSS STATUS,C
            BCF PORTB,7h
            MOVLW 0xF9             ; DC(9600) , F6(28800)  F9 38400
            CALL ad0001
            NOP                    ; 28800
            DECFSZ LOOP,F
            GOTO ad00E5
            GOTO ad00EF            ; DEL
ad00EF      BSF PORTB,7h
ULAZNA      BSF STATUS,RP0
            BSF TRISB,7h           ; ULAZNI
            BCF STATUS,RP0
            RETURN
            ;

ad00F4      MOVLW 0x08            ; READ DATA
            MOVWF LOOP
ad00F6      BTFSS INTCON,T0IF
            GOTO ad00F6
ad00F8      BTFSS PORTB,7h
            GOTO ad00F8
ad00FA      BTFSC PORTB,7h
            GOTO ad00FA
            MOVLW 0xFB            ; E8(9600), 6 (28800) F9  , FB 38400
ad00FD      BTFSC PORTB,7h
            GOTO ad00FA           ;
            ADDLW 0x01
            BTFSS STATUS,Z        ; MOZDA STAVITI NOP
            GOTO ad00FD
            MOVLW 0xFC            ; 14H(9600)EB , 5h (28800) FA  , 0 (115000)??, FC 38400 +NOP
            CALL ad0001
ad0104      BCF STATUS,C
            BTFSC PORTB,7h
            BSF STATUS,C
            RRF PODATAK,F
            MOVLW 0xF9           ; (28800) F6 , (9600) DC , F9 38400
            CALL ad0001
            DECFSZ LOOP,F
            GOTO ad0104
            MOVLW 0xBC
            MOVWF TMR0
            MOVF PODATAK,W
            BCF INTCON,T0IF
            RETURN
            ;
READINS     BSF STATUS,RP0         ; READING INSTRUCTION FROM SCRIPT
            BSF TRISB,7h           ; ULAZNI
            BCF STATUS,RP0
RDINS       CALL ad00F4             ;0135
            XORLW 0x01
            BTFSS STATUS,Z
            GOTO RDINS
            CALL ad00F4
            MOVWF CMD              ; NAREDBA // DECODING INSTRUCTION
            CALL ad00F4
            MOVWF INTCMD              ; DODATAK NAREDBI
            CALL  ad00F4
            MOVWF  INTLEN             ; INTERNI LEN
            CALL ad00F4
            MOVWF LOWADR              ; ADR LOW
            CALL ad00F4
            MOVWF HIADR              ; ADR HI
            CALL ad00F4
            MOVWF SHIADR              ; ARD SHI
            CALL  ad00F4             
            MOVWF LEN              ; LEN   ZA GW =32 BYTA         
            MOVWF 0x21
            MOVF  0X21,W
            BTFSC  STATUS,Z
            RETURN
            MOVLW  0X2F
            MOVWF  FSR
ad0156      CALL ad00F4
            MOVWF  INDF
            INCF  FSR,F
            DECFSZ 0x21,F
            GOTO ad0156
ad015D      RETURN
            ;            

INIHW       BSF STATUS,RP0             ; POCETAK + INI HARDVER !
            BCF   TRISB,3h             ; OUT XTAL//CK(PIC)
            BCF   TRISB,2h             ; OUT SCK
            BSF   TRISB,6h
            BCF   TRISB,5h
            CLRF  TRISA                ; OUT
            BCF STATUS,RP0
            CLRF  PORTA
            CLRF  PORTB
            MOVLW 0x5F
            OPTION
            CLRF  EPADC
            CLRF  DEVCFG
POCETAK     CALL  POSABC          ; BEGIN  DECODING INSTRUCTION
           ; BSF   PORTA,2h        ; SET Vcc
            CALL  READINS                ; PORT READING !!!
            MOVF  CMD,W
            XORLW  0XD0
            BTFSC  STATUS,Z
            CLRF  PORTA
            MOVF  CMD,W
            XORLW  0XD2                 ; microchip --test---
            BTFSC  STATUS,Z
            GOTO   PICMOD
            MOVF  CMD,W
            XORLW  0XDA
            BTFSC  STATUS,Z
            GOTO   IDENT               ; IDENT MODE  ATMEL//PIC
            MOVF  CMD,W
            XORLW  0XD5                 ; READ FUSE AND LOCK BITS ATMEL
            BTFSC  STATUS,Z
            GOTO  ATCLR
            MOVF  CMD,W
            XORLW  0XD7
            BTFSC  STATUS,Z
            GOTO  ATCLW                 ; WRITE LOCK AND FUSE BITS
            MOVF  CMD,W
            XORLW  0XDF              ; INTRNE NAREDBE ZA ATMEL CARD
            BTFSC  STATUS,Z
             GOTO  ATINCM
            MOVF   CMD,W
            XORLW  0XC3              ;   EXT EEPROM MODE
            BTFSC  STATUS,Z
            GOTO  I2CWRX              ;  EXT EPWR
            MOVF  CMD,W
            XORLW  0XC5
            BTFSC  STATUS,Z
            GOTO  I2CRDX             ; EXT EEP READ
            GOTO  POCETAK            ; P
            ;
            
IDENT      CLRF  PORTA
           GOTO  ATCARD
PTL3       CLRF  PORTA
           NOP
           CLRF  MARKER
           ;CALL  PICID        ; 877/876
PTL4       CALL  IZLAZNI
            MOVF  MFD,W        ; MANFACTURER DATA
            CALL  ad00D7
            MOVF  DEVD,W       ; DEVICE DATA  ORGINAL PROCITAN IZ FLASHA
            CALL  ad00D7
            MOVF  DEVCFG,W     ; NADJENI IZ BAZE !!!!! NIJE CFG TO JE TRECI ID BYTE
            CALL  ad00D7
            GOTO  POCETAK
            ;
POSABC      BSF  STATUS,RP0       ; PORTOVI SU IZLAZNI / OUT PORT
            BCF  PORTB,1h
            BCF  PORTB,2h         ; SCK    /// PORTB.4 == I/O DATA PORT// MOSI
            BCF  PORTB,5h         ; MOSI  
            BCF  PORTB,3h         ; XTAL 
            BCF  STATUS,RP0
            CLRF  PORTB
            RETURN
            ;            
ATCARD   BSF   MARKER,7h       ; //ATMEL CARD ALGO FOR ISP MODE///////
         CALL  POSABC
         CLRF  PORTB
         BSF   PORTA,2h        ; Vcc== 5V
         NOP
         NOP
         NOP
         NOP
         CALL   PINP           ; RB4==MISO  //  RB5==MOSI
         BCF   PORTA,2h         ; RESET Vcc
         NOP
         NOP
         BCF   PORTB,2h         ; SCK==0  ////ATM128/103 
         NOP
         CALL  RESPL           ; RESET PULSE RES=1 2x XTAL=PULSE RES=0
         NOP
         BSF   PORTA,2h         ; SET Vcc
         MOVLW   0XFF
         CALL  DEL01
         MOVLW  0X20
         MOVWF  LOOP           ; MAX 32 POKUSAJA
         MOVLW   0X25         ; RADIO SA 15
         MOVWF   DELB         ; DELAY BAYTE
         CALL  ATRDEL         ; RES PULSE + DELAY 20 mS         
ATC1     MOVLW   0XAC          ; ---ENEABLE SERIAL PROGRAMING  AC 53 XX XX----
         MOVWF   PIC1           ; 4A
         MOVLW   0X53          ; TEST BYTE
         MOVWF   PIC2
         MOVWF   PIC3
         MOVWF   PIC4
         CALL   ATCK          ; END ENEABLE PROGRAMMING
         MOVF   PIC3,W       ; ECHO BYTE (4C)
         XORLW   0X53
         BTFSC  STATUS,Z
          GOTO  ATCID          ;  ATC7
        ; CALL   SCKPL       ; ZA AVR I ATMEGA DO 163  POZITIV IMPULS ZA SCK
         CALL   RESPL       ; ZA AT128/103  POZITIV IMPULS ZA RESET (IDU I AVRI I ATM 163)?
          DECFSZ  LOOP,F
          GOTO   ATC1
          GOTO   ATCEND         ; NIJE DOBAR /// IZADJI
ATCID    CLRF   MARKER         ;-----READ ID  30 00 XXXXXXBB 00-------
         MOVLW   0X30
         MOVWF   PIC1
         MOVWF   PIC2
         CLRW
         MOVWF   PIC3
         MOVWF   PIC4
         CALL   ATCK
         MOVF   PIC5,W
         MOVWF  MFD       ; READED MFD,DEVD,DEV ID
         MOVLW   0X01
         MOVWF   PIC3
         MOVWF   PIC4
         CALL   ATCK
         MOVF   PIC5,W
         MOVWF  DEVD      ; DEVD
         MOVLW   0X02
         MOVWF   PIC3
         MOVWF   PIC4
         CALL   ATCK
         MOVF   PIC5,W
         MOVWF  DEVCFG    ; SIGN ID
         MOVF  MFD,W       ; OBRADA AKO JE ZAKLJUCAN
         BTFSS  STATUS,Z
         GOTO   PTL4
         BTFSS  DEVD,0h
         GOTO  ATCEND
         BTFSS  DEVCFG,1h     ; 00 01 02 LOCKED CARD R52 DEVCFG // DETECTING LOCK CODE
         GOTO  ATCEND
         MOVLW   0X1E       ; MFD  ATMEL mcu AVR
         MOVWF  MFD
ATCLC    GOTO PTL4            ;
         ;
ATINCM      MOVF INTCMD,W               ;NAREDBE !!!!// INSTRUCTIONS FOR MANIPULATE WHITH ATMEL
            XORLW 0x80                   ; 01DF80.....
            BTFSC STATUS,Z
            GOTO ATCER                  ; ERASE // BRISI SVE//
            MOVF  INTCMD,W                   ; T
            XORLW 0x03
            BTFSC STATUS,Z
            GOTO  ATCRD                   ; READ INT EEP
            MOVF  INTCMD,W
            XORLW  0x02
            BTFSC  STATUS,Z
            GOTO ATCRD                   ; READ FLASH
            MOVF  INTCMD,W
            XORLW 0x11
            BTFSC  STATUS,Z
            GOTO  ATCWR                  ; WRITE  DF11 INT EEP
            MOVF  INTCMD,W
            XORLW  0X10
            BTFSC  STATUS,Z
            GOTO  ATCWR                 ;   WRITE FLASH
            GOTO  POCETAK

ATCER     MOVLW   0XAC      ;------ATMEL CARD ERASE MODE--------
          MOVWF   PIC1
          MOVLW   0X80
          MOVWF   PIC2
          CLRF    PIC3
          CLRF    PIC4
          CALL   ATCK
          MOVLW   0X40       ; 1E=8 mS 40==35 mS
          BTFSC   DEVCFG,1h    ; TEST ATMEGA 163
          MOVLW   0X25       ;  9mS ///30mS
          MOVWF   DELB
          CALL    ATC4     ; IMEDJU DODATI BLANK CHEK !!! I DEL ZA 20 mS
          MOVWF   0X14
          MOVWF  DELB
          CALL   ATRDEL    ; POSITIVE PULSE TO RESET AND 20mS DELAY
ATC0F     ;CALL   IZLAZNI
          MOVF  MARKER,W   
          CALL  ad00D7
          GOTO    POCETAK 
          ;

ATCRD     CLRF  MARKER        ; -------ATM CARD READ MODE ------- 2B
          CLRF  SHIADC
          CLRF   PIC2
          CLRF   PIC3
          CLRF   PIC4
ATCRD4    MOVLW   0X08
          MOVWF   LOWADR
ATCRD3    MOVLW   0X2F    ; ZA 32 BYTE
          MOVWF   FSR
          MOVF   INTLEN,W
          MOVWF  HIADC
ATCRD5    MOVLW   0X20      ; 20=LOW BYTE  28=HI BYTE
          BTFSC   INTCMD,0h
          MOVLW   0XA0      ; NAREDBA ZA INT EEPROM
          MOVWF   PIC1
          MOVF   FSR,W
          MOVWF  LEN
          CALL   ATCK
          MOVF   LEN,W
          MOVWF  FSR
          MOVF   PIC5,W
          MOVWF  INDF     ; POHRANI PROCITANI  BYTE U SRAM// STORE READED DATA FROM CARD TO SRAM
          INCF   FSR,F
          BTFSC  INTCMD,0h      ; CMND TEST
          GOTO  ATCRD0       ; ZA READ INT EEPROM
          BSF   PIC1,3h     ; 28 HI BYTE
          MOVF   FSR,W
          MOVWF  LEN
          CALL   ATCK
          MOVF   LEN,W
          MOVWF  FSR
          MOVF   PIC5,W
          MOVWF  INDF
          INCF   FSR,F
ATCRD0    INCF   PIC3,F     ; LOW ADR ZA 4 CYKL NAREDBE ZA ATMEL CARD
          MOVF   PIC3,W
          BTFSC  STATUS,Z
          INCF   PIC2,F
          DECFSZ  HIADC,F
           GOTO  ATCRD5
          CALL   SEND32
          DECFSZ  LOWADR,F  ;  8X32=256 BAYTA /// FIXNA VRIJEDNOST U RUTINI
           GOTO  ATCRD3  
          DECFSZ  HIADR,F  ;  MAXL
           GOTO   ATCRD4
          MOVF  SHIADR,W
           BTFSC STATUS,Z
           GOTO  ATCNG
           DECF  SHIADR,F
           MOVLW  0XFF
           MOVWF  HIADR
           GOTO  ATCRD4
          ;
          
ATCNG     MOVLW    0X01
          CALL   ad00D7      ; 01 END
          GOTO  POCETAK
          ;
SEND32    MOVLW   0X2F
          MOVWF   FSR
          MOVLW   0X20
          MOVWF  LOOP1
ATCRD6    MOVF  INDF,W    ; ISPUCAVA U PC 32 BYTA// SEND TO PC 32 BYTE OF READED DATA BLOCK
          CALL   ad00D7
          INCF   FSR,F
          DECFSZ  LOOP1,F    ; TEST
           GOTO   ATCRD6
           CALL  ULAZNA
          CALL  ad00F4       ; CITAJ BYTE// READ BYTE
          RETURN
          ;
ATCWR     CLRF  MARKER      ; ZA SAD  ------- ATM CARD WRITE MODE -------
          MOVF  LOWADR,W
          MOVWF  PIC2      ; za BL start poziciju atmxxx
         ; MOVF  HIADR,W
          ;CLRF  PIC2
          CLRF  PIC3
ATCWRU    MOVLW  0X08
          MOVWF  CMD        ; BLOK COUNTER
ATCWRO    CALL  READ32
          MOVF  INTLEN,W
          MOVWF  SHIADC
          MOVLW  0X2F
          MOVWF  FSR
ATCWR3    MOVLW   0X40      ; 40=LOW BYTE  48=HI BYTE // ATM163 TO JE LOAD PAGE DATA
          BTFSC  INTCMD,0h
          MOVLW   0XC0      ; INTEEP WRITE
          MOVWF   PIC1
ATCWR2    MOVLW  0X06     ;    6=4,2mS /// 5=3mS
          MOVWF  DELB      ; ZA DELAY
          MOVF   INDF,W     ; 
          MOVWF  PIC4       ; PODATAK
          INCF   FSR,F
          INCF   EPADC,F      ; LOOP COUNTER
          BTFSC  DEVCFG,1h
          GOTO   ATCWR5
          MOVF   PIC4,W     ; BIO PIC4 (PODATAK)
          XORLW  0XFF
          BTFSC  STATUS,Z
          GOTO  ATCWR8
ATCWR5    MOVF   FSR,W       ; POCINJE  UPISNA JEZGRA ///////
          MOVWF  HIADC
          CALL   ATCK
          MOVF   HIADC,W
          MOVWF  FSR       ; UPIS NAREDBE ZA AT90 WRITE MODE /ATM LOAD PAGE DATA
          BTFSC INTCMD,0h
           GOTO  ATCWXZ
          BTFSS  DEVCFG,1h  ; TEST ZA ATMEGU
ATCWXZ    CALL  ATC4        ; DELAY
ATCWR8    BTFSC  INTCMD,0h    ; BIO R33,0h
          GOTO  ATCWR7      ; TEST I MOD ZA INT EEPROM
          BTFSC  PIC1,3h
          GOTO  ATCWR7
          BSF   PIC1,3h    ; 48 HI BYTE
          GOTO  ATCWR2
ATCWR7    INCF   PIC3,F    ; LOW ADR
          DECFSZ  SHIADC,F  ; BIO R2E // INT LEN NE MOZE ! MORAT CE IC PO DEV CFG BYTE 93/95/97?
          GOTO   ATCWR3 
          BTFSC  DEVCFG,1h  ; ATMEGA TEST
          CALL   ATWMG      ; PROGRAMIRANJE ATMEGA CHIPA
          MOVF  PIC3,W
          BTFSC  STATUS,Z
          INCF   PIC2,F     ; HIADR
          MOVF   MARKER,W
          CALL   ad00D7     ; SEND 00
          DECFSZ  CMD,F
           GOTO  ATCWRO     ; BLOK COUNTER 32X8 =256
          DECFSZ  HIADR,F   ; MAXL 93==
           GOTO  ATCWRU
          INCF  MARKER,F    ; KRAJ UPISA// END OF WRITE
          GOTO   ATC0F
          ;

ATWMG     BTFSC  INTCMD,0h  ;-------NASTAVAK ZA AT MEGA KARTICE--------
          RETURN            ;ADDED ROUTINE FOR MEGA SERIE 128 AND 64 WORD PAGE 
          MOVF  DEVD,W       ; ------!!!! dodje DEVD !!!!!--------
          XORLW  0X97       ; TEST ATMEGA 128 !!!
          BTFSS  STATUS,Z
          GOTO   ATM63
          MOVF   EPADC,W
          BTFSS  STATUS,Z
          RETURN
          GOTO  ATWMG1      ; UPISI U FLASH 256 BYTE 128W
ATM63     MOVF  EPADC,W     ;NAPRAVITI 00,40,80,C0 TEST
          XORLW   0X80      ;TEST DALI JE SVIH 64W==80H/128DEC UPISANO
          BTFSS   STATUS,Z
          RETURN
ATWMG1    DECF   PIC3,F    ; RADI TOCNE POSICIJE BLOKA !!!
          MOVLW  0X4C
          MOVWF  PIC1     ; WRITE PAGE MEM ZA AT MEGA !!! 4C-->R4A
          MOVF   FSR,W       ; POCINJE  UPISNA JEZGRA ///////
          MOVWF  HIADC
          CALL   ATCK
          MOVF   HIADC,W
          MOVWF  FSR       ;
          MOVLW  0X12
          MOVWF  DELB     ; DELAY
          CALL   ATC4
          CLRF   EPADC
          INCF  PIC3,F
          RETURN
          ;          

         ;
ATCLW    ;MOVLW  0X14     ; ------UPIS LOCK AND FUSE  BITOVA ----------
         ;MOVWF  DELB
         MOVF   INTLEN,W
         MOVWF   PIC1
         MOVF   LOWADR,W          ; ATm163 LOCK BIT ILI FUSE BIT(LOW HIGH) E0=LB A0=FBL A8=FBH
         MOVWF  PIC2
         MOVF   HIADR,W
         MOVWF  PIC3        ; BITOVI ZA UPIS
         MOVF   SHIADR,W
         MOVWF  PIC4        ; BITOVI ZA UPIS
         MOVWF  MARKER     ; TU CUVA CFG BYTE
LW0X     CALL  ATCK
         CALL  ATC4
         CALL  ATCL1        ; CITA UPISANI BYTE
         ;MOVF   PIC5,W
         ;MOVWF  MARKER
         ;CALL  ATC0F
         ;
ATCLR    MOVF  INTLEN,W     ; -------READ LOCK ILI FUSE BITS--------
         MOVWF  PIC1
         MOVF  LOWADR,W    ; 00 = FB & LOCK LOW --08= FB HIGH
         MOVWF  PIC2
         MOVF   HIADR,W
         MOVWF  PIC3       ; TREBA ZA CALIBRACIONI BYTE
ATCL1    CLRF  PIC5
         CALL  ATCK
         MOVF   PIC5,W
         CALL  ad00D7
         GOTO   POCETAK
         ;
ATCK      MOVLW  0X25        ;---INST ROUTINE   GLAVNA RUTINA -----
          MOVWF  FSR
          MOVLW  0X04        ; 
          MOVWF  LOOP2       ; 34
ATCR1     MOVF   INDF,W
          MOVWF  PODATAK
          MOVLW  0X08             ; ----4 CYKLE INSTR  ROUTINE----
          MOVWF  LOOP1             ; SEND 8 BITS  R34
          BCF  STATUS,C
          CLRF   PIC5
          CALL  ATCR
          MOVF   PIC5,W          ; --DODJE 4E
          BTFSC  MARKER,7h         ; 3E 
          MOVWF  INDF
          INCF   FSR,F
          DECFSZ  LOOP2,F        ; 35
          GOTO   ATCR1
          RETURN
          ;
ATCR      RLF   PODATAK,F              ; REGISTAR MEDIATOR
          BCF   PORTB,5h          ; RRF LSB--->MSB  //  RLF MSB--->LSB
          BTFSC  STATUS,C
          BSF   PORTB,5h          ; MOSI OUT
          BCF    STATUS,C
          MOVLW  0X0C
          MOVWF  PORTB            ; SET SCK PD2 I XTAL1 PD3
          BCF   PORTB,3h
          BTFSC  PORTB,4h
          BSF    STATUS,C
          BSF   PORTB,3h
          BCF   PORTB,3h
          MOVLW  0X08
          MOVWF   PORTB         ; SCK=0 XTAL1=1
          BCF   PORTB,3h
          BSF   PORTB,3h
          BCF   PORTB,3h
          RLF   PIC5,F             ; DATA READED ,ECHOED
          DECFSZ  LOOP1,F
          GOTO   ATCR
          RETURN
          ;
ATCEND   BSF   PORTB,1h     ;--- POSTAVI RESET NA 1 KRAJ RADA-------
         NOP                ; --SET RESET FOR END ------ 
         BCF   PORTB,1h     ; RESET Vcc==0V
         NOP
         BCF   PORTA,2h
         GOTO PTL3
          ;
RESPL    BSF   PORTB,1h       ; --RESET POSITIVE PULSE I DELAY mS//RESET==1
         BSF   PORTB,3h
         BCF   PORTB,3h       ; XTAL IMPULSI ==CPU CYKE TIME PULSE
         BSF   PORTB,3h
         BCF   PORTB,3h
         BCF   PORTB,1h        ; RESET=0
         RETURN
         ;
ATRDEL   CALL   RESPL         
ATC4     MOVLW   0x00          ; I DIDNT USE TIMER ----YOU CAN IF YOU WISH
ATC5      ADDLW  0x01           ; 
         BTFSS  STATUS,Z
         GOTO  ATC5            ; W==00==1miliS
         DECFSZ  DELB,F
         GOTO  ATC4
         RETURN
         ;            
AMDEL    MOVLW   0xA0
DEL01    ADDLW  0x01
         BTFSC  STATUS,Z
         GOTO   DEL01
         RETURN
         ;
SCKPL     MOVLW  0X0C         ; BIO 0C (XTAL=1 SCK=1)/ AT128 IDE RESET=1 I 2x XTAL=PULSE ==9
          MOVWF   PORTB       ; SET SCK I XTAL1
          BCF   PORTB,3h
          BSF   PORTB,3h
          BCF   PORTB,3h
          MOVLW  0X08
          MOVWF  PORTB
          BCF   PORTB,3h
          BSF   PORTB,3h
          BCF   PORTB,3h
          RETURN
          ;
           
PICM     CALL  POUT         ;--------- MICHROCHIP  MCU PROGRAMMING ALGO ------------
         BSF  PORTA,2h        ; Vcc=5V  //BIO PORTE
         NOP
         NOP
         CALL   POSABC        ; 
         CLRF  PORTB
         BCF  PORTB,4h      ; DATA 
         BCF  PORTB,3h      ; CK    
         NOP
         BSF  PORTA,3h      ;--- SET Vpp ==12,5 V
         NOP
         NOP                  ; SAD JE SPREMAN ZA PROGRAMIRANJE U ICSP MODU
         MOVLW  0X07         ; -- NOW IS READY FOR PROGRAMMING IN ISP SERIAL MODE----
         MOVWF  DELB          ; OKO 7 miliS
         CALL   ATC4
         RETURN
         ;
PICMOD   CALL   PICM
         BTFSC   INTCMD,2h    ;--read flash -- int==04
         GOTO  PICRDM
         BTFSC  INTCMD,5h
         GOTO   PICID         ; PIC IDENTIFIKACIJA 2006
         BTFSC   INTCMD,3h    ; ERASE- BULK----
         GOTO    PICER
         BTFSC  INTCMD,1h     ; WRITE PGM  02 /03 PDM
         GOTO   PICPWR
         BTFSC  INTCMD,4h     ; CHIPERASE
         GOTO   CERPI
         GOTO   POCETAK
         ;

PICWDT    MOVLW   0X0E       ;-------UPIS PODATKA 14 BITNOG U MCU------------
          MOVWF   LOOP       ; --------WRITE 14 BIT DATA IN MCU -----------
          CALL  POUT
          BCF  PORTB,4h
          BSF  PORTB,3h
          BCF  PORTB,3h       ; INICIJALIZACIJA ZA START BIT ck(VIDJETI KOJI JE NA ATMELU)
PICW1     BCF   STATUS,C
          RRF   PIC1,F
          RRF   PIC2,F
         BCF   PORTB,4h       ; RRF LSB--->MSB  //  RLF MSB--->LSB
         BTFSC  STATUS,C
         BSF   PORTB,4h          ;  RB6 DADTA OUT 
         BSF   PORTB,3h
         BCF   PORTB,3h
          DECFSZ   LOOP,F
          GOTO   PICW1
         BCF   PORTB,4h 
         BSF   PORTB,3h
         BCF   PORTB,3h        ; STOP BIT 
          RETURN
          ;
PICRE     MOVLW   0X0E         ;-------------CITAJE  PODATKA -------
          MOVWF   LOOP         ; 14 BITNI POINTER + START BIT + STOP BIT = 16
          CALL  PINP       ; ZAMIJENITI ZA  MOVLW b'01000000' TRIS PORTB!!
          CLRF  PIC1         ; ---READ 14 BIT DATA FROM MCU----------
          CLRF  PIC2
          BCF  PORTB,4h
          BSF  PORTB,3h
          BCF  PORTB,3h       ; INICIJALIZACIJA ZA START BIT
PICR1     BCF  STATUS,C
          BSF  PORTB,3h
          BCF  PORTB,3h
          BTFSC  PORTB,4h
          BSF  STATUS,C
          RRF   PIC1,F
          RRF   PIC2,F
          DECFSZ   LOOP,F
          GOTO   PICR1
          BCF  PORTB,4h      ;---
          BSF  PORTB,3h
          BCF  PORTB,3h       ; STOP BIT
          BCF   STATUS,C
          RRF  PIC1,F
          RRF  PIC2,F
          BCF  STATUS,C
          RRF  PIC1,F
          RRF  PIC2,F
          RETURN
          ;
PICMD    MOVWF   PODATAK          ;------------UPIS NAREDBE -------------------
         CALL   POUT          ; ---WRITE INSTRUCTION IN MCU--------------
         MOVLW   0X06          ; --BROJ BITOVA NAREDBE PRVO LSB---.MSB
         MOVWF   LOOP
PICA1    BCF   STATUS,C 
         RRF   PODATAK,F              ; --NAREDBA-ILI PODATAK--------
         BCF   PORTB,4h       ; RRF LSB--->MSB  //  RLF MSB--->LSB
         BTFSC  STATUS,C
         BSF   PORTB,4h          ;  DATA
         BSF   PORTB,3h
         BCF   PORTB,3h          ; CK
         DECFSZ  LOOP,F
         GOTO  PICA1
         RETURN
         ;            
PICID       CALL  PICM
            MOVLW  0X00       ;--------PIC----IDENTIFIKACIJA-------------
            CALL  PICMD       ; LOAD CONFIG !
            CLRF   PIC1       ; POSTAVKE  NAREDBA O1D101 IL LA HA SA 00
            CLRF   PIC2       ;  IL(INTLEN)= 00 READ CFG BYTE //01 WRITE CFG BYTE 
            CALL   PICWDT     ; UPISUJE 14 BITNI PODATAK IZ REG PARA 4F,4E MSB-->LSB
            MOVF  LOWADR,W     ; U R62 POHRANJEN LOWADR //06 ID  OSTALO OD 00-08 CFG BYTES
            MOVWF  LOOP1
PICID1      MOVLW  0X06       ; INC ADR
            CALL   PICMD
            DECFSZ  LOOP1,F    ;  LA(LOWADR)= CFG BYTE ADR (BASE + IL)
            GOTO  PICID1      ;  HA(HIADR)=  HI DATA   SA(SHIADR)= LOW DATA ===> CFG DATA
            BTFSS  INTLEN,0h
            GOTO   PICIDW
            MOVF   HIADR,W    ; TO JE SAMO ZA MANIPULACIJU SA CFG BYTOVIMA
            MOVWF  PIC1       ; 0x4f=pic1 0x4e=pic2
            MOVF   SHIADR,W
            MOVWF  PIC2
            CALL   PICPW3     ; UPISI CFG BYTE U MEM
            RETURN
            ;
PICIDW      MOVLW  0X04
            CALL  PICMD       ; READ PRG MEM CMD
            CALL  PICRE       ; READ MEM  ROUTINE
            MOVF  PIC1,W
            MOVWF  MFD      ; MFD DATA
            MOVF  PIC2,W
            MOVWF  DEVD      ; ID DEV
            RETURN          ;  BIO RETURN
            ;
PICRDM    BTFSS  LOWADR,0h  ;  CFG MODE  ILI USER MODE READ-- FLASH-----
          GOTO  PICRD0
          MOVLW   0X00
          CALL   PICMD      ; LOAD CFG BEGIN FROM CFG ADR
          MOVLW   0XFF
          MOVWF  PIC2
          CLRF   PIC1
          CALL   PICWDT     ; UPISUJE 14 BITNI PODATAK IZ REG PARA 4F,4E MSB-->LSB
          NOP
          NOP
PICRD0    MOVF   HIADR,W
          MOVWF  PIC3      ; TU JE BLOK LEN 8 ZA FLASH 4 ILI MAJE ZA DATA
PICRD2    MOVLW  0X20       ; -CITANJE DATA MEM-
          BTFSS  INTCMD,0h 
          MOVLW  0X10       ;------- CITANJE FLASH MEMORIJE-------
          MOVWF  0X2E       ; 10h==16 X 2 == 32 BAYTA
          MOVLW  0X2F
          MOVWF  FSR        ; START SRAMA
PICRD4    MOVLW  0X04       ; READ PRG MEM
          BTFSC  INTCMD,0h
          MOVLW  0X05       ; READ DATA MEM
          CALL  PICMD       ; UPIS NAREDBE ZA CITANJE MEMORIJE
          CALL  PICRE
          BTFSC  INTCMD,0h
          GOTO   PICRD6     ; UPISUJE SAMO NIZI BAYT U SRAM U DATA MODU
          MOVF   PIC1,W      ; procitani bit
          MOVWF  INDF
          INCF   FSR,F
PICRD6    MOVF   PIC2,W
          MOVWF  INDF
          INCF   FSR,F
          MOVLW   0X06       ; INC ADR  PC+1
          CALL  PICMD
          DECFSZ  0X2E,F     ;CITA 32 BAYTA X8==256 BAYTA/128 BYTA U READ DATA
          GOTO   PICRD4
          CALL   SEND32       ; SEnd 32 BYTA U PC
          DECFSZ  PIC3,F
          GOTO   PICRD2      ; 8X32=256 /128
          DECFSZ  SHIADR,F       ;  MAXLEN VALUE
           GOTO   PICRD0      ;  END READ
PICRD7    BCF    PORTA,3h     ; RESET Vpp
          BCF    PORTA,2h     ; RESET Vcc
          GOTO  ATCNG
          ;
PICPWR    CLRF  MARKER     ;-------------WRITE FLASH AND DATA------------
          CLRF  PIC2
          MOVF   HIADR,W
          MOVWF  PIC3      ; TU JE BLOK LEN 8 ZA FLASH 4 ILI MAJE ZA DATA
          BTFSS  LOWADR,0h  ;  CFG MODE  ILI USER MODE WRITE-- FLASH-----
          GOTO  PICPWS
          MOVLW   0X00
          CALL   PICMD      ; LOAD CFG BEGIN FROM CFG ADR
          MOVLW   0XFF
          MOVWF  PIC2
          CLRF   PIC1
          CALL   PICWDT     ; UPISUJE 14 BITNI PODATAK IZ REG PARA 4F,4E MSB-->LSB
          NOP
          NOP
PICPWS    MOVF   HIADR,W
          MOVWF  PIC3      ; TU JE BLOK LEN 8 ZA FLASH 4 ILI MAJE ZA DATA
PICPW0     CALL  READ32
           MOVLW  0X20
           BTFSS  INTCMD,0h
           MOVLW  0X10        ; 10hX2=32DEC
           MOVWF  0X2E        ; 
           MOVLW  0X2F
           MOVWF  FSR
PICPW1     BTFSC  INTCMD,0h    ; PDM MODE
           GOTO  PICPW5
           MOVF  INDF,W
           MOVWF PIC1        ; ZA USPOREDBU
           MOVWF  PIC4       ; TEST BYTE U RUTINI Z
           INCF  FSR,F
           MOVF  PIC1,W
           XORLW   0XFF         ; AKO JE PIC1 BIO 3F XOR FF==C0
           BTFSC  STATUS,Z
            GOTO   PICPW6
           XORLW   0XC0       ; 3F
           BTFSC  STATUS,Z
            GOTO  PICPW6
PICPW5     MOVF  INDF,W
           MOVWF  PIC5        ; ZA USPOREDBU
           MOVWF  PIC2
           INCF  FSR,F
           MOVLW  0X3F
           ANDWF  PIC4,F     ; AKO JE FLASH MODE TADA MAX VAL PIC4==3F
           MOVF   PIC2,W     ; IMA GRESKU KOD SNIMANJA PODATKA U INT EEPROM ??
           XORLW   0XFF      ; POSTAVIO SAM PIC1 NA POCETKU U 00 MOZDA JE TO RIJESENJE !
           BTFSC  STATUS,Z
           GOTO  PICPW6
PICPW7     CALL   PICPW3      ; LOAD DATA + BEGIN PROGRAM + WAIT
           MOVLW   0X04
           BTFSC  INTCMD,0h
           MOVLW   0X05
           CALL    PICMD      ; READ DATA
           CALL    PICRE      ; PODACI U 4F I 4E TU CEMO IC NA VERIFIKACIJU
           MOVF    PIC2,W
           XORWF   PIC5,F
           BTFSS   STATUS,Z   ; VERIFIKACIJA UPISANOG BAYTA
           GOTO   PWGRE
           BTFSC  INTCMD,0h
           GOTO    PICPW6
           MOVF  PIC1,W
           XORWF  PIC4,F
           BTFSS  STATUS,Z
           GOTO  PWGRE       ; VERIFY  NOK
PICPW6     MOVLW   0X06
           CALL   PICMD       ; INC ADR PC+1
           DECFSZ  0X2E,F
           GOTO   PICPW1
           CALL   PCWRAN      ; ODGOVOR NA VERIFIKACIJU OK
           DECFSZ  PIC3,F
           GOTO   PICPW0      ; 8X32=256 /128
           DECFSZ  SHIADR,F       ;  MAXLEN VALUE
           GOTO   PICPWS      ;  END READ
PICPWO     BCF    PORTA,3h     ; RESET Vpp
           BCF    PORTA,2h     ; RESET Vcc
PWGRE      INCF   MARKER,F   ; PRIJAVA GRESKE KOD UPISA           
           GOTO   ATC0F
           ;
PICPW3     MOVLW   0X02       ; UPIS 14 BITA U MEM  PGM MODE
           BTFSC   INTCMD,0h
           MOVLW   0X03       ; PDM MODE
           CALL   PICMD       ; ---LOAD DATA FOR PRG MEM
           CALL   PICWDT      ; -UPISUJE 14 BITA ZA PROGRAMIRANJE U MCU
           MOVLW   0X18
           CALL   PICMD       ; BEGIN PRG ONLY CYKLE
           MOVLW   0X05       ; BIO 0E/// OKO 3mS
           BTFSS   MFD,3h     ; 40H  A5 80+20 GW 60 40+20
           MOVLW   0X0B       ; 10 mS ZA GW
           MOVWF   DELB
           CALL   ATC4       ; DELAY TREBA 0D 2mS-5mS ZA 16F8XX 10mS ZA 16F8X
           RETURN
           ;
READ32     MOVLW  0X2F        ;---- READ 32 BYTE OF DATA TO SRAM-----
           MOVWF  FSR
           CALL  ULAZNA
           MOVLW  0X20
           MOVWF  LOOP2
READ3S     CALL   ad00F4
           MOVWF  INDF
           INCF   FSR,F
           DECFSZ  LOOP2,F
           GOTO   READ3S
           RETURN
           ;
PCWRAN:    ;MOVF  CMD,W
           ;CALL  ad00D7      ; SALJE ODGOVOR XX00
           MOVF  MARKER,W
           CALL  ad00D7
           RETURN
           ;
PICER       MOVLW  0XFF
            MOVWF  PIC2
            MOVLW  0X3F
            MOVWF  PIC1       ;  00FF
           MOVLW   0X02      ; LOAD DATA FOR PRG MEM
           CALL   PICMD
           CALL   PICWDT     ; UPISUJE 14 BITNI PODATAK IZ REG PARA 4F,4E MSB-->LSB
           BTFSS  MFD,3h
           GOTO  ERGW        ; BLERASE GW
           MOVLW   0X09       ;------------ BULK ERASE PROBA--09------
           CALL   PICMD       ; 1F BI TREBALO 5 BITOVA + 1 NEVAZECI// 3F 6BITOVA=1
           CALL   BLKER
           CLRF   PIC1
           MOVLW  0XFF
           MOVWF  PIC2
           MOVLW  0X03       ; LOAD DATA FOR DATA MEM !
           CALL  PICMD
           CALL  PICWDT
           BTFSS  MFD,3h
            GOTO  ERGW
           MOVLW   0X0B      ;-BULK ERASE DATA MEM 0B
           CALL   PICMD
           CALL   BLKER
           BCF   PORTA,3h    ; RESET Vpp
           BCF   PORTA,2h    ; Vcc==0
           GOTO  POCETAK
           ;
BLKER      MOVLW   0X18       ; 
           CALL   PICMD       ; BEGIN PROGRAMMING
           MOVLW  0X10        ;---OKO 12 mS-----
           MOVWF  DELB
           CALL   ATC4       ;--- BULK ERASE TIMING----
           RETURN
           ;
CERPI     MOVLW   0X00       ;---------CHIP ERASE--
          CALL   PICMD
          MOVlw   0x3F
          MOVWF  PIC1
          MOVLW  0XFF      ; 3FF0 3FF9 GW 3D39 876/877
          MOVWF  PIC2
          CALL   PICWDT
          MOVLW  0X07
          MOVWF  LOOP2
CERP1     MOVLW   0X06       ; inc adr////ADR NA 2007
          CALL   PICMD
          DECFSZ  LOOP2,F
          GOTO   CERP1
ERGW      MOVLW   0X01
          CALL   PICMD
          MOVLW   0X07
          CALL   PICMD
          MOVLW  0X08
          CALL   PICMD
          MOVLW   0X0F       ;14mS-- 12mS ZA 16F8XX 10mS 16F8X
          MOVWF   DELB
          CALL    ATC4
          MOVLW   0X01
          CALL   PICMD
          MOVLW   0X07
          CALL   PICMD
          BCF  PORTA,3h   ; RESET Vpp
          BCF  PORTA,2h
          GOTO  POCETAK
          ;
;---------------------EXT EEPROM RUTINA -------------
I2CP1       BSF  STATUS,RP0 
            BCF  TRISA,0h                ; SDL // PORTA,1== SCL
            BCF STATUS,RP0
            RETURN
            ;
I2CP2       BSF  STATUS,RP0 
            BSF  TRISA,0h                ; ULAZNI  FD SDA
            BCF STATUS,RP0
            RETURN
            ;
I2C13      CLRW                ; THIS ROUTINE SHUD BE USED TO READ AND WRITE I2C MEM
            BTFSC STATUS,C     ; WHERE NEED TO BE DATA BASE FOR SUPORTED DEVICES
I2C15      IORLW 0x01          ; WHOLE ANSWERS TO PC PROM PROGRAMMER
            IORLW 0xA0         ; AND OF COURSE BOOTLOADER FILES FOR 128 AND 163 MEGA CARDS/MCUs
I2C17      MOVWF 0x20
           CALL  I2CP1         ; I STOP HERE COSE PRIMARY INTENTION WAS TO MAKE FOR
            MOVLW 0xF8         ; MYSELF SIMPLE PROGRAMMER FOR PROGRAMMING ATMEGA128
I2C1B      RLF 0x20,F          ; FOR MY MAIN PROJECT PROGRAMMER..........
            CALL I2C2C
I2C1D      ADDLW 0x01
            BTFSS STATUS,Z
            GOTO I2C1B
            GOTO I2C31
I2C21      CALL  I2CP2        ; PORTA 0== ULAZNI(SDL) PORTA1==IZLAZNI(SCL)
I2C23      MOVLW 0xF8
I2C24      CALL I2C33
I2C25      RLF 0x20,F
            ADDLW 0x01
            BTFSS STATUS,Z
            GOTO I2C24
I2C29      RETURN
            ;
I2C2A      CALL  I2CP1
I2C2C      BSF PORTA,0h
            BTFSS STATUS,C
I2C2E      BCF PORTA,0h
            BSF PORTA,1h
            GOTO I2C48
I2C31      CALL  I2CP2
I2C33      BSF PORTA,1h
            NOP
            BSF STATUS,C
            BTFSS PORTA,0h
            BCF STATUS,C
            GOTO I2C48
I2C39      BSF PORTA,0h
           CALL  I2CP1
I2C3C      NOP
            BSF PORTA,1h
            NOP
            BCF PORTA,0h
            GOTO I2C48
I2C41      BCF PORTA,0h
           CALL  I2CP1
            NOP
            BSF PORTA,1h
            NOP
            BSF PORTA,0h
I2C48      NOP
            BCF PORTA,1h       ; SCL
            RETURN
            ;
I2C4B     ; MOVWF 0x21         ; LOADR
I2C4C      CALL I2C39
            BCF STATUS,C
            CALL I2C13
            BTFSC STATUS,C
            GOTO I2C4C
            MOVF HIADR,W       ; HIADR
            ;ANDLW 0x7F         ;; LIMIT ZA 8K HI ADR
            CALL I2C17
            MOVF LOWADR,W        ;; LOW ADR I2C EEP
            GOTO I2C17
I2CRD      CALL I2C4C          ; 56//CITANJE  EXT EEPROMA BIO 4B
I2C57      CALL I2C39       
            BSF STATUS,C
            CALL I2C13
I2C5A      CALL I2C21
            BSF STATUS,C
I2C5C      CALL I2C2A
            CALL I2C41
            MOVF 0x20,W       ;; DATA REG
            RETURN
            ;
I2CWR      CALL I2C4C           
I2C7B      CALL I2C57           
           ; XORWF INDF,W
           ; BTFSC STATUS,Z
            ;GOTO I2C7A
I2C7F       CALL  I2C4C
I2C7D       MOVF  INDF,W     ; SEQUENC MOD
            CALL  I2C17
I2C7A       INCF FSR,F
            ;INCF LOWADR,F      ; TU CE MOZDA TREBAT DODAT I UVECANJE HIADR
            DECFSZ LOOP3,F
            GOTO I2C7D
            GOTO  I2C41
            RETURN
            ;
I2CSET     MOVF  LEN,W         ; UPIS // PRIPREMA REG
I2CSER     MOVWF  LOOP3
           MOVLW 0X2F
           MOVWF FSR
           RETURN
           ;
I2CWRX     CALL  I2CSET
           CALL  I2CWR
           GOTO  POCETAK
           ;
I2CRDX     MOVLW 0X20
           CALL  I2CSER
I2CBF      CALL  I2CRD
           MOVWF  INDF
           INCF  FSR,F
           INCF  LOWADR,F
           DECFSZ  LOOP3,F
           GOTO  I2CBF
           CALL  SEND32
           GOTO  POCETAK
           ;
         END
