Monthly Archives: May 2018

AK47m Project Part 6 (Adding a LCD display to the system)

I have added a LCD two line display to my project. I obtained a two line by 16 characters wide that costs about £6. LCD displays generally have a standard interface based on the Hitachi model. It is easily hooked up to a 16F628 PIC. There are 4 data lines, 2 control lines and 5v power lines. Only 4 of the 8 data lines are used and the LCD is programmed using 4 bit nibbles of data. The LCD display that I obtained has the pin out arranged as two lines of solder connections. The usual ones have a single line. The solder pad with a square surround is pin 1 and the pins are numbered from left to right so the odd numbered pins are on the left and the even numbered pins are on the right.

Note that the LCD is powered from the 5v supply but the control and data lines are driven to +3.5v from the PIC. The 5v supply is taken from the USB connection to the power supply. The 3.5v supply for the PIC is derived from the Raspberry PI.

The excellent MikroElectronika pascal compiler has a library to drive the LCD and a simple means to configure the PIC IO pins used to connect to the display. The LCD display connections to the PIC are as follows:-

Pin1 - Ground

Pin2 - +5v

Pin3 - contrast (grounded)

Pin4 - RS (PIC A1)

Pin5 - RW (grounded)

Pin6 - En (PIC A0)

Pin7-10 - High Data (grounded)

Pin11-14 - Low Data (PIC B4-B7)

Pin15 -Backlight LED (+5v)

Pin16 -Backlight LED (grounded)


The PASCAL program requires a declaration of the pin out as follows:

var LCD_RS : sbit at RA1_bit;
var LCD_EN : sbit at RA0_bit;
var LCD_D4 : sbit at RB4_bit;
var LCD_D5 : sbit at RB5_bit;
var LCD_D6 : sbit at RB6_bit;
var LCD_D7 : sbit at RB7_bit;

var LCD_RS_Direction : sbit at TRISA1_bit;
var LCD_EN_Direction : sbit at TRISA0_bit;
var LCD_D4_Direction : sbit at TRISB4_bit;
var LCD_D5_Direction : sbit at TRISB5_bit;
var LCD_D6_Direction : sbit at TRISB6_bit;
var LCD_D7_Direction : sbit at TRISB7_bit;

The display is initialised using the following code, note that the compiler automatically links in the LCD driver library.

 Lcd_Init(); // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

The display is updated as the following example:

 txt := 'PHSC (c)2018';
 LCD_Out(1,1,txt); // Write text in first row from character pos 1
 txt := 'AK47M Vs 1.0';
 LCD_Out(2,1,txt); // Write text in second row from character pos 1

When I first ran my program, nothing happened ! Eventually I realised that the contrast was not connected on Pin3. This is normally driven by a voltage divider to adjust the voltage. I simply grounded the pin and it worked fine.

These photos show the individual components:-

This is the LCD display with wires soldered on that connect to the PIC.


This is the display PIC. The wires to the right go to the LCD display. The wires to the left take power to the configuration PI card and receive MIDI data.


This is the connector to a PI B+ card to obtain power and send MIDI data to the PI. There are four connections : +5v, +3.5v, Ground, UART Receive.


Here are the three components connected up.


The PIC uses its internal UART to monitor the MIDI messages. I have defined a set of MIDI messages for internal use. These are kept short as there is no flow control. This means I can use a one wire connection to operate the LCD display.

Internal MIDI messages have the general format:

$F1 <length of data> <function> <data>

except for key press messages that have the format

$F1 $00 <key press>

Here is a summary of the messages :-

Key press - $F1 $00 <key press>
Display 1 - $F1 <length> $01 <text up to 16 chars>

Display 2 - $F1 <length> $02 <text up to 16 chars>

Select Style File - $F1 <length> $03 <style file name with extension>

Style Params - $F1 $18 $04 [Voice PC]x8 [Voice Vol]x8 [Voice Enable]x8

Style Aux - $F1 $04 $05 <tempoH> <tempoL> <transpose> <variation select>

Part Params - $F1 $10 $06 [Part PC]x4 [Part Vol]x4 [Part Flags]x4 <Split point> <Split Enable> <spare> <spare>

Chord Finder - $F1 $02 $07 <chord pitch> <chord type>


Midi Mate Revisited

Some while back I developed the “Midi-Mate” that connected my Yamaha electric piano to the BK7M. The BK7M unit does not work anymore so I needed an alternative at short notice. I decided to use my old Yamaha PSR keyboard as an alternative sound generator and link it to my piano using the Midi-Mate.

I re-programmed the PIC to hold a set of pre-programmed performance settings internally and recall them when a key sequence was entered. I was able to double up two lead instruments and split the keyboard exactly in the same way as the BK7M. The PIC was able to hold about 100 settings internally. This worked well except that the settings were static and could not be edited.

I decided to use the hardware developed for my AK47M project to extend this concept further and allow me to program the MIDI controller.

A PIC microcontroller is connected to my electric piano via MIDI and also scans a 3×4 numeric keypad. The PIC is connected to a PI zero W using a MIDI serial link so that the PI can accept keypad messages. The PI serial output is linked back to the PIC. This is done so that the PI can send performance configuration messages to the PIC in response to a keypad selection.
The PIC has all the logic to select instruments on 4 MIDI channels and route piano MIDI messages to any of the channels. It can also handle the channel volumes, split the keyboard and perform transposition.

The PI is simply being used to store and recall performance configurations from file and provide a real time editing function for a selected performance. The software uses exactly the same formats as the BK7M performance files so that configurations can easily be edited by my BK7M editor program. (which I have ported to the PI)
The latency is kept to a minimum as the PIC is handling the routing of key on/off messages.

I run the PI in headless mode and it connects to my wifi network. I can run a virtual terminal on my PC or Android tablet in order to use the editor. The screen shots show the completed unit and the editor/configuration software.


This is the completed unit. There are two MIDI sockets, one for the Piano and one for the Synth. Power is provided using a USB connection to a USB power supply.

A performance is selected by typing in the performance number followed by a hash. The following numbers provide special functions :-

9991 – restart PI

9992 – shutdown PI

9999 – MIDI panic

The software loads the performance file “” by default. A different performance file can be selected by typing a number followed by the star key.



This is the configuration software running on the PI. When a number is entered on the keypad, the appropriate performance configuration is recalled from file and displayed. Parameters can be selected and changed and then sent to the PI by pressing “Update and Send”. The Previous and Next keys allow you to move to the previous or next performance in the file. The entire performance file with edits may be saved using the “Save to File” button.

Note that the performance file is 100% compatible with the BK7M and the BK7M editor. I have ported a version of the Windows BK7M editor to run on the Raspberry PI.