Can individual pins of different port of a microcontroller be mapped to a register and their values be changed when changing the register value?

It appears your question comes down to having a 8 bit value in the firmware, and wanting to read and write that from and to a arbitrary collection of port pins.

There is no direct hardware way to do this. You have to write two routines, one to read the 8 bit value and one to write it. Others have mentioned using unions, but that is a bad idea. With unions, you have to deal with every bit separately, and the code becomes dependent on the bit order of the micro. This might be the way to go anyway if all the 8 bits are scattered completely independently. If so, there is little you can do but to make special code for each bit.

The better way to do this, especially if you can group the bits in a few contiguous chunks on the physical ports is to use masking, shifting, and ORing. For example, if the low three bits of the internal byte are on bits <6-4> of a port, right shift that port value by 4 and AND it with 7 to get those bits into their final position. Shift and mask (or mask and shift) bits from other ports into place and assemble the final 8 bit byte by ORing the results into it.

This kind of low level bit twiddling is easier to do in assembler than C. I'd probably put the byte read and write routines in a single assembler module and make the interface callable from C.


In general this is not possible. As far as I know, it's not possible with PICs.

There is only one microcontroller I know which can do this, the Cypress PSoC. It's a highly configurable system on chip. Of the many things it allows you to do is to literally define your own register (1-8 bits) and connect it to any pins you like, or even to internal circuits.

PSoC Wiring

For example, here I have created a 6-bit control register. 5 of the bits go straight to pins, while the 6th bit I'm using to XOR with the input from a 7th pin.

PSoC Pins

On the chip, I can choose to allocate these pins to any of the available GPIO pins. (It's the grey ones one the image)