MJB logo

Resources for Embedded Firmware Development

Embedded C Coding Standard
Four-wire Keypad Interface to Micro-controller
Graphics Function Library for Monochrome LCD Module
using KS0108 or ST7920 controller chip (128x64 pixels)
Generalised User Interface for Embedded Applications
using graphics LCD module and keypad (or push-buttons)
Digital Audio (PCM) Sound Effects Software
for embedded micro-controller applications (PIC32 target)
PIC32MX Application Framework, including...
* USB Host Mass Storage (Flash Drive), * FAT32 File System
* Command-Line Interpreter, * Real-Time Kernel (RTCC)
AT91SAM7 Application Framework, including...
* USB Device Serial Comm's (USB CDC "Virtual UART"),
* Command-Line Interpreter, * Resident Debug Facility
ATmega AVR 'Training Kit' and example code
using 2-line x 16-char LCD, 4x4 keypad, 7-segment LED digit, etc.
USB Test & Measurement Class device firmware...
USBTMC/USB488 Reference Design (AT91SAM7 target)
Electronic Musical Instrument Project
Real-time high-quality sound synthesis using a PIC32MX

PIC32 synth testing


Your feedback, comments and enquiries are welcome... send email to:

Embedded C Coding Standard

Source code should be well structured and it should look elegant. It should be easy to understand, easy to maintain and easy to extend. As far as practical, C code should be "self-documenting" so as to avoid excessive comments. A Coding Standard is a set of guidelines for software developers, designed to help meet these objectives and to ensure consistency of coding style within an organisation. There are many C coding standards in existence, but none that I have found ideal for embedded micro-controller firmware applications. So I wrote my own. It has evolved over many years of experience in professional firmware development. I have tried not to make it overly prescriptive, because developers need some freedom of expression.

Download MJB's Embedded C Coding Standard (pdf)

Four-wire Keypad Interface to Micro-controller

This article describes a technique to connect a 16-key (4 x 4) or larger size keypad to a micro-controller using only four wires. Two of the four I/O pins used on the MCU need to be configurable as either digital or analog inputs. All four pins need to be configurable as either digital inputs or outputs. The keypad is wired as a 4 x 4 matrix, i.e. 4 rows by 4 columns, with 680 ohm resistors wired between each of the rows and between each of the columns... more...

Download Software (zip)

Graphics Function Library for Monochrome LCD Module

This software package is designed for monochrome (1 bit-per-pixel) graphics LCD modules with a display format of 128 x 64 pixels. The package includes device drivers for GLCD modules based on the KS0108 controller chipset or ST7920 chip. Suitable LCD modules are sold by Adafruit and Sparkfun. Compatible LCD modules may be available at lower cost via eBay, AliExpress, etc.

The package is not a hack of the Arduino "GLDC" library! My software was developed from scratch to be simpler, easier to read, more efficient and to optimise write speeds for text and bitmap image rendering. The drivers are designed for Microchip PIC devices, but hardware dependencies are confined to the low-level driver header files, to make the code easily portable to other MCU device types, e.g. ATmega.

Functions provided in the LCD Graphics library:

void LCD_Init(void); // LCD controller initialisation
void LCD_ClearScreen(void);
// Clear LCD GDRAM and MCU RAM buffers
void LCD_Mode(uint8 mode);
// Set pixel write mode (set, clear, flip)

void LCD_PosXY(uint16 x, uint16 y);
// Set graphics cursor position to (x, y)
uint16 LCD_GetX(void);
// Get graphics cursor pos x-coord
uint16 LCD_GetY(void);
// Get graphics cursor pos y-coord

void LCD_BlockFill(uint16 w, uint16 h);
// Fill area, w x h pixels, at cursor (x, y)
uint8 LCD_PutImage(uint8 *image, uint16 w, uint16 h);
// Show bitmap image at (x, y)
uint16 *LCD_ScreenCapture();
// Return a pointer to the screen buffer

void LCD_SetFont(uint8 font_ID);
// Set font for char or text display
void LCD_PutChar(uint8 uc);
// Show ASCII char at (x, y)
void LCD_PutText(uint8 *str);
// Show text string at (x, y)

// These macros draw solid rectangular objects at the current cursor position (x, y)
#define LCD_PutPixel() LCD_BlockFill(1, 1)
#define LCD_DrawBar(w, h) LCD_BlockFill(w, h)
#define LCD_DrawLineHoriz(len) LCD_BlockFill(len, 1)
#define LCD_DrawLineVert(len) LCD_BlockFill(1, len)

The application includes support for Microchip's "Graphics Resource Converter" (GRC) utility. Bitmap image definitions generated by GRC can be transformed into a bitmap format suitable for display using the function LCD_PutImage().

The download package includes all project files required for development using Microchip MPLAB'X IDE. 

Current firmware release: v1.3.00  (June 2016) -- adds 8pt proportional-spaced font.
Added KS0108 device drivers (Oct. 2018)

Download LCD Graphics Package (zip)


Generalised User Interface for Embedded Applications
... using a graphics LCD module and keypad (or touch-panel)

This article is concerned with firmware design and implementation for microcontroller-based devices incorporating a "local" user interface (front panel) comprising an LCD screen and keypad (or a number of push-buttons). The technique can be extended to build a graphical user interface (GUI) comprising an LCD screen with touch-panel.

Typical applications will have many "screens" to be presented and, for each screen, a selection of user options for various actions, most of which will result in a switch to a different screen or a change in displayed information on the current screen. For all but the most trivial of applications, navigation from one screen to the next presents a challenge to the developer and can easily become a convoluted mess if not handled methodically...

Download article (pdf)

Digital Audio (PCM) Sound Effects Software

Scope Trace - AM sine with ramp

Appealing sound effects are generally too complex to be generated in real time by the target micro-controller of an embedded system. Therefore, sound effects may be synthesized by a purpose-built application and stored as a sequence of PCM samples in a data file. Saved sound effects can be loaded into flash memory in the target embedded system for fast access by an audio player function. The storage medium is typically a "serial data flash" (SDF) memory device interfaced to the MCU via SPI. However, the micro-controller’s internal flash program memory can also hold sound FX data.

PCM sound effects data files can also be produced from standard audio file formats such as WAV, MP3, etc, using "Audacity" -- a free open-source audio editor application. There is a plethora of sound effects available for download on the internet. Audacity can also be used to record sounds using a microphone, CD player, sound synthesizer or musical instrument, etc. Once loaded into Audacity, audio data can be re-sampled at the required sample rate and then exported in “RAW” data format (stripped of header information) compatible with the target embedded player.

The software described in this article comprises a simple sound FX synthesizer application, an audio player function and various utilities to transfer sound FX data files between the SDF memory device and a removable mass-storage device, e.g. a USB flash drive. The software was originally developed for Microchip’s PIC32MX (32-bit) micro-controller family, but could be easily customized to suit others... more...

The Digital Audio software is included in the PIC32MX "Application Framework" package (below).

PIC32MX Application Framework

The "PIC32MX Application Framework" is a suite of example applications comprising a command-line user interface (CLI), USB mass storage (Flash Drive) support, FAT32 file system with file management commands, real-time kernel with task scheduler, hardware RTCC support, digital audio (PCM sound effects), serial data flash memory support (SST25VF016), I2C EEPROM (24LCxx) and more.

The package includes device drivers for on-chip peripherals, e.g. UARTs (interrupt or polled Rx data, queued Tx data), I2C, SPI, timers, OCx (PWM), etc, plus drivers for external devices, e.g. I2C EEPROM (24LCxx), serial data flash (SST25VF016), LCD modules (ST7920, KS0108), wireless comm's module (MRF24J40MA), etc.

The example applications provide a reliable framework upon which to develop different applications. Include the code modules you need in your project; remove those not required. It is highly recommended to use the project directory structure noted in the download package, although project folders may be re-named without upsetting MPLAB'X.

The hardware platform I chose for development is a low-cost "PIC32-MX460" development board from Olimex (www.olimex.com). The MCU is a PIC32MX460F512L clocked at 80MHz. The board has a prototyping area which I used to fit connectors for I/O expansion, a numeric keypad and graphics LCD module, as shown in the picture below. I shortened the board so it would fit in my chosen enclosure. I added an ICSP header (6-pin SIL, right-angle, 0.1" pitch) accessible through a cut-out in the enclosure, for more convenient connection of a programming/debugging tool (PICkit-3, etc). The tiny (useless) LCD screen was removed and a battery-backed I2C real-time clock module (MCP79410, Microchip part # AC164140) fitted in the space left vacant. The space provided for an optional RF comm's module was used instead for adding a 2MB SPI flash memory chip (SST25VF016), which I used to store PCM audio sound clips.

Olimex PIC32MX460 board mod's

The download package includes all project files required for development using Microchip MPLAB'X IDE. 

Download PIC32MX Projects (zip)

All dependencies on Microchip's legacy peripheral library (PLIB) have been removed from the framework code, except for modules extracted from Microchip's Library of Applications (MLA), i.e. the USB host mass-storage stack and the MDD File System. Apart from those modules, MPLAB XC32 compiler version 1.34 and later may be used without problem. If your project needs a Microchip MLA module, you can use XC32 v1.33, or you can try to remove the PLIB dependencies by importing code from the relevant PLIB source file(s). Good luck!

AT91SAM7S Firmware Framework

The "SAM7 Firmware Framework" is a complete example application comprising target-resident debugging facilities and peripheral I/O API function library. It runs on Atmel's AT91SAM7S-EK evaluation board and compatible hardware platforms.
The example application provides a reliable framework upon which to develop other applications.

The embedded USB CDC "Virtual UART" communications stack (developed by MJB) is well structured, richly annotated, and much simpler to apply than Atmel's example "USB framework" code... more...

Download AT91SAM7S Firmware Framework (zip)

ATmega AVR 'Training Kit' with assembler source code

ATmega16 dev board LCD and keypad

ATmega16 Development Board (Futurlec)
with I/O expansion components fitted in prototyping area

2-line x 16-char LCD panel
and 4x4 keypad

This documentation package describes the assembly and usage of an ATmega micro-controller "training kit" intended for students learning electronics technology and/or embedded programming. The kit is designed around a pre-built development board based on Atmel's ATmega16 AVR micro-controller. The board is available from Futurlec

With the addition of an LCD module and 4x4 keypad, plus a few sundry components, a complete ATmega-AVR training setup can be assembled with little effort and at low cost. The updated download package includes AVR assembler source code examples.

Note: The ATmega16 prototyping board is not available from this site. The kit is assembled from parts obtained from various online suppliers. A parts list with suggested suppliers (for delivery in Australia) is included in the doc package.

Download AVR Training Kit doc package (v2)

ATmega AVR 'Application Framework' -- C source code

This example C code makes a good starting point for the development of ATmega-AVR micro-controller applications. It includes a debug command set which is accessed via a serial port (UART). A target-resident debug facility is particularly useful if you don't have access to a JTAG ICE debugging tool. This firmware was developed using WinAVR under AVR Studio.

Download ATmega-AVR Application Framework

USBTMC/USB488 Device Firmware Stack and Reference Design

Design your Test & Measurement instrument with a VISA-compliant USB-TMC/488 interface using the IEEE488.2 command/response message syntax, to get the following benefits:

  • Avoid the need to develop and maintain USB device driver software for the host computer
  • Choice of host computer development environments for your instrument application software...
    e.g. NI LabVIEW, Microsoft Visual Studio (VB, C/C++), etc
  • Use an existing standard API function library (VISA) -- freely available from NI or Agilent
  • USBTMC/USB488 Reference Design, with source code, available now (see below)

Initial target platform is Atmel AT91SAM7-xxx (ARM7) with on-chip USB peripheral. The example "application layer" and USBTMC "class layers" (source modules) have been designed to be independent of hardware platform, so the code can be more easily ported to other platforms. The source code and supporting annotation is of professional quality.

More info on the USBTMC Reference Design

A licence must be purchased to use this USBTMC firmware stack in a commercial product. For licensing details, send email to the address at the top of this page. Please include your country, city, company name, web address, job title, and the hardware platform you plan to use.

A "Simplified/Student Edition" (SE) intended for non-commercial, private and educational purposes may be downloaded freely.
The 'SE' software package does not support all of the standard USB-TMC/488 "class requests", but is otherwise fully usable with a VISA host.

Download USB-TMC 'SE' Package (zip)

Disclaimer: Source code and other "intellectual property" offered as free downloads on this website are original works of M. J. Bauer, except where acknowledged to the contrary. Otherwise, any resemblance to prior art originated or developed by others is purely coincidental, or due to derivation from similar works or well-established art in the public domain. The author does not accept liability for any adverse consequence of the use of "intellectual property" obtained from this site.

MJB icon

Last updated: October 2018