Arduino/캐릭터 LCD

LCD에 한 글자씩 출력하기

DesignatedRoom 2020. 10. 26. 18:37

하드웨어의 구성은 아래에서 참고하자.

designatedroom87.tistory.com/240

 

포텐시오미터와 캐릭터 LCD의 화면 밝기 조절 하기

그림은 ArduinoGetStarted.com에서 발췌 부품에 대해서 알아보자. 가변 저항과 캐릭터 LCD와 아두이노는 다음과 같이 연결한다. 연결이 상당히 복잡하다. 그림을 실제 연결한 그림을 보자. 아두이노의 5

designatedroom87.tistory.com

 

 

프로그래밍의 예제는 문자 a부터 z를 LCD에 출력을 하는데(출력의 방향은 기본으로 오른쪽 방향)

만약 문자가 m이면 방향을 변경하고 다시 문자가 s이면 다시 방향을 변경한다.

그리고 마지막으로 문자 z를 출력하면 다시 위의 과정을 반복한다. 

LiquidCrystal를 헤더파일로 추가하고, 이 라이브러리에 있는 함수들을 사용한다.

 

소스 코드

더보기
/*
 LiquidCrystal Library - TextDirection

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.

 This sketch demonstrates how to use leftToRight() and rightToLeft()
 to move the cursor.

 The circuit:
  * LCD RS pin to digital pin 12
  * LCD Enable pin to digital pin 11
  * LCD D4 pin to digital pin 5
  * LCD D5 pin to digital pin 4
  * LCD D6 pin to digital pin 3
  * LCD D7 pin to digital pin 2
  * LCD R/W pin to ground
  * 10K resistor:
  * ends to +5V and ground
  * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 modified 7 Nov 2016
 by Arturo Guadalupi

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystalTextDirection

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int thisChar = 'a';

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // turn on the cursor:
  lcd.cursor();
}

void loop() {
  // reverse directions at 'm':
  if (thisChar == 'm') {
    // go right for the next letter
    lcd.rightToLeft();
  }
  // reverse again at 's':
  if (thisChar == 's') {
    // go left for the next letter
    lcd.leftToRight();
  }
  // reset at 'z':
  if (thisChar > 'z') {
    // go to (0,0):
    lcd.home();
    // start again at 0
    thisChar = 'a';
  }
  // print the character
  lcd.write(thisChar);
  // wait a second:
  delay(1000);
  // increment the letter:
  thisChar++;
}

 

 

 

그리고, 캐릭터 LCD는 가로로 16개의 글자를 출력할 수 있다고 하였는데,

만약 "abcdefghijklmnopqrstu" 이와 같이 16자가 넘는 길이의 문자열이 입력되었다면

그 LCD의 아래 줄에 표시가 되도록 해보자.

소스 코드

더보기
//  2가지 일을 동시에 하려니까 느리다. 인터럽트 혹은 쓰레드를 이용하여야 한다.
#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int x, y = 0;

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  
  lcd.setCursor(x,y);

  // turn on the cursor:
  lcd.cursor();

  Serial.begin(9600);
}

void loop() {
  
  lcd.cursor();

  delay(500);

  lcd.noCursor();

  delay(500);

  //  한 문자가 들어왔다면 
  if (Serial.available())
  {
    lcd.print((char)Serial.read());
    x += 1;
    
    if (x >= 16)
    {
      y += 1;
      x = 0;
      lcd.setCursor(x,y);
    }
  }
}