Monday, July 24, 2017

A GPS adjusted clock

We recently got a new cable box.  The new box has a clock that is a little smaller than the old one, and my wife has a hard time seeing it. We had an LCD clock with huge letters but there just isn't enough light in the place we want it.

So, NE06 GPS modules are getting pretty cheap. Why not get some huge seven segment LEDs and build up a clock based on a GPS?  Turns out large LED displays are fairly hard to come by, and pretty expensive. Plus, all that wiring to all those pins is a pain.

Then it hit me: I have some TFT displays, not huge, but plenty adequate for inch or inch and a half high digits.  If I used the TFT, I wouldn't even have to build the board; I could use my serial graphics terminal board.

First step was to see if I could make huge letters without too much hassle (the largest font I have is only about 3/8" high).  The first whack isn't too bad. It will work, although I might yet do some prettying up of the font.

So, on to the GPS.

I started with my serial terminal code, which may have added more complexity than I needed, but it works.  This code fills a circular buffer with serial interrupts from the GPS and the mainline has to process the contents of the buffer. This makes missing characters less likely but it does mean the mainline is futzing with trying to understand whether there are characters in the buffer and identify the start and end of the GPS data.

Parsing the GPS data is tedious, but not especially difficult.  With the PIC32MX150F128B there is plenty of memory, so I can be pretty aggressive with my code.  Most of the code is the TFT library so the stuff I'm working on is pretty small anyway.

At this point I can get the time and coordinates (which I don't need).  I am displaying all sorts of values which eventually won't be used.

Next up is to get the date, and go through the tedious business of determining whether it is daylight savings or standard time. The correction is trivial but knowing when to apply it is a pain.

Following that I'll get after the PIC's real time clock calendar.  Supposedly the PIC can maintain the time to within a third of a second per month.  I'm not convinced can actually accomplish that, but adjusting it from time to time through the GPS should keep the time always correct.

Since even in the basement with overcast skies I can still see 8 to 11 satellites that might be overkill.  If I have too much trouble with the RTCC I might just skip that, but the next priority is the daylight savings time thing.

The code is currently partial, and pretty rough, but as always I keep everything in git and the current code is in gitlab:

The board hardware is at

And the TFT library code at

They will be updated as time goes on.

OK, so some code cleanup, and slightly larger digits, but still significant work to do:

The digits are actually a tan color, but it doesn't show in the picture.

And yet another little tweak.  After seeing the clock it occurred to me it would be a nice addition to the shack, too, providing it showed UTC as well as local.


 Time to get on to making cases, perhaps.

Sunday, July 16, 2017


It has been a long time since I wrote about the dsPIC-EL-GM. This thing has turned out to be a very successful tool for me.

Over the past couple of years, this has been the platform for dozens of projects.  In some cases the entire project was done on the board plus a shield. In others, it became the prototype for a purpose built board.

The choice of the dsPIC33EV was a good one.  Besides being 5 volts, meaning inexpensive displays work with it, it is fast, available in a variety of memory sizes (32-cheap, up to 256-huge), has a huge range of peripherals, and PPS.

The decision to put connectors for shields on the board turns out to be critical. Most projects need an LCD.  Hand wiring all those pins from the PIC to the display, plus power and programming (which I seemed to always get wrong) tended to present a barrier to starting new projects. With the shields I can just grab a shield and get started, and all the tedious stuff is behind me.

Plus, by having a standard part, standard display/LED/button pinouts, I have built up a number of libraries that make getting a project started easier and faster.

There are a couple of features that turn out not to have been so useful.  The jumper allowing me to use PIC24EV parts hasn't gotten used very much. I was attracted by the low price of some of those parts, but the 33EV32 isn't all that much more expensive, and it has more memory and way more speed.

Also, I had added a connector offset slightly from the shield connectors to allow me to plug in standard perfboards. But the extremely low cost of custom prototyping boards made that less useful. The demise of Radio Shack also shoved that feature into obscurity.  It was a significant advantage to be able to run down to the corner to get a perfboard. Now that it has to be mail order, I just keep an adequate supply of prototyping shields on hand.

I have been tempted to build another PIC-EL for one of the PIC32s. The PIC32MZ series offers blinding speed, crazy big memory, and even floating point. By having a flexible platform, the need for a DIP package is mitigated, and the price of the MZ, while not cheap, isn't really crazy. I had a lot of fun doing a graphics card with the PIC32MX250F128B, so I feel like the PIC32 isn't that alien of an animal.  But that is a project for another day.

I am not trying to sell dsPIC-ELs. However, needed information for building your own is available on gitlab:
  • Gerbers for the dsPIC-EL-GM are available here.
  • And for the proto shields here.
We did kit a few for the high school electronics club, which necessitated creating detailed construction instructions:
  • Build instructions here.
Should you build your own, let me know how you did in the comments below.

Saturday, January 14, 2017

Really ancient history

A long, long time ago, roughly around the time of the invention of the wheel, I did a series of lessons on 8-bit PICs. These lessons were very well received, downloaded thousands of times, and although aimed at hobbyists, many professionals also used them as an introduction to microcontrollers.

Craig Johnson made an experimenter's board to help with these lessons, which was originally kitted by the American QRP Club and later by Kanga U.S. Over the years this board underwent a couple of revisions, and is still available from Craig directly.

I have continued to get questions about these lessons, even though it is over a decade, and the target micro, the PIC16F84A, pretty long in the tooth.   Most of the more recent questions have to do with the development environment, which has changed pretty dramatically since MPLAB 6.

I finally broke down and decided to update a couple of the lessons, those that go into great detail about manipulating MPLAB.
Of course, I based them on MPLAB-X, rather than MPLAB 8.  MPLAB-X was pretty rough during the beta testing, but has evolved into quite a nice IDE. Reviewing these old MPLAB documents reminded me of just how much the environment has improved. I also updated the screenshots and some of the prose for Windows 10.  I don't use Windows much myself, but these days most people do, so it seemed to make sense.

The lessons are both available on gitlab in pdf; Lesson 3 which does some initial exploration of MPLAB-X which is available here, and Lesson 4 which explores the MPLAB-X simulator in some detail here.

 The only other lesson that is tempting is Lesson 11.  It goes into a lot of detail about manipulating a program called FPP, which is no longer available. The replacement, tho, hardly seems worth it.  On MPLAB-X with the PIC-EL III it reduces to "click this button".

So perhaps that will reduce the emails in the future.  In any event, it should make things easier for those who are finally getting around to doing Elmer 160.

Wednesday, December 7, 2016

Serial Graphics Terminal

It has been a while since I last posted here, and there have been a number of fun projects under the bridge.

One of the more fun, with more work still planned, is the development of a serial graphics terminal.

Most PIC projects use an LCD for a display. It is cheap and easy, but it has pretty limited real estate, and it uses a bunch of pins.  For quick little test projects, you often spend more time hooking up the LCD than you do on the rest of the project. Serial LCDs are expensive, and don't solve the problem.

More recently there have been some relatively inexpensive TFTs available. Fairly small 320x480 displays have tended to be among the least expensive, but they take even more pins than the LCDs, and quite a bit more code.

Suppose we could connect one of those things up to the serial interface.  That would allow a competent display with only one or two pins.  If we put a lot of smarts into the terminal, the performance might be decent, and we might even end up with a small software footprint on the host project.

Unfortunately, there are relatively few PICs available in 40 pin DIP, and those that are tend to be pretty slow or have limited memory.  I wanted a fast PIC with gobs of memory in a DIP package that I could prototype easily.

By using the MCP23S17 I/O expander, I could get enough I/O to handle a TFT, and selected the PIC32MX250F128B.  Turns out that the PIC32MX150F128B is the same for this purpose, and cheaper, but at the time I could get the 250 quicker.

So I breadboarded up the thing, mounted on my breadboard dsPIC-EL as a test host, and the thing worked.

I developed first text terminal code, then graphics terminal code, and decided that a custom PCB would be worthwhile.

The serial performance, even though limited by the I/O expanders, is quite good.  I developed a graphics library for the PIC32 that the terminal code builds on. The board is capable of the things you would hope such a terminal would do.

One of the things that is a real pain is drawing decent graphs.  All the piddling around in getting the axes and legends straight is a major hassle, so I decided to put a graphing feature into the PIC32 so that a small host application could draw decent graphs.

The TFT library makes it tempting to do projects directly on the TFT board. Unfortunately, there are only a few pins left (which are brought out to a connector), but a simple logic analyzer was irresistible:

Although the performance is quite good, and for most things, even with a serial connection, speed is not an issue, there is one place where the performance falls down, and it is a common one: drawing a continuously varying graph like a strip chart recorder.

A strange thing about this particular TFT controller is that it takes as many commands (and as much time) to fill a rectangle as it does to draw a pixel. When drawing lines at odd angles, like a graph, each pixel must be drawn individually. In this case, the MCP23S17, even though SPI is running at 10 MHz, simply can't keep up.

A large buffer in the PIC32 mitigates this problem for finite lines, if you want to continuously monitor a variable at some point you overflow the buffer.  Even with a huge buffer, at some point it fills.  I have done some experimenting with XON/XOFF, but this requires more complex code in the host.

The next step will be to prototype something with the PIC32MZ1024EFH100 (the 100 pin part is necessary to get all the pins in a single register). If the prototype can substantially improve performance, then I will look at a PCB for that part.

Again, I have no plans in marketing this, but everything needed is in GitLab. PCBs can be cheaply made and all parts, except the TFT, are available from DigiKey. The TFT is a SainSmart 20-011-918.

Project Components (all gitlab projects except document):

The following is a little video to give an idea of the performance of the serial terminal:

Saturday, March 19, 2016

Getting started with MPLAB-X

 A long, long time ago I switched to MPLAB-X from MPLAB 8.  Since at the time MPLAB-X was in beta, the transition was not all that smooth.  Since then, MPLAB-X has gotten more stable, I've gotten accustomed to it, and the feature set has expanded.  Now it is way better than the old MPLAB ever hoped to be.

An even longer time ago I wrote a series of lessons on programming the PIC16F in assembler language using MPLAB 6 (yeah, that long ago).  Craig Johnson, AA0ZZ, produced a board to go with the lessons, and that board, and its descendants, continue to live on through a Yahoo group.

I never updated the course for the newer PICs or the newer IDE, but occasionally questions come up on that group which prompted me to make a series of screenshots using MPLAB-X.  I considered (for about a millisecond) updating the lesson on MPLAB but got over it.

But here I will provide a little prose for those wanting to get off the ground in MPLAB-X.  I should mention that this assumes you want to do 8-bit assembly programming, not because I think that is a good thing in this day and age, but more because I think that is the audience.  These days I am much more of a fan of the 16 and 32 bit parts in C.  The 8-bit C compilers (and they are legion) are all pretty weak  (see my SDCC post), and I am a fan of C.  But for the 8-bit parts, I still think assembler is the way to go.

When launching MPLAB-X, the opening screen is kind of busy.

Start page, first time

This shows the 'Start Page', a tab (lots of tabs in MPLAB-X) showing some Microchip marketing stuff (actually, some of it quite useful), but most importantly, your recent projects.  If you are launching MPLAB-X for the first time, of course there are no recent projects.

Recent Projects List

In the screen shot this is the first time MPLAB-X is opened, so there are no previous projects.

Clicking the new project icon (the yellow-ish folder icon on the left) launches the new project wizard.

New Project Wizard

The first panel of the wizard is a little confusing at first. What you want is 'Microchip Embedded' on the left and 'Standalone Project' on the right.  The other choices are useful, but not at first.

Clicking Next leads you to a panel to select the processor.

Processor Selection

You may choose a family and then a processor from the drop down menus, but there are a lot of PICs in each family.  it is easier to simply highlight the Device field and type in the name, or part of the name to narrow the search.

The next panel allows you to specify your programmer.

Programming Tool Selection

That is pretty simple business.  The next panel lets you select the toolchain.

Toolchain Selection

The default installation provides the Hi-Tech compiler and MPASM.  To do 16 or 32 bit work you will need to install additional compilers.  They will then show up here.  When you update compilers, previous versions will continue to be shown until you uninstall the older version.

Finally, you get to name your project.

Project Name

The first time, Microchip defaults to an MPLABXProjects folder.  This is a perfectly good place, but if you would prefer your projects somewhere else, click Browse and navigate to your favorite place.  The directory defaults to the last location you used, so there is no penalty for disagreeing with Microchip.

When you click Finish, your project will be created, however it will have no source files.

Creating new file

Right-clicking Source files on the left will bring up a context menu that allows you to specify whether you want to create a new file or add a file you have previously created.  Rolling over New will launch a sub-menu allowing you to specify the type of file you wish to create.

Additional File types

Should you choose Other, you get a dialog giving you additional choices.

New File Name

Clicking Next will allow you to finally give the new file a name.  MPLAB-X will provide the extension appropriate to the file.

If you are following the Elmer 160 lessons, the early lessons use absolute code, but MPASM defaults to relocatable code.  To specify absolute:

Project Properties Button
first click the Project Properties button alongside the Dashboard.  Then highlight mpasm (Global Options) and tick the Build in absolute mode check box.  Click Apply then OK.

mpasmx Options Selection

Double-clicking the file name in the left pane will open the file in the right pane.  You may now edit the file.  If the file has not been saved, its name in the tab will be bold.  For most files, the source code will be highlighted in color.  

Editing the code

When you think you are ready to build, clicking on the Build Main Project button (the hammer) will assemble and link your project.

Build Main Project Button

You will see all sorts of messages fly by in the lower right pane.  What is important is that at the end of those messages,  you see BUILD SUCCESSFUL in green.

Build Success

During the build there may be a lot of messages produced.  If you see the green BUILD SUCCESSFUL you can ignore them.  But if there was an error, you will get a red BUILD FAILED message.  In this case you need to scroll that window up and find the first message that isn't black.  Sometimes this can be quite a way up, but when something fails it can often cause other things to fail.  It is important to find the first message, because if you try to fix the later messages first, you may end up breaking more things.  Be sure to find the first message and fix that.  Then review the later messages and see whether they still make sense.

Build error

Once you have a successful build, you can program your target project by clicking the Make and Program Device button (with the green downward pointing arrow).

Make and Program Device Button

Normally this will go fairly quickly, but depending on what you have done (or not done) previously, it can take a while.  The first time you program a particular family, MPLAB has to download code into the programmer.  This can be pretty slow.  If you haven't changed device families, it will go quite quickly when you program the same device again.  Programming some PIC32 devices with a PICkit can also be quite slow.  If you are working with these devices you might consider an ICD instead which is significantly faster.  Ditto with very large programs.

Programming Success

Once again, a number of messages will go flashing by in the lower right pane.  When you see Programming/Verify complete your project will be ready to test on the target hardware.  And again, if you see red, there is a problem.

Programming Failure

I have an old YouTube showing this process.  In spite of all the steps it can be quite quick.  The video is for a dsPIC on a much older version of MPLAB, but the steps are all the same, even if the screens look a little different.

WARNING: There are lots and lots of tabs in MPLAB-X, and almost every tab can be dragged almost anywhere within the window.  If you happen to drag a tab off the window it will become un-docked and separate from the main window.  You can re-dock it from a menu on the small window.  Should it land on the window border, it will be hidden and marked with a tiny icon on that border.  It can be opened from that icon.

Right-click is your friend.

There are a lot of features in MPLAB-X.  Later posts will explain some of these features.

Friday, March 18, 2016

MPLAB Xpress

Recently Microchip introduced MPLAB Xpress.  This is basically MPLAB-X in your web browser.  It looks for all the world like MPLAB-X except you don't need to install anything on your PC.  Just point your browser to the IDE and have at it.

MPLAB Xpress in Internet Explorer

At the current time it looks as if it only supports 8-bit PICs.  Kind of a pity because, IMO, the 8 bit C compiler is trash.  The 16 and 32 bit compilers are ports of gcc, the benchmark for C compilers.  But still, if you like the 8-bit stuff, it is pretty neat.  You can do MPASM in MPLAB Xpress although getting started isn't real obvious.

If you want to use your PICkit online, you do need to install a bit of software.  There is a USB bridge to connect your PICkit to the online IDE.

If you don't have your PICkit connected, the hex file can be downloaded.   Otherwise, you can keep your entire project "in the cloud".  Actually, kinda handy if you want to do development on a tablet or across a number of devices.

To push this model, Microchip offered a free evaluation board.  It is supposed to be available on MicrochipDirect soon, but I couldn't find it today.  The target price was supposed to be quite low, too.

MPLAB Xpress Evaluation Board

This is a way cool little board.  It has a microUSB connector, and when you connect it to your PC it presents itself as a USB drive.  To program the PIC, simply drag the hex file to the USB drive.

The board has a PIC16F18855, a pot, a button, and a few LEDs.  All the pins are brought out to the edge of the board although not all the connectors are populated.  There are a number of demo programs available on the Xpress IDE.

The PIC16F18855 is a pretty impressive little part, even more impressive when you check the Digikey price.

The part includes:
  • 8K words of program memory
  • 1K bytes of SRAM (pretty fat for a 16F)
  • 256 bytes EEPROM
  • 24 channel A/D
  • A DAC
  • A CRC generator
  • 3 waveform generators
  • A numerically controlled oscillator
  • 4 configurable logic cells
  • A data signal modulator
  • Peripheral Pin Select (a huge win, IMO)
  • And of course timers, comparators, PWM, I2C, SPI all the normal stuff
The evaluation board contains the 28 pin PIC in a tiny (4x4mm) UQFN package.  Curiously, the PIC18LF25K50 used for programming is in a larger QFN (6x6mm) package.  The PIC is available in a 28 pin SPDIP and its bigger brother in a 40 pin PDIP if you want to move your development to a more hobbyist-friendly environment.

Digikey currently shows the SPDIP version for under two bucks, and the 40 pin PIC16F18875 for $2.04.  Pretty impressive for all those peripherals.

All in all, a pretty cool way for someone to get started in PIC development without installing anything.

Monday, March 7, 2016

More on the dsPIC-EL

OK, so a few months down the road we can give a progress report on the dsPIC-EL and the high school club.

Solderless Breadboard
First of all, the members of the high school Electronics and Wireless Communications Club are wicked smart.  These kids are just unbelievable.  Before building the dsPIC-EL boards we gave them a handful of parts, a solderless breadboard, a schematic, and asked them to make the LED blink.  Your standard first PIC project, but the guidance they got was very limited.  All of the students had little problem accomplishing the task (for that one we used the PIC24FV16KM202 because at the time we didn't have enough of an inventory of dsPICs for all the club members.)

We felt it was useful to start with a "from scratch" sort of build so the club members understood that they don't need to buy an Arduino or some prepared kit to do something interesting.  They are prefectly capable of doing whatever they want.

After they had the LED blinking we gave them a few more LEDs and resistors and told them to play.  Interesting results, and I think it is useful for them to go off with little guidance and explore on their own.

They then built the dsPIC-EL.  The provided dsPIC33EV32GM002 contained an acceptance test so they could see right away that they were successful. (Refer to the construction instructions link on the previous post.) A provided library for the LCD allowed the club members to experiment with the buttons and LEDs and display what they were doing.
Shield with DS1821 annotated

Next up was to add sensing. The ultimate goal is to do a high altitude balloon launch in the spring.  This balloon is to carry a payload containing sensors for those measurements of interest to the club members.  A shield was built for the dsPIC-EL containing a DS1821 digital thermostat.  Again, a library was provided to ease the handling of the Dallas One Wire protocol.

LDR voltage divider
For sensing, analog input will be a must, so next the students added a light dependent resistor to their shield and learned how to read voltage.

The final, sort of "directed" experiment was to add a serial EEPROM to the shield.  In the spring launch, this will be needed to store the measurements for analysis after the payload is recovered.

The club members then split into small teams of two or three members.  One team was responsible for the control processor, one for the storage processor, and the remainder for each sensor module.  The plan is for each sensor to take commands from the control processor, report the measurement, and then have the storage processor store the result.  K8VFO guided the teams in preparing functional specifications and then designs for each of the modules.  Students did Internet research to select sensors and are currently working on firmware for each module.

Initial testing and development is being done on the dsPIC-EL, but the launch payload will use a dsPIC-EL for the control processor, and purpose-built shields for the measurement modules.

K8VFO is also walking the club members through the process of converting their schematics to a PCB layout, sending the design out to manufacture, and testing the resulting board.

Of course, some of our stronger club members will be graduating about the time we do the launch.  We hope the remaining members will return in the fall when we plan to build on this year's progress and address telemetry from the balloon to an earth station.

As I said, these kids are wicked smart.