• AVR Freaks

Hot!Need help with pointers

Author
davea
Senior Member
  • Total Posts : 153
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: 0
  • Status: offline
2019/09/22 11:42:27 (permalink)
0

Need help with pointers

16LF15323
XC-8 PRO at opt 3
                  NON_A0_ball_cnt -= BALL_buf[BALL_pt]; // remove old from buffer 
                  BALL_buf[BALL_pt] = Ball_cnt; // add new to buffer
                  NON_A0_ball_cnt += Ball_cnt; // add to cnt

 
I would like to use a pointer
something like this but know how to
uint16_t  *Address_pnt
Address_pnt = BALL_buf[BALL_pt]; //  get location
NON_A0_ball_cnt -= Address_pnt; // location data
Address_pnt = Ball_cnt; //                put Ball_cnt in location
thanks for any help..
#1

9 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23893
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Need help with pointers 2019/09/22 13:22:20 (permalink)
    +1 (1)
    To get the address of an array variable:
    Address_pnt = &BALL_buf[BALL_pt]; //  get location

     
    I have no idea what you are trying to do here. The ONLY thing it is valid to subtract an address from is another address!
    NON_A0_ball_cnt -= Address_pnt; // location data

     
    To write a value to an indirect address:
    *Address_pnt = Ball_cnt; //                put Ball_cnt in location

     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    davea
    Senior Member
    • Total Posts : 153
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: Need help with pointers 2019/09/22 14:21:02 (permalink)
    0
    I did this
    uint8_t  *Addr;  (BALL_buf is in page 0 at 0xE0)
    131: Addr = &BALL_buf[BALL_pt];
    0076 0140 MOVLB 0x0
    0077 084E MOVF BALL_pt, W
    0078 3EE0 ADDLW 0xE0
    0079 00F6 MOVWF Addr
    132: NON_A0_ball_cnt -= *Addr; // remove old from buffer and sub from avg
    007A 0086 MOVWF FSR1
    007B 0187 CLRF FSR1H
    007C 0801 MOVF INDF1, W
    007D 02CF SUBWF NON_A0_ball_cnt, F
    133: *Addr = Ball_cnt; // add new to buffer
    007E 0876 MOVF Addr, W
    007F 0086 MOVWF FSR1
    0080 0852 MOVF Ball_cnt, W
    0081 0081 MOVWF INDF1

    it looks like it might work
    what I am try to do is reduce the  # instruction
    but it went from 12 to 11 not worth it
    I thought there might be a way of NOT using the FSR's
    do you think it could be done shorter in ASM
    Just trying to learn..
    post edited by davea - 2019/09/22 14:23:16
    #3
    ric
    Super Member
    • Total Posts : 23893
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Need help with pointers 2019/09/22 15:39:49 (permalink)
    +1 (1)
    davea
    what I am try to do is reduce the  # instruction
    but it went from 12 to 11 not worth it

    If you look at a single access, then you're not really changing anything, it still has to do all the same calculations.
    A pointer helps when you keep an address in the pointer, and just increment or decrement it.

    I thought there might be a way of NOT using the FSR's

    Huh? The ONLY way to do an indirect access is via the FSR registers, so I don't understand what you are thinking here.

    do you think it could be done shorter in ASM

    Of course, but is it worth the effort?
    You need to analyse the entire algorithm, not just one isolated part.
    You haven't revealed enough of the entire algorithm for anyone to give a definitive answer.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    1and0
    Access is Denied
    • Total Posts : 9771
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Need help with pointers 2019/09/22 16:03:07 (permalink)
    +1 (1)
    ric
    davea
    I thought there might be a way of NOT using the FSR's

    Huh? The ONLY way to do an indirect access is via the FSR registers, so I don't understand what you are thinking here.

    I guess OP does not know pointer is synonymous with the FSR registers.
     
    ric
    davea
    do you think it could be done shorter in ASM

    Of course, but is it worth the effort?
    You need to analyse the entire algorithm, not just one isolated part.
    You haven't revealed enough of the entire algorithm for anyone to give a definitive answer.

    Agree.  But I'm surprised PRO mode did not remove this

    007E 0876 MOVF Addr, W
    007F 0086 MOVWF FSR1

     
    post edited by 1and0 - 2019/09/22 16:05:11
    #5
    Nikolay_Po
    Super Member
    • Total Posts : 1897
    • Reward points : 0
    • Joined: 2012/04/01 13:49:27
    • Location: Russia, Novorossiysk
    • Status: offline
    Re: Need help with pointers 2019/09/22 16:14:21 (permalink)
    +1 (1)
    About index/pointers variables naming.
    davea
                      NON_A0_ball_cnt -= BALL_buf[BALL_pt]; // remove old from buffer 
     
                      BALL_buf[BALL_pt] = Ball_cnt; // add new to buffer
                      NON_A0_ball_cnt += Ball_cnt; // add to cnt


     
    You need to discriminate the pointers and indexes in your coding manner! I'm recommending to write:
                      NON_A0_ball_cnt -= BALL_buf[BALL_idx];
     
                      BALL_buf[BALL_idx] = Ball_cnt;
     
                      //...
     
                      Address_ptr = &BALL_buf[BALL_idx];
     

    to keep in mind that BALL_idx is an index of array element, not a pointer and Address_ptr is a pointer, not an index.
     
    Also don't modify good code with an indexes to pointers unless you know exactly for what purpose you do it. Nowadays compilers, especially at high optimization levels, are clever enough to beat the programmer with pointers. Quite often the code rewritten with a pointers can't gain more speed or space comparing with array index usage.
    Address_pnt = &BALL_buf[BALL_pt]; //  get location

    #6
    davea
    Senior Member
    • Total Posts : 153
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: Need help with pointers 2019/09/22 17:50:21 (permalink)
    0
    in main.c
    bank0 uint8_t BALL_buf[10];
    bank0 uint8_t BALL_pt;
     
    uint8_t *Addr;  // test code is at end of ISR
     
                      if ((Ball_modl & 1) == 0) { // every other
                      NON_A0_ball_cnt -= BALL_buf[BALL_pt]; //
                      BALL_buf[BALL_pt] = Ball_cnt; // add new to buffer
                      NON_A0_ball_cnt += Ball_cnt; // add to cnt
                      if (BALL_pt < 10 - 1) {
                         ++BALL_pt;
                      } else {
                         BALL_pt = 0; // wrap pointer
                      }
                   }

    OK every 100Ms it loops in a ISR not as a CALL from Interrupt_Manager
    so the machine state is on the stack
    safer for ASM
     
    it is a rotary buffer[10]
    it removes the oldest  from the total
    replaces oldest with newest in the buffer
    adds newest to total
    inc pointer
    check for end of buffer
    total is the sum of the buffer 
     
    131:                    Addr = &BALL_buf[BALL_pt];
    0076  084C     MOVF BALL_pt, W
    0077  3E52     ADDLW 0x52   //  base addr has changed from before
    0078  00F6     MOVWF Addr
    this compiler code is what makes me think it could be done without FSR
    as BALL_buf and BALL_pt are in bank0
    what do you think..
     
    EDIT : this is the compiler output
    83:                              NON_A0_ball_cnt -= BALL_buf[BALL_pt]; // remove old from buffer and sub from avg
    0027  084C     MOVF BALL_pt, W  //   first *****
    0028  3E52     ADDLW 0x52
    0029  0086     MOVWF FSR1
    002A  0187     CLRF FSR1H
    002B  0801     MOVF INDF1, W
    002C  02CD     SUBWF NON_A0_ball_cnt, F
    84:                              BALL_buf[BALL_pt] = Ball_cnt; // add new to buffer
    002D  084C     MOVF BALL_pt, W // second how DUM is this
    002E  3E52     ADDLW 0x52        // ***
    002F  0086     MOVWF FSR1       // ***
    0030  0879     MOVF Ball_cnt, W
    0031  0081     MOVWF INDF1
    85:                              NON_A0_ball_cnt += Ball_cnt; //  add to cnt
    0032  0879     MOVF Ball_cnt, W
    0033  07CD     ADDWF NON_A0_ball_cnt, F
    post edited by davea - 2019/09/22 18:06:53
    #7
    ric
    Super Member
    • Total Posts : 23893
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Need help with pointers 2019/09/22 18:33:38 (permalink)
    +1 (1)
    davea
    ...
    OK every 100Ms it loops in a ISR not as a CALL from Interrupt_Manager
    so the machine state is on the stack
    safer for ASM

    I'm not sure what you're trying to say here.
    I wouldn't use MCC for interrupts. I assume you're just saying you are doing all the ISR code yourself.
     

    it is a rotary buffer[10]

    The usual term is "circular buffer"
     

    it removes the oldest  from the total
    replaces oldest with newest in the buffer
    adds newest to total
    inc pointer
    check for end of buffer
    total is the sum of the buffer

    Sounds like the standard way to implement a "sliding window" average calculation.
     

    131:                    Addr = &BALL_buf[BALL_pt];
    0076  084C     MOVF BALL_pt, W
    0077  3E52     ADDLW 0x52   //  base addr has changed from before
    0078  00F6     MOVWF Addr
    this compiler code is what makes me think it could be done without FSR
    as BALL_buf and BALL_pt are in bank0
    what do you think..

    The only way the compiler can use the value in the "Addr" variable is to load it into the FSR register.
    It's irrelevant which bank the data is in.
     
    Is there a reason for making your buffer size 10?
    Using 8 or 16 would make some of the logic easier.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    davea
    Senior Member
    • Total Posts : 153
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: Need help with pointers 2019/09/22 19:57:10 (permalink)
    0
    @ ric
    yes, yes, yes has to be 10
    try to think outside the box
    I have been looking over the instruction set
    and I can almost see it
     
    did you look at my last post #6
    compiler output at the bottom
    is bloated
     
     
    #9
    1and0
    Access is Denied
    • Total Posts : 9771
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Need help with pointers 2019/09/22 20:05:59 (permalink)
    0
    Try this:
        if ((Ball_modl & 1) == 0) {
            NON_A0_ball_cnt -= BALL_buf[BALL_pt];
            NON_A0_ball_cnt += BALL_buf[BALL_pt] = Ball_cnt;
            if (++BALL_pt == 10) {
                BALL_pt = 0; // wrap pointer
            }
        }

    #10
    Jump to:
    © 2019 APG vNext Commercial Version 4.5