|
|
View previous topic :: View next topic |
Author |
Message |
guest Guest
|
how to access the lower 4 bits of port b in one go |
Posted: Wed Feb 23, 2005 4:16 am |
|
|
Hi to all.
Hope I can explain what I am trying to do do. I am using a pic16f876.
I have portb set up to drive 4 relays with the bottom 4 bits(Rb 0 to 3) and the top 4 bits are used for digital outputs.
How can I access the bottom 4 bits without affecting the to 4 bits , in 1 instruction.
For example , I want to turn on relays 1 and 3. I don't want to say:
relay1 = 1;
relay2 =0;
relay3 =1;
relay4 = 0;
I created a structure like this :
struct {
unsigned char relay1:1;
unsigned char relay2:1;
unsigned char relay3:1;
unsigned char relay4:1;
}relays;
#locate relays = 6.0
When I say relays = 5;
The correct relays are turned on , but the top bits are also affected , ie if
they were all 1 they are now 0.
Is there a way around this problem , or am I barking up the wrong tree altogether.
Cheers
Rob |
|
|
Ttelmah Guest
|
|
Posted: Wed Feb 23, 2005 5:12 am |
|
|
In your example, the problem is that the instruction 'relays = 5', will overwrite the entire 'carrier' byte, since the compiler does not know there is another potential variable there...
Try something like:
Code: |
struct bbyte{
unsigned char relay1:1;
unsigned char relay2:1;
unsigned char relay3:1;
unsigned char relay4:1;
unsigned char dummy:4;
};
struct bnib{
unsigned char rbits:4;
unsigned char dummy:4;
};
union {
struct bbyte bit;
struct bnib nib;
}relays;
#locate relays = 6.0
|
Then:
relays.nib.rbits=5;
will put 5 into the bottom nibble of the byte, but know that it has to read, and rewrite the top nibble at the same time.
relays.bit.relayx
Will allow access to the individual bits.
Obviously if you include the 'digital' definitions in the same structures (rather than 'dummy'), then the whole thing can just be mapped to address 6, and allow access to the other bits as needed.
Best Wishes |
|
|
guest Guest
|
|
Posted: Wed Feb 23, 2005 8:30 am |
|
|
Hi there.
Thanks for the help.
Cheers
rob |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|