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

pointer envy

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







pointer envy
PostPosted: Tue Nov 12, 2002 6:56 pm     Reply with quote

I thought that I knew what I was doing, but I was wrong.

I created a structure for some flags. And all was well, until I decided to save them to EEprom.

struct System_Flags{
int1 Alarm_PT;
int1 Local_SP;
int1 Servo;
int1 extra1;
int1 extra2;
int1 extra3;
int1 extra4;
int1 extra5;
};
struct System_Flags Sys_Flag;

void Toggle_Flag(){
Sys_Flag.Alarm_PT ^=1; /* this works fine */
}

void Write_EE(){
If((char)Sys_Flag != Read_EEprom(15))
Write_EEprom(15,(char)Sys_Flag);
}

Now, this seems to work. But was I right to cast as I did?

At first I thought that Sys_Flag was the base address of the structure, but it's really a variable of type System_Flags, right?

my first try was this, and it went every which way but loose!

void Write_EE(){
char *charptr;

charptr=Sys_Flag;
If(*charptr != Read_EEprom(15))
Write_EEprom(15,*charptr);
}


Would a union have been more elegant?

Would this have fixed the above code?
charptr=&Sys_Flag;

I'm just looking for some assurance that i haven't created some kind of pointer time-bomb that'll come back to haunt me a few revisions from now. Long after I would've suspected this little enhancement.

So if one of you could stop laughing long enough to point me (yes, I said POINT me) in the right direction, I'll promise never to de-reference in public again....today.
___________________________
This message was ported from CCS's old forum
Original Post ID: 8825
Dale Herman
Guest







Re: pointer envy
PostPosted: Tue Nov 12, 2002 8:06 pm     Reply with quote

No problem woth your code. Here was my solution:


union{
struct {
short motion_complete:1; // set if a profiled motion completed according to moton_complete_mode
short position_wrap:1; // set if position wraps around in either direction
short tracking_error:1; // set if abs(commanded (profiled) position - actual position) > psition_tracking_limit
short in_positive_limit:1; // set if positive move went into limit (must be using limits)
short in_negative_limit:1;
short instruction_error:1; // illegal instruction or parameter
short captured:1; // set if the homing capture has occurred (can use limit, home, or index)
short res7:1;
} bits;
int8 all_bits;
} servo_event_status;
___________________________
This message was ported from CCS's old forum
Original Post ID: 8826
patrick muldoon
Guest







Re: pointer envy
PostPosted: Tue Nov 12, 2002 8:50 pm     Reply with quote

I like...
Thanks
___________________________
This message was ported from CCS's old forum
Original Post ID: 8827
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: pointer envy
PostPosted: Wed Nov 13, 2002 2:50 pm     Reply with quote

You could simplify a lot like this for readability and functionality

int8 System_Flags;
#bit Alarm_PT = System_Flags.0
#bit Local_SP = System_Flags.1
#bit Servo = System_Flags.2
#bit extra1 = System_Flags.3
#bit extra2 = System_Flags.4
#bit extra3 = System_Flags.5
#bit extra4 = System_Flags.6
#bit extra5 = System_Flags.7

Then just save the int8 System_Flags to EEPROM and use the flags directly in code rather than using a function. You also have the option to #Locate System_Flags in the bank that will cause the least number of bank switches.

to me
Sys_Flag.Alarm_PT ^=1
is not as elegant or clean as
Alarm_PT ^=1
___________________________
This message was ported from CCS's old forum
Original Post ID: 8865
patrick muldoon
Guest







Re: pointer envy
PostPosted: Thu Nov 14, 2002 7:41 am     Reply with quote

The #bit is something i've never played with before, thanks for the suggestion. Now that I know how to use it, I will.

I do switch my bits in code (within larger functions). I just showed a Readers Digest Condensed version of the usage to save everyone from having to sift thru my whole function.

:=You could simplify a lot like this for readability and functionality
:=
:=int8 System_Flags;
:=#bit Alarm_PT = System_Flags.0
:=#bit Local_SP = System_Flags.1
:=#bit Servo = System_Flags.2
:=#bit extra1 = System_Flags.3
:=#bit extra2 = System_Flags.4
:=#bit extra3 = System_Flags.5
:=#bit extra4 = System_Flags.6
:=#bit extra5 = System_Flags.7
:=
:=Then just save the int8 System_Flags to EEPROM and use the flags directly in code rather than using a function. You also have the option to #Locate System_Flags in the bank that will cause the least number of bank switches.
:=
:=to me
:=Sys_Flag.Alarm_PT ^=1
:=is not as elegant or clean as
:=Alarm_PT ^=1
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 8900
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