The purpose of this exercise is to reinforce understanding of the dsPIC accumulators by examining an example of their use. We will be adding two arrays of numbers and calculating the scalar average of their resulting sum.

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C Compiler

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files



Open Project

Start MPLAB® X, then click on the Open Project Main_Open_Project.png icon on the main toolbar

Navigate to the the folder where you have saved the downloaded files

Click on the Lab01.X folder.

Select Open Project OpenProjectButton.png.


Debug Project

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator. Click on the Halt Debug_Pause.png button. This will stop execution so that we may examine the arrays and their average.


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. As we can see from the code above, the first parameter in our average function is arrayA which means that W0 would contain the address of arrayA. The second parameter is arrayB which then goes to W1, and so on.

  • Pass to average.s
      • W0 : address of arrayA[]
      • W1 : address of arrayB[]
      • W2 : BUFFER_SIZE = 64

In the assembly code below we can observe that the first step is to PUSH our accumulators/registers onto the stack. Then, after configuring and clearing the accumulators, we see our DO loop starts with the first element of arrayA[]. We proceed to sum that first value into Accumulator A, ACCA; subsequently, we post increment W0 to read the next value of arrayA[] in the following pass of the loop. We continue adding values into Accumulator A in this manner until we finish with all the elements. We do the same thing for arrayB[], except this time we sum its values into Accumulator B, ACCB. We execute this loop 64 times to finally add all the elements in the arrays.


To find the average of the sum of the arrays, we add ACCA and ACCB and save the result in ACCA.

\begin{align} averagedArrays = 1/N ( \sum_{j=0}^{N-1} arrayA[j] + \sum_{j=0}^{N-1} arrayB[j] ) \end{align}
\begin{align} W0 = 1/W2 ( \sum_{j=0}^{W2-1}W0[j] + \sum_{j=0}^{W2-1} W1[j] ) \end{align}

After that, we can right shift A by #6 which is the same thing as dividing by 64 and store that result into the W0 register.


Lastly, we save the 40-bit accumulator into 16-bit result averagedArrays. From the main.c file, we call the average function created in assembly and observe the following results:


To be able to see the results of this lab you will have to follow these steps:


Open the Watches Window

After starting the debug session Main_Debug_Project.png and halting it Debug_Pause.png, you will have to open the Watches Window by going to Window > Debugging > Watches or by pressing Alt+Shift+2.


Inside the Watches window double click on where it says <Enter new watch>, type in arrayA and hit Enter. This will add arrayA to our list of Watches. Do the same thing for arrayB and averagedArrays.



Place a breakpoint on line 20 in the main.c file. To set a breakpoint all you have to do is click on the line number where you wish to place the breakpoint.



Click on the Debug_Continue.png button to continue running the debug session. You will notice the program will stop at line 20. At this point, the averagedArrays watch will display the final result. You can also click on the plus signs next to arrayA and arrayB to see all their elements. (Keep in mind results will be different every time since we are using the rand function.)

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.