CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

newbie Q: Overlaying structs on ports

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cwatters



Joined: 18 Dec 2003
Posts: 5
Location: Belgium

View user's profile Send private message

newbie Q: Overlaying structs on ports
PostPosted: Sun Dec 21, 2003 1:19 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sun Dec 21, 2003 5:17 pm     Reply with quote

Yep
Jim Hearne
Guest







PostPosted: Mon Dec 22, 2003 6:52 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Dec 23, 2003 7:39 pm     Reply with quote

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....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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