STRUCTURE DATA TYPE EXAMPLE
PICC standard

//Following is an example of placing a structure on an I/O port
//This creates the structure on which an example LCD interface is based

STRUCT
port_b_layout{
    int data   : 4;   
//4 bits, etc

    int rw     : 1;

    int cd     : 1;

    int enable : 1;

    int reset  : 1;
};


//This creates a variable based on that structure

STRUCT port_b_layout port_b;


//This assigns the RAM address of the port to the variable "port_b", PIC16F876 etc
#BYTE port_b = 6


//These set up constants for use later on, applied to the port
STRUCT port_b_layout CONST INIT_1   = { 0,1,1,1,1};

STRUCT port_b_layout CONST INIT_2   = { 3,1,1,1,0};

STRUCT port_b_layout CONST INIT_3   = { 0,0,0,0,0};

STRUCT port_b_layout CONST FOR_SEND = { 0,0,0,0,0};   // Data is all outputs

STRUCT port_b_layout CONST FOR_READ = {15,0,0,0,0};   // Data is all inputs

                                  

VOID main(VOID) {

    int x;

    //This constant structure is treated like a byte and is used to set the data direction
    set_tris_b((
CHAR) FOR_SEND);

   
//These constant structures are used to set all fields on the port with a single command

    port_b = INIT_1;

    delay_us(25);

    port_b = INIT_2;

    delay_us(25);   
    port_b = INIT_3;
 

    set_tris_b((int)FOR_READ);

   
   
//Here the individual fields are accessed independently.
    port_b.rw=0;

    port_b.cd=1;

    port_b.enable=0;

    x = port_b.data;

    port_b.enable=0

}