PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit Beaglebone.org.

Thursday, June 19, 2014

On 11:23 AM by Richard St-Pierre

Overview

Put your work on 'display' with this tutorial .  A step-by-step guide to adding a low-cost alpha-numeric Liquid Crystal Display to your BeagleBone project.  Present useful information,  measurement readings and messages .  The LCD interfacing method provided uses only 3 pins from the BeagleBone, leaving most pins available for other parts of your project. How do we do this?... Let's see!

What you will need


    Instructions

    This project uses the Adafruit_BBIO Python library, to access the GPIO pins of the BeagleBone.
    You may need to download this library{Angstrom Linux}.

    opkg update && opkg install python-pip python-setuptools      
    pip install Adafruit_BBIO
    

    Step 1

    Connect the display circuit as shown. 
    Note this circuit only requires three signals (as well as power: +5V and ground) from the BeagleBone.
    Some displays have extra pin for backlighting. Verify the LCD pin assignments in the datasheet for the display you are using.


    Connection Summary

    IC1= 74HC164 DIP14, potentiometer = 10 kilohms

    From
    To
    Signal Description
    LCD-1P9-1Ground
    LCD-2P9-7+5V
    LCD-3Potentiometer Pin 2 Contrast (center pin /wiper)
    LCD-4P8-18Register select (also data in)
    LCD-5GroundRead/write (always write)
    LCD-6P8-14LCD Enable (strobe)
    LCD-7IC1-3LCD Data 0
    LCD-8IC1-4LCD Data 1
    LCD-9IC1-5LCD Data 2
    LCD-10IC1-6LCD Data 3
    LCD-11IC1-10LCD Data 4
    LCD-12IC1-11LCD Data 5
    LCD-13IC1-12LCD Data 6
    LCD-14IC1-13LCD Data 7
    Ground (P9-1)IC1-7IC ground
    +5V (P9-7)IC1-14,9IC Power and Reset pin
    +5VPotentiometer Pin 3High side of 10K potentiometer 
    GNDPotentiometer Pin 1Low side of 10K potentiometer
    P8-16IC1-8Clock signal
    P8-18IC1-1,2Serial Data Input

    Step 2

    Adjust the contrast level to approximately 0.7~1.0 V (depends on the display you are using).

    Step 3

    Project Software Program

    Download lcm.py File and save this file (as lcm.py) to a working location on your BeagleBone.

    Program Listing

    #!/usr/bin/python
    # FileName: lcm.py
    # Author  : Richard St-Pierre
    # Version : 0.0 Nov 6, 2013     Beagle Bone Black version
    #
    
    import Adafruit_BBIO.GPIO as GPIO
    import time
    
    # --- Interface Pins ---
    CSA_PIN = "P8_14"
    SCK_PIN = "P8_16"
    SDI_PIN = "P8_18"
    
    # --- LCD commands ---
    CMD_CLS   = 0x01
    CMD_HOME  = 0x02
    CMD_ENTRY = 0x06
    CMD_SCROL = 0x07
    CMD_OFF   = 0x08
    CMD_ON    = 0x0C
    CMD_CURS  = 0x0E
    CMD_LINE1 = 0x80
    CMD_LINE2 = 0xC0
    CMD_FSET  = 0x30
    CMD_FSET2 = 0x38
    
    msg       = ['A','B','C']
    logo      = 'Logic Supply'
    
    
    #--- Initialize pins ---
    GPIO.setup(SCK_PIN, GPIO.OUT)
    GPIO.setup(SDI_PIN, GPIO.OUT)
    GPIO.setup(CSA_PIN, GPIO.OUT)
    
    #--- Logo ---
    print "BBB SPI LCM test/demo"
    
    #--- Send DATA to LCM ---
    def lcm_data (data):
      GPIO.output(SCK_PIN, GPIO.LOW)
      for i in range (8):
           if ((data<<i) & 0x80):
              GPIO.output(SDI_PIN, GPIO.HIGH)
           else:
              GPIO.output(SDI_PIN, GPIO.LOW)
           GPIO.output(SCK_PIN, GPIO.HIGH)
           GPIO.output(SCK_PIN, GPIO.LOW)
      GPIO.output(SDI_PIN, GPIO.HIGH)       #data
      GPIO.output(CSA_PIN, GPIO.LOW)        #strobe/en
      GPIO.output(CSA_PIN, GPIO.HIGH)
    
    
    
    #--- Send CMD to LCM ---
    def lcm_cmd (cmd):
      GPIO.output(SCK_PIN, GPIO.LOW)
      for i in range (8):
            if ((cmd<<i) & 0x80):
               GPIO.output(SDI_PIN, GPIO.HIGH)
            else:
                GPIO.output(SDI_PIN, GPIO.LOW)
            GPIO.output(SCK_PIN, GPIO.HIGH)
            GPIO.output(SCK_PIN, GPIO.LOW)
      GPIO.output(SDI_PIN, GPIO.LOW)        #cmd
      GPIO.output(CSA_PIN, GPIO.LOW)        #strobe/en
      GPIO.output(CSA_PIN, GPIO.HIGH)
    
    
    def lcm_msg (msg):
        for char in msg:
           lcm_data(ord(char))
    
    
    def lcm_init ():
        lcm_cmd(CMD_FSET)
        lcm_cmd(CMD_FSET)
        lcm_cmd(CMD_ENTRY)
        lcm_cmd(CMD_ON)
        lcm_cmd(CMD_CLS)
    
    
    #--- Init and Display ---
    
    lcm_init()
    time.sleep(0.1)
    
    while True:
      lcm_cmd(CMD_CLS)
      lcm_data(0x42)
      lcm_data(0x45)
      lcm_data(0x41)
      lcm_data(0x47)
      lcm_data(0x4C)
      lcm_data(0x45)
      lcm_data(0x20)
      lcm_data(0x42)
      time.sleep(2)
      lcm_cmd(CMD_CLS)
      time.sleep(0.1)
      lcm_cmd(CMD_CLS)
      lcm_msg("Logic")
      time.sleep(2)
      lcm_cmd(CMD_CLS)
      lcm_msg("Supply")
      time.sleep(2)
    
    

    Step 4

    To run the program start putty or open a terminal and enter the following:

    python lcm.py


    Your display will sequence through a number of messages. You are on your way.
    Customize the messages to suit your needs.

    Program Overview

    This program takes data (characters) and sends it serially one bit at-a-time to an IC (Integrated Circuit) that converts it back to 8-bits.  This saves many pins that can be used for controlling other devices.

    Let's go through the program listing, and review the program flow.
    This program imports the required libraries for GPIO and time. Then three pins are declared.

    CSA  (Enable) strobes the display with a command or data.
    SCK (Serial Clock) controls the shifting of the serial data bits.
    SDI   (Serial Data In) is used to send serial data bits on the rise of the SCK signal.

    The most common commands for the display are listed next.
    We then initialize the three pins as outputs. An introduction message is sent to the computer terminal.

    Next, we define four functions:
    lcm_data(), lcm_cmd(), lcm_msg(), lcm_init()

    lcm_data()
    This function takes an 8-bit data value (a character to display) and sends it bit-by-bit to the IC chip. Each bit is shifted out (Most-Significant-Bit first) and each data bit is clocked out (8 times). Once the data has been sent, the SDI_Pin is set high. This signal to the display that the 8-bits of data is a characters to be displayed. Now that the data has been serially shifted out to the IC, the data is ready for the LCD display to read it. Rising the CSA_Pin signals the display to take and process this data.

    lcm_cmd()
    This function is similar to lcm_data(), with one small change. The SDI_PIN is set low after the 8-bits have been sent. This signal to the display, that the information sent is a command (such as clear display, set location, control cursors...)

    lcm_msg()
    This function is used to display a message, by calling lcm_data() for each character in the word or message to display.

    lcm_init()
    This final function simply sends the required commands to initialize the display. Setting the number of lines, entry mode, turning ON the display and clearing the display for a fresh start.

    The last section of the program, is a simple loop that demonstrates the functions in use. Sending commands, individual characters and complete messages. Some pauses are inserted to allow the messages to be viewed before a new one is displayed.

    There you have it.

    Hope you get the "message"!