microcontrollermicrochipmplabxc16

Microchip XC16 : Can we access Port using its address ?? for ex : &PortA?


As my question says, to access Port by its address, Can we write it as "&PORTA" ??

In my problem, I want to read/write port value from/to HMI, using Modbus Protocol.

I have an array of structure :

typedef struct func_code_reg {
    volatile uint16_t addr;
    volatile uint16_t *data;
}RW_REG_DATA;

// described as
RW_REG_DATA rwCoilStatusTbl[] = {
    //      Addr        Data_Register
    {       0,           &rwCoil_0000      },
    {       1,           &rwCoil_0001      },
};

Whenever HMI reads data , it reads the current value of register &rwCoil_000x

Whenever HMI writes data, the register &rwCoil_000x gets updated.

Instead, I would like to use &PORTA to read Port status or to update Port Status.

Is it possible ?? & if possible, is it the correct way to update the Port status ??

Or any better way, please guide me.

(I am using dsPic33E series)


Solution

  • PORTx is already mapped to the contents of the PORTx register, you don't need its address. To read from a port, use the PORTx register. To write, use the LATx register.

    So if you want the value rwCoil_000x to be reflected on a port (A), simply write:

    LATA = rwCoil_000x; 
    

    And if you want to read from the port into the same variable, write:

    rwCoil_000x = PORTA;
    

    Of course, this assumes PORTA is set to be a general purpose output.