Reputation: 47
in C if we have a structure defined as
struct PortBreg {
unsigned int B0 :1;
unsigned int B1 :1;
unsigned int B2 :1;
unsigned int B3 :1;
unsigned int B4 :1;
unsigned int B5 :1;
unsigned int B6 :1;
unsigned int B7 :1;
};
#define Breg (*(volatile struct PortBreg *)(0x38)),
If I want to read value from port B bit B3 and write value to port B bit B2, can I do like
int i=Breg.B3; //to read
Breg.B2=i; //to write ?
Upvotes: 1
Views: 107
Reputation: 6406
Not always. If the port is memory-mapped, the simple act of reading and writing to the right bit from C may be enough to trigger a signal on the wires. But you might have to do other things. Often you have to disable interrupts. And reading often also clears the port, often you have to test a bit to see if there is data, then read and clear.
So you need to take a look at the hardware docs. Fundamentally the port is controlled by writing to bits as though they were plain memory, however.
Upvotes: 0
Reputation: 34575
That might depend if the port is readable and writable, but for a plain variable this code works: copying B3 to B2. Please be consistent with your types, int i
is not unsigned int i
. Note too that I print each member in the usual sequence, but the struct definition of an actual port read might need to be reversed, so that B7 is first.
#include <stdio.h>
struct PortBreg {
unsigned int B0 :1;
unsigned int B1 :1;
unsigned int B2 :1;
unsigned int B3 :1;
unsigned int B4 :1;
unsigned int B5 :1;
unsigned int B6 :1;
unsigned int B7 :1;
};
void show(struct PortBreg Qreg)
// bit order MSB -> LSB
{
printf("%u%u%u%u%u%u%u%u\n",
Qreg.B7, Qreg.B6, Qreg.B5, Qreg.B4, Qreg.B3, Qreg.B2, Qreg.B1, Qreg.B0);
}
int main(void)
{
struct PortBreg Breg = {0, 0, 0, 1,};
unsigned i;
show(Breg);
i = Breg.B3;
Breg.B2 = i;
show(Breg);
return 0;
}
Program output:
00001000 00001100
Upvotes: 1