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

Bit toggling a variable

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



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

Bit toggling a variable
PostPosted: Wed Oct 12, 2005 6:29 am     Reply with quote

I have spent an hour trying to sort this out, and failed.

I am trying to toggle a bit in a variable.

I define a flag

int16 General_Flags1;
then define the bit
#define T_1Sec_Toggle_Flag T_Timer_Flags,15

so, i think that bit 15 of General_Flags1 holds my boolean

I now wish to toggle that bit by using something like :-


#define bit_toggle(my_bit) {#asm btg my_bit #endasm}

of course, this only works for a byte, so i get an error saying it can only be 0..7, not 15

can some guru help ?

is there a better way.

Basically, I am looking for a function like output_toggle(pin), which works with a variable of more than 8bits, rather than a pin.

Mike
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Wed Oct 12, 2005 6:40 am     Reply with quote

The PIC is an 8 bit machine and thus only 0-7 is allowed. What you must do is use bit 7 of the upper byte. For CCS this is &myvar + 1 when using assembly. Another option would be to use a struct to define the bits and then just do a myvar.bit15 = !myvar.bit15;
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Wed Oct 12, 2005 8:06 am     Reply with quote

This is not a function but it is easy to read.

Code:
int16 General_Flags1;
#bit  T_1Sec_Toggle_Flag = T_Timer_Flags.15
T_1Sec_Toggle_Flag=!T_1Sec_Toggle_Flag;
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Wed Oct 12, 2005 8:17 am     Reply with quote

looks good to me, many thanks

MikeW
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