Q: Can individual pins of different port of a micro-controller be mapped to a register and their values be changed when changing the register value?
Scenario: I have used up some pins from each port(8-bits) of the micro-controller. Now I want to interface a device which needs an 8-bit bus (suppose D0 to D7 IN SEQUENCE) that is to say I need 8 pins from controller so that I can connect them in one-to-one fashion
portx0 -> D0 // x is the name of port followed by bit location on that port portx1 -> D1 ... portx7 -> D7
but I don't have a whole port of 8 pins which I can connect with this device, rather I have some pins from portx, some from porty and some pins from portz. The new connection scenario is as (connection from micro-controller to device respectively )
portx0 -> D0 portx1 -> D1 portx2 -> D2 porty4 -> D3 porty5 -> D4 porty6 -> D5 porty7 -> D6 portz1 -> D7
In this condition if I want to send a value say
unsigned char dataReg = 0xFA;
to my device from controller I have to perform bit wise operations on the value to be sent and set each pin according to the value in the register individually. For example
portx0 = ((dataReg & 0x01) >> 0 ); // Masking and shifting as bit position portx1 = ((dataReg & 0x02) >> 1 ); portx2 = ((dataReg & 0x04) >> 2 ); porty4 = ((dataReg & 0x08) >> 3 ); porty5 = ((dataReg & 0x10) >> 4 ); porty6 = ((dataReg & 0x20) >> 5 ); porty7 = ((dataReg & 0x40) >> 6 ); portz1 = ((dataReg & 0x80) >> 7 );
Now, coming to the main question, to avoid these individual calculations on each bit on different ports, can individual pins of different port of a micro-controller be mapped to a register and their values be changed when changing the register value?