ماژول نمایشگر LCD 128 X 64 دارای ارتباط I2C
ترتیب اتصال پینها :
Arduino |
LCD |
6 |
LED |
7 |
A0 |
8 |
RST |
9 |
CS |
11 |
SDA |
13 |
SCL |
GND |
GND |
VCC |
VCC |
نمونه کد :
کد:
#include <U8glib.h>
byte ledBacklight = 150;
const byte lcdLED = 6; // LED Backlight
const byte lcdA0 = 7; // Data and command selections. L: command H : data
const byte lcdRESET = 8; // Low reset
const byte lcdCS = 9; // SPI Chip Select (internally pulled up), active low
const byte lcdMOSI = 11; // SPI Data transmission
const byte lcdSCK = 13; // SPI Serial Clock
const byte bufferSize = 60;//32
// SW SPI:
//U8GLIB_MINI12864_2X u8g(lcdSCK, lcdMOSI, lcdCS, lcdA0, lcdRESET);
// HW SPI:
U8GLIB_MINI12864_2X u8g(lcdCS, lcdA0, lcdRESET);
const byte LINE_MAX = 30; // setup input buffer
byte line_buf[LINE_MAX];
byte line_pos = 0;
const byte ROW_MAX = 12; // setup a text screen to support scrolling
byte screen[ROW_MAX][LINE_MAX];
byte rows, cols;
const byte LINE_PIXEL_HEIGHT = 7; // line height, which matches the selected font (5x7)
// Print character arrays to console
void u8g_print(char* theString) {
char ch;
byte i = 0;
while ((ch = theString[i++]) != '\0') {
read_line(ch);
}
}
// Print character arrays with a new line at the end
void u8g_println(char* theString) {
u8g_print(theString);
read_line('\r');
}
// Print character arrays saved in program memory to console
void u8g_print_P( const char *theString) {
char ch;
while ((ch = pgm_read_byte(theString++)) != '\0') {
read_line(ch);
}
}
// Print character arrays saved in program memory with a new line at the end
void u8g_println_P(const char *theString) {
u8g_print_P(theString);
read_line('\r');
}
// Clear entire screen, called during setup
void clear_screen(void) {
byte i, j;
for( i = 0; i < ROW_MAX; i++ )
for( j = 0; j < LINE_MAX; j++ )
screen[i][j] = 0;
}
// Append a line to the screen, scroll up
void add_line_to_screen(void) {
byte i, j;
for( j = 0; j < LINE_MAX; j++ )
for( i = 0; i < rows-1; i++ )
screen[i][j] = screen[i+1][j];
for( j = 0; j < LINE_MAX; j++ )
screen[rows-1][j] = line_buf[j];
}
// U8GLIB draw procedure: output the screen
void draw(void) {
byte i, y;
// graphic commands to redraw the complete screen are placed here
y = 0; // reference is the top left -1 position of the string
y--; // correct the -1 position of the drawStr
for( i = 0; i < rows; i++ )
{
u8g.drawStr( 0, y, (char *)(screen[i]));
y += u8g.getFontLineSpacing();
}
}
void exec_line(void) {
// Add the line to the screen
add_line_to_screen();
// U8GLIB picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
// Clear current input buffer
void reset_line(void) {
line_pos = 0;
line_buf[line_pos] = '\0';
}
// Add a single character to the input buffer
void char_to_line(char c) {
line_buf[line_pos] = c;
line_pos++;
line_buf[line_pos] = '\0';
}
// Handle new console character
void read_line(char c) {
if ( line_pos >= cols-1 ) {
exec_line();
reset_line();
char_to_line(c);
}
else if ( c == '\n' ) {
// ignore '\n'
}
else if ( c == '\r' ) {
exec_line();
reset_line();
}
else {
char_to_line(c);
}
}
// From http://playground.arduino.cc/Code/AvailableMemory
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void setup()
{
char buf[bufferSize];
// Turn on LED backlight
analogWrite(lcdLED, ledBacklight);
// Setup U8glib
u8g.begin();
u8g.setFont(u8g_font_5x7); // Set font for the console window
u8g.setFontPosTop(); // Set upper left position for the string draw procedure
// Calculate the number of rows for the display
rows = u8g.getHeight() / u8g.getFontLineSpacing();
if ( rows > ROW_MAX )
rows = ROW_MAX;
// Estimate the number of columns for the display
cols = u8g.getWidth() / u8g.getStrWidth("m");
if ( cols > LINE_MAX-1 )
cols = LINE_MAX-1;
clear_screen(); // Clear screen
delay(1000); // Delay
reset_line(); // Clear input buffer
// Print some information
u8g_println_P(PSTR("U8glib Fio Console"));
u8g_println_P(PSTR("semifluid.com"));
u8g_println_P(PSTR(""));
u8g_println_P(PSTR("Starting..."));
u8g_print_P(PSTR("Free Memory: "));
String(freeRam(),DEC).toCharArray(buf, sizeof(buf));u8g_println(buf);
}
int i = 0;
void loop()
{
char buf[bufferSize];
// Start timer
unsigned long theStart = micros();
// Display an incrementing counter
String(i,DEC).toCharArray(buf, sizeof(buf));u8g_print(buf);
u8g_print_P(PSTR("-"));
String(i,HEX).toCharArray(buf, sizeof(buf));u8g_print(buf);
u8g_print_P(PSTR("-"));
String(i,BIN).toCharArray(buf, sizeof(buf));u8g_print(buf);
u8g_print_P(PSTR("-"));
i++;
// Approximate time in microseconds that it took to display the information
unsigned long theEnd = micros();
unsigned long delta = theEnd - theStart;
String(delta,DEC).toCharArray(buf, sizeof(buf));u8g_println(buf);
// No delay, so loop as quickly as possible
}