XC8 individuals bits configuration

Thread Starter

micropad

Joined Dec 24, 2011
106
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
 

JohnInTX

Joined Jun 26, 2012
4,787
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:
Rich (BB code):
87:                LATC6=1;
7FC2  8C8B     BSF LATC, 6, ACCESS
88:                LATCbits.LATC6=0;
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:

Thread Starter

micropad

Joined Dec 24, 2011
106
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
 

ErnieM

Joined Apr 24, 2011
8,377
Rich (BB code):
#define COMM_PORT PORTC
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.
 

Thread Starter

micropad

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

Rich (BB code):
LATD  |= ( 1<<LATD0 | 1<<LATD1 | 1<<LATD2 | 1<<LATD3 );
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
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:

Rich (BB code):
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:

Rich (BB code):
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:

Rich (BB code):
LATD  |= 0x0F;
 
Top