2017년 8월 8일 화요일

캐릭터 LCD 16x2 I2C로 출력하기

캐릭터 LCD 16X2 디스플레이의 장점은 저렴하게 출력장치를 구현할  수 있다는 것인데요.

단점은 필요한 핀이 너무 많다는 거죠. 그래서 I2C 포트를 이용할 수 있게 해주는

I2C 컨버터를 연결해서 I2C 포트로 전에 이용해본 RTC 모듈로 시간을 출력해 보겠습니다.

아두이노의 SCL, SDA 핀에 연결합니다. I2C 통신을 위해 Wire 라이브러리가 필요한데

아두이노 스케치 프로그램내에 기본 라이브러리로 포함되어 있습니다.

I2C 모듈은 각자 자기 고유의 주소값이 있는데 확인하는 방법은 http://cafe.naver.com/diymaker/15 글을 확인하세요.

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

RTC_DS1307 RTC;
 
void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__)); //현재 시간설정
  }
 
}
 
void loop () {
    DateTime now = RTC.now();

    lcd.clear();
    lcd.home(); //첫행
    lcd.print(now.year(), DEC);
    lcd.print('/');
    lcd.print(now.month(), DEC);
    lcd.print('/');
    lcd.print(now.day(), DEC);
    
    lcd.setCursor(0, 1); //다음 행
   lcd.print(getTimeStr(now.hour()));
    lcd.print(':');
    lcd.print(getTimeStr(now.minute()));
    lcd.print(':');
    lcd.print(getTimeStr(now.second()));
 
    delay(1000);
}

String getTimeStr(int time)
{
    String str;
    if (time < 10) { str = "0" + String(time); } 
    else { str = String(time); }
    return str;
}


댓글 없음:

댓글 쓰기