1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Copy Buffer Code Optimize

Discussion in 'Microcontrollers' started by Suraj143, Mar 1, 2018.

  1. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,635
    Likes:
    2
    Location:
    South Mald Isld
    ONLINE
    In my LED matrix I implemented a display double buffer & need to copy the working buffer to display buffer.I wrote a code but it takes lots of time closer to 650uS :(

    Note: The working buffer is in Bank0 & the Display buffer in Bank 1.

    Code (text):
               
    ;physical placement of working buffer          
    ;D2 D4 D6 D8 D10 D12 D14 D16 D18 D20 D22 D24 D26 D28 D30 D32 D34 D36 D38 D40 D42 D44 D46 D48 D50 D52 D54 D56 D58 D60 D62 D64
    ;D1 D3 D5 D7 D9  D11 D13 D15 D17 D19 D21 D23 D25 D27 D29 D31 D33 D35 D37 D39 D41 D43 D45 D47 D49 D51 D53 D55 D57 D59 D61 D63          
               
                cblock    30h    ; Working buffer 64 bytes
                D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,D11,D12,D13,D14,D15,D16            ;30-3Fh
                D17,D18,D19,D20,D21,D22,D23,D24,D25,D26,D27,D28,D29,D30,D31,D32        ;32 40-4F
                D33,D34,D35,D36,D37,D38,D39,D40,D41,D42,D43,D44,D45,D46,D47,D48        ;48 50-5F
                D49,D50,D51,D52,D53,D54,D55,D56,D57,D58                        ;58 60-69h
                endc
               
                cblock    0D0h    ; Display buffer 64 bytes
                ; 64 bytes RAM
                endc
               
    Copy_Buffer        movlw        30h
                movwf        FSR
                movlw        .32
                movwf        Counter
                ;
    Copy_Loop        movf        INDF,W    ; B0 INDF    ;    ;1
                movwf        Temp                ;    ;2
                movlw        .48                ;    ;3
                addwf        FSR,F        ; B1 INDF    ;    ;4
                movf        Temp,W            ;    ;5
                movwf        INDF                ;    ;6
                ;
                movlw        .47                    ;7
                subwf        FSR,F        ; B0 INDF + 1    ;8
                movf        INDF,W                ;9
                movwf        Temp                    ;10
                movlw        .48                    ;11
                addwf        FSR,F        ; B1 INDF        ;12
                movf        Temp,W                ;13
                movwf        INDF                    ;14
                ;  
                movlw        .48                    ;15
                subwf        FSR,F        ; back to B0    ;16
                decfsz    Counter,F                ;17
                goto        Copy_Loop                ;19
                return
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    Why copy the buffer? When we double buffered something we updated one buffer whilst displaying the other.

    Mike.
     
  3. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,635
    Likes:
    2
    Location:
    South Mald Isld
    ONLINE
    Hi,

    I haven't double buffered yet. I currently have only one buffer "Working buffer". In the main loop I'm doing animating patterns to this buffer. In the ISR I have the same buffer for display part as well.

    I'm going to implement a double buffer by assigning another 64 RAM named as display buffer in Bank 1.
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    What processor are you using?

    Mike.
     
  6. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,635
    Likes:
    2
    Location:
    South Mald Isld
    ONLINE
    Hi,

    I use PIC16F886.
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,710
    Likes:
    421
    Location:
    Brisbane Australia
    ONLINE
    If you use the 64 bytes located at 0x20 and 0x120 and use indirect addressing to access them then you will only need to flip one bit (status,irp) to switch between buffers.

    A single flag can indicate which buffer is being displayed and which to work on.

    Mike.
    Edit, alternatively, use 0x30 and 0x130.
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  9. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,635
    Likes:
    2
    Location:
    South Mald Isld
    ONLINE
    That's a great suggestion.I'll implement it.
    Somehow at the end of the each frame (16mS) I have to update the display buffer.Did a small code to copy buffers & it takes now only 416 uS.

    Code (text):

    ;copy working buffer to display buffer @ end of frame  
    Copy_Buffer       movlw        30h
                    movwf        FSR
                    movlw        .32
                    movwf        Counter
                    ;
    Copy_Loop            movf        INDF,W        ;1
                    bsf        STATUS,IRP        ;2
                    movwf        INDF            ;3
                    bcf        STATUS,IRP        ;4
                    incf        FSR,F            ;5
                    movf        INDF,W        ;6
                    bsf        STATUS,IRP        ;7
                    movwf        INDF            ;8
                    bcf        STATUS,IRP        ;9
                    incf        FSR,F            ;10
                    decfsz        Counter,F        ;11
                    goto        Copy_Loop        ;13
                    return
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice