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 CCS Technical Support

Passing a #BIT in c possible?

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



Joined: 02 Aug 2005
Posts: 8

View user's profile Send private message

Passing a #BIT in c possible?
PostPosted: Thu Feb 18, 2010 2:00 pm     Reply with quote

Is it possible to pass something defined with #BIT to a c function? Here is an example of what I'd like to do:

Code:

unsigned char ucOption;
#BIT bOption   = ucOption.7

void SetBit(unsigned char data)
{
data = 1;
}

void main()
{
SetBit(bOption);

}


Obviously this doesn't work, but how would I go about doing this?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 18, 2010 3:17 pm     Reply with quote

How doesn't it work ? I made a little test program and passed
parameters of 1 and 0 to the SetBit() function several times.
I displayed the value of the 'data' parameter inside the function
with a printf statement, and I get this, which is correct:
Quote:

data = 01
data = 00
data = 01
data = 00

This was tested with compiler vs. 4.104 in the MPLAB simulator.
Code:

#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

unsigned char ucOption;
#BIT bOption = ucOption.7

void SetBit(unsigned char data)
{
printf("data = %x\r", data);
}

//==============================
void main()
{
bOption = 1;
SetBit(bOption);
bOption = 0;
SetBit(bOption);
bOption = 1;
SetBit(bOption);
bOption = 0;
SetBit(bOption);
   
while(1);
}
 
tstein



Joined: 02 Aug 2005
Posts: 8

View user's profile Send private message

PostPosted: Thu Feb 18, 2010 3:25 pm     Reply with quote

I think most of my problem has to do with passing a value instead of a pointer. But I can't seem to figure it out.

I'm wanting to directly write the bit passed from within the function.

I think the reason yours works is because your dumping the value and not modifying the bit variable.

Any ideas how I can modify the passed #BIT variable?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 18, 2010 3:36 pm     Reply with quote

I'm not sure why you want to do this, because you can just set or clear
a bit variable with a line of code. But if you want to use a pseudo
function, you can do it with a macro. I've created the SetBit() macro
below. The results of the test are:
Quote:

bOption = 00
bOption = 01


Code:

#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

unsigned char ucOption;
#BIT bOption = ucOption.7

#define SetBit(x)  x = 1

//==============================
void main()
{
bOption = 0;
printf("bOption = %x\r", bOption);

SetBit(bOption);
printf("bOption = %x\r", bOption);
   
while(1);
}
 
tstein



Joined: 02 Aug 2005
Posts: 8

View user's profile Send private message

PostPosted: Thu Feb 18, 2010 3:47 pm     Reply with quote

The reason why I would need to do it in a function is because I have a ton of #BIT variables defined already. So there is a desire to create one function to pass a #BIT type by reference and modify it from within the function.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 18, 2010 3:50 pm     Reply with quote

What's wrong with the macro ?

If you look at the .LST file, the macro creates a "one liner" to set the bit:
Code:

.... SetBit(bOption); 
00CA:  BSF    05.7
tstein



Joined: 02 Aug 2005
Posts: 8

View user's profile Send private message

PostPosted: Thu Feb 18, 2010 3:57 pm     Reply with quote

My apology in advance for not thoroughly describing my problem from the start, but I appreciate your help greatly! My example was a poor one unfortunately.

The reason why I need a function is because I have more going on in my SetBit function than just setting the bit. I need to evaluate the location of the bit, and perform other activites within the function depending on what location it falls in.

The reality is that I need to not only have the value, set the value, but also know the address location. Would a function with #BIT variable passed by reference be what I am after?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 18, 2010 4:42 pm     Reply with quote

The CCS manual says this:
Quote:

Basic and Special types --

Pointers to bits are not permitted.
Ttelmah
Guest







PostPosted: Fri Feb 19, 2010 5:02 am     Reply with quote

The 'obvious' solution, is to apply the same cheat, that used to be used for accessing the CCS bit numbers, before variable accesses were allowed. Declaring your own 'pointer', with the address of the byte concerned, *8, plus the bit number required, and accessing this through normal bit operations.
So, something like:
Code:

void setbit(int16 locn) {
   int8 *b;
   int8 mask=0;
   bit_set(mask,locn&7);
   b=(int8 *)(locn/8);
   *b=*b|mask; //set the bit
}

main() {
   int8 val;
   int16 valptr;

   //Your initialisation
   valptr=((&val)*8)+7; //for bit 7
   val=0;
   setbit(valptr);

   //Here bit 7 of 'val' has been set
   valptr--;
   setbit(valptr);

   //Now bit 6 has been set as well
   while(TRUE);
}

Obviously, the routine, can read the bit, as easily as it writes it.
For up to 8K of RAM, this can access any location in memory.

Best Wishes
tstein



Joined: 02 Aug 2005
Posts: 8

View user's profile Send private message

PostPosted: Fri Feb 19, 2010 7:30 am     Reply with quote

Thanks for the suggestions and help. Consider this problem resolved!
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