RaspberryPI - HD44780 (1602a 16x2 Text Lcd) 제어하기

OS/Raspberry Pi 2015.10.02 00:36 Posted by 파란크리스마스

출처 : Using a 16x2 LCD Display with a Raspberry Pi
LCD Display Tutorial for Raspberry Pi
Interfacing 16x2 LCD with Raspberry Pi using GPIO & Python
16×2 LCD Module Control Using Python
rasbperry pi B gpio between extention board mapping
Raspberry Pi 2 Model B GPIO 40 Pin Block Pinout
How To Use A MCP23017 I2C Port Expander With The Raspberry Pi ? Part 1

Using a 16x2 LCD Display with a Raspberry Pi

Breadboard 설치된 최종 모습

회로도

실행 - lcd.lcd_init()

$ sudo python
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.chdir('/home/pi')
>>> import lcd
>>> lcd.lcd_init()

 

실행 - Line 1 문자열 출력

$ sudo python
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.chdir('/home/pi')
>>> import lcd
>>> lcd.lcd_init()
>>> lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
>>> lcd.lcd_string("Raspberry PI", 2)

 

실행 - Line 2 문자열 출력

$ sudo python
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.chdir('/home/pi')
>>> import lcd
>>> lcd.lcd_init()
>>> lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
>>> lcd.lcd_string("Raspberry PI", 2)
>>> lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
>>> lcd.lcd_string("Hello world", 2)

 

실행 - 최종

$ sudo python
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.chdir('/home/pi')
>>> import lcd
>>> lcd.lcd_init()
>>> lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
>>> lcd.lcd_string("Raspberry PI", 2)
>>> lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
>>> lcd.lcd_string("Hello world", 2)
>>> lcd.GPIO.cleanup()
>>> quit()

소스 lcd.py

#!/usr/bin/python
#
# HD44780 LCD Test Script for
# Raspberry Pi
#
# Author : Matt Hawkins
# Site   : http://www.raspberrypi-spy.co.uk
# 
# Date   : 03/08/2012
#

# The wiring for the LCD is as follows:
# 1 : GND
# 2 : 5V
# 3 : Contrast (0-5V)*
# 4 : RS (Register Select)
# 5 : R/W (Read Write)       - GROUND THIS PIN
# 6 : Enable or Strobe
# 7 : Data Bit 0             - NOT USED
# 8 : Data Bit 1             - NOT USED
# 9 : Data Bit 2             - NOT USED
# 10: Data Bit 3             - NOT USED
# 11: Data Bit 4
# 12: Data Bit 5
# 13: Data Bit 6
# 14: Data Bit 7
# 15: LCD Backlight +5V**
# 16: LCD Backlight GND

#import
import RPi.GPIO as GPIO
import time

# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E  = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18
#LED_ON = 15

# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line 

# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005

def main():
  # Main program block

  # Initialise display
  lcd_init()

  # Toggle backlight on-off-on
  #GPIO.output(LED_ON, True)
  #time.sleep(1)
  #GPIO.output(LED_ON, False)
  #time.sleep(1)
  #GPIO.output(LED_ON, True)
  #time.sleep(1)

  # Send some centred test
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Rasbperry Pi",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("Model B",2)

  time.sleep(3) # 3 second delay

  # Send some left justified text
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("1234567890123456",1)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("abcdefghijklmnop",1)

  time.sleep(3) # 3 second delay

  # Send some right justified text
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Raspberrypi-spy",3)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(".co.uk",3)

  time.sleep(30)

  # Turn off backlight
  #GPIO.output(LED_ON, False)

def lcd_init():
  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  #GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable  
  # Initialise display
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD)  
  lcd_byte(0x06,LCD_CMD)
  lcd_byte(0x01,LCD_CMD)  

def lcd_string(message,style):
  # Send string to display
  # style=1 Left justified
  # style=2 Centred
  # style=3 Right justified

  if style==1:
    message = message.ljust(LCD_WIDTH," ")  
  elif style==2:
    message = message.center(LCD_WIDTH," ")
  elif style==3:
    message = message.rjust(LCD_WIDTH," ")

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)   

if __name__ == '__main__':
  main()

pi4j - 핀 배열

pi4j - 예제

import java.text.SimpleDateFormat;
import java.util.Date;

import com.pi4j.component.lcd.LCDTextAlignment;
import com.pi4j.component.lcd.impl.GpioLcdDisplay;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiGpioProvider;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.RaspiPinNumberingScheme;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

public class LcdExample2 {

	public final static int LCD_ROWS = 2;
	public final static int LCD_ROW_1 = 0;
	public final static int LCD_ROW_2 = 1;
	public final static int LCD_COLUMNS = 16;
	public final static int LCD_BITS = 4;

	public static void main(String args[]) throws InterruptedException {

		System.out.println("<--Pi4J--> GPIO 4 bit LCD example program");
		
		// create gpio controller
		final GpioController gpio = GpioFactory.getInstance();

		// initialize LCD
		//new GpioLcdDisplay(LCD_ROWS, LCD_COLUMNS, RaspiPin.GPIO_06, RaspiPin.GPIO_05, RaspiPin.GPIO_04, RaspiPin.GPIO_00, RaspiPin.GPIO_01, RaspiPin.GPIO_03);
		final GpioLcdDisplay lcd = new GpioLcdDisplay(LCD_ROWS, // number of row
																														// supported by LCD
				LCD_COLUMNS, // number of columns supported by LCD
				RaspiPin.GPIO_11, // LCD RS pin          7
				RaspiPin.GPIO_10, // LCD strobe pin      8
				RaspiPin.GPIO_06, // LCD data bit 1     25
				RaspiPin.GPIO_05, // LCD data bit 2     24
				RaspiPin.GPIO_04, // LCD data bit 3     23
				RaspiPin.GPIO_01); // LCD data bit 4    18

		// provision gpio pins as input pins with its internal pull up resistor
		// enabled
		/*
		final GpioPinDigitalInput myButtons[] = { 
				gpio.provisionDigitalInputPin(RaspiPin.GPIO_13, "B1", PinPullResistance.PULL_UP),
				gpio.provisionDigitalInputPin(RaspiPin.GPIO_07, "B2", PinPullResistance.PULL_UP),
				gpio.provisionDigitalInputPin(RaspiPin.GPIO_04, "B3", PinPullResistance.PULL_UP),
				gpio.provisionDigitalInputPin(RaspiPin.GPIO_12, "B4", PinPullResistance.PULL_UP)
		};

		// create and register gpio pin listener
		gpio.addListener(new GpioPinListenerDigital() {
			@Override
			public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
				if (event.getState() == PinState.LOW) {
					lcd.writeln(LCD_ROW_2, event.getPin().getName() + " PRESSED", LCDTextAlignment.ALIGN_CENTER);
				} else {
					lcd.writeln(LCD_ROW_2, event.getPin().getName() + " RELEASED", LCDTextAlignment.ALIGN_CENTER);
				}
			}
		}, myButtons);
		*/
		
		// clear LCD
		lcd.clear();
		Thread.sleep(1000);

		// write line 1 to LCD
		lcd.write(LCD_ROW_1, "The Pi4J Project");

		// write line 2 to LCD
		lcd.write(LCD_ROW_2, "----------------");

		// line data replacement
		for (int index = 0; index < 5; index++) {
			lcd.write(LCD_ROW_2, "----------------");
			Thread.sleep(500);
			lcd.write(LCD_ROW_2, "****************");
			Thread.sleep(500);
		}
		lcd.write(LCD_ROW_2, "----------------");

		// single character data replacement
		for (int index = 0; index < lcd.getColumnCount(); index++) {
			lcd.write(LCD_ROW_2, index, ">");
			if (index > 0)
				lcd.write(LCD_ROW_2, index - 1, "-");
			Thread.sleep(300);
		}
		for (int index = lcd.getColumnCount() - 1; index >= 0; index--) {
			lcd.write(LCD_ROW_2, index, "<");
			if (index < lcd.getColumnCount() - 1)
				lcd.write(LCD_ROW_2, index + 1, "-");
			Thread.sleep(300);
		}

		// left alignment, full line data
		lcd.write(LCD_ROW_2, "----------------");
		Thread.sleep(500);
		lcd.writeln(LCD_ROW_2, "<< LEFT");
		Thread.sleep(1000);

		// right alignment, full line data
		lcd.write(LCD_ROW_2, "----------------");
		Thread.sleep(500);
		lcd.writeln(LCD_ROW_2, "RIGHT >>", LCDTextAlignment.ALIGN_RIGHT);
		Thread.sleep(1000);

		// center alignment, full line data
		lcd.write(LCD_ROW_2, "----------------");
		Thread.sleep(500);
		lcd.writeln(LCD_ROW_2, "<< CENTER >>", LCDTextAlignment.ALIGN_CENTER);
		Thread.sleep(1000);

		// mixed alignments, partial line data
		lcd.write(LCD_ROW_2, "----------------");
		Thread.sleep(500);
		lcd.write(LCD_ROW_2, "<L>", LCDTextAlignment.ALIGN_LEFT);
		lcd.write(LCD_ROW_2, "<R>", LCDTextAlignment.ALIGN_RIGHT);
		lcd.write(LCD_ROW_2, "CC", LCDTextAlignment.ALIGN_CENTER);
		Thread.sleep(3000);

		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

		// update time
		while (true) {
			// write time to line 2 on LCD
			//if (gpio.isHigh(myButtons)) {
				lcd.writeln(LCD_ROW_2, formatter.format(new Date()), LCDTextAlignment.ALIGN_CENTER);
			//}
			Thread.sleep(1000);
		}

		// stop all GPIO activity/threads by shutting down the GPIO controller
		// (this method will forcefully shutdown all GPIO monitoring threads and
		// scheduled tasks)
		// gpio.shutdown(); <--- implement this method call if you wish to terminate
		// the Pi4J GPIO controller
	}
}

컴파일 및 실행

$ javac -cp ../lib/pi4j-core.jar:../lib/pi4j-device.jar LcdExample2.java
$ sudo java -cp .:../lib/pi4j-core.jar:../lib/pi4j-device.jar LcdExample2

실행 결과





 

티스토리 툴바