Archive for the MPLAB Category

Changing habits… and dreaming a bit

It has been a hot month of August as I visited back home in Italy and even here in Germany, I mean… everything is relative!

Returning home after a few days off is typically a moment of (small) revelations. Things that had gone unnoticed, in the day to day routine, can stand out when the background changes. Yesterday morning for example, the satellite TV receiver was looking at me sad and lonely from the back of the living room!  It had been months since I had turned it on last.  Actually I just realized that the only user of the TV screen in my house for the last several months has been my 20 months old son. Not much of a user either, as he has only been watching Bamby II (in 15 minutes installments) before going to bed each night …

What happened to the news? I don’t trust the TV anymore… I check things on the internet.

What happened to the occasional movies? I hate the frequent commercial interruptions (even if less frequent here than in the US) so I mostly watch them off DVDs (on my wife Mac mostly) or streaming directly from the “media server” ( a Linux machine by the way…)

Speaking of PCs, I turned my attention to the “desktop” which is a Windows XP machine we keep in the office room. That one has been off for several months now as well. Last I remember using it was because of a printing job (we are not printing that much either… apparently, good for the trees). The cheap inkjet printer I bought last year (for Euro 59) is not natively supported by my Linux distro (Ubuntu), so I attached it to the XP machine making it probably the last and only useful application for it!?

All the action is now on the Linux media server and my (and my wife’s) laptop(s). While my wife is already on OS X, which covers pretty much all her needs, I am still running on a (company issue) crancky IBM laptop under Windows XP.

Could that last installation of Windows on my lap be replaced by something else one of these days ?

There are three critical systems I use every day on that laptop: Office, Outlook and MPLAB.

The copy of Microsoft Office I use is so old and basic that I am pretty sure it could be easily and compatibly replaced by OpenOffice on Linux or similar package on the Mac.

The version of Outlook I am using is very old as well, and it was not a long time ago when we were using a completely different system. I am not sure why we did the switch to MSFT as I did not notice any advantage in the transition, at least there was none from a user perspective…

But it is truly MPLAB that seems to be last bastion of Windowsness…

Now I heard rumors that that might be changing… well’ see… perhaps next year, I will be writing a similar post and realize that I am now completely TV and Windows free and working on my shiny brand new and ultra-thin Mac Air !?

Loosing your Head(er)

Contrary to what most MPLAB users believe, the “Headers” folder in MPLAB  Project window is not affecting the compiler selection and access to .h files. It is there merely to let you access them quickly and open them in the editor window.
If you want to make sure that the compiler finds the .h files you are using you have to make sure to do one of two things:
A) Put the .h files (or copies) in the main project directory and use:
#include “xxx.h” (note the use of quotes).
or
B) Make sure to add the directory(-ies) where the .h files are to the “include search path” list in the Build Options for Project Dialog Box. Now you can use #include <xx.h> (note the angled brackets used as quotes)

Method A) is used for simplicity in the first half of the book.

Method B) is used in the second half of the book as we start building more advanced modules/libraries. The steps to add the include directory path to the Build Options for Project Dialog box are described in Chapter 10 (Day 10) page 238 and following…

Don’t loose your head(er)…

Reporting Memory Usage

I think I can say we all love the little Memory Usage gauges introduced in MPLAB a couple of years ago.. or was it more?

Memory Usage Gauges

When working in assembly with an 8-bit microcontroller and a couple of Kbytes of FLASH memory they definitely do their job, but when using the PIC24 or PIC32 type of devices, the memory space grows into the hundreds of Kbytes and things get … well, more complicated!

You start including a stack here (USB?) and a library there (Advanced Graphics), the compiler throws in a couple library modules of its own (math libs, stdio…) and both FLASH and RAM spaces become a little more crowded. It does not take long before we start wondering who is using up more memory, and why… the little memory gauges just don’t cut it anymore.

The experts will tell you right away that the sensible thing to do is to turn on the “map” option of the linker, (Project Build Options, Linker Tab, Select Diagnostics pane, then Check Create Map file) and inspect the resulting .map file in the editor window.  But the text file generated is a pretty complex (and long) document and browsing through it in search of clues about what happened to our program and data memory can be quite hard work.

Fortunately with the latest version 1.05 of MPLAB C32 compiler there is a new  option for the linker to produce a detailed summary of your project memory usage: –report-mem. Read the rest of this entry »

Visualizing Data - A taste of Visual C#

Our brain is wired for image processing and all things visual just reach deep in our mind faster! If there is one moment when I just cannot seem to ever get enough of a “view”, that’s when I am debugging code. The Watch windows, the local variables windows, the stack window are not what I am talking about. The Logic Analyzer window (in MPLAB SIM) and the DCMI (with simulator and emulators) are barely starting to scratch the surface, when I need to debug some complex code, I am willing to use literally everything at my disposal, beyond MPLAB, using hardware (scopes and logic analyzers) but also additional software.

Read the rest of this entry »

Visualizing Data - DMCI

I guess one of the advantages of getting older (if there is any) is that I am becoming somewhat more patient and I am learning to resist the temptation of trying out on the hardware target every single line of new code I write. I am finding myself using the simulator (MPLAB SIM) more and more every day to test thoroughly sections of my code before throwing it out to the ICD. The result is overall less development cycles (code, program (ICD), run, crash, … repeat), more bugs caught early and more productivity.

Read the rest of this entry »

Look Mom No … Linker Script

There are so many new features being added to MPLAB at each major (and minor) new release that I struggle to keep up. Just a couple of days ago for example, I was discussing with a colleague how it would be nice to have the MPLAB New Project Wizard to use a default linker script in every new project … so that in most standard cases the repetitive  task of adding one (navigating through the many subdirectories inside Program Files) can be spared and … I got stopped mid-sentence and promptly reminded that this feature was already there! In fact since last November, with the introduction of version 8.00, MPLAB has been quietly offering a default linker script when the MPLAB C30 toolsuite is selected.

Thanks !

Watching Expressions

MPLAB has become such a large application, or I should better say “group” of applications, and it keeps evolving so fast that one can hardly keep up with the pace of monthly (when it is not weekly) updates. Typically before rushing to install a new version I scan quickly through the readme files to see if there is anything new that I could use immediately, otherwise I tend to postpone the update to let it … settle a bit, if you know what I mean.

With MPLAB 8.00 things were different. The PIC32 had just been announced and this was the first new version of MPLAB to openly support it. I was too curious to pass the opportunity and I did the install without paying much attention to what other features had been added.  Turns out, I made a mistake because I failed to notice a powerful update to the Watch window capabilities!

Now you can inspect/watch:

  • Aspecific element of an array: ar[12]
  • An object pointed to by a pointer: *ptr
  • An element of a structure/union: str.mbr
  • An element of a structure/union via a pointer: p->mbr
  • Perform simple math: vrbl-1
  • Use constants defined in the program in all of the above: ar[M_SIZE-1]

Just type these simple expressions directly in the watch window in the Symbol Name column

Watch Expressions

it will work seamlessly allowing you to get a better picture of your… bugs!

Updating Chapter 4

There are no changes required to the code in chapter 4 after the upgrade to MPLAB C30 v.3.02. But there are some changes in MPLAB 8.00 behavior that will have you puzzled when looking for the subroutines (library modules) used by the compiler for the long long (64-bit) integer multiplications.

In fact, the listing in the Disassembly window will not show anymore the long long multiplication routine muldi3.c as MPLAB 7.40 and older versions did. In a way the Disassembly listing has been cleaned up a bit, but don’t despair. If you follow the next few steps you’ll be able to find the missing routine anyway:

  1. Take note of the subroutine address, the number after the rcall mnemonic:
    (00280 in my case)
    long long mul
  2. Open the Program Memory Window and make sure to select the Symbolic view
    (use the tabs at the bottom left corner of the window)
  3. Press CTRL+F to open the search dialog box and type the address (00280) or simply scroll down the window until you reach the address.
    00280
  4. There you will recognize the label __muldi3 marking the entry point of the 64-bit multiplication routine used by the compiler. (That’s pretty dry reading, isn’t it?)

Looking at the exercises proposed in Chapter 4:

  1. Using Timer2 to self time the number of cycles used by each arithmetic operation is just a good idea. Exercise 4-1
    Most/all operations will fall withing the 16-bit resolution of the timer (within 65,536 cycles) so that it is easy to obtain all the cycle counts in a single watch window shot:
    multiplications
    Notice how some of the values changed a little bit since version 1.30 of the MPLAB C30 compiler?
  2. Checking all the divisions at once is just as simple now: Excercise 4-2
    Here are the results:
    divisions
    Do you notice anything interesting? Who said float math has to be always slower than integer?
  3. Trigonometric operations (sin()) is performed using polynomial approximations and therefore multiple additions and multiplications. This has to be “hard”… Excercise 4-3
    trig
    Looking at these numbers you can almost guess the order of the polynomials…
  4. Complex math as well is composed of multiple operations of the fundamental type (integer/float): Excercise 4-4
    complex

Setting the configuration bits

Speaking of configuration bits, in the book I have made clear from the beginning that I would expect the reader to set them always in a specific configuration chosen for debugging with the Explorer16 board. In fact the checklists titled “Device Configuration for Explorer16 demonstration board” guides the reader through a few steps accessing the “Configure” menu and opening the Configuration Bits dialog box. This dialog box in particular has been enhanced in the latest versions of MPLAB to include one new checkbox at the top titled: “Configuration Bits set in Code“. In order to select the configuration manually, as suggested in the book, this checkbox must be unchecked!

On the other side, with the checkbox checked, we can take advantage of this new feature and make our configuration part of the source code. Before we delve into the details of the syntax to be used, we must consider the pros and cons that come with it, let’s start with the pros:

  1. Documentation: when publishing the code, the configuration is automatically included
  2. Permanent association: code and configuration are now permanently joined

On the other side the cons include:

  1. Device dependence: the configuration is strictly related to the device chosen
  2. Cryptic: the syntax as we well see shortly is far from readable or self explanatory
  3. Repetitive: we would need to include the same lines of code at the top of each project in the book or hide them in an include file ad hoc (hence voiding the number 1 pro: documentation)

My preference would be for the first chapters to maintain the use of the manual configuration, which is persistent in MPLAB as we create new projects and more readable. I ‘d suggest a transition to the configuration in code for the later chapters and the more advanced projects.

PIC24 Device Configuration in Code

The MPLAB C30 compiler does not offer any special extension to take care of the device configuration bits, but rather leaves the job to the linker. After all, it is a matter of setting a few bits (initializing) a couple of special locations in FLASH memory, a task easily accomplished by a few pre-processor macros (and the linker).

You will find them inside the device include file “p24fj128ga010.h” almost at the very end of the long long file, including examples of their use and some useful hints on where to place them:

/*
** Only one invocation of CONFIG1 should appear in a project,
** at the top of a C source file (outside of any function).
**
** The following constants can be used to set CONFIG1.
** Multiple options may be combined, as shown:
**
** _CONFIG1( OPT1_ON & OPT2_OFF & OPT3_PLL )

*/

To match the device configuration proposed in the book’s checklists, place the following two lines of code at the top of the main source code of each project (but after the #include <p24…> statement):

_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx2 & FWDTEN_OFF)

_CONFIG2( FNOSC_PRIPLL & FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS)

Now you understand what I meant above when I mentioned the code to be a bit cryptic…

MPLAB C30 v 3.02

Part of the challenge of writing a book based on new tools and products is to make sure that the content remains valid and relevant as they tend to evolve quickly in the months and years after the book release. In our case, both the PIC24 product line and most of the tools surrounding it have changed considerably since the book release in March. This is without even considering the fact that most of the actual book content was written on average another 12 months before publication. Think of all the new PIC24 models introduced in 2007 (40+), the new peripherals introduced (think of the Peripheral Pin Select) and the changes in the MPLAB IDE , the MPLAB SIM simulator stimulus interface etc… The recent release of version 3.02 of the MPLAB C30 compiler represents a tipping point for the projects in the book…

We need to go back chapter by chapter and re-check all the projects to provide the new readers a better cleaner experience. In fact we can do this together…

Let’s start. Here are my notes for Chapter 1: The First Flight

1- I had to re-build the workspace. The latest version of MPLAB uses a different window manager. The docking mechanism has changed in particular… so close all the windows (Including the Project and the Output window) and then re-open them again. Reposition the windows as you prefer.

2- Some readers have asked for separate files to be offered for each phase of experimentation in the project. So I have now saved 4 separate files:

  1. Hello1. ccontains the very first experiment with PORTA.
  2. Hello2.c contains the fix to make PORTA output pins work.
  3. Hello3.c contains the test of PORTB.
  4. Hello4.c contains the fix required to re-gain control of the analog pins on PORTB.

NOTE: A common problem reported by many readers (who did not use the checklists) is the lack of control of 4 pins of PORTA. They are in particular pin RA0, RA1, RA4 and RA5 and they seem to misbehave no matter what they do to the TRISA and PORTA registers. It is the JTAG port that takes control of those pins when enabled. Since we don’t use it, at least none of the Microchip development tools does, disable it setting the appropriate configuration bit.


|