본문 바로가기
EmbeddedSystem/Arduino

[Arduino] LCD Display(1602A - parallel communication)

by TSpoons 2024. 10. 1.

https://docs.arduino.cc/learn/electronics/lcd-displays/

 

https://docs.arduino.cc/learn/electronics/lcd-displays/

 

docs.arduino.cc

 
 
간단히 원하는 문자열과 현재 시스템의 시간 등의 정보를 시각화하기 위해 LCD 디스플레이를 사용하고자 한다.
 
 

I2C(Inter-Integrated Circuit) 인터페이스

- 새로운 쉴드가 필요함
 

 
 

 

병렬 인터페이스:

마이크로컨트롤러가 여러 핀을 동시에 제어하여 LCD 디스플레이를 조작

 

PIN map

- RS (Register Select) 핀: LCD의 메모리에서 어떤 레지스터를 사용할지를 선택
-- 명령 레지스터에 접근하여 LCD의 동작을 제어하는 명령(0)
-- 데이터 레지스터에 접근하여 화면에 표시할 문자를 전송(1)
- R/W (Read/Write) 핀: 읽기 모드(1) or 쓰기 모드(0)
- Enable 핀(E): 데이터 전송을 활성화하는 신호
- 데이터 핀 (D0-D7): 이 핀들의 상태(High/Low)는 레지스터에 기록되거나 읽힐 비트 값들을 나타냄
 
- Vo: 디스플레이의 대비 제어.(0~255)
- 전원 핀: +5V와 GND로 LCD에 전원 공급
- LED 백라이트 핀: Bklt+와 Bklt-로 백라이트를 on off
 
 

 



https://github.com/arduino-libraries/LiquidCrystal

 

GitHub - arduino-libraries/LiquidCrystal: Liquid Crystal Library for Arduino

Liquid Crystal Library for Arduino. Contribute to arduino-libraries/LiquidCrystal development by creating an account on GitHub.

github.com

 
 

라이브러리 다운 후 코드 작성

 

#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);

// 이전 시간 저장 변수
unsigned long previousMillis = 0;
int previousSecond = -1;  

void setup() {
  // LCD init
  lcd.begin(16, 2);
  // row 1 
  lcd.setCursor(0, 0);
  lcd.print("Hello, world!");
}

void loop() {

  unsigned long currentMillis = millis();

  int currentSecond = currentMillis / 1000;

  if (currentSecond != previousSecond) {
    
    // row 2
    lcd.setCursor(0, 1);
    lcd.print("Seconds: ");
    lcd.print(currentSecond);
    lcd.print("   "); // 5칸 이상 삭제
    

    previousSecond = currentSecond;
  }
}

 


 
한 자리 당 5*8= 40개의 pixel로 이루어져 있고, 이를 활용해서 정보를 전달할 수 있다.

1602A LCD datasheet를 보면
 

 
 
 "hello, world!" 같은 알파벳은 가능
기본적으로 HD44780 LCD 드라이버에 내장된 CGRAM(Character Generator RAM)을 사용한다.
만약 사용자 정의 문자를 사용하고 싶다면, CGRAM을 직접 설정해야 함.
 


 
※근데 가변저항(potentiometer)이 없어서 그냥 9번 핀(PWM 가능)이용해서 조절하려고 한다.

const int contrastPin = 9; 
int contrastValue = 10;   

void setup() {
  pinMode(contrastPin, OUTPUT);
  analogWrite(contrastPin, contrastValue);


}

 
 
근데 PWM은 전압이 일정하지 않아 RC 필터를 이용해서 일정한 DC 전압으로 유지해야 하는데,
이건.. 뒤로 미뤄야겠다. 

#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);

const int contrastPin = 9; 
int contrastValue = 55;

// 이전 시간 저장 변수
unsigned long previousMillis = 0;
int previousSecond = -1;  

void setup() {
  pinMode(contrastPin, OUTPUT);
  analogWrite(contrastPin, contrastValue);
  // LCD init
  lcd.begin(16, 2);
  // row 1 
  lcd.setCursor(0, 0);
  lcd.print("Hello, world!");
}

void loop() {

  unsigned long currentMillis = millis();

  int currentSecond = currentMillis / 1000;

  if (currentSecond != previousSecond) {
    
    // row 2
    lcd.setCursor(0, 1);
    lcd.print("Seconds: ");
    lcd.print(currentSecond);
    lcd.print("   "); // 5칸 이상 삭제
    

    previousSecond = currentSecond;
  }
}