View previous topic :: View next topic |
Author |
Message |
future
Joined: 14 May 2004 Posts: 330
|
struct syntax |
Posted: Mon Dec 06, 2004 2:48 pm |
|
|
Hello,
I am planning to use a struct to take care of portb status.
The reason is that I can set all pins at the same time.
like:
#byte portb = 0xf81
struct {
int1 pin0;
int1 pin1;
int1 pin2;
int1 pin3;
int1 pin4;
int1 pin5;
int1 pin6;
int1 pin7;
} port;
port.pin0 = 1;
port.pin3 = 1;
portb = port; // bits 0 and 3 goes high
Is this construct correct and/or is there a better way?
Thank you. |
|
|
Guest
|
|
Posted: Mon Dec 06, 2004 3:07 pm |
|
|
How about this:
Code: | struct {
int1 pin0;
int1 pin1;
int1 pin2;
int1 pin3;
int1 pin4;
int1 pin5;
int1 pin6;
int1 pin7;
} portb;
#byte portb = 0xf81 |
Code: | portb.pin0 = 1;
portb.pin3 = 1; |
or
Code: | portb = {1, 0, 0, 1, 0, 0, 0, 0}; |
|
|
|
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Mon Dec 06, 2004 3:34 pm |
|
|
Basicaly they are the same thing, your suggestion won't work how I need...
In main(), sensors are read and data processed.. depending on the results it sets bits.
In the end of all processing, the byte is copied to portb setting all bits at the same time.
What I asked was ideas on how to write this in C... using structs the code looks nice. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Mon Dec 06, 2004 4:05 pm |
|
|
future wrote: | Basicaly they are the same thing, your suggestion won't work how I need...
In main(), sensors are read and data processed.. depending on the results it sets bits.
In the end of all processing, the byte is copied to portb setting all bits at the same time.
What I asked was ideas on how to write this in C... using structs the code looks nice. |
What is wrong with the "output_b()" command?
An incomplete (and untested) example:
Code: |
void main(void)
{
int portb_shadow;
portb_shadow=0;
while(1)
{
output_b(portb_shadow);
portb_shadow ^= 0xFF;
delay_ms(100);
}
}
|
_________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|