1602LCDのパラレル接続とシリアル接続

1602LCDを8bit接続した場合

抵抗はバックライトの明るさ設定

 

 

 

 

 

 

 

 

int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};
int Enable = 2;

void LcdCommandWrite(int value) {
// Define all pins
int i = 0;
for (i=DB[0]; i <= DI; i++)
{
digitalWrite(i,value & 01);
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1); 
digitalWrite(Enable,LOW);
delayMicroseconds(1); 
}

void LcdDataWrite(int value) {
// Define all pins
int i = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[7]; i++) {
digitalWrite(i,value & 01);
value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1);
}

void setup (void) {
int i = 0;
for (i=Enable; i <= DI; i++) {
pinMode(i,OUTPUT);
}
delay(100);
// After a short pause, LCD
// For LCD control needs
LcdCommandWrite(0x38); 
delay(64); 
LcdCommandWrite(0x38); 
delay(50); 
LcdCommandWrite(0x38); 
delay(20); 
LcdCommandWrite(0x06); 
delay(20); 
LcdCommandWrite(0x0E);
delay(20); 
LcdCommandWrite(0x01); 
delay(100); 
LcdCommandWrite(0x80); 
delay(20);

LcdCommandWrite(0x01);
delay(10); 
LcdCommandWrite(0x80+3); 
delay(10);

LcdDataWrite('W');
LcdDataWrite('e');
LcdDataWrite('l');
LcdDataWrite('c');
LcdDataWrite('o');
LcdDataWrite('m');
LcdDataWrite('e');
LcdDataWrite(' ');
LcdDataWrite('t');
LcdDataWrite('o');
delay(10);
LcdCommandWrite(0xc0+1);
delay(10); 
LcdDataWrite('D');
LcdDataWrite('i');
LcdDataWrite('g');
LcdDataWrite('i');
LcdDataWrite('t');
LcdDataWrite('a');
LcdDataWrite('l');
LcdDataWrite(' ');
LcdDataWrite('S');
LcdDataWrite('W');
LcdDataWrite('R');
LcdDataWrite(' ');
LcdDataWrite('v');
LcdDataWrite('2');
LcdDataWrite(' ');
delay(2000);
LcdCommandWrite(0x01);
delay(10);
LcdDataWrite(' ');
LcdDataWrite('C');
LcdDataWrite('o');
LcdDataWrite('p');
LcdDataWrite('y');
LcdDataWrite('r');
LcdDataWrite('i');
LcdDataWrite('g');
LcdDataWrite('h');
LcdDataWrite('t');
LcdDataWrite(' ');
LcdDataWrite('2');
LcdDataWrite('0');
LcdDataWrite('1');
LcdDataWrite('8');
LcdDataWrite(' ');
LcdCommandWrite(0xc0+1);
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite('b');
LcdDataWrite('y');
LcdDataWrite(' ');
LcdDataWrite('J');
LcdDataWrite('G');
LcdDataWrite('1');
LcdDataWrite('I');
LcdDataWrite('P');
LcdDataWrite('Z');
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite(' ');
delay(2000);
} 
void loop (void) { // 以下は表示デモ
LcdCommandWrite(0x01);
delay(10);
LcdDataWrite('F');
LcdDataWrite('W');
LcdDataWrite('D');
LcdDataWrite(':');
LcdDataWrite('1');
LcdDataWrite('0');
LcdDataWrite('0');
LcdDataWrite('W');
LcdDataWrite(' ');
LcdDataWrite('R');
LcdDataWrite('E');
LcdDataWrite('F');
LcdDataWrite(':');
LcdDataWrite('0');
LcdDataWrite('0');
LcdDataWrite('W');
LcdCommandWrite(0xc0+1);
delay(10); 
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite('S');
LcdDataWrite('W');
LcdDataWrite('R');
LcdDataWrite(':');
LcdDataWrite('1');
LcdDataWrite('.');
LcdDataWrite('0');
LcdDataWrite('0');
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite(' ');
LcdDataWrite(' ');
delay(5000);
}

 

1602LCDを4bit接続した場合

抵抗はバックライトの明るさ設定

 

 

 

 

 

 

 

 

 

int LCD1602_RS=12; 
int LCD1602_RW=11; 
int LCD1602_EN=10; 
int DB[] = { 6, 7, 8, 9};
char str1[]="Welcome to";
char str2[]="geek-workshop";
char str3[]="this is the";
char str4[]="4-bit interface";

void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=command & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(command & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1); 
digitalWrite( LCD1602_EN,LOW);
}

void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=dat & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(dat & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1); 
digitalWrite( LCD1602_EN,LOW);
}

void LCD_SET_XY( int x, int y )
{
int address;
if (y ==0) address = 0x80 + x;
else address = 0xC0 + x;
LCD_Command_Write(address); 
}

void LCD_Write_Char( int x,int y,int dat)
{
LCD_SET_XY( x, y ); 
LCD_Data_Write(dat);
}

void LCD_Write_String(int X,int Y,char *s)
{
LCD_SET_XY( X, Y ); //Set address
while (*s) //Write string
{
LCD_Data_Write(*s); 
s ++;
}
}

void setup (void) 
{
int i = 0;
for (i=6; i <= 12; i++) 
{
pinMode(i,OUTPUT);
}
delay(100);
LCD_Command_Write(0x28);//4 lines and 2 lines 5x7
delay(50); 
LCD_Command_Write(0x06);
delay(50); 
LCD_Command_Write(0x0c);
delay(50); 
LCD_Command_Write(0x80);
delay(50); 
LCD_Command_Write(0x01);
delay(50);

}

void loop (void)
{
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(3,0,str1);/First lines, fourth addresses
delay(50);
LCD_Write_String(1,1,str2);/Second lines, second addresses
delay(5000);
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(0,0,str3);
delay(50);
LCD_Write_String(0,1,str4);
delay(5000);

}
Common view copy code to save code print code
int LCD1602_RS=12; 
int LCD1602_RW=11; 
int LCD1602_EN=10; 
int DB[] = { 6, 7, 8, 9};
char str1[]="Welcome to";
char str2[]="Arduino";
char str3[]="this is the";
char str4[]="4-bit interface";

void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=command & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(command & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1); 
digitalWrite( LCD1602_EN,LOW);
}

void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=dat & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(dat & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1); 
digitalWrite( LCD1602_EN,LOW);
}

void LCD_SET_XY( int x, int y )
{
int address;
if (y ==0) address = 0x80 + x;
else address = 0xC0 + x;
LCD_Command_Write(address); 
}

void LCD_Write_Char( int x,int y,int dat)
{
LCD_SET_XY( x, y ); 
LCD_Data_Write(dat);
}

void LCD_Write_String(int X,int Y,char *s)
{
LCD_SET_XY( X, Y ); //Set address 
while (*s) / /Write string
{
LCD_Data_Write(*s); 
s ++;
}
}

void setup (void) 
{
int i = 0;
for (i=6; i <= 12; i++) 
{
pinMode(i,OUTPUT);
}
delay(100);
LCD_Command_Write(0x28);//4 lines and 2 lines 5x7
delay(50); 
LCD_Command_Write(0x06);
delay(50); 
LCD_Command_Write(0x0c);
delay(50); 
LCD_Command_Write(0x80);
delay(50); 
LCD_Command_Write(0x01);
delay(50); 

}

void loop (void)
{
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(3,0,str1);//First lines, fourth addresses from
delay(50);
LCD_Write_String(1,1,str2);//Second lines, second addresses from
delay(5000);
LCD_Command_Write(0x01);
delay(50);
LCD_Write_String(0,0,str3);
delay(50);
LCD_Write_String(0,1,str4);
delay(5000);

}

 

 

1602LCDの裏にシリアル変換ボードを取り付けてA4,A5に接続した場合

VRはバックライトの明るさ調整

 

 

 

 

 

 

 

 

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 16, 2); // 1602LCD = 16,2 0x3f or 0x27
 
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Wellcome to");
lcd.setCursor(0,1);
lcd.print("JG1IPZ");
}
void loop()
{
}

 

 

アナログポート2つだけで済むのでデジタル側を自由に使える。

ヘッダーファイルのおかげでスケッチが簡易化された。

今まで計測データをPCに転送して読んでいたが、これからはLCD表示にできる。

大量に購入!