Macro Techniques using PIC16C711 Microcontroller

Lots of news before getting into this article on Macros and other ways to simplify your application development. The first is, I have finished going through the page proofs of “PC PhD” and it should be ready for going to the printers later this week. This book is about interfacing hardware to the PC along with a focus on MS-DOS software. Windows operation is also presented. The book contains four PICMicro projects that provide some basic interfaces for you to build from. This book is scheduled to be available in late August.

I am happy to announce that “Programming and Customizing the PIC Microcontroller” is finally available with a CD-ROM. The primary reason for going with a CD-ROM with this book is to avoid the problems that some people had with the diskette that came with it. The CD-ROM is identical to the diskette except that the YAP files have been updated with the latest level of code along with datasheets for the parts used in the book and an html interface. If you would like any of these additional files, please let me know and I will provide links to them for you.

Macro Techniques

The “El Cheapo” programmer has been generating a lot of interest from people and I have made a slight change to the software to better support more PCs. Check out article 4 for Version 2.1 and if you’ve built an El Cheapo – I’m interested in seeing any pictures of what you’ve come up with along with any comments.

For any applications that require PC code, I am thinking of only developing the applications from now on using Boland’s “Turbo C”. The reason for this is very simple, it can be downloaded free of charge from: http://community.borland.com/museum

I will be warming up my skills with “Turbo C” version 2.01. I have previously been using Microsoft “C” Version 3 which hasn’t been available since 1988. When you download the code, note that you will have to load three diskettes with the Compiler, linker, include files and IDE before you can install them.

Lastly, I have a contest – check out the end of this article for your opportunity to win a copy of a special “Programming and Customizing the PIC Microcontroller”.

I have received quite a few messages about my previous article about the bit-banging serial interface routines. These emails were questioning the macro code I was presenting and how advisable is using complex macros like these. The issues that were brought up were the difficulty in debugging the resulting code, understanding what it actually is doing and the loss of control over the application. These concerns are understandable and completely valid – but I think I have come up with a formula that avoids these problems.

This article is a primer on the various features of MPASM (and most other “macro assemblers”) which can be used to make your application development simpler and easier to work through to understand what is happening. As I work through these features, I am presenting the knowledge toward creating macros that can greatly simplify your assembly language programming, both from the development point of view as well as from the debugging and understanding somebody else’s application.

As I wrap up the article, I will go through the development of a “delay” macro that can be used with the Bit-Banging Serial Interface macros I’ve previously presented that takes advantage of all the features I’m going to talk about in this article and point toward a “standard” for developing interface macros which will simplify sharing code between users as well as make the development of PICMicro applications much more efficient.

There are six programming constructs are used prior the assembly or compilation of your application. This takes place in the “Macro Processor”, which are also be known as the “Pre-Processor”. This program’s function is to read through the application source code, bring in all the “included” files and identify which blocks of code are to be ignored by the assembler and convert all the labels to numeric values to allow the assembler to convert the source code into a .hex file.

Normally, the macro processor converts all labels to constants except for addresses. The address conversion is carried out by the assembler as it works through the code.

In this article, while I am discussing how the six programming constructs work, what I am really doing is introducing you to a whole new way of programming that executes an application before your application is assembled into a file that the PICMicro can use.

The six constructs I am going to discuss in this article are:
– Equates and Variables
– Defines
– Include Files
– Macros
– Directives
– Conditional Assembly Code

“Equates” are the most basic method of assigning a user friendly label to a constant numeric value. The Statement

   ArbitraryPin EQU 4

will insert the numeric “4” for the string “ArbitraryPin”, each time it is encountered.

I am stressing the idea that a numeric is evaluated and assigned to the label to help differentiate the “equate” from the “Define” or “Variable”.

For example,

   SecondPin EQU ArbitraryPin * 5

will pass the numeric value “20” instead of the string “ArbitraryPin * 5” or even “4 * 5”.

When you look at the Microchip include (“.inc”) files for the different PICMicro devices, you’ll see that all the hardware registers and their labeled bits are equates.

Variables are memory locations that can be defined within an application to help with the processing of operational parameters. For example, if you wanted to calculate the number of cycles between I/O pulses working at 9600 bps in a PICMicro running at 5 MHz, you could use the code:

   variable IOCycles

   IOCycles = (Frequency / 4) / Speed

to calculate the value “130”.

The “variable” probably seems very similar to the “equate” as it saves a numeric value as well, but there is one important difference. “Variables” can be updated multiple times in the application, where as the “equate” can only be set to a value once.

Defines, on the other hand have the string assigned to them after instead of a numeric value. This is useful for setting up string data or multi-parameter data values.

The format for a “define” is:

   #define ThirdPin ArbitraryPin * 10

In the case above, every time “ThirdPin” is encountered, the string “ThirdPin” will be replaced by “ArbitraryPin * 10”. After the replacement is complete, the assembler will then look for any other labels and replace them with the appropriate equates, defines, variables or macros.

“multi-parameter data values” is the term that I use for Defines that have more than one piece of information. The classic example, for the PICMicro, is to use defines to specify complete bit information.

For example, the “GIE” bit of the “INTCON” register could be “defined” as:

   GIE EQU 7               ; Define “GIE” in Intcon
#define GIEBit INTCON, GIE

which allows the GIE bit to be used without the programmer remembering which register the bit is in (and save a bit of typing).

To set the “GIE” bit, with this define, the following statement can be used:

   bsf GIEBit              ; Set the “GIE” Bit

which is easy to code and avoids much of the hassle of having to go back and reference the bit definition to understand what register it is associated with.

For more detail: Macro Techniques using PIC16C711 Microcontroller

Lots of news before getting into this article on Macros and other ways to simplify your application development. The first is, I have finished going through the page proofs of “PC PhD” and it should be ready for going to the printers later this week. This book is about interfacing hardware to the PC along with a focus on MS-DOS software. Windows operation is also presented. The book contains four PICMicro projects that provide some basic interfaces for you to build from. This book is scheduled to be available in late August.

I am happy to announce that “Programming and Customizing the PIC Microcontroller” is finally available with a CD-ROM. The primary reason for going with a CD-ROM with this book is to avoid the problems that some people had with the diskette that came with it. The CD-ROM is identical to the diskette except that the YAP files have been updated with the latest level of code along with datasheets for the parts used in the book and an html interface. If you would like any of these additional files, please let me know and I will provide links to them for you.

The “El Cheapo” programmer has been generating a lot of interest from people and I have made a slight change to the software to better support more PCs. Check out article 4 for Version 2.1 and if you’ve built an El Cheapo – I’m interested in seeing any pictures of what you’ve come up with along with any comments.

For any applications that require PC code, I am thinking of only developing the applications from now on using Boland’s “Turbo C”. The reason for this is very simple, it can be downloaded free of charge from: http://community.borland.com/museum

I will be warming up my skills with “Turbo C” version 2.01. I have previously been using Microsoft “C” Version 3 which hasn’t been available since 1988. When you download the code, note that you will have to load three diskettes with the Compiler, linker, include files and IDE before you can install them.

Lastly, I have a contest – check out the end of this article for your opportunity to win a copy of a special “Programming and Customizing the PIC Microcontroller”.

I have received quite a few messages about my previous article about the bit-banging serial interface routines. These emails were questioning the macro code I was presenting and how advisable is using complex macros like these. The issues that were brought up were the difficulty in debugging the resulting code, understanding what it actually is doing and the loss of control over the application. These concerns are understandable and completely valid – but I think I have come up with a formula that avoids these problems.

This article is a primer on the various features of MPASM (and most other “macro assemblers”) which can be used to make your application development simpler and easier to work through to understand what is happening. As I work through these features, I am presenting the knowledge toward creating macros that can greatly simplify your assembly language programming, both from the development point of view as well as from the debugging and understanding somebody else’s application.

As I wrap up the article, I will go through the development of a “delay” macro that can be used with the Bit-Banging Serial Interface macros I’ve previously presented that takes advantage of all the features I’m going to talk about in this article and point toward a “standard” for developing interface macros which will simplify sharing code between users as well as make the development of PICMicro applications much more efficient.

There are six programming constructs are used prior the assembly or compilation of your application. This takes place in the “Macro Processor”, which are also be known as the “Pre-Processor”. This program’s function is to read through the application source code, bring in all the “included” files and identify which blocks of code are to be ignored by the assembler and convert all the labels to numeric values to allow the assembler to convert the source code into a .hex file.

Normally, the macro processor converts all labels to constants except for addresses. The address conversion is carried out by the assembler as it works through the code.

In this article, while I am discussing how the six programming constructs work, what I am really doing is introducing you to a whole new way of programming that executes an application before your application is assembled into a file that the PICMicro can use.

The six constructs I am going to discuss in this article are:
– Equates and Variables
– Defines
– Include Files
– Macros
– Directives
– Conditional Assembly Code

“Equates” are the most basic method of assigning a user friendly label to a constant numeric value. The Statement

   ArbitraryPin EQU 4

will insert the numeric “4” for the string “ArbitraryPin”, each time it is encountered.

I am stressing the idea that a numeric is evaluated and assigned to the label to help differentiate the “equate” from the “Define” or “Variable”.

For example,

   SecondPin EQU ArbitraryPin * 5

will pass the numeric value “20” instead of the string “ArbitraryPin * 5” or even “4 * 5”.

When you look at the Microchip include (“.inc”) files for the different PICMicro devices, you’ll see that all the hardware registers and their labeled bits are equates.

Variables are memory locations that can be defined within an application to help with the processing of operational parameters. For example, if you wanted to calculate the number of cycles between I/O pulses working at 9600 bps in a PICMicro running at 5 MHz, you could use the code:

   variable IOCycles

   IOCycles = (Frequency / 4) / Speed

to calculate the value “130”.

The “variable” probably seems very similar to the “equate” as it saves a numeric value as well, but there is one important difference. “Variables” can be updated multiple times in the application, where as the “equate” can only be set to a value once.

Defines, on the other hand have the string assigned to them after instead of a numeric value. This is useful for setting up string data or multi-parameter data values.

The format for a “define” is:

   #define ThirdPin ArbitraryPin * 10

In the case above, every time “ThirdPin” is encountered, the string “ThirdPin” will be replaced by “ArbitraryPin * 10”. After the replacement is complete, the assembler will then look for any other labels and replace them with the appropriate equates, defines, variables or macros.

“multi-parameter data values” is the term that I use for Defines that have more than one piece of information. The classic example, for the PICMicro, is to use defines to specify complete bit information.

For example, the “GIE” bit of the “INTCON” register could be “defined” as:

   GIE EQU 7               ; Define “GIE” in Intcon
#define GIEBit INTCON, GIE

which allows the GIE bit to be used without the programmer remembering which register the bit is in (and save a bit of typing).

To set the “GIE” bit, with this define, the following statement can be used:

   bsf GIEBit              ; Set the “GIE” Bit

which is easy to code and avoids much of the hassle of having to go back and reference the bit definition to understand what register it is associated with.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter