Nowadays, LCD technology is used in advanced displays for mobile phones, PC monitors, and televisions. For color displays, each pixel is made up of three subpixels for red, green, and blue. Each subpixel can be set to 256 different shades of its color, so it is therefore possible for a single LCD pixel to display 256∗256∗256=16.8million different colors. The pixel color is therefore usually referred to by a 24-bit value where the highest 8 bits define the red shade, the middle 8bits define the green shade and the lower 8bits represent the blue shade, as shown in Table 8.6.
Table 8.6
24-bit color values.
Color
24-Bit Value
Red
0xFF0000
Green
0x00FF00
Blue
0x0000FF
Yellow
0xFFFF00
Orange
0xFF8000
Purple
0x800080
Black
0x000000
White
0xFFFFFF
Given that each pixel needs to be assigned a 24-bit value and a 1280×1024 LCD computer display has over 1million pixels (1280∗1024=1310720), we clearly need to send a lot of data to a color LCD display. Standard color LCDs are set to refresh the display at a frequency of 60Hz, so the digital input requirements are much greater than those associated with the alphanumeric LCD discussed previously in this chapter.
The uLCD-144-G2 display module is a compact 1.44″ LCD color screen, as shown in Fig. 8.13 and described in Refs. [7,8]. The display uses a UART serial communications interface to receive display data from a connected microprocessor. Connecting the display to an mbed can be as shown in Table 8.7.
Table 8.7
Pin connections for the uLCD-144-G2 display module.
Function
uLCD-144-G2 Pin
mbed Pin
Power (5V)
1
39
Ground
7
1
TX/RX
3
10
RX/TX
5
9
Reset
9
11
The 4DGl-uLCD-SE library, by developer Jim Hamblen found in Ref. [9], can be used to interface the display module. This library is actually a modification of an earlier one that was originally developed for a color display module called the uLCD_4DGL. Hence its header file still refers to that original module, though it is updated to also support the newer uLCD-144-G2 hardware variant that we are using. The library shares many similar API functions with the C12832 library and display discussed previously. However, for this display, colors are defined as 24-bit hexadecimal numbers as detailed in Table 8.6. Program Example 8.13 prints formatted text in red green and blue using the library's color( ) and printf( ) functions.
/∗Program Example 8.13: Displaying color text on the uLCD-144-G2 ∗/ #include "mbed.h" #include "uLCD_4DGL.h"// library also supports uLCD-144-G2 variant
uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
int main() { uLCD.color(0xFF0000);// set text color to red uLCD.printf("Text in RED
"); uLCD.color(0x00FF00);// set text color to green uLCD.printf("Text in GREEN
"); uLCD.color(0x0000FF);// set text color to blue uLCD.printf("Text in BLUE
"); }
Program Example 8.13: displaying color text on the uLCD_4DGL
Exercise 8.12
Connect a uLCD_144-G2 color display to the mbed as described in Table 8.7. Compile Program Example 8.13 and verify that the text is displayed in the correct colors.
Modify the text colors to test different 24-bit color values. You can try all the color values shown in Table 8.6 and some other 24-bit values of your own choosing.
Program Example 8.14 gives code that defines the uLCD object and draws a number of consecutive blue circles with increasing radii. The circles are drawn with a shared center at pixel (64,64), which is the center pixel of the display. Note that the circles increment in radius by 3pixels on each iteration until the maximum radius of 64 is reached.
/∗Program Example 8.14: drawing concentric color circles on the uLCD_4DGL ∗/ #include "mbed.h" #include "uLCD_4DGL.h"// library also supports uLCD-144-G2 variant
uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
int main() { while(1) { for (int r=0; r<=64; r+=3) {// increment r by 3 each time uLCD.circle(64, 64, r, 0x0000FF);// draw blue circle of radius r wait(0.1); } uLCD.cls(); } }
Program Example 8.14: drawing concentric color circles on the uLCD-144-G2
Exercise 8.13
Compile Program Example 8.14 and verify that the concentric circles are drawn.
• Modify the color of the circles to test different 24-bit color values.
• Modify the radius increment value and observe the changes. You can modify the maximum increment value and the wait time too.
• Can you make the circles shrink back down in size, rather than reset each time the for loop completes?
• Can you make the color change with each increment?
8.7. Mini Project: Digital Spirit Level
Design, build, and test a digital spirit level based on the mbed. Use an ADXL345 (Section 7.3) accelerometer to measure the angle of orientation in two planes, a digital push-to-make switch to allow calibration and zeroing of the orientation, and a color LCD to output the measured orientation data, in degrees.
To help you proceed consider the following:
1. Design your display to show a pixel or image moving around the LCD screen with respect to the orientation of the accelerometer. The spirit level should be able to respond to movements in two axes (sometimes referred to as tilt and roll), we can refer to these as the x and y axes for simplicity.
2. Add the digital switch to allow simple calibration and zeroing of the data.
3. Improve your display output to give measurements of x and y axes angles in degrees from the horizontal. For example, a perfectly flat spirit level will read 0degrees in both the x and y axes. Tilting the spirit level will cause a positive or negative reading in the x axis, whereas rolling the spirit level to either the left or right will give a positive or negative reading in the y axis.
4. How accurate are your x and y readings? Set up a number of known angles and test your spirit level; continuously improve your code until accurate readings are achieved at all angles in both axes.
Chapter Review
• LCDs use an organic crystal which can polarize and block light when subjected to an electric field.
• Many types of LCDs are available and, when interfaced with a microcontroller, they allow digital control of alphanumeric character displays and high-resolution color displays.
• The PC1602F is a 16 column by 2 row character display which can be controlled by the mbed.
• Data can be sent to the LCD registers to initialize the device and to display character messages.
• Character data is defined using the 8-bit ASCII table.
• The mbed TextLCD library can be used to simplify working with LCDs and allow the display of formatted data using the printf( ) function.
• The NHD-C12832 display, which is installed on the mbed application board, has 128×32 pixels, allowing graphics and images, as well as alphanumeric text, to be displayed.
• Color LCDs frequently transfer data through a serial interface, with each pixel given a 24-bit color setting.
• The uLCD-144-G2 display module is a color LCD display screen that uses a UART serial communications interface to receive display data from a microprocessor, such as the mbed.
Quiz
1. What are the advantages and disadvantages associated with using an alphanumeric LCD in an embedded system?
2. What types of cursor control are commonly available on alphanumeric LCDs?
3. How does the mbed BusOut object help to simplify interfacing an alphanumeric display?
4. What is the function of the E input on an alphanumeric display such as the PC1602F?
5. What does the term ASCII refer to?
6. What are the ASCII values associated with the numerical characters from 0–9?
7. Referring to the TextLCD library, describe the C code required to display the value of a floating point variable called “ratio” to 2 decimal places in the middle of the second row of a 2×16 character alphanumeric display?
8. What is a bitmap and how can it be used to display images on an LCD display?
9. List and describe five practical examples of a color LCD used in an embedded system.
10. If a color LCD display is filled to a single background color, what colors will the following 24-bit codes give: