• AVR Freaks

Hot!Port bit linearity.

Author
Murton Pike Systems
Super Member
  • Total Posts : 224
  • Reward points : 0
  • Joined: 2020/09/10 02:13:01
  • Location: 0
  • Status: offline
2021/02/23 11:26:29 (permalink)
2 (1)

Port bit linearity.

Been writing some code for usb oscilloscope using pic32mx274.
Using an external a2d which is 10 bits.
Sadly I cant use portb 0-9 linearly.
Some of the port bits are used by usb.
So ended up with  a mess of code to read the port and slow as well.


unsigned short reada2dright()
{
unsigned short ax;
unsigned short bx;
ax=PORTB;
bx=ax&0x1f;

if (ax&sb7)
bx=bx+sb5;

if (ax&sb8)
bx=bx+sb6;

if (ax&sb9)
bx=bx+sb7;

if (ax&sb13)
bx=bx+sb8;

if (ax&sb15)
bx=bx+sb9;


return bx;
}

#1

10 Replies Related Threads

    ric
    Super Member
    • Total Posts : 29914
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Port bit linearity. 2021/02/23 12:18:27 (permalink)
    0
    How are the "sb##" values defined?
     

    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
    Murton Pike Systems
    Super Member
    • Total Posts : 224
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Port bit linearity. 2021/02/23 12:23:44 (permalink)
    0
    ric
    How are the "sb##" values defined ?

    sb0 =1
    sb1=2
    sb2=4
    sb3=8 etc bit set for power of 2.
    sb4=16
    #3
    ric
    Super Member
    • Total Posts : 29914
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Port bit linearity. 2021/02/23 12:26:11 (permalink)
    4 (1)
    Doing it with variables is the worst way.
    Try:
    #define sb0 1
    #define sb1 2

    and so on (although I'd change them all to upper case to show they are constants)

    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
    ric
    Super Member
    • Total Posts : 29914
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Port bit linearity. 2021/02/23 12:28:35 (permalink)
    0
    and also change
    bx=bx+sb5;

    to
    bx |= sb5;


    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!
    #5
    Murton Pike Systems
    Super Member
    • Total Posts : 224
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Port bit linearity. 2021/02/23 12:29:26 (permalink)
    0
    Ok thanks.
     
    #6
    crosland
    Super Member
    • Total Posts : 2176
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: Port bit linearity. 2021/02/23 13:44:21 (permalink)
    0
    I would change the PCB to do the bit swapping :)
     

    if (ax&sb7)
    bx=bx+sb5;

    if (ax&sb8)
    bx=bx+sb6;

    if (ax&sb9)
    bx=bx+sb7;

    could be

    ax = ax>>2;
    bx = bx | (ax & 0xe0);

    #7
    rpg7
    Super Member
    • Total Posts : 1432
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re: Port bit linearity. 2021/02/23 23:35:11 (permalink)
    0
    uint16_t ChkPortBits(void)
    {
     return (PORTB & 0x1F) | ((PORTB >> 2) & 0xE0) | ((PORTB >> 5) & 0x0100) | ((PORTB >> 6) & 0x0200);

    }

    Attached Image(s)

    #8
    Murton Pike Systems
    Super Member
    • Total Posts : 224
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: Port bit linearity. 2021/02/24 04:41:38 (permalink)
    0
    Thanks that looks about shortest.
     
    #9
    rpg7
    Super Member
    • Total Posts : 1432
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re: Port bit linearity. 2021/02/24 06:10:56 (permalink)
    0
    Using a temp, non-volatile variable speeds it up a little more
    uint16_t ChkPortBits(void)
    {
      uint32_t pb = PORTB;
      return (pb & 0x1F) | ((pb >> 2) & 0xE0) | ((pb >> 5) & 0x0100) | ((pb >> 6) & 0x0200);
    }
    #10
    moser
    Super Member
    • Total Posts : 619
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: Port bit linearity. 2021/02/24 08:46:44 (permalink)
    0
    If you call that function at only one place, and with high frequency, then consider to define that function as static inline uint16_t ChkPortBits(void), to get rid of the function call overhead.
    #11
    Jump to:
    © 2021 APG vNext Commercial Version 4.5