Very useful indeed. Thanks Bob. I programmed the table lookup method as well, this code is comparable in ti= me. I like the calculus more than table lookup. The minus sign in the addlw generates an error in my compiler, but I change= d the signs so -9F =3D + x61. Did not test it in full yet, but it should wo= rk. What is OP? Arjen ________________________________ Van: piclist-bounces@mit.edu namens Bob Ammerman = Verzonden: vrijdag 11 augustus 2017 18:19 Aan: 'Microcontroller discussion list - Public.' Onderwerp: RE: [PIC] pic16f628a memory bank switching in a large circular b= uffer. Here is an implementation of the OPs actual requirements, as I understand t= hem. One of the neat things about this implementation is that the routines = are isochronous, which means that they always take exactly the same amount = of time. Again, untested code! -- Bob Ammerman RAm Systems Again, we have the two snippets of code already defined, converted into macros FIXINDEX: Convert W from a number in the range 00h..9Fh into a number in one of the r= anges 20h..6F or A0h..0EFh BUMPINDEX Increment W circularly, wrapping from 09Fh back to zero. ; INSP is the insertion pointer into ; the buffer. OFFSET is the number of ; samples 'back' in the buffer for output ;=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D ; INSERTION ROUTINE: MOVF INSP,W FIXINDEX MOVWF FSR MOVF INBYTE,W MOVWF INDF MOVF INSP,W BUMPINDEX MOVWF INSP RETURN ;=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D ; OUTPUT ROUTINE ; Note: since INSP is pointer to ; one slot before the latest ; value, we want to subtract 1 ; from the result of our sum of ; Convert the offset from ; a distance backward to ; a distance forward MOVWF OFFSET,W SUBLW 0 ; Now compute the desired location ; in the buffer ADDWF INSP,W ; at this point we have to ; wrap any value greater ; than 09Fh. The next instruction ; will set carry if we have a sum ; greater then 9Fh. ADDLW 61H ; now we subtract 0A0h if we ; are over 9Fh. Note that we ; we use ADDLW instead of ; SUBLW because the latter ; computes 'literal minus W' ; rather than 'W minus literal', ; which is what we want. BTFSC STATUS,C ADDLW 100h-0A0h ; Finally subtract back out ; the 61H we added above. The ; same comment about ADDLW vs. ; SUBLW applies ADDLW 100h-061h ; Convert the value to a pointer ; and get the byte FIXINDEX MOVWF FSR MOVF INDF,W ; Done! RETURN -- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive PIClist, Microchip PIC www.piclist.com The PICList is a collection of people interested in the Microchip PIC proce= ssor. View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .