Connect with us

PIC EQU section.

Discussion in 'Electronic Basics' started by ian field, Feb 21, 2008.

Scroll to continue with content
  1. ian field

    ian field Guest

    In a sample fragment of code (EQU section) of a program example from the
    book PIC in Practice:

    TMR0 EQU 1 ;TMR0 is FILE 1.
    ZEROBIT EQU 2 ;ZEROBIT is Bit 2.
    EEADR EQU 9 ;EEPROM address register
    EEDATA EQU 8 ;EEPROM data register
    EECON1 EQU 8 ;EEPROM control register1
    EECON2 EQU 9 ;EEPROM control register2
    RD EQU 0 ;read bit in EECON1
    WR EQU 1 ;Write bit in EECON1
    WREN EQU 2 ;Write enable bit in EECON1

    How does the assembler tell the difference between a file and a bit?

    The lines starting with ZEROBIT and WREN do not seem to have anything to
    distinguish them from file EQUs.

  2. Greg Neill

    Greg Neill Guest

    It doesn't know anything about bits or files. All it
    knows is that you have assigned the constant 2 to a
    symbol ZEROBIT. It doesn't know or care what you
    subsequently do with that symbol.

    However... when it comes across that symbol in code
    that you write (code that generates instruction
    sequences), it will substitute the number 2 for the
    symbol in the context in which it is found. It's up
    to you to use the symbol in the appropriate context.
  3. ian field

    ian field Guest

    The author of the book this came from has opted to write his own .inc files
    to replace the ones supplied by Microchip, except they have an .asm
    extension and each is used as a template to which the program code is added
    for a variety of programming examples.

    From what I'd read, the label names were completely arbitrary - "ZEROBIT"
    could be called whatever you want as long as consistency was maintained
    throughout - if I'm wrong about that it probably answers my question.
  4. Tom2000

    Tom2000 Guest

    Ian, all you're doing with the equates is setting up an alias that the
    assembler will use. Think search and replace in a word processor.
    Every time you write ZEROBIT in you source code, the assembler will
    replace that with '2' when it assembles your source.

    Now, *how* ZEROBIT is used, and its context, is strictly
    up to you. In your source code, you'll use ZEROBIT in
    an expression that can only refer to a bit operation. An
    in that expression, it won't matter if you write it as

    .... ,ZEROBIT


    .... ,2

    Since the assembler will replace ZEROBIT with 2, it doesn't care a
    whit how you've written the expression: 2 or ZEROBIT. The context and
    form of the instruction you write tells the assembler what to do with
    that 2 or its alias.

    The same holds for ports, registers, memory addresses, constants, or
    anything else set as aliases.

    You would use these (or not, as you choose) simply to improve the
    readability of your program.

    Does that clear it up for you?

    Have fun!

  5. ian field

    ian field Guest

    Thanks - now it seems like one of those things that's so obvious I just
    can't see it.
  6. EQU statements just assign numbers to names, so the programmer can use
    meaningful names in his program, rather than numbers. The assembler
    doesn't care what you think the names or numbers mean.

    Whenever the assembler sees "ZEROBIT" or "WREN" in your source, it
    will substitute the number 2. Presumably you would use each word in a
    place where it makes sense to the reader, but if you exchange them, it
    will only confuse the reader, not the assembler.

    EQUs are also used to name frequently used numbers in the program.
    For example, if you start out planning to use 6 ADC channels you might
    have this statement:


    and use LAST_ADC wherever you are counting channels to see if you've
    done them all. Later, someone decides you really need 8 channels, so,
    if you hadn't used that line, you'd have to search through the program
    for all the places you used "6" and change it to 8. Instead, with the
    EQU you just change the above line to:


    and the assembler automagically makes all the required changes.
  7. Yes - the label names are completely up to you - but if the chip
    documentation provides names for bits in various registers, it makes
    sense to use those names in your program rather than making up your
    own names. Using equates, particularly for using "standard" bits or
    bit patterns, greatly improves the readability of the program.
  8. My 8051 assembler seriously objects to that statement.
  9. Eeyore

    Eeyore Guest

    Since when has a BIT had a value of 2 ?

    Looks like sloppy documentation to me.

  10. Eeyore

    Eeyore Guest

    What IS all this 'file' talk about anyway ?

    At last !

    Buy that man a cigar.

  11. Eeyore

    Eeyore Guest

    I 'm not familiar with PIC assembler but I'd have thought an equate assigns a
    *constant* value, not a temporary value to a variable.

  12. Eeyore

    Eeyore Guest

    I assume it means a value of 0000010B. Calling that a 'bit' is highly

    That book sounds dodgy to me.

  13. Eeyore

    Eeyore Guest

    Do PICs start numbering such things at '1' ? I'd have expected "LAST_ADC EQU 5".

  14. Read my answer in ABSE.

    You'd better crosspost next time.

    petrus bitbyter
  15. EQU assigns an assembler expression to a label; it must remain constant.
    SET works much like EQU but can be reassigned as desired.
    #define is also availabe for pure text substitutions.
    Don't get these confused with the CONSTANT and VARIABLE directives.
  16. "file registers", haven't you heard of them before? No doubt it's a silly
    name, but an 805x expert should know these things.
  17. In PIC assembler, if you want to let's say set the LSBit of a byte in RAM,
    you'd code something like this:
    BSF locsym, 0
    the MSBit would be set by:
    BSF locsym, 7

    BSF stands for "Bit Set f". "f" is Microchip's stand-in for "register
    file". The first operand is the RAM location you want to modify, the second
    operand is the physical bit position (7 to 0).
  18. Eeyore

    Eeyore Guest

    As I imagined. In PL/M it would be a literal.


  19. Eeyore

    Eeyore Guest

    Intel don't use the term 'file'. The 8051 has the usual working registers plus
    the all important 'special function registers' that determine many aspect of
    how the internals work - such as seeting up timers (TCON and TMOD), UARTs,
    interrupts and the like.

  20. Eeyore

    Eeyore Guest

    In PL/M I might set an entire SFR (special function register) thus .....

    TCON = 00101100B; or TCON = 02CH; or TCON = 44;
    I like to set them using binary though (except for counter timer count values)
    since you can quickly transpose the value to the individual bits.

    or an individual bit of a register (for the bit-adressable ones) like this

    EA = 1; (or 01B for that matter - default is decimal)

Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day