View previous topic :: View next topic |
Author |
Message |
Sherpa Doug Guest
|
Proper C syntax question |
Posted: Wed Nov 07, 2001 3:40 pm |
|
|
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 |
Posted: Wed Nov 07, 2001 3:58 pm |
|
|
:=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
|
Re: Proper C syntax question |
Posted: Wed Nov 07, 2001 5:11 pm |
|
|
:=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 |
Posted: Thu Nov 08, 2001 12:08 am |
|
|
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 |
Posted: Thu Nov 08, 2001 12:16 pm |
|
|
:=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 |
Posted: Fri Nov 30, 2001 4:19 pm |
|
|
#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 |
|
|
|