LCD12864 shield 兼容Arduino

LCD12864 shield(SKU:DFR0287)

目录

1 概述
2 技术指标
3 库文件
4 示例代码

概述
LCD12864 Shield采用12864点阵液晶制作的一款显示扩展板,为Arduino控制器提供显示单元。
可直接插接到大部分Arduino控制器上使用,可显示中文、英文、图片等。
模块具有5个模拟扩展端口和8个数字扩展端口,可连接DFRobot大部分的模拟或数字传感器,还具有一个小型的手机摇杆,可实现5个按键操作。
LCD12864 Shield是项目开发和制作互动作品不可多得的显示器。

技术指标
模块接口:Arduino兼容
占用端口:模拟口0,数字口7,8,9,10,11,13
工作电压:3.3V
液晶点阵:128x64
1个复位按键
1个手机摇杆,实现5个按键操作(占用模拟口0)
具有背光控制(占用数字口7)
5个模拟接口,8个数字接口扩展
调用库文件可直接使用Arduino控制器驱动
尺寸:60x55x20mm

库文件
U8glib库文件下载
下载后将文件解压,把U8glib文件夹复制到arduino-1.0.4\libraries中。
使用IDE打开例程,会发现有很多设备驱动被屏蔽掉了,需要找到我们设备的驱动文件,我们的设备选择U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);
将它前面的//删除掉即可,下载成功后会发现液晶的对比度太深,那么我们需要修改库文件中的一些参数来调整对比度。
对比度调整,需要找到arduino-1.0.4\libraries\U8glib\utility文件夹中的u8g_dev_st7565_nhd_c12864.c,
将0x008, /* contrast: 0x008 is a good value for NHD C12864, Nov 2012: User reports that 0x1a is much better */
这行代码的0x008, 修改为0x000, 保存文件即可,重新下载例程,就会发现对比度调整好了。

示例代码

/*

This sample codes is for testing the LCD12864 shield.

Editor : Mickey

Date  : 2013.11.27

Ver  : 0.1

Product: LCD12864 shield

SKU  : DFR0287

*/

#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 < 100) return 0;
else if (input < 300) return 1;
else if (input < 500) return 2;
else if (input < 700) return 3;
else if (input < 900) return 4;
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.DFRobot.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, ih+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.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;
}

}

Nextredirectltr.png购买LCD12864 shield 兼容Arduino (SKU:DFR0287)

标签: Arduino传感器