PIC Assembly Macros

I make fairly heavy use of macros in my PIC assembly programs. In fact, parts of my programs almost read like English because of all the macros. I like using macros in my programs for two reasons:

  1. It makes the program easier to read.
  2. It makes the program easier to change.

Macros to simplify conditions

The thing that still gives me the most trouble when writing PIC assembly programs is the various skip instructions, especially when using them on input pins. The fact that you're dealing with negative logic, on top of the fact that you need to consider whether the pin is pulled up or down, makes me have to think really hard about each condition.

To simplify the process, I have written the following set of code:

   PULLED_HIGH          EQU  1
   PULLED_LOW           EQU  0

   IF_ON macro pulled_status, port, pin
     if pulled_status == PULLED_HIGH
       btfss port, pin
     endif
     if pulled_status == PULLED_LOW
       btfsc port, pin
     endif
    endm

   IF_OFF macro pulled_status, port, pin
     if pulled_status == PULLED_HIGH
       btfsc port, pin
     endif
     if pulled_status == PULLED_LOW
       btfss port, pin
     endif
    endm

For the pulled_status parameter, pass in either PULLED_HIGH or PULLED_LOW. This code makes writing conditions on input pins straight-forward. For each input pin, simply write two macros, such as:

   IF_BUTTON_PRESSED macro
     IF_ON PULLED_HIGH, PORTB, 4
    endm

   IF_BUTTON_NOT_PRESSED macro
     IF_OFF PULLED_HIGH, PORTB, 4
    endm

Then, in the body of the code, all you need to do is write:

   IF_BUTTON_PRESSED
     goto handle_button_press

No more complicated conditions.

Back to the index.

Page last modified on 03/13/2004