Archive for the Tips and Tricks Category

Bending the laws of physics

As a reader pointed out today, when preparing one of the pictures in Chapter 3,  I did use a little trick, I had almost forgotten…

hello.GIF

I did reduce the DELAY constants by a factor of 100 to … speed things up … so to speak. Otherwise the Logic Analyzer window would not be able to contain all the samples required to produce the complete “Hello” message.

I do these things quite a bit when I use the Simulator … Read the rest of this entry »

Inexplicably Working Errata

A few days ago, I received an email from a reader who reported a new errata on page 123 (in the “Learning to fly the PIC24″ book) in the write() function, an example of redirecting the “stdout” output stream.

The function receives a pointer to a buffer containing characters that need to be forwarded to the output device of choice and a counter. A loop is performed to print sequentially the required number of characters, but (here is the bug) I apparently omitted to increment the buffer pointer.  To the reader’s greatest surprise the code example seems to work anyway! How is this possible?

Read the rest of this entry »

Modifying and Testing the code for the new GA1 and GB1 families

I have received a lot of request recently from readers, future PIC24 pilots, concerning the possibility to port the code from the book to the the newest families of PIC24 microcontrollers. The differences are not that big, but I have to be honest, I spent a lot of time recently “playing” with the PIC32 and I just did not have the time to go through the painstaking process of modifying all the code examples and testing them on the new GA1 and GB1 families. I have always encouraged you (my readers) to share the code (in the book and CDROM) and to modify it freely in your applications, but I also encourage you to share any enhancement that could be useful to the community.

Wayne Duquaine, an experienced programmer I had corresponded with a few times in the last few months, has followed this suggestions and recently, after working with an AV16 kit for a few weeks, he has surprised me with a long email where he has listed the code and all the results of his extensive experimentations with the new GA1 and GB1 PIMs and the MPLAB C30 compiler v.3.02.

I decided to post his code and his “scorecard” on the PIC24 web site  in the main download page with a link to his email address (he has agreed to share) with the understanding that he will be willing to provide support answering your emails on a time permitting basis. Wayne is a busy professional that travels often abroad for extensive periods of time. So I am sure you will be patient and you will appreciate any (personal) time he will be willing to dedicate to you.

Thanks Wayne!

Now, has anybody been playing/modifying the code for the PIC24H families recently?

How about  the little guys: GA004 and GA002?

3D Graphics

Using the new double buffered graphic library (see previous post) we can now create simple animations. In chapter 12 we have already seen how to visualize a 2D function using a grid and a bit of perspective (isometric). The next obvious step is to look into 3D objects and how to animate them by moving and rotating them in space. Read the rest of this entry »

Graphic Animation

In the good old times, when studying at the University of Trieste, I particularly enjoyed Rational Mechanics, a required class for any EE Master back then, presented by professor Enzo Tonti. One of the things that were always making his lectures a pleasure (and a challenge) was his constant reference to, and practical use of, personal computers to illustrate visually the concept at hand.

Mind those were the early days of the Apple II, the Commodore C64, the Sinclair Spectrum and for the few lucky (and rich) ones among us the first IBM XT personal computers. The graphic capabilities of those personal computers were primitive to say the least. In fact, a single PIC24F128GA010 has today more program memory than any of those personal computers and the resolution of the video output, using the AV16 board we developed in the book (without any hardware assistance), is actually pretty close. Yet prof. Tonti used to teach us how to develop effective demos using little more than a few lines of BASIC or, I should say, of the “soup” of BASIC dialects that were popular at the time… (Actually prof. Tonti developed a brilliant scheme to identify a sort of minimum common denominator among all those disparate systems and Basic dialects that he called Inter-Basic if I recall correctly… there must be a web site somewhere where all this is documented)…

We simulated graphically the motion of planets around a sun, the complex motion of pendulums of various kinds, we animated objects in 2D and 3D! Key to all those graphic exercises was the ability to alternate fast images on the display to obtain the illusion of motion….

Read the rest of this entry »

Optimizing the graphic library

While working on the porting to the PIC32 of the code developed for the original PIC24 book it occurred to me that there were some obvious optimizations I had yet to explore.

For a starter, the graphic.c library was using several resources including Timer3, the Output Compare 3 module, the Output Compare 4 module and the SPI1 port in addition to one general purpose I/O (RG0 in this case). The OC3 module was used to generate the Horizontal Synchronization pulse portion of the composite video signal, but the module output pin RD2 (active as soon as the module is enabled) was not used. Rather RG0 was “manually” set during the OC3 interrupt service routine and reset during the Timer3 interrupt service routine. This added unnecessary overhead to the application as the OC3 module could easily be configured to do it all by itself…

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 !

Of Explorer 16 boards and 5V LCD displays…

I read with interest today an errata for the Explorer16 demonstration board…

I know, it seems strange to think of an errata for a demonstration board, but it can happen and as in this case, there is a good lesson to learn for all of us, living the dangerous lives of the Embedded Control designers on the edge between 5V legacy technology and the new 3V generations of products.

In order to keep their options as open as possible, the Explorer16 designers arranged for a number of different LCD displays to be used alternatively on the board. Three different layouts were pre-wired and, depending on market conditions (price) and availability, one of several different models of LCD displays (from different manufacturers) will be mounted at any given point in time. Since they are all compatible (or I should say there is a common minimum denominator among all of them) there are virtually no changes required to the demos and code offered with the board.
Interestingly some of these LCD displays are 5V and some are 3V devices. Since most of the PIC24 digital I/O pins are 5V tolerant, theoretically the two technology can be alternated without consequences except for one case that was recently uncovered.

When the Lumex™ LCM-S01602DTR/M module was used (the errata document will teach you how to recognize it) things got more complicated. This 5V LCD module has internal pull-up resistors connected to the 5V rail. When the PIC24 I/Os connected to it (for example PMPA0 which is multiplexed with the AN15 analog input function) is left three stated (very high impedence) it will let 5V get inside the PIC24!

Notice that this will not necessarily damage the part (the pull ups are pretty weak after all), but it will produce a more subtle problem, it will skew the internal analog voltage references and produce a large offset on the A/D module readings for example.

Reading the errata carefully, you will realize how an essential part of the problem is the high impedence of the input pins once three stated, which produces, with the LCD internal pull ups, a partition with a very high output voltage (close to 5V).

Decreasing the impedence, by turning pins to outputs (as you do when you enable the PMP), or by adding pull down resistors (even if of relatively high values) solves the problem as the partition is modified to produce a lower voltage …

The lesson learned: Once more, make sure that when you interface to a 5V device you either:

  • use exclusively pins that are pure digital inputs (5V tolerant)
  • use open drain outputs if you want to swing a 5V signal

Watch out from all pins that are multiplexed with analog functions, even if you have the analog function disabled in your particular configuration.

The pin-out cross reference table published in the previous posting “The Missing Pinout Table” might help you double check…