View previous topic :: View next topic |
Author |
Message |
cwatters
Joined: 18 Dec 2003 Posts: 5 Location: Belgium
|
newbie Q: Overlaying structs on ports |
Posted: Sun Dec 21, 2003 1:19 pm |
|
|
Can I check that I've understood this right... Is it possible to twiddle individual output pins in a port by twiddling the corresponding bit in an overlay struct as follows....
struct port_b_layout {
int digit_en0 : 1;
int digit_en1 : 1;
int digit_en2 : 1;
int digit_en3 : 1;
int digit_en4 : 1;
int digit_en5 : 1;
int digit_en6 : 1;
int digit_en7 : 1;
};
struct port_b_layout port_b;
#byte port_b = 6
..and later in main loop
set_tris_b(0); //all outputs
port_b.digit_en0=1 //toggle pin digit_en0=RB0
delay_us(25)
port_b.digit_en0=0
delay_us(25) |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Dec 21, 2003 5:17 pm |
|
|
Yep |
|
|
Jim Hearne Guest
|
|
Posted: Mon Dec 22, 2003 6:52 am |
|
|
I would have done it like this, it's even simpler and doesn't need the struct.
#byte port_b = 6
#bit digit_en0 =port_b.0
#bit digit_en1 =port_b.1
#bit digit_en2 =port_b.2
#bit digit_en3 =port_b.3
#bit digit_en4 =port_b.4
#bit digit_en5 =port_b.5
#bit digit_en6 =port_b.6
#bit digit_en7 =port_b.7
..and later in main loop
set_tris_b(0); //all outputs
digit_en0=1 //toggle pin digit_en0=RB0
delay_us(25)
digit_en0=0
delay_us(25) |
|
|
burnsy
Joined: 18 Oct 2003 Posts: 35 Location: Brisbane, Australia
|
|
Posted: Tue Dec 23, 2003 7:39 pm |
|
|
Yeah, but some of us just LOVE to use structures ;) It doesn't make much difference but I would have laid it out like this..
defines somewhere......
#define RESET = 0
#define IS_RESET == 0
#define SET = 1
#define IS_SET != 0
then the structure.....
struct {
char sw0 : 1;
char sw1 : 1;
char sw2 : 1;
char sw3 : 1;
char sw4 : 1;
char sw5 : 1;
char lamp1 : 1;
char lamp2 : 1;
}port_b, tris_b;
#byte port_b = 0x06
#byte tris_b = 0x86
In the code...
if (port_b.sw1 IS_RESET)
{
port_b.lamp1 SET; /* turns on lamp1 */
port_b.lamp2 RESET; /* turns off lamp2 */
}
Careful when ever you are testing the structure as a whole with the IF statement. In my experience with PCM 3.058 this..
if (port_b == 42)
would produce strange results. If i did this..
x = port_b;
if (x == 42)
would produce what I expected in the first place??? not sure why but adding this line under the structure definition fixes it.
#define PORT_B_BITS (*((UCHAR*)(&port_b)))
if (PORT_B_BITS == 42)
Works like a charm, no increase in code size. Credit goes to my former employer for that line _________________ This is the last code change until its ready.... |
|
|
|