12864min LCD
12864g
12864MIN can display 128 * 64 dot matrix monochrome images, or 16 * 16 dot matrix display Chinese characters 8 * 4-line, or 8 * 16 dot matrix display in English, numbers, symbols 16 * 4 lines. Or 5 * 8 dot matrix display in English, numbers, symbols 21 * 8 lines.
- Control IC UC 1701x
Specification of LCD
- Rom is the Chinese words internal library, which is not used in this LCD, leave it NC
IC Documents
- Module use library u8glib, find more information on this page:https://code.google.com/p/u8glib/
- File:U8glib.zip
Arduino demo code
#include "U8glib.h"
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, CD = 9, RST = 8
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr( 0, 20, "Electrodragon");
}
void setup(void) {
u8g.setContrast(0); // Config the contrast to the best effect
u8g.setRot180();// rotate screen, if required
// set SPI backup if required
//u8g.setHardwareBackup(u8g_backup_avr_spi);
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(void) {
// picture loop
u8g.firstPage();
do {
draw();
}
while( u8g.nextPage() );
// rebuild the picture after some delay
delay(500);
}
- Please config the driving pin using this U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8); command.. And notice to enable this command when using the u8glib example codes also.
- Use "setContrast" to config the contrast as you want. We highly recommend you to setContrast to 0 to get the best display effect.
- "setRot90/setRot180/setRot270" functions will be helpful to rotate the display direction as you want.Recommend to use setRot180.
Wiring
- as defined in the demo code below:
- U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, CD = 9, RST = 8
Arduino | LCD12864 |
---|---|
D13 | SCK |
D11 | SDA |
3V3 | VDD, LEDA |
GND | VSS |
D10 | CS |
D8 | RST |
D9 | RS |
12864g LCD arduino shield
Demo Code
- remember to add contrast setting to set up the contrast of LCD
- use example sketch analog read (but remove the equation and only read value straight) to read each button value and fit for: 4-up, 0-left, 2-down, 1-select, 3-right
#include "U8glib.h"
// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The complete list of supported devices is here: http://code.google.com/p/u8glib/wiki/device
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
#define KEY_NONE 0
#define KEY_PREV 1
#define KEY_NEXT 2
#define KEY_SELECT 3
#define KEY_BACK 4
uint8_t uiKeyCodeFirst = KEY_NONE;
uint8_t uiKeyCodeSecond = KEY_NONE;
uint8_t uiKeyCode = KEY_NONE;
int adc_key_in;
int key=-1;
int oldkey=-1;
// Convert ADC value to key number
// 4
// |
// 0 -- 1 -- 3
// |
// 2
int get_key(unsigned int input)
{
if (input < 10) return 4;
else if (input < 200) return 0;
else if (input < 400) return 2;
else if (input < 600) return 1;
else if (input < 800) return 3;
else return -1;
}
void uiStep(void) {
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
{
oldkey = key;
if (key >=0){
//Serial.println(key);
if ( key == 0 )
uiKeyCodeFirst = KEY_BACK;
else if ( key == 1 )
uiKeyCodeFirst = KEY_SELECT;
else if ( key == 2 )
uiKeyCodeFirst = KEY_NEXT;
else if ( key == 4 )
uiKeyCodeFirst = KEY_PREV;
else
uiKeyCodeFirst = KEY_NONE;
uiKeyCode = uiKeyCodeFirst;
}
}
}
delay(100);
}
#define MENU_ITEMS 6
char *menu_strings[MENU_ITEMS] = { "LCD12864 Shield", "www.ElectroDragon.com", "Temperature", "Humidity" ,"About","OK"};
uint8_t menu_current = 0;
uint8_t menu_redraw_required = 0;
uint8_t last_key_code = KEY_NONE;
void drawMenu(void) {
uint8_t i, h;
u8g_uint_t w, d;
u8g.setFont(u8g_font_6x12);//4x6 5x7 5x8 6x10 6x12 6x13
u8g.setFontRefHeightText();
u8g.setFontPosTop();
h = u8g.getFontAscent()-u8g.getFontDescent();
w = u8g.getWidth();
for( i = 0; i < MENU_ITEMS; i++ ) {
d = (w-u8g.getStrWidth(menu_strings[i]))/2;
u8g.setDefaultForegroundColor();
if ( i == menu_current ) {
u8g.drawBox(0, i*h+1, w, h);
u8g.setDefaultBackgroundColor();
}
u8g.drawStr(d, i*h+1, menu_strings[i]);
}
}
void updateMenu(void)
{
switch ( uiKeyCode ) {
case KEY_NEXT:
menu_current++;
if ( menu_current >= MENU_ITEMS )menu_current = 0;
menu_redraw_required = 1;
break;
case KEY_PREV:
if ( menu_current == 0 )menu_current = MENU_ITEMS;
menu_current--;
menu_redraw_required = 1;
break;
}
uiKeyCode = KEY_NONE;
}
void setup() {
u8g.setContrast(0);
u8g.setRot180();// rotate screen, if required
menu_redraw_required = 1; // force initial redraw
//Serial.begin(9600);
}
void loop() {
uiStep(); // check for key press
updateMenu(); // update menu bar
if ( menu_redraw_required != 0 ) {
u8g.firstPage();
do {
drawMenu();
} while( u8g.nextPage() );
menu_redraw_required = 0;
}
}