XC8 individuals bits configuration

Discussion in 'Embedded Systems and Microcontrollers' started by micropad, Jun 8, 2014.

  1. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    Dear friends
    I found following individuals bit setting methods
    Please advice what is the deference of this two method

    Compiler XC8
    MCU PIC18F

    LATC6 = 1;
    LATCbits.LATC6 = 1;

    Both method final result is same
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,340
    1,022
    The first uses the bit variable type declaration of LATC and the second treats LATC as a structure with bitfields. Both methods are defined in <xc.h> and generate the same code :
    From a disassembly listing:
    Code ( (Unknown Language)):
    1.  
    2. 87:                [B]LATC6=1;[/B]
    3. 7FC2  8C8B     BSF LATC, 6, ACCESS
    4. 88:                [B]LATCbits.LATC6=0;[/B]
    5. 7FC4  9C8B     BCF LATC, 6, ACCESS
    In the manual DS52053BP
    Sec 5.4.2.1 BIT DATA TYPES AND VARIABLES describes the first construct and
    Sec. 3.4.4.2 HOW CAN I ACCESS INDIVIDUAL BITS OF A VARIABLE and 5.4.4.2 BIT-FIELDS IN STRUCTURES describe the second one..

    There may be times when you want to specify the port as a variable and use a pointer to the structure to select the port but for most IO, use the simpler one.
     
    Last edited: Jun 8, 2014
  3. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    Dear JohnInTX

    Thanks for the reply

    Can I have more general info about " structure with bitfields"
     
  4. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    Hi

    In the AVR following method is working
    XMEGA
    #define COMM_PORT PORTC
    COMM_PORT.OUT =~((1<<LCD_E)|(1<<LCD_RW));

    But in the PIC 18F XC8 does not support it

    Can you please advice
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Code ( (Unknown Language)):
    1. #define COMM_PORT PORTC
    2. COMM_PORT.OUT =~((1<<LCD_E)|(1<<LCD_RW));
    That is standard C, so the problem is not with XC8, the problem is something is undefined, either LCD_E, LCD_RW, or both.

    Those definitions probably depend on how you wired in an LCD display.
     
    micropad likes this.
  6. micropad

    Thread Starter Member

    Dec 24, 2011
    38
    0
    Hi
    This code should be ON LED connected to the PORTD 0,1,2,3 bits but it only ON bit 0,1

    Code ( (Unknown Language)):
    1.  
    2. LATD  |= ( 1<<LATD0 | 1<<LATD1 | 1<<LATD2 | 1<<LATD3 );
    3.  
    4.  
     
    Last edited: Jun 8, 2014
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Again, it depends on how the symbols are defined. From your first post you know these are equivalent terms:

    LATDC0 = 1;
    LATDbits.LATD0 = 1;

    So your expression could be rewritten as:

    Code ( (Unknown Language)):
    1. LATD  |= ( 1<<LATDbits.LATD0 | 1<<LATDbits.LATD1 | 1<<LATDbits.LATD2 | 1<<LATDbits.LATD3 );
    Now "1<<LATDbits.LATD0" has a value of either 1 or 2 depending on LATD0 being either 0 or 1. There is no way you can or together these values and get all 4 lower bits set.

    The constant I believe you were looking for is named "_LATC_LATD0_POSN" in the device header file. I believe you could use the expression:

    Code ( (Unknown Language)):
    1. LATD  |= ( 1<<_LATC_LATD0_POSN | 1<<_LATC_LATD1_POSN | 1<<_LATC_LATD2_POSN | 1<<_LATC_LATD3_POSN );
    But it may be simpler to just say:

    Code ( (Unknown Language)):
    1. LATD  |= 0x0F;
     
Loading...