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

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.