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

about #define PORTB 0x6 and #byte RB=6 question

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



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

about #define PORTB 0x6 and #byte RB=6 question
PostPosted: Wed Jun 08, 2011 11:40 pm     Reply with quote

Hello, everyone,

I meet a question, and it puzzle me that # define and # byte difference
when i use the "#define PORTB 0x6;" the DEMO program is success.
Detail info. as follow:

#include <16f877a.h>
//#device icd=ture
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)

#define PORTA 0x5
#define PORTB 0x6
#define PORTC 0x7
#define PORTD 0X8

#use fast_io(D) //set IO

void main(void)
{
int i,led;

set_tris_d(0x00);
*PORTD=0x00;

while(1)
{
led=0x01;
for(i=0;i<8;i++)
{
*PORTD=led;
delay_ms(500);
led=led<<1;
}
}
}

But I change the "#define "to "#byte", the program is failure.

Program as follow:

#include <16f877a.h>
//#device icd=ture
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(CLOCK=10000000)

#byte RA = 5
#byte RB = 6
#byte RC = 7
#byte RD = 8
#byte RE = 9

#use fast_io(D) //set IO

void main(void)
{
int i,led;

set_tris_d(0x00); //set io direction
*RD=0x00;

while(1)
{
led=0x01;
for(i=0;i<8;i++)
{
*RD=led;
delay_ms(500);
led=led<<1;
}
}
}


Who can explain it to me ?

Thank you very much!
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 6:14 am     Reply with quote

When using #byte, such as #byte RD = 8, it is defining a name for memory location 8. So when using #byte, do not use *RD = 0x00; instead, just assign the value directly to the memory location: RD = 0x00.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
Ttelmah



Joined: 11 Mar 2010
Posts: 19569

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 8:12 am     Reply with quote

Yes.
They are very different things.

#define is the 'macro language' for C. it allows you to 'define' a block of text to replace another block of text. has some quite powerful features that are very rarely used. So the line:

#define PORTB 0x6

simply creates a text substitution, where wherever you type 'PORTB', the number '0x6' is substituted. On a lot of C's, you can look at the output after this bit of processing is done, and if you did so, in the first use of PORTD in your example, you would see:

*0x6 = 0x00;

There is a caveat in the substitution, which is that it can be 'bad practice not to use brackets. The problem is that in some cases the substitution won't give the expected results. The classic example in K&R, is a macro:

#define square(x) x*x

which if you perform with a mathematical value like 2+3, gives:

square(2+3) -> evaluates to 2+3*2+3

which since multiplication is performed before addition, solves as 11.....

Better to always 'bracket' numbers and values used inside macros.

#byte, is a very different beast. It allows you to locate a variable _at_ a memory location. If no variable exists with the name, a byte sized one is created.

So:

#byte PORTB=0x6

creates a byte variable 'PORTB', which uses the memory _at_ location 6 in the RAM. So if you use:

*PORTB=0;

You would read the memory value at location six (whatever PORTB, was last set to - 0xFF on boot....), and use this number as the address to write '0' to. Corrupting the memory at this location.
To write to location 6, simply needs:

PORTB=0;

It is worth realising that the function also allows you to put 16bit variables (or even larger) at a location. So:

int16 two_ports;
#byte two_ports=0x5

Creates a 16bit variable 'two_ports' sitting at locations 5, and 6 in the memory.

Best Wishes
litledan



Joined: 16 Aug 2013
Posts: 1

View user's profile Send private message

PostPosted: Fri Aug 16, 2013 12:17 pm     Reply with quote

Thank you very much for the explanation Ttelmah, it helped me a lot ;)
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