• AVR Freaks

Hot!Individual bits work but complete register doesn't

Author
Teun
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2020/11/28 06:25:14
  • Location: 0
  • Status: offline
2020/12/02 01:52:40 (permalink)
2 (1)

Individual bits work but complete register doesn't

Hi,
I was working with an old Cerebot MX4ck board I had. I noticed that I can turn on the LEDS on the board by turning on the individual bits but not by the complete register.
 
So for example, this works:
PORTBbits.RB10 = 1;
 
But this doesn't:
PORTB = 0xFF;
 
I use the MPLAB X IDE with the XC32 compiler and have the legacy libraries installed. In the past with an older version of MPLAB IDE this worked. What am I missing?
#1

13 Replies Related Threads

    domble
    Super Member
    • Total Posts : 184
    • Reward points : 0
    • Joined: 2007/01/25 04:11:53
    • Location: UK
    • Status: offline
    Re: Individual bits work but complete register doesn't 2020/12/02 08:48:42 (permalink)
    5 (4)
    Cerebot MX4ck uses a PIC32MX460F512L according to duckduckgo...
    So PORTB register is using its LS 16 bits to control 16 IO pins.
    And PORTB = 0xFF is setting the pins to 0x00FF.  So RB10 is being set to zero.
    Also, better to use LATB for writes to port pins.
     
    dom.
    post edited by domble - 2020/12/02 09:00:56
    #2
    ric
    Super Member
    • Total Posts : 29459
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Individual bits work but complete register doesn't 2020/12/02 14:01:08 (permalink)
    4.5 (4)
    As above. Do NOT use PORTBbits for controlling individual pins, use LATBbits.
    Always write to LATx, read from PORTx

    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!
    #3
    Teun
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2020/11/28 06:25:14
    • Location: 0
    • Status: offline
    Re: Individual bits work but complete register doesn't 2020/12/03 08:48:28 (permalink)
    0
    Yep PIC32MX460F512L thats correct.
    I thought I tried 0xFFFF as well(or 0x03C0 I believe). But I would have to confirm that. And I thought we used to leave the MSB away when we used the board at school a few years ago. I think there may have been a setting to use only the last 16 bits?
    post edited by Teun - 2020/12/03 08:51:10
    #4
    ric
    Super Member
    • Total Posts : 29459
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Individual bits work but complete register doesn't 2020/12/03 12:25:42 (permalink)
    0
    The PIC32 architecture does not have a hardware "write byte" mode, but it does have a mirror addresses to allow you to just SET, CLR, or INV bits in any SFR. (See "12.1.1 CLR, SET AND INV REGISTERS" in the datasheet)
    So, try:
    SETB = 0xFF;

    which will set bits 0-7, but leave the higher bits alone.
     
     

    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
    toms
    Junior Member
    • Total Posts : 119
    • Reward points : 0
    • Joined: 2006/03/07 18:06:24
    • Location: London, UK
    • Status: offline
    Re: Individual bits work but complete register doesn't 2020/12/06 03:55:54 (permalink)
    4.5 (2)
    Semantics, but if you look at the datasheet for a PIC32 it might tell you that writes to PORTx will actually write to LATx instead.
     
    And indeed, that is the case for the OPs device - page 101 of the datasheet shows that WR PORT and WR LAT effect the same operation.
     
    But this will not be the case for all PICs - so its definitely still good practice to write to LATx and read from PORTx (I do this myself), and will help develop good habits that might help avoid headache if you happen to work with smaller devices at some stage.
    #6
    Murton Pike Systems
    Super Member
    • Total Posts : 151
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: online
    Re: Individual bits work but complete register doesn't 2020/12/06 08:40:16 (permalink)
    1.5 (2)
    ric
    As above. Do NOT use PORTBbits for controlling individual pins, use LATBbits.
    Always write to LATx, read from PORTx



    Reading from port reads from actual pins.
    Reading from LAT reads from i/o latch register.
    Writing to port or lat writes to latch.
     
    #7
    Mysil
    Super Member
    • Total Posts : 4068
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: online
    Re: Individual bits work but complete register doesn't 2020/12/06 12:38:22 (permalink)
    4 (1)
    Actually,
    the MIPS  archicecture and instruction set have Load and Store instructions for single bytes, half words (16 bits),
    as well as full 32 bit words.
    Byte Load and Store instructions are used if you declare a byte array, e.g. for use as a character buffer in memory, and read or write to it.
    I would be surprised if byte transfers were not actually also possible on the Peripheral Bus,
    but they are not usually used, as register struct definitions in the device support header files,
    typically define a union of a 32 bit register, and the various bits and fields in the register.
    It does not matter, that many SFR registers have only 16 working bits, or even less.
     
    Those bits that are not implemented, will return zero when reading from the register,
    and ignore whatever is written to the register.
     
        Mysil
    #8
    Teun
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2020/11/28 06:25:14
    • Location: 0
    • Status: offline
    Re: Individual bits work but complete register doesn't 2020/12/07 01:18:51 (permalink)
    0
    Yes it's a half word thing I guess. Had a good look at the datasheet yesterday and found exactly that.
     
    With LATx I can turn on the LEDS. Although like toms said in #6 I used to do it with PORTx. 
    Is that not working a result of the new XC32 compiler compared to the C32?
    #9
    toms
    Junior Member
    • Total Posts : 119
    • Reward points : 0
    • Joined: 2006/03/07 18:06:24
    • Location: London, UK
    • Status: offline
    Re: Individual bits work but complete register doesn't 2020/12/07 02:19:07 (permalink)
    5 (1)
    On your particular device, writing to PORT and writing to LAT is the same thing. The compiler does not change this.
     
    If it was working with one, but is not working with the other, I would ask if you are writing the same value.
    #10
    Teun
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2020/11/28 06:25:14
    • Location: 0
    • Status: offline
    Re: Individual bits work but complete register doesn't 2020/12/12 02:24:33 (permalink)
    3 (1)
    I was. PORTB = 0xFFFF; and LATB = 0xFFFF; are just different somehow. Only conclusion I can come to is that the compilers differ.
    #11
    toms
    Junior Member
    • Total Posts : 119
    • Reward points : 0
    • Joined: 2006/03/07 18:06:24
    • Location: London, UK
    • Status: offline
    Re: Individual bits work but complete register doesn't 2020/12/12 02:51:41 (permalink)
    0
    In your first post you had PORTB = 0xFF, so was that a typo, or did you fix it after updating to write to LAT instead? Going by that, originally you were only writing the first 8 bits but RB10 is the 11th bit...

    Anyway, you can verify this by looking at the assembly output from both compilers, that will answer the question pretty quickly, because the compiler won't change how the hardware works.

    You could even attach the assembly here and I will take a look for you if you don't know how to read it. This is quite interesting.
    #12
    Teun
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2020/11/28 06:25:14
    • Location: 0
    • Status: offline
    Re: Individual bits work but complete register doesn't 2020/12/12 05:05:06 (permalink)
    0
    I wasn't behind my laptop with the PIC32 then. Due to the login problems I wrote that a day or 2 later. As said, I thought the last 2 bytes were enough but I did in fact use 0xFFFF for both registers.
     
    The login problems seem to fixed though so that should make things less confusing for everyone... I hope;)
    #13
    Murton Pike Systems
    Super Member
    • Total Posts : 151
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: online
    Re: Individual bits work but complete register doesn't 2020/12/12 17:21:31 (permalink)
    4.5 (2)
    Teun1988
    I wasn't behind my laptop with the PIC32 then. Due to the login problems I wrote that a day or 2 later. As said, I thought the last 2 bytes were enough but I did in fact use 0xFFFF for both registers.
     

    0xFF isnt 2 bytes, its only one. 0xFF is 1 byte. For a 16 bit port 0xFFFF sets all bits.
    #14
    Jump to:
    © 2021 APG vNext Commercial Version 4.5