Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

#define statement for PIC

Status
Not open for further replies.
I'm using a 16F628 and want to "store" about twenty different characters in the Ram, for comparison and communication purposes. these won't be altered at any time during operation. I was wondering if i could do something like

Code:
IDA equ 20
#define IDA 0xXX

for each of the characters, thus giving them their own file and fixed value.
Is this necessary? could i just have them as constants by leaving off the equates section? Any advice is greatly appreciated as I'm new to PIC's.

Thanks in advance
 
#define is a C language construct. It looks like you are using assembly though. One of the PIC assembly experts can give you specific help with the syntax for assembly.

#define doesn't actually store anything in Ram. It is a simple replacement that the compiler does before compiling the code. The compiler will go through the code and replace every IDA with 0xXX before compiling. In C the define can replace anything. You can have a define something like

#define isdone (done == 1)

In assembly I think you can only do value replacements.

If you actualy want to store the values you can store them in flash along with your program code. In the C compilers I've uses it uses the compiler directive "code".

Brent
 
All the instructions for the assembler are in the MPASM helpfile, which is installed along with MPLAB. The appendices at the end are particularly helpful.

As far as I know you can't just declare a variable as a constant, due to the Harvard architecture data and program memory are completely seperate. So any variables are volatile - they vanish when you turn the power off.

To set up variables as the program runs, use MOVLW and MOVF instructions.
 
thanks both for the advice. I think I have a better idea now.

So to clarify, I could set up twenty constants by:

Code:
cblock 0x20
    constant1
    constant2
    etc.
endc

and then assign values to them

Code:
movlw 0x55
movwf constant1
etc..

would I have to reinstate these values after each use of a constant? eg if i compared it to a received byte by doing a subtraction of one on the other?
 
Thinlking about it, could I not just use the equates section to specify the hex values of the constants that I want to use? I could then have the easy coding that I'm after (as it would just replace the constants with the hex). Am I correct in thinking this?

eg
Code:
IDA equ 0x55

   ;then in another subroutine

movlw  IDA
MOVWF
call TransmitByte
 
would I have to reinstate these values after each use of a constant? eg if i compared it to a received byte by doing a subtraction of one on the other?

No, you don't have to. When you do a compare using subtraction, send the result to WREG instead of back to the file register.
 
why store it in the RAM when you can write it into the EEPROM?? When the power goes off the data stays in the EEPROM. I haven't had experience with with writing to this sorta stuff, but it looks like a good option.
 
Morning all. First thanks for all the opinions.

On advice, I checked the help files for MPLAB and found that I can use either the equ, set or #define functions. equ or #define are the better ones to use, as set could be reset to a different value if i wasn't careful. As a simple substitution is required, I'm going to use equ.

If I wanted to use a string, it'd be the #define function.

Thanks everyone for the input, it's all valuable.

As an aside, if I wanted to do a comparison between a received byte and a "constant" byte, is a substituion followed by a check of the zero flag the best way to do it? If the "smaller" byte is "on top" would i be right in saying the zero wouldn't set? so would i do the substitution twice, each way round, to make sure they both equal zero?


Mark
 
Mark Lazarides said:
Thinlking about it, could I not just use the equates section to specify the hex values of the constants that I want to use? I could then have the easy coding that I'm after (as it would just replace the constants with the hex). Am I correct in thinking this?

eg
Code:
IDA equ 0x55

   ;then in another subroutine

movlw  IDA
MOVWF
call TransmitByte

Yes you could, there's no need to use a constant at all, simply do the MOVLW IDA every time you need to access the variable. It gives the advantage that you simply have to alter the EQU statement to change all the values. I use a similar method in my tutorials, to define things like the port used for the LCD - then you can simply alter that one define to change the port you use.

Another advantage, is that you can't accidently alter the value, as it's hard coded in each line it's used, it's permanent.
 
motion said:
once again, zillion replies and all completely useless
And yours is?
Mark Lazarides wrote:
#define IDA 0xXX
You can't do that.
Assuming you're using pic assembly
Beep! wrong answer!

Beep my ass, man (or yours)!

In my CV assmebler you can't use that. I looked up microchip assembler and yes apperantly it is allowed, but I don't care cause my cv assmbler is 10x better than microchips
 
tom2000 said:
In my CV assmebler you can't use that. I looked up microchip assembler and yes apperantly it is allowed, but I don't care cause my cv assmbler is 10x better than microchips

Interesting! - your assembler won't accept perfectly correct code (from the chip manufacturers specifications), yet it's '10x better' - how's that then?.

So why is the CV assembler better?.
 
because the '#' sign is used for other stuff

there probably is a define function, but i don't use it (nor care)
 
you use equ to define you variables
e.g PORTA equ 85h
now instead of writing 85h you only need write PORTA
you use define# for definign 2 location type thing
e.g. define# LED PORTA,0
now when you wan tto use port a bit 0 you simply use LED
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top