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.
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.