0

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)

skg
  • 948
  • 2
  • 19
  • 35

2 Answers2

1

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.

Ed King
  • 1,833
  • 1
  • 15
  • 32
  • I dont know when will rwCoil_000x value will be changed from HMI. So, How can I write : ** LATA = rwCoil_000x ** ?? I should be aware when to do this ?? Can you guide bit more on this ??? HMI sends request packet to read/write randomly in case of each register. – skg Feb 13 '17 at 08:23
  • I don't understand the problem. Even though you don't know *when* the HMI will do a write, it *will* do a write, so that's when you update the output value via the LATx register. If this doesn't make sense, then I'm wrongly assuming your hardware setup and you need to explain it. – Ed King Feb 13 '17 at 10:01
0

If you want to generalize over many different ports, you can build an array of volatile references to *PORT.

I did this once for the other way, the outputs, LAT registers, see Using an array of LATs to toggle outputs. type of (byte) pointer to lat

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • I viewed your link. Just one thing...did **&LATA** worked in your case ??? Means As an array of structure, if i write it as **{ 1, &LATA }**, it should be written to the PortA whenever a request has been sent from HMI ?? – skg Feb 13 '17 at 08:20
  • No, you need the bitmask system. bit numbers don't work, that's what I tried originally with the asm routine and that is not fast. PORTA is for reading btw. – Marco van de Voort Feb 13 '17 at 08:38