solar ⋅ avrlcd ⋅ lstripe ⋅ joule
AVR 128x64 LCD
IntroductionThis document desribes how to attach a 128x64 LCD unit, available at SparkFun for $20, to an AVR microcontroller. The datasheets, unfortunately, are barely in english and somewhat misleading, and I was not able to find much in the way of good example code. This is written for an AVR Mega48 but should be easy to adapt to others.
The module has a built-in -5v source (Vee); an external potentiometer is required to set Vo to somewhere between -5v and 5v. The reset pin should be tied to Vcc. Since PORTB on the AVR includes the SPI port, it's best to use a pulldown resistor on E so the LCD does not assert the data lines while SPI programming is in progress.
/* lcd_init(): initialize lcd port directions * enable both display chips * set display line to zero */ void lcd_init(); /* lcd_clear(): clear the entire lcd */ void lcd_clear(); /* lcd_setbit(): sets the bit at (x,y) to value v * may not appear until lcd_flush() is called. */ void lcd_setbit(uint8_t x, uint8_t y, uint8_t v); /* lcd_flush(): write pending bits out to display */ void lcd_flush(); /* lcd_set_cursor(): set the cursor position to the given 6x8 cell */ void lcd_set_cursor(uint8_t row, uint8_t col); /* lcd_putch(): write an ascii character at the cursor and advance */ void lcd_putch(uint8_t ch); /* lcd_putstr(): write a string at the cursor */ void lcd_putstr(const char* str);
The code supports two modes of operation: setting single bits and writing characters to a grid of 6x8 cells.
The LCD is logically made up of 128x8 columns, each 1x8. Since it would require a kilobyte of ram to store the entire display state, the implementation uses the module's onboard memory. Writing a single bit requires reading the appropriate 1-byte column, altering it as required, and writing it back. The loading and storing is done lazily, so it is most efficient to write loops that access many neighboring pixels with the y-coordinate in the inner loop, as that will minimize loads/stores. lcd_flush() is called to force it to write back to the module.
The display has no built in concept of fonts, so when using character mode, a 5x7 font defined in font.h is used. This is stored in program memory and uses about 500 bytes.