More headless CPUs and development platforms for .NET Micro Framework are coming. Even if we don't need the WPF features, some kind of displaying capabilities are always fine. The easiest and cheapest way is to use alphanumeric displays. Nice two line display with sixteen characters on each line can be purchased for less then 10USD. More size configuration exists: 8x1, 8x2, 16x1, 16x2, 20x1, 20x2, 20x4 and others. The best thing is, that it's possible to control them all in a same way.
How does LCD display work?
Basically all of the alphanumeric LCDs today are controlled by HD44780 parallel interface chipset. There are eight data pins and three control pins, which serves as 11bit long byte to write commands or data into HD44780. The protocol is very simple - one byte represents commands and each bit in it represents arguments. By toggling the R/S bit, operation mode is chosen: low for command; high for data. The only problem here is, that wasting 11 GPIOs pins on micro-controller is too much. Luckily, HD44780 can operate in 4bit data mode as well. It means that 7bit long byte is used and everything is send twice to the LCD. Seven GPIOs is better than eleven but why not try to use even less pins.
Connecting LCD using shift register. Left = Little Endian; Right = Big Endian
4094 Shift Register
I believe that you already heard about shift registers. If not, you should definitely read this intro on AllAboutCircuits.com. In this case 4094 8bit serial-in/parallel-out shift register is used. What shift registers does, is reading bit on it's Data pin and whenever the Clock pin goes from LOW to HIGH the bit is shifted. It means, that after eight clock ticks the register is loaded with 8bit byte. Whenever it's necessary to propagate stored byte into shift register's parallel outputs, just set the Strobe pin to HIGH. Strobe pin is sometimes called Clear or RCLK.
Now we know the mechanism, how to use three CPU gpios to set eight outputs. Serial-in/Paralle-out shift register can be understand as a type of IO extender for output. Since we will operate in 4bit mode, it means 7bits all together there are one bit left in the register. This least significant bit in register (when using little endian wiring)) will be used to control display back-light. Output 0 of the register is connected to base of the BC548B transistor used to turn on 120mA back-light LED.
To be able easily control the LCDs in 4bit mode within .NET Micro Framework applications, I wrote Bansky.SPOT.LCD library. At this moment there are two classes: LCD4Bit providing API for HD44780 powered displays in 4bit mode; and HC4094 class that represent transport layer into LCD display. Since the HD44780 commands can be transferred via Shift register, I2C bus, UART, SPI etc.. I've decided to split API and transport layer. There is interface ITransferProvider that exposes one method SendByte, which sends one byte over some medium.
Following code demonstrates how to write simple message onto display. First the transfer provider is initialized, in this case it's HC4094. Then the transfer provider is passed to the constructor of LCD4Bit. After that, API methods are called to control HD44780 operated display.
// Create instance of shift register HC4094 shifter = new HC4094( Pins.GPIO_PORT_Y_7, // Data pin Pins.GPIO_PORT_Y_6, // Clock pin Pins.GPIO_PORT_Y_5, // Strobe pin false); // Little Endian // Create new LCD instance and use shift register as a transport layer LCD4Bit lcd = new LCD4Bit(shifter); // Turn display on, turn back light on, hide small cursor, show big blinking cursor lcd.Display(true, true, false, true); lcd.Clear(); // Clear screen lcd.Write("Hello world!"); // Write message lcd.SetPosition(40); // Move to second line lcd.Write("Micro Framework"); // Write second line of message
More ways to go
Using shift register to control HD44780 parallel interface is just one option but probably the cheapest one. There are serial as well as SPI interfaces for HD44780 but it's cost is about 10 USD and more. The 4094 shift register and BC548B transistor cost me less than 50 cents. Next time I will show how to use I2C bus to control HD44780.
The demo project is based around EDK Plus development platform from SJJ Micro and Powertip PC1602 display. First three pins on the EDK header 1 is used for connection with 4049 shift register. The 1K potentiometer is used to adjust contrast of display.
Note: The PC1602 is 16x2 display and it's display data RAM is organized as an 20x2. It means that it's necessary to set "cursor" position to 40 to move onto the second line.
Demo project and library: AlphanumericLCD_library.zip [77,6 Kb] 19 Oct 2008
Demo project and library are now in my GitHub repository
Check the next article using I2C port extender for LCD interfacing.