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

what's the advantage of #define set_bit_var(x) bit_set (*(in

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







what's the advantage of #define set_bit_var(x) bit_set (*(in
PostPosted: Tue Apr 03, 2007 2:54 pm     Reply with quote

Hi,

In previous post, I saw macros:

#define set_bit_var(x) bit_set (*(int8 *)(x >>3), x & 7)

#define clear_bit_var(x) bit_clear (*(int8 *)(x >>3), x & 7)

#define read_bit_var(x) bit_test (*(int8 *)(x >>3), x & 7)

I am not sure the advantages of these macros, when CCS already has built-in functions to bit_set, bit_clear and bit_test.

Can some one please clarify. Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 03, 2007 3:15 pm     Reply with quote

They accept a CCS-style pin number, in a variable, as the argument.
Example:
Code:

#include <16F877.H>
#fuses HS, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define set_bit_var(x) bit_set (*(int8 *)(x >>3), x & 7)

//==========================
void main()
{
int8 pin;

pin = PIN_B0;

set_bit_var(pin);


while(1);
}
michael
Guest







PostPosted: Tue Apr 03, 2007 3:29 pm     Reply with quote

Hi PCM Programmer,

I tried to compile some examples and the lst files are different. One is writng to the port and the other to the latach. But both perform the same function. May be just for personal prefereces. Regards.


.................... set_bit_var(pin_a0);
002A: BSF F80.0
.................... output_bit (pin_a0, 1);
002C: BSF F89.0
002E: BCF F92.0
....................
.................... clear_bit_var(pin_b0);
0030: BCF F81.0
.................... output_bit (pin_b0, 0);
0032: BCF F8A.0
0034: BCF F93.0
....................
.................... a = read_bit_var(pin_c0);
0036: BCF 06.0
0038: BTFSC F82.0
003A: BSF 06.0
.................... b = bit_test (*PORTC, 0);
003C: BCF 06.1
003E: BTFSC F82.0
0040: BSF 06.1
....................
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 03, 2007 4:03 pm     Reply with quote

You're using constants as the arguments. Try it with a variable.

If you have vs. 3.249 or before, and if you plug in a variable as
the argument, you will get this compiler error message:
Quote:
Expression must evaluate to a constant


With vs. 4 of the compiler, CCS has stated that these functions will
now accept a variable. I compiled a test program with the output_bit()
with vs. 4.031 and it does compile OK. There's no error message.
I didn't test it further than that.
michael
Guest







PostPosted: Tue Apr 03, 2007 4:51 pm     Reply with quote

Hi PCM Programmer,

I am not sure what do you mean by variable.

In the example you posted, pin = PIN_B0. PIN_B0 is 31752 (int16). when assigned to pin (int8), it becomes hex 08. When you plug it in set_bit_var (pin), it will get devided by 8 and use as an int8 pointer and get the content of it and set the bit.

Regards,



#include <16F877>
#fuses HS, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define set_bit_var(x) bit_set (*(int8 *)(x >>3), x & 7)

//==========================
void main()
{
int8 pin;

pin = PIN_B0;

set_bit_var(pin);


while(1);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 03, 2007 4:57 pm     Reply with quote

This is a constant:
Code:
#define PIN_B0  31752


This is a variable:
Code:
int16  pin_number;

(using an int16 because pin numbers in the 18F series are 16-bit constants).

--------------------------
There is quite a difference between passing a variable and passing
a constant to the function. Here is the generated code for my 16F877
example above, when modifed to show both methods:
Code:
pin = PIN_B0;

set_bit_var(pin);  // Pass it a variable

set_bit_var(PIN_B0);  // Pass it a constant

Code:

.................... set_bit_var(pin);
0018:  RRF    21,W
0019:  MOVWF  77
001A:  RRF    77,F
001B:  RRF    77,F
001C:  MOVLW  1F
001D:  ANDWF  77,F
001E:  MOVF   77,W
001F:  MOVWF  04
0020:  MOVF   21,W
0021:  ANDLW  07
0022:  MOVWF  22
0023:  MOVLW  01
0024:  MOVWF  77
0025:  MOVF   22,W
0026:  MOVWF  78
0027:  BTFSC  03.2
0028:  GOTO   02D
0029:  BCF    03.0
002A:  RLF    77,F
002B:  DECFSZ 78,F
002C:  GOTO   029
002D:  MOVF   77,W
002E:  IORWF  00,F
.................... 
.................... 
.................... set_bit_var(PIN_B0);
002F:  BSF    06.0
.................... 
michael
Guest







PostPosted: Tue Apr 03, 2007 5:36 pm     Reply with quote

Hi PCM programmer,

Thanks for the clarifications. I compiled using 18F452 and I got similar list files. It is neat the the same macro can work for both 16F and 18F devices. Still that's a lot of instructions to set a bit.

Regards

.................... set_bit_var(c);
003E: RRCF 08,W
0040: MOVWF 03
0042: RRCF 07,W
0044: MOVWF 02
0046: RRCF 03,F
0048: RRCF 02,F
004A: RRCF 03,F
004C: RRCF 02,F
004E: MOVLW 1F
0050: ANDWF 03,F
0052: MOVFF 02,FE9
0056: MOVFF 03,FEA
005A: MOVF 07,W
005C: ANDLW 07
005E: MOVWF 0D
0060: CLRF 0E
0062: MOVLW 01
0064: MOVWF 00
0066: MOVF 0D,W
0068: MOVWF 01
006A: BZ 0074
006C: BCF FD8.0
006E: RLCF 00,F
0070: DECFSZ 01,F
0072: BRA 006C
0074: MOVF 00,W
0076: IORWF FEF,F
    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