Quantcast
Channel: JH7UBCブログ
Viewing all articles
Browse latest Browse all 440

Arduino I2C LCD1602

$
0
0
 先日、Raspberry Piで、I2Cシリアルパラレル変換モジュール付のLCD1602の表示テストをして、うまくいきました。

 そこで、今回は、そのプログラムをArduino用に移植してみました。

 Arduinoでは、LCDの表示は、ライブラリーを使いますが、今回のテストでは、ライブラリーを使わないで、I2C LCD1602に文字を表示することができました。

イメージ 1

 ArdinoとI2C LCDとの接続は
 Arduino  LCD
 GND     GND
 5V        Vcc
 A4        SDA
 A5        SCL
です。

 テストしたスケッチは、次の通りです。

--------------------------------------------------------
/*
 * I2C LCD1602 Display test
 * 2017/6/20
 * JH7UBC Keiji Hata
 */
 #include <Wire.h>
 #define  addr  0x27
 #define  LCD_EN    0b00000100//Enable
 #define  LCD_BL    0b00001000//Back Light
 #define  LCD_CMD 0x00
 #define  LCD_CHR 0x01
 #define  LCD_LINE1 0x80
 #define  LCD_LINE2 0xC0
 
void setup() {
  Wire.begin();
  LCD_init();
  LCD_display();
}
void LCD_write(byte bits,byte mode){
  //High 4bits
  Write_data((bits & 0xF0) | mode);
  //Low 4bits
  Write_data(((bits << 4) & 0xF0) | mode);
}
void Write_data(byte data){
  Wire.beginTransmission(addr);
  Wire.write(data | LCD_EN | LCD_BL);
  Wire.write(data | LCD_BL);
  Wire.endTransmission();
  delayMicroseconds(100); 
}
void LCD_init(){
  LCD_write(0x33,LCD_CMD);
  LCD_write(0x32,LCD_CMD);
  LCD_write(0x06,LCD_CMD);
  LCD_write(0x0C,LCD_CMD);
  LCD_write(0x28,LCD_CMD);
  LCD_write(0x01,LCD_CMD);
  delay(1);
}
void LCD_clear(){
  LCD_write(0x01,LCD_CMD);
  delay(1);
}
void LCD_home(){
  LCD_write(0x02,LCD_CMD);
  delay(1);
}
void LCD_cursor(byte x,byte y){
  if (y == 0){
    LCD_write(LCD_LINE1+x,LCD_CMD);
  }
  if (y == 1){
    LCD_write(LCD_LINE2+x,LCD_CMD);
  }
}
void LCD_text(String s){
  for (int i=0; i< s.length(); i++){
    LCD_write(s.charAt(i),LCD_CHR);
  }
}
void LCD_display(){
  LCD_text("Hello World!");
  LCD_cursor(8,1);
  LCD_text("JH7UBC");
}

void loop() {
}
--------------------------------------------------------
以外に簡単なスケッチで、表示させることができます。

Viewing all articles
Browse latest Browse all 440

Trending Articles