The purpose of this exercise is to reinforce understanding of the dsPIC® Multiply-Accumulate (MAC) class instructions by examining an example of their use. We will be applying a second order polynomial to the averaged result of summing two arrays by using MAC instructions.
Theory of Operation
Many types of measurement sensors do not provide linear output. An example of this is a typical thermocouple system. Fortunately, these outputs can be linearized by use of standard polynomials. These polynomials and their coefficients are provided by the sensor vendor as well as other sources. For this exercise, we are using the following standard polynomial from the “Thermal Linear Intelligent Sensor PICtail™ Plus Demo Board User Guide”:(1)
$linearOut = ($0x2C3C $* (averagedArrays) -$ 0x0179 $* (averagedArrays)^2)$
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
| Windows|| Linux|| Mac OSX|
Project and Source Files
Open ProjectStart MPLAB® X, then click on the Open Project icon on the main toolbar
Navigate to the the folder where you have saved the downloaded files
Click on the Lab02.X folder.Select Open Project .
Debug ProjectClick on the Debug Project button. This will build and send the program to the simulator. Click on the Halt button. This will stop execution so that we may analyze the code.
What just happened?
We took a pre-configured MPLAB® X project, which included a complete program, (both C and assembly files included) along with the configuration settings for the tools, and compiled the code contained in the project. After compiling the code, we ran it in the simulator that is built into MPLAB X. (The simulator is capable of reproducing almost all of the functions of a PIC® microcontroller.)
Using DSP accumulator-based operations, the code performs the steps listed below:
Populate arrayA with 64 values
arrayA[i] = 0x0100 + (rand() & 0x001F);
In this step we are generating 64 random values between 0x0100 and 0x011F (because we have added a pseudo-random integral number in the range between 0 and 0x001F to our original 0x0100).
Populate arrayB with 64 values
arrayB[i] = 0x0040 + (rand() & 0x000F);
In this step we are generating 64 random values between 0x0040 and 0x004F (because we have added a pseudo-random integral number in the range between 0 and 0x000F to our original 0x0040).
After we have populated our arrays, we need to pass those parameters from C to assembly. C functions pass their parameters to other functions (including assembly functions) through the W registers. It is important to note that the W registers are used in the order that the parameters are passed to the function.
In order to be able to pass both coefficients to just one W register (W0), we created an array of 2 elements called coeffs. As we can see in the code above, the parameter coeffs in the linearize function doesn't need the '&' symbol (which indicates we are passing an address) because C automatically passes the address of the first element inside an array. To access the second coefficient we then just increment W0 as can be seen below:
- Passed to linearize.S:
- W0 : polynomial coefficient a1
- W0+2 : polynomial coefficient a2
- W1 : pointer to averagedArrays
- Returned from linearize.S:
- W2 : pointer to linearOut
$linearOut = ($ 0x2C3C $* (averagedArrays) )- ($ 0x0179 $* (averagedArrays)^2))$
[w2] = ( [w0] * [w1] ) - ([w0+2] * [w1]2)
In the assembly code below we can observe that the first step is to PUSH our registers onto the stack. Then, after configuring accumulators A and B, we move the addresses of coefficients a1 and a2 into W8 and W9 respectively. Lastly, we move the address of averagedArrays into W10 which is a pointer to Y data space.
To calculate the first part of the linearization, we clear Accumulator A, preload W4 with a1, and pre-load W5 with averagedArrays, all in a single instruction. After that, using the MPY instruction, we multiply a1 with averagedArrays and store the result in Accumulator A.
For the second part of the linearization, we first need to square averagedArrays. To do this, we will multiply W5 by W5 since averagedArrays was previously loaded into this register, and store the result in Accumulator B. During that same MPY instruction we are also going to pre-load W4 with the second coefficient, a2. The next step is to store averagedArrays2 into W5.
Lastly, in line 38, we multiply a2 by averagedArrays2 and subtract that from Accumulator A, which previously contained the result of multiplying a1 and averagedArrays together. That result gets stored back into ACCA. Line 42 then stores the result in Accumulator A into W2 which points to the function linearOut.
To be able to see the results of this lab you will have to follow these steps: