Sei sulla pagina 1di 11

4/22/2019

EMBEDDED SYSTEMS

CHAPTER FIVE
LCD & ANALOG I/O
ARDUINO PROGRAMMING
Al-Hussein Bin Talal University
Faculty of Engineering
Department of Computer Engineering
Spring 2019

Dr. Moath Alsafasfeh

LCD

■ The LiquidCrystal library allows you to control LCD displays that are compatible with
the Hitachi HD44780 driver.
■ you can usually tell them by the 16-pin interface.
■ The LCDs have a parallel interface, meaning that the microcontroller has to
manipulate several interface pins at once to control the display

1
4/22/2019

LCD cont..

LCD Interface

■ The LCDs have a parallel interface, meaning that the microcontroller has to
manipulate several interface pins at once to control the display. The interface
consists of the following pins:
– A register select (RS) pin that controls where in the LCD's memory you're
writing data to. You can select either the data register, which holds what goes
on the screen, or an instruction register, which is where the LCD's controller
looks for instructions on what to do next.

2
4/22/2019

LCD Interface

– A Read/Write (R/W) pin that selects reading mode or writing mode


– An Enable pin that enables writing to the registers
– 8 data pins (D0 -D7). The states of these pins (high or low) are the bits that
you're writing to a register when you write, or the values you're reading when
you read.
– There's also a display constrast pin (Vo), power supply pins (+5V and
Gnd) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the
LCD, control the display contrast, and turn on and off the LED backlight,
respectively.

LiquidCrystal Library

■ This library allows an Arduino board to control LiquidCrystal displays (LCDs) based
on the Hitachi HD44780 (or a compatible) chipset, which is found on most text-
based LCDs.
■ The library works with in either 4- or 8-bit mode (i.e. using 4 or 8 data lines in
addition to the rs, enable, and, optionally, the rw control lines).

3
4/22/2019

LCD Functions
■ LiquidCrystal(): Creates a variable of type LiquidCrystal. The display can be controlled
using 4 or 8 data lines. If the former, omit the pin numbers for d0 to d3 and leave
those lines unconnected. The RW pin can be tied to ground instead of connected to a
pin on the Arduino; if so, omit it from this function's parameters.
– Syntax: LiquidCrystal(rs, enable, d4, d5, d6, d7)

■ begin(): Initializes the interface to the LCD screen, and specifies the dimensions (width
and height) of the display.
– Syntax: lcd.begin(cols, rows)
■ clear(): Clears the LCD screen and positions the cursor in the upper-left corner.
– Syntax lcd.clear()

■ home(): Positions the cursor in the upper-left of the LCD


– Syntax: lcd.home()

LCD Functions

■ setCursor(): Position the LCD cursor


– Syntax lcd.setCursor(col, row)
■ write(): Write a character to the LCD
– Syntax: lcd.write(data)
■ print(): Prints text to the LCD.
– Syntax: lcd.print(data)
lcd.print(data, BASE)
■ cursor(): Display the LCD cursor
– Syntax: lcd.cursor()
■ noCursor(): Hides the LCD cursor.
– Syntax: lcd.noCursor()

4
4/22/2019

LCD Functions

■ blink() Display the blinking LCD cursor.


– Syntax: lcd.blink()
■ noBlink(): Turns off the blinking LCD cursor.
– Syntax: lcd.noBlink()
■ display(): Turns on the LCD display, after it's been turned off with noDisplay(). This will restore the
text (and cursor) that was on the display.
– Syntax: lcd.display()
■ noDisplay(): Turns off the LCD display, without losing the text currently shown on it.
– Syntax: lcd.noDisplay()
■ scrollDisplayLeft(): Scrolls the contents of the display (text and cursor) one space to the left
– Syntax: lcd.scrollDisplayLeft()

LCD Functions

■ scrollDisplayRight(): Scrolls the contents of the display (text and cursor) one space to the right.
– Syntax: lcd.scrollDisplayRight()
■ autoscroll(): Turns on automatic scrolling of the LCD. This causes each character output to the display to push previous characters
over by one space.
– Syntax: lcd.autoscroll()
■ noAutoscroll(): Turns off automatic scrolling of the LCD.
– Syntax: lcd.noAutoscroll()
■ leftToRight(): Set the direction for text written to the LCD to left-to-right, the default.
– Syntax: lcd.leftToRight()
■ rightToLeft(): Set the direction for text written to the LCD to right-to-left (the default is left-to-right)
– Syntax: lcd.rightToLeft()
■ createChar(): Create a custom character (glyph) for use on the LCD. Up to eight characters of 5x8 pixels are supported (numbered
0 to 7).
– Syntax: lcd.createChar(num, data)

5
4/22/2019

LCD Example
"Hello World!“
Schematic

LCD Example "Hello World!“, Code

6
4/22/2019

LCD Blink

Sensing analog inputs and continuous


values
■ analog inputs can continuously provide variable values by measuring voltage from 0
V to 5 V. It means a value of 1.4 V and another value of 4.9 V would be interpreted
as totally different values. This is very different from a digital input that could
interpret them as…1
■ in the continuous world of analog inputs, we can sense a flow between the different
values, where digital inputs can provide only steps

7
4/22/2019

Sensing analog inputs and continuous


values
■ So how many values can be distinguished by Arduino's analog inputs? 1024.
■ Because Arduino's chip works in the digital domain for all calculations, we have to
convert analog values from 0 V to 5 V to a digital one. The purpose of the analog-
todigital converter, housed within the chipset itself, is exactly this. This device is also
referred to using the acronym ADC.
■ Arduino's ADCs have a 10-bit resolution. This means that every analog value is
encoded and mapped to a 10-bit, encoded integer value

Reading analog inputs


Changing the blinking delay of an LED with an analog input

■ potentiometer
is a variable
resistor.

8
4/22/2019

Analog Functions
■ analogWrite(): Writes an analog value (PWM wave) to a pin. Can be used to light a
LED at varying brightnesses or drive a motor at various speeds.
■ Syntax: analogWrite(pin, value)

■ analogRead(): Reads the value from the specified analog pin. Arduino boards
contain a multichannel, 10-bit analog to digital converter. This means that it will
map input voltages between 0 and the operating voltage(5V or 3.3V) into integer
values between 0 and 1023.
– Syntax: analogRead(pin)

Example reads the voltage on


analogPin and displays it.

9
4/22/2019

Example X

■ Reads an analog input pin, maps the result to a range from 0 to 255 and uses the
result to set the pulsewidth modulation (PWM) of an output pin.
■ prints the results to the serial monitor.

Example X
Code

10
4/22/2019

Analog read and write


Example: Sets the output to the LED proportional to the value
read from the potentiometer.

11

Potrebbero piacerti anche