Adding LCD character display to Attiny85 project

LCD character displays are a pretty useful addition to a project for providing a simple test output.  However if we use them in 4-bit or 8-bit parallel-mode they can be pretty greedy on pins, not to mention the software overhead with custom libraries.  I've been programming Attiny85 chips recently, where pins are certainly very much at a premium.  I wanted to add an 4-line LCD character output to the project, and luckily there are version of LCD displays that use serial inputs (I2C/UART/SPI) to control the input, meaning that if we use UART we only have to use a single pin to control output - excellent!



We can use the module in more or less the same way as "normal" UART output, but we have to track the row-position of the cursor.  Firstly we have to define the UART output in the usual way in setup().  The default is 9600 bps but we can see how to change that later.  I've hooked up the 5V and GND to the module, and then connected the RX line of the LCD display to GPIO0 (pin 5) of the Attiny85.  We're using the SoftwareSerial because sometimes it's fun to write our own UART libraries, and sometimes we can't be bothered! :-P

So we declare the global "Serial" like so,
#define RX  -1
#define TX  0
SoftwareSerial Serial(RX, TX);

Note that "-1" as a parameter to SoftwareSerial indicates we are not interested in that function. In setup() we define:

Serial.begin(9600);

Easy!  To do things like clear the screen and turn off the flashing cursor, we need to send commands.  These are pre-programmed and a full list was a bit difficult to find but it looks like there is some stuff in here.  Page 35 appears most relevant, though some commands are for a different graphic drawing module.  To send a command to clear the screen, we just send "CL", i.e.

void clearScreen()
{
  Serial.print("CL");
  row=0;
  col=0;
}

To turn off the cursor:
Serial.print("CS0"); // Turn off cursor
Turn on the cursor:
Serial.print("CS1"); // Turn on cursor
To display a string, we have to send "TT", followed by the string to display, followed by the "0x00" character.  To display a string and start a new line, we send "TT", the string to display, "0x00", "0x0A" and "0x0D".  To update the cursor position we can send "ETP" followed by the column and row.  To put all this together, we can define a template  function so we can output char arrays, floats, ints, etc, as well as a version that starts a newline.  I use the definition of LCD_OUT to change between LCD output, or to output a normal UART stream.

#define LCD_OUT 1
uint8_t row = 0, col = 0;

template < typename T > void Serialprint( T data);
template < typename T > void Serialprintln( T data);

template < typename T > void Serialprint( T data) {
#ifdef LCD_OUT
  if (row == 0 && col == 0) writeStr("CL"); // Clear screen if returning to home cursor position (0,0)
  writeStr("TT");
#endif

Serial.print(data);

#ifdef LCD_OUT
  Serial.write((byte)0x0); // end write
#endif  
}

template < typename T > void Serialprintln( T data) {
  
  Serialprint(data);
  Serial.write(0x0A); // end write
  Serial.write(0x0D); // end write

#ifdef LCD_OUT
  row++; if  (row >= 4) {
    row = 0;
    col = 0;
  }
  writeStr("ETP"); Serial.write((byte)0x0); Serial.write(row);
#endif  
}

The above functions are all you need to control the LCD character display with an Attiny85 chip!  "row" and "col" are used simply to track cursor position, though actually only row tracking is implemented as it is used to trigger clearing the screen on overflow.  To use the functions we simply call them as we would the analogous Serial.print() and Serial.println() functions, though with a single parameter, i.e.

Serialprint("Volt:");
Serialprint((int)volts);
Serialprintln(" mV");

No prizes for guessing what my Attiny85 project is used for!  I will post on using the Attiny85 in an ammeter/voltmeter project soon, as well as using Processing to output to a PC for visualisation.  Until then, happy hacking!

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

Arduino and Raspberry Pi serial communciation

Getting started with the Pro Micro Arduino Board