Finite State Machine Based LCD Controller
One of the most used functions of the PIC is to control an LCD. LCD come inmany shapes and sizes, but most of them all use the UART interface. This makes it a convenient and compact way to communicate and control the LCD, since it only takes one wire (and return) to send characters to the screen.
In this article, I will show you how to control a Matrix Orbital MOS series LCD screen. I will be using a 16×2 character screen, and configuring the PIC24 to control the screen using a finite state machine (FSM).

So what is an FSM? It’s is an easy way to figure out all the possible configurations of a machine and giving it instructions on what to do when that configuration is reached. These configurations are called “states”, and since we are trying to figure out all the states (and there must be a finite number of them), the term FSM is used to describe these machines. A calculator for example is a FSM. There are only a finite number of possible outcomes that can be configured, albeit a large number of states can exist when using a calculator. Finite state machines are useful not only in hardware synthesis such as VHDL or VLSI, but also in low level programing. This is because it allows you to create a complicated program without having to deal with a labyrinth of if/else statements.
A Simple Example
Suppose we want to create a very simple circuit/program. We have an LED driver, and a debounced button connected to a microcontroller. We want the microcontroller to keep track of the state of the LED. If the LED is on, and the button is pressed, turn off the LED. If the LED is off, and the button is pressed, then turn on the LED. We must also define what happens when the circuit is booted up. We can arbitrarily select the LED to be turned on. To represent this machine, we have 3 possible states: Start, LED on, LED off. A FSM diagram of this program might look like the following:

How would we convert the diagram into code? Well the code might look something like this (this is pseudo code, I don’t know if it’ll compile).
#define startState 0
#define ledOnState 1
#define ledOffState 2
#define buttonPressed 1
#define buttonNotPressed 0
int state = startState;
int input = buttonNotPressed;
int main(void)
{
//init start state
state = startState;
while(1)
{
//get input first
input = getInput();
//
switch(state)
{
case startState:
state = ledOnState; break;
case ledOnState:
if(input == buttonPressed)
{
state = ledOffState;
ledOff();
}
break;
case ledOffState:
if(input == buttonPressed)
{
state = ledOnState;
ledOn();
}
break;
default: state = startState; //if unknown state, reboot
break;
}
}
}
Using the same concept, I have developed a driver board for the MOS LCD. I won’t go into the details of how it functions, but I’ve included a project using the LCD drivers. I’m sure you can figure out how I constructed the FSM by looking at the code. It is quite an ingenious way to control the LCD, if I may say so myself.
Button 1 clears the LCD screen, button 2 should make the following text appear:

The circuit configuration for this project is pretty standard. If you have been reading the tutorials, you should be able to figure it out by looking at the code.
General Usage
For general usage, the driver makes it very simple to write to the LCD. You must first initiate the LCD drivers by calling LCDInit(). To write a piece of text, first use LCDIdle() to determine if the mircontroller is in the middle of writing something. Next use the functions LCDText(int, string) and LCDText2(string, string) to write text. There are some other functions in there for other effects. You can call LCDClear() to also clear the current LCD text. Of course, if you don’t like what I have written, you can always write your own driver, or modify mine. Enjoy! If you got any questions, please feel free to leave a comment.
Engscope LCD Driver Project
Please read the disclaimer before downloading.

Entries (RSS)
“Don’t taze me bro!” hahaha…
reminds me of an incident at UF in 2008!
Nice website btw, its quite helpful for PIC24 users.