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

Proper C syntax question

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







Proper C syntax question
PostPosted: Wed Nov 07, 2001 3:40 pm     Reply with quote

I'm a hardware guy who would like to learn to program in proper style. This code works to send the LS bit of a char to the output_bit function without altering the char:

char foo
output_bit(pin_B0, foo & 0x01);

But is there a more graceful way? Should I be casting to a short? If I just cast foo to a short do I know what bit it will use?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1003
Sherpa Doug
Guest







Re: Proper C syntax question
PostPosted: Wed Nov 07, 2001 3:58 pm     Reply with quote

:=I'm a hardware guy who would like to learn to program in proper style. This code works to send the LS bit of a char to the output_bit function without altering the char:
:=
:=char foo
:=output_bit(pin_B0, foo & 0x01);
:=
:=But is there a more graceful way? Should I be casting to a short? If I just cast foo to a short do I know what bit it will use?

Actually casting foo to a short chokes the compiler. Also
output_bit(pin_B0, foo) causes the pin to be set if ANY bit of foo is set.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1004
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Proper C syntax question
PostPosted: Wed Nov 07, 2001 5:11 pm     Reply with quote

:=I'm a hardware guy who would like to learn to program in proper style. This code works to send the LS bit of a char to the output_bit function without altering the char:
:=
:=char foo
:=output_bit(pin_B0, foo & 0x01);
:=
:=But is there a more graceful way? Should I be casting to a short? If I just cast foo to a short do I know what bit it will use?

Look in the help file under the heading
"How does one map a variable to an I/O port?"
___________________________
This message was ported from CCS's old forum
Original Post ID: 1006
Tomi
Guest







Re: Proper C syntax question
PostPosted: Thu Nov 08, 2001 12:08 am     Reply with quote

Try this:

char foo;
#bit toSend = foo.0

output_bit(PIN_B0,toSend);

:=I'm a hardware guy who would like to learn to program in proper style. This code works to send the LS bit of a char to the output_bit function without altering the char:
:=
:=char foo
:=output_bit(pin_B0, foo & 0x01);
:=
:=But is there a more graceful way? Should I be casting to a short? If I just cast foo to a short do I know what bit it will use?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1008
Tomi
Guest







Re: Proper C syntax question
PostPosted: Thu Nov 08, 2001 12:16 pm     Reply with quote

:=Also output_bit(pin_B0, foo) causes the pin to be set if ANY bit of foo is set.

Of course. Bits could be either 0 or 1. Traditionally 0 means "null" and "1" means "not null". So your bit var. will be "1" when foo is "not null" but "foo is not null" interpreted as "not 0".
This is a standard C definition: e.g.
char foo; // note that foo could be int16, int32, etc.
You can write:
if (foo != 0) Something(); // the body of if() statement is a boolean (bit) expression
But you can write also:
if (foo) Something();
means "execute Something() if foo is "not null".
In this aspect the expression:
if (a-b) ;
is equivalent to
if (a != b) ;
and:
if (!(a-b)) ;
is equ. to:
if (a == b) ;
Note that the compiler uses the "if (a-b) ;" if you write "if (a != b) ; ":
0000 00260 ... if (foo != goo) ;
002F 0822 00261 MOVF 22,W
0030 0221 00262 SUBWF 21,W
0031 1903 00263 BTFSC 03,2 // if (a-b) is null
0032 2833 00264 GOTO 033
___________________________
This message was ported from CCS's old forum
Original Post ID: 1018
David
Guest







Re: Proper C syntax question
PostPosted: Fri Nov 30, 2001 4:19 pm     Reply with quote

#byte PORTB = 6
#bit OddChar = PORTB.0

OddChar = foo & 0x01;

:=char foo
:=output_bit(pin_B0, foo & 0x01);
:=
:=But is there a more graceful way? Should I be casting to a short? If I just cast foo to a short do I know what bit it will use?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1408
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