microcontroller lcd display free sample
An example C program is given here to show how the LCD can be set up. The connection of the LCD to the microcontroller is shown in Figure 6.9 (if you are using the EasyPIC 7 development board, all you have to do is just connect the LCD to the board). PORT B lower pins (RB0 to RB3) are connected to the upper 4 bits of the LCD (DB4 to DB7). In addition, RB4 and RB5 pins are connected to LCD RS and E pins, respectively.
Lcd_Initialise: This function initialises the LCD in 4-bit mode, as described earlier. The enable clock pulses are sent to the LCD by calling to function Toggle_E, which simply sets the E line to HIGH and then LOW. The 4-bit LCD commands are sent using function Lcd_Write_Cmd.
Lcd_Write_Cmd: This function receives a byte, separates it into two nibbles, sends the high nibble, followed by the low nibble. The E line is toggled after each output.
Lcd_Write_Char: This function sends a character to the LCD. The upper nibble is sent first, followed by the lower nibble. Line E is toggled between each output.
In this tutorial, you will learn to interface anLCD with a pic microcontroller. It is very simple and easy to understand the project for beginners and is commonly used in several electronic products. LCD (Liquid Crystal Display)provides a user-friendly interface and can be very useful for debugging purposes. After completion of this tutorial, you will be able to display data on an LCD using MPLAB XC8 Compiler and Mikro C compiler. We will provide examples with two Compilers such as MPLAB XC8 Compiler and Mikro C for PIC.
The reason LCD is more popular than LED, Seven Segment displays. Because we can display characters, numbers and custom characters with ease ( Just by easily programming a module).
First of all, to interface LCD with a pic microcontroller, we used GPIO pins. GPIO pins are general-purpose input-output pins. Because we send control and data signals to LCD through these I/O pins. Therefore, you should know how to use digital input-output pins of the pic microcontroller. To know about GPIO pins programming, check these tutorials:
It can work in two modes, 4-bit and 8-bit. In this tutorial, we have used the 4-bit mode which uses only 4 data lines, thus saving pins of the microcontroller. So It is recommended to use LCD in four bits mode to save pins of the microcontroller for other applications.
As you can see in this diagram, if we use 8-bit mode interfacing, we will need to use 11 pins of pic microcontroller. On the other hand, if we use 4-bit mode, we need only 6 GPIO pins. Therefore, it is recommended to use 4-bit mode interfacing. The only difference between 4-bit and 8-bit is that data transfer speed is faster for 8-bit mode. However, it doesn’t make any major difference.
A variable resistor is used to adjust the contrast of 5×8 dot pixels according to background light. Therefore, if you are not able to see anything on LCD after programming, the maximum changes are that you need to adjust contrast with the variable resistor. This contrast register makes adjust to the voltage applied on the VEE pin.
For MPLAB XC8 Compiler, we will use the PIC18F4550 microcontroller. For MikroC Pro for PIC, we will use the PIC16F877A microcontroller. In the case of MPLAB XC8, we will develop our own LCD library. Because the XC8 compiler does not provide built-in libraries. In the contrary, MikroC Pro provides libraries for all modules such as LCD, Keypad, ADC Module, UART module.
In this section, we will see how to write example code for 16×2 LCD interfacing with PIC18F4550 microcontroller. Although, you can use see code with other Pic microcontrollers also.
As we mentioned earlier, we can use the 8-bit mode and 4-bit mode interfacing. But due to the efficient use of MCU pins, we will be using 4-bit Mode. To interface LCD, we follow these steps:
In this circuit, we used the PORTB of PIC18F4550. But you can use any PORT. To do this, we need to change the pin assignment inside the code. I will show you how to assign pins for LCD in the next section.
These lines define which pins of the pic microcontroller should connect with LCD. For instance, in this example, we used the PORTD of PIC18F4550 microcontroller. Connect RD0-RD3 pins with D4-D7 pins of LCD respectively and other pins with RW, EN, RS and Power pins. But you can change PORT to other PORT of PIC microcontroller also by changing the PORT name with these commands.
LCDWriteNibble() function is used to write a nibble. Nibble is basically a half a byte. Because we are using LCD in four bits mode. Therefore, we need to send 8-bit commands/data in four bits chunks. This function writes the specified nibble to the LCD.
Because we will use 4-bit mode, data and commands transfer in 4-bits format. Even it requires at least 8-bit to display a character. To resolve this issue, we send data in a 4-bits format two times.
void LCDPutChar(char ch): Writes a character to LCD at current cursor position. This function displays the specified ASCII character at the current position on the LCD.
LCDGoto(char pos, char ln): This function positions the cursor at the specified line and column. Column and line at which the cursor should be positioned between 0-15 for pos (column) and 0-1 for ln(row).
In last section, we have seen how to display ASCII characters or string. But in almost all practical projects, we need to display, integer, float values. This code displays the counter value which increments from 0-9 after every one second. This is the main function of code only. Because the rest of the code is same as the previous program example.
In this section, we will see how to interface LCD with pic microcontroller and programming examples using MikroC for pic. MikroC pro has a built-in library.
We have used 16×2 LCD which means there are 2 rows and 16 characters in each row. So we can display a total of 32 characters at a time in two rows with 16 characters in each row.
This is the main command which prints our text on LCD. It also gives the privilege to specify the position of the text. In the horizontal direction, we count rows number and in a vertical direction, we count the column number. In above command,
However if your string is longer than the number of characters that could be displayed in a row from the starting position, the last characters will not be displayed. E.g. Lcd_Out (1, 6 “LCD Interface”);will display text in row 1 starting from column position 6 and will display only LCD Interfacethe rest of the characters will not be displayed as there is no room for them.
void Lcd_Out_Cp(char *text);will start printing the text from the current cursor position. For example after printing Lcd_Out (1, 1, “LCD”);if you write Lcd_Out_Cp(“Hello”);it will display “Hello”at a position from column position of 4 in row 1.
void Lcd_Chr(char row, char column, char out_char);allows only single characters to be displayed at specified positions. E.g. Lcd_Chr(2, 5, ‘A’); will print only A on column 5 row 2.
void Lcd_Chr_Cp(char out_char); allows to print only single character from current cursor position like after Lcd_Chr(2, 5, ‘A’);if your writeLcd_Chr_Cp(‘B’);it will be printed at row 2 column 6.
To interface LCD withPIC16F877A and display the text ‘LCD INTERFACE’ on it. LCDs come in different sizes and shapes. For this project, we have selected a 16×2 character, alphanumeric LCD. It contains 2 rows of 16 character.
When using PIC microcontroller, the mikroC compiler has a built-in LCD library that supports the commands to carry out LCD initialization. The library consists of a number of functions to control LCDs with 4-bit data interface.
The main program first clears the LCD screen and then displays “LCD INTERFACE” in the first row of LCD. The LCD pin directions are all set as outputs. The RS pin of LCD is set to 1, which indicates that the information received from DB4-DB7 is a valid text to be printed on LCD screen. The EN pin is also set to 1 which indicates that data is send to the LCD.
Programmed LCDs are vastly used for industrial as well as commercial applications. LCDs are used in UPSs or inverters, where voltage and current readings are displayed on the screen. Instructions to be followed are displayed on an LCD screen in airports, banks, hospitals, etc. If you still have any issue after reading this article, feel free to comment on this post with your issues.
Liquid Crystal Display(LCDs) provide a cost effective way to put a text output unit for a microcontroller. As we have seen in the previous tutorial, LEDs or 7 Segments do no have the flexibility to display informative messages.
This display has 2 lines and can display 16 characters on each line. Nonetheless, when it is interfaced with the micrcontroller, we can scroll the messages with software to display information which is more than 16 characters in length.
The LCD is a simple device to use but the internal details are complex. Most of the 16x2 LCDs use a Hitachi HD44780 or a compatible controller. Yes, a micrcontroller is present inside a Liquid crystal display as shown in figure 2.
It takes a ASCII value as input and generate a patter for the dot matrix. E.g., to display letter "A", it takes its value 0X42(hex) or 66(dec) decodes it into a dot matrix of 5x7 as shown in figure 1.
Power & contrast:Apart from that the LCD should be powered with 5V between PIN 2(VCC) and PIN 1(gnd). PIN 3 is the contrast pin and is output of center terminal of potentiometer(voltage divider) which varies voltage between 0 to 5v to vary the contrast.
The LCD module interface with a microcontroller is simple and it is a primitive means of adding a visual appeal to your embedded application. There are two basic types of LCD modules in the market they are, Character LCD and Graphics LCD. Character LCDs are the some of the cheapest means LCD displays available today.
This post is first of a series of four posts that walks through entire process of interfacing an LCD module with a (any) microcontroller with all the basic concepts dealt in detail. Subscribe to our posts and get free updates on these follow-up posts.LCD Module Basic Theory (LCD Controllers, CG&DD RAM, PIN description,Timing Diagram, Commands)
This post will cover the basic theory that you should have a clear understanding of, before getting started with the programming. Some of the sections below are not really essential for the interface but it is a good practice to have a thorough knowledge about what you are indulging in. Whereas some listed below are absolutely mandatory to understand how the LCD module works and to predict how it will behave for a given situation.
The LCD module has display controller that are used to receive the data from the controller and uses it to display the data in a legible format. These controllers have an embedded font set that can be addressed by sending the corresponding ASCII value of the the character to be printed.
Most LCD modules have a HD44780 or compatible controller which is specially designed to build LCDs with one or two lines with a maximum of 40 character positions each. They are ASIC (Application Specific Integrated Circuit). A single HD44780 is able to display two lines of 8 characters each.
This the most common configuration of LCD that most people prefer mostly due to reduced cost and small footprint. In a 16 x 2 line display LCD module, each the two lines have 40 character positions of which only 16 can be displayed at a time. The remaining positions are invisible and cannot be seen. To display the remaining 24 characters, the LCD has an option to move the window of characters displayed to the right or left so that; it appears as though the characters are scrolling. Here is a table of the DD RAM addresses that are within the visible data region. Note that in this module, DD RAM locations 10 to 27 on the first line and 51 to 67 are not covered by the displayable window of 16A character per line.Line/Col0123456789101112131415One000102030405060708090A0B0C0D0E0F
The 20 x 4 display module is a slight variant of the 16 x 2 Module such that, a single 40 character (of which 16 are displayable) line is split up into 2 halves of 20 displayable characters each to make 4 lines. Here the first line displays the first 20 DD RAM locations (00 - 13) and the third line displays the remaining 20 DD RAM locations (14 - 53) of the first line in the case of 16 x 2 LCD Module and the second line displays the first 20 DD RAM locations (40 - 53) and the fourth line displays the remaining 20 DD RAM locations (54 - 67) of the second line in the case of 16 x 2 LCD Module. This is the module that I am using in this post. It has the disadvantage of not being able to scroll but looks better with 4 displayable lines.Here is a table of the DD RAM addresses that are within the visible region.#000102030405060708090A0B0C0D0E0F101112131000102030405060708090A0B0C0D0E0F10111213
7DB0LCD Data Bus line. They are responsible for the parallel data transfer. DB7 is used to check the busy Flag.In 4 bit mode, DB0 to DB3 are not used and are left open.
There are two registers in an LCD, they are Instruction register and the Data register. The register select (RS) pin is used to select either of the two the registers. When held low, the Instruction register is selected and similarly, when it is high, the data register is selected. A write to the data register will write to the Display Data RAM (DD RAM) in the address last pointed by the address pointer. The address pointer is automatically incremented after each write operation.
In the 20 x 4 LCD Module, all the locations of the DD Ram are mapped on to a character position in the display. Hence a write to the data register with proper ASCII code will produce proper displayable character in the screen. You can find a good ASCII table here. Some of the values in the ASCII table are not printable and hence are not mapped on to any character. A write to the DD Ram with one such data will display some glyph that you cannot recognize.
The LCD datasheet comes with a lot of electrical and Mechanical specifications. Though they are not redundant, for now we will consider only the command sheet and the timing diagram without which it is impossible to interface the module.
The command sheet is a table which contains the various commands that can be issued to the LCD module so that it behaves as intended. I have not attached an image of the command sheet as I could not find any of a good readable resolution. So I created a HTML version of the command sheet that you could use at any resolution dYtm, You can find the Command Sheet here (or I should call it command page). The cells that are filled with absolute values have to be used as such and the ones that are having letters are variables and take either 0 or 1 based on the task it has to perform.
Here, D4 to D7 are 0, D3 is 1 and RS and R/W are held low. These are all constant values and hence have to be used as such. But the, bits D0 to D2 are all variables. Depending on the values return at positions B, C and D the following action are performed by the LCD controller,
According to this description, the value has to be written to the command register. That is if you want, display ON, cursor ON and the character at the cursor to be static, you have to write, 0x0E while holding the RS and RW lines Low.
As you know these LCDs have a built in font set and can be used by indexing the ASCII value of the corresponding character. It capable of operating on 8 data lines (D0 to D7) or on 4 data lines (D4 to D7). The upcoming posts will discuss the 8 bit and 4 bit mode of LCD interface. Other than the data lines the LCD needs 3 command lines - RS, R/W and EN. Therefore in total, an LCD interface will need 11 (8+3) or 7 (4+3) pins of the microcontroller.
It is possible to further reduce the total number of port pins required from 7 (4+3) to 6 (4+2) by shorting the R/W pin to ground. If the R/W pin is connected to the ground, the LCD can be used to write data only. Reading from it is not possible. So we are not able to read the busy flag from the module. To live with this disability, we are forced to provide ample amount of delay loops (and hence compromise on the speed of execution) so that the LCD is seldom busy doing thing when new data is given.
In this project, we will have brief discussion on how to interface 16×2 LCD module to AT89C51, which is an 8051 family microcontroller. We use LCD display for the displaying messages in a more interactive way to operate the system or displaying error messages etc. Interfacing 16×2 LCD with 8051 microcontroller is very easy if you understanding the working of LCD.
16×2 Liquid Crystal Display which will display the 32 characters at a time in two rows (16 characters in one row). Each character in the display is of size 5×7 pixel matrix. This matrix differs for different 16×2 LCD modules, if you take JHD162A, this matrix goes to 5×8. There are 16 pins in the LCD module, the pin configuration us given below
4RSRS is the register select pin used to write display data to the LCD (characters), this pin has to be high when writing the data to the LCD. During the initializing sequence and other commands this pin should low.
So by reading the above table you can get a brief idea how to display a character. For displaying a character you should enable the enable pin (pin 6) by giving a pulse of 450ns, after enabling the pin6 you should select the register select pin (pin4) in write mode. To select the register select pin in write mode you have to make this pin high (RS=1), after selecting the register select you have to configure the R/W to write mode that is R/W should be low (R/W=0).
Commands:There are some preset commands which will do a specific task in the LCD. These commands are very important for displaying data in LCD. The list of commands given below:
RST Pin is pulled-LOW with the help of a 10KΩ Resistor. With the help of a 10μF Capacitor and a Push Button, you can reset the 8051 Microcontroller. EA is pulled-HIGH with the help of a 10KΩ resistor.
The data pins of the LCD are connected to PORT0 (first, the PORT0 pins must be pulled-HIGH with the help of a 1KΩ Resistor Pack). RS and E are connected to PORT2 pins P2.0 and P2.1.
The programs given below will use above functions and display the complete string which is given by the programmer to display the data. We have provided two demo codes working properly and easy to understand.
At this point, we can leverage the onboard debugger to step through the library code: in this way, we get an idea of which bytes are sent from the microcontroller to the LCD display through I2C.
LCD connected to this controller will adjust itself to the memory map of this DDRAM controller; each location on the LCD will take 1 DDRAM address on the controller. Because we use 2 × 16 type LCD, the first line of the LCD will take the location of the 00H-0FH addresses and the second line will take the 40H-4FH addresses of the controller DDRAM; so neither the addresses of the 10H-27H on the first line or the addresses of the 50H-67H on the second line on DDRAM is used.
To be able to display a character on the first line of the LCD, we must provide written instructions (80h + DDRAM address where our character is to be displayed on the first line) in the Instruction Register-IR and then followed by writing the ASCII code of the character or address of the character stored on the CGROM or CGRAM on the LCD controller data register, as well as to display characters in the second row we must provide written instructions (C0H + DDRAM address where our character to be displayed on the second line) in the Instructions Register-IR and then followed by writing the ASCII code or address of the character on CGROM or CGRAM on the LCD controller data register.
As mentioned above, to display a character (ASCII) you want to show on the LCD, you need to send the ASCII code to the LCD controller data register-DR. For characters from CGROM and CGRAM we only need to send the address of the character where the character is stored; unlike the character of the ASCII code, we must write the ASCII code of the character we want to display on the LCD controller data register to display it. For special characters stored on CGRAM, one must first save the special character at the CGRAM address (prepared 64 addresses, namely addresses 0–63); A special character with a size of 5 × 8 (5 columns × 8 lines) requires eight consecutive addresses to store it, so the total special characters that can be saved or stored on the CGRAM addresses are only eight (8) characters. To be able to save a special character at the first CGRAM address we must send or write 40H instruction to the Instruction Register-IR followed by writing eight consecutive bytes of the data in the Data Register-DR to save the pattern/image of a special character that you want to display on the LCD [9, 10].
We can easily connect this LCD module (LCD + controller) with MCS51, and we do not need any additional electronic equipment as the interface between MCS51 and it; This is because this LCD works with the TTL logic level voltage—Transistor-Transistor Logic.
The voltage source of this display is +5 V connected to Pin 2 (VCC) and GND power supply connected to Pin 1 (VSS) and Pin 16 (GND); Pin 1 (VSS) and Pin 16 (GND) are combined together and connected to the GND of the power supply.
Pins 7–14 (8 Pins) of the display function as a channel to transmit either data or instruction with a channel width of 1 byte (D0-D7) between the display and MCS51. In Figure 6, it can be seen that each Pin connected to the data bus (D0-D7) of MCS51 in this case P0 (80h); P0.0-P0.7 MCS-51 connected to D0-D7 of the LCD.
Pins 4–6 are used to control the performance of the display. Pin 4 (Register Select-RS) is in charge of selecting one of the 2 display registers. If RS is given logic 0 then the selected register is the Instruction Register-IR, otherwise, if RS is given logic 1 then the selected register is the Data Register-DR. The implication of this selection is the meaning of the signal sent down through the data bus (D0-D7), if RS = 0, then the signal sent from the MCS-51 to the LCD is an instruction; usually used to configure the LCD, otherwise if RS = 1 then the data sent from the MCS-51 to the LCD (D0-D7) is the data (object or character) you want to display on the LCD. From Figure 6 Pin 4 (RS) is connected to Pin 16 (P3.6/W¯) of MCS-51 with the address (B6H).
Pin 5 (R/W¯)) of the LCD does not appear in Figure 6 is used for read/write operations. If Pin 5 is given logic 1, the operation is a read operation; reading the data from the LCD. Data will be copied from the LCD data register to MCS-51 via the data bus (D0-D7), namely Pins 7–14 of the LCD. Conversely, if Pin 5 is given a voltage with logical 0 then the operation is a write operation; the signal will be sent from the MCS51 to LCD through the LCD Pins (Pins 7–14); The signal sent can be in the form of data or instructions depending on the logic level input to the Register Select-RS Pin, as described above before if RS = 0 then the signal sent is an instruction, vice versa if the RS = 1 then the signal sent/written is the data you want to display. Usually, Pin 5 of the LCD is connected with the power supply GND, because we will never read data from the LCD data register, but only send instructions for the LCD work configuration or the data you want to display on the LCD.
Pin 6 of the LCD (EN¯) is a Pin used to enable the LCD. The LCD will be enabled with the entry of changes in the signal level from high (1) to low (0) on Pin 6. If Pin 6 gets the voltage of logic level either 1 or 0 then the LCD will be disabled; it will only be enabled when there is a change of the voltage level in Pin 6 from high logic level to low logic level for more than 1000 microseconds (1 millisecond), and we can send either instruction or data to processed during that enable time of Pin 6.
Pin 3 and Pin 15 are used to regulate the brightness of the BPL (Back Plane Light). As mentioned above before the LCD operates on the principle of continuing or inhibiting the light passing through it; instead of producing light by itself. The light source comes from LED behind this LCD called BPL. Light brightness from BPL can be set by using a potentiometer or a trimpot. From Figure 6 Pin 3 (VEE) is used to regulate the brightness of BPL (by changing the current that enters BPL by using a potentiometers/a trimpot). While Pin 15 (BPL) is a Pin used for the sink of BPL LED.
4RSRegister selector on the LCD, if RS = 0 then the selected register is an instruction register (the operation to be performed is a write operation/LCD configuration if Pin 5 (R/W¯) is given a logic 0), if RS = 1 then the selected register is a data register; if (R/W¯) = 0 then the operation performed is a data write operation to the LCD, otherwise if (R/W¯) = 1 then the operation performed is a read operation (data will be sent from the LCD to μC (microcontroller); it is usually used to read the busy bit/Busy Flag- BF of the LCD (bit 7/D7).
5(R/W¯)Sets the operating mode, logic 1 for reading operations and logic 0 for write operations, the information read from the LCD to μC is data, while information written to the LCD from μC can be data to be displayed or instructions used to configure the LCD. Usually, this Pin is connected to the GND of the power supply because we will never read data from the LCD but only write instructions to configure it or write data to the LCD register to be displayed.
6Enable¯The LCD is not active when Enable Pin is either 1 or 0 logic. The LCD will be active if there is a change from logic 1 to logic 0; information can be read or written at the time the change occurs.
The purpose of this page is to present a basic application that describes how to operate the LCD c42048 that comes standard with the ATxmega128B1 Xplained Kit from Atmel. The project combines functions that are scattered throughout the Atmel Software Framework (ASF) into a single Atmel Studio 6 project made up of only a few files, while maintaining a standard project flow that has been implemented in ASF for Xmega devices. It is meant to provide an easy introduction to the integrated LCD controller that is included in the Xmega B-family of microcontrollers.
The ATxmega device family is the highest performance 8-bit AVR microcontroller family from Atmel. They deliver excellent power consumption characteristics for 8-bit microcontrollers while integrating features such as 12-bit ADC, USB device connectivity, AES and DES crypto modules, DMA controller and Event System, CRC generator, along with multiple serial communication ports. The family is made up of the A, B, C, and D Series. The D Series is the entry level Xmega series with no USB connectivity or integrated LCD controller. The C Series adds USB Full-speed Device connectivity on top of the D Series functionality. The B Series adds an integrated LCD controller, capable of up to 4x40 segments, on top of the C Series functionality. The A Series adds AES/DES crypto engine on top of C Series functionality but has no integrated LCD controller. The Xmega chip-set is available in multiple different package types such as QFN, QFP, and BGA with available program memory ranging from 16 to 384 KB. The reader is strongly advised to read the datasheets corresponding to the microcontroller being used to determine which specific features are available, along with electrical characteristics and pin assignments. The images on this page and most of the information was found in the documents isted below. This page focuses on the ATxmega128B1 microcontroller loaded on the Xmega128B1 Xplained kit.
The C42048A was designed specifically for the Atmel Xmega Xplained kit. It was designed to evaluate the full capability of the integrated LCD controller. It contains a group of seven 14-segment alphanumeric digits, a group of five 7-segment digits, a bar graph of 9 segments, along with 13 additional icons. The LCD pixels are mapped using 40 segments and 4 common terminals which can be accessed directly from the Display Memory registers.
Each pixel on the display has a unique connection between a Common line and a Segment line, much like a point has a unique coordinate on an x-y plane. Figure 1 illustrates the “grid” of connections between the Segment and Common lines.
For a pixel to become visible, an absolute voltage above a certain threshold must be applied across the corresponding Segment and Common pins. However, this voltage cannot be held at a constant (DC) value to prevent degrading of the liquid crystal. Therefore the voltage is altered in a sequence that’s dependent on the number of Common lines used by the display. The frequency of the alternating signal determines the frame rate of the LCD. The integrated LCD controller takes care of the voltage level present on each Common and Segment line and the frame rate of the LCD. However, the user needs to specify the number of Common lines present and configure the LCD registers accordingly.
The LCD controller within the Xmega128B1 is capable of controlling LCDs using 1 to 4 common lines. The C42048 LCD uses 4 Common lines. For an LCD with 4 Common lines, the frame length must be divided into 4 individual periods. This is known as 1/4 Duty. Each Common line of the LCD is energized for 1/4 of the frame. The voltage level on the Segment line and Common line is divided into even thirds between Ground and V_lcd. This is known as 1/3 Bias. Figures 3 and 4 below illustrate the timing diagram for energizing a pixel connected to Common line 0 and Segment line 0 (0,0). To make pixel (0,0) visible, the voltage on the Segment line 0 is phase shifted so that | SEG0 - COM0 | > | V_threshold |.
This section describes some typical considerations a designer must account for in every Xmega design, along with some considerations for LCD-specific Xmega applications. Each sub-section includes general considerations, along with some register configuration examples. The reader is encouraged to have the Xmega-B manual available for easy reference. All example code is written in C-Programming language. If global definitions are used in the examples, the 8-bit hex value is included in comments for easy reference.
The selected source for the 32kHz RTC is also used to source the system clock of the LCD. This can be sourced from an internal 32kHz oscillator within the Xmega, or an external 32kHz source such as a crystal. The LCD clock rate is then derived from a prescaler that divides the 32kHz source. The actual LCD frame rate is dependent on the number of Common lines used by the display, the RTC source frequency, and the LCD Prescaler configuration. The frame rate for a 1/4 Duty LCD can range from 31.25 Hz up to 512 Hz.
The designer must ensure the LCD peripheral is enabled by writing a ‘0’ to the LCD bit of the PRGEN register. An example of this is shown below. No LCD register configurations will take effect if the LCD peripheral is disabled. This seems like an obvious statement, but it is an easy thing to overlook if peripherals were disabled to save power.
LCD Control Register B (LCD.CTRLB) is used to define the frame rate of the display. Consult the device datasheet for all available frame rate options. For this example, the LCD frame rate was set to 62.5 Hz with Low Power waveform enabled using 1/4 Duty and 1/3 Bias. An example of loading this frame rate based on the 32kHz source is shown below.
Note: For this example, the CPU clock and all of the system clock prescalers were set. However, all that is absolutely necessary to operate the LCD is that the RTC source is configured properly.
The LCD controller in Xmega B devices supports a user configurable interrupt. If enabled, the interrupt will execute every time a specified number of frame periods has passed. The period between interrupts can be in the range of 1 to 32 for default waveforms. If Low Power waveforms are enabled, the range is 2 to 64 frames. Since, the LCD clock operates off of the RTC source, the LCD interrupt is also derived from the RTC source. This allows a convenient means of performing scheduled updates of the LCD, such as scrolling text, incrementing a progress bar, blinking icons, or updating a digital clock.
The LCD Interrupt Control Register (LCD.INTCTRL) is used to specify the interrupt period and the priority level of the interrupt. For this example, the LCD interrupt was scheduled to execute every 16 frames and was given high level priority. An example of configuring this interrupt period is shown below.
The LCD Controller also utilizes an Interrupt Flag Register (LCD.INTFLAGS). The FCIF bit is set whenever the specified number of frames has been reached, causing the interrupt. The bit is cleared automatically by hardware when the ISR is carried out in software. The designer could also use this register in a polling scheme to execute periodic updates, even with the interrupt disabled. Generally, it’s a good idea to explicitly clear this bit during configuration. This is demonstrated in the following bit of code.
On the Xmega128B1 Xplained board, the backlight of the LCD is controlled using PORTE, PIN5. Therefore, the pin needs to be configured as an output using the PORTE Direction Register (PORTE.DIR). This pin controls the gate of a MOSFET which allows current through the LED backlight of the LCD. Pulse-width-modulation (PWM) can be used to finely control the intensity of the backlight. If fine control is not required, the user can turn the backlight fully-on by writing a ‘1’ to PE5, or turn the backlight completely off by writing a ‘0’ to PE5. The PORTE Out-set and Out-clear Registers (PORTE.OUTSET, PORTE.OUTCLR) can be used to turn individual bits ON or OFF. For this example, the backlight was turned on full-bright. An example of this is shown below.
If using an LCD that doesn’t require all 40 segment pins of the Xmega, up to 16 segment pins (SEG39:24) can be converted to general purpose I/O pins. If configured as GPIO, the highest 8 segment pins (SEG39:32) become PORTG. If configured as GPIO, the next 8 segment pins 32:24 become PORTM. The lowest 24 segment pins (SEG23:0) cannot be converted to GPIO. If using an LCD with less than 20 segment pins, the remaining dedicated segment pins on the Xmega can be tied to ground. The 6 least significant bits of the LCD Control Register C (LCD.CTRLC) are used to define which pins are used as segment drivers. Below shows an example of using a 30 segment LCD and converting the highest 10 segment pins to GPIO.
The LCD Control Register A (LCD.CTRLA) controls many of the global functions of the LCD. Specifically, it is used to enable the LCD, specify an internal or external LCD voltage bias source, control Data Memory shadowing, swap Common/Segment pins, clear the Display Memory contents, enable/disable all segments at once, and blank the display. For this example, Control Register A was configured to simply enable the LCD peripheral and enable all segments. An example of this is demonstrated in the following code.
The LCD Control Register D (LCD.CTRLD) supports hardware blinking of pixels to reduce software complexity if desired. This register is used to enable hardware blinking and specify blink rates of 0.5, 1, 2, or 4 Hz. For this example, hardware blinking was not implemented, so Control Register D was left at it’s default value of 0x00. However, the following line of code demonstrates how a designer could implement a 2Hz hardware controlled blink rate.
Only pixels connected to Segment lines 0 and 1 support individual hardware blinking control. Control Register E (LCD.CTRLE) is used to specify which segments to enable blinking on. The 4 most significant bits specify which pixels on SIG1 have hardware blinking enabled. Likewise, the 4 least significant bits of Control Register E are used to specify which pixels connected to SEG0 have hardware blinking enabled. If Control Registers D and E are configured properly, the pixel will blink as soon as the segment is enabled in Display Memory. Refer to Fig. 2 above to determine which pixels are supported by hardware blinking (all those connected to SEG0 or SEG1). The selected pixels would blink at the rate specified in Control Register D. If no segments are specified in Control Register E, but the Blink Enable bit is set, blinking will commence on all segments set in Display Memory, regardless of whether or not they are connected to SEG0 or SEG1. For this example, hardware blinking was disabled. However, the following bit of code demonstrates how a user would enable hardware blinking on the “Warning” pixel (0,1).
LCD Control Register F is used to finely control the contrast of the display. The 6 least significant bits of the register are used to enter a signed 6-bit value to adjust the voltage across each pixel’s Segment and Common pins. Acceptable decimal values are in the range of -32 to 31. These values correspond to a voltage range of approximately 2.5 to 3.5V. Therefore, the default value of 0x00 will achieve a voltage of approximately 3.0V across all enabled pixels. The following code demonstrates how a user could set the LCD contrast 50% higher than default (approx. 3.25V across pixel terminals).
The Xmega LCD controller supports 7-segment digits with 3 or 4 Common pins, 14-segment digits with 4 Common pins, and 16-segment digits with 3 Common pins. Control Register G (LCD.CTRLG) is used to specify which type of digit is being used. This register is also used to specify which Segment terminal to start writing the decoded display to. Control Register H is used to enter the desired display data. The digit decoder then automatically converts the data to individual display codes which are written to Display Memory. For this example, 14-segment digits with 4 Common pins were used to display the alpha characters, while the 7-segment digits with 4 Common terminals were used to display numeric characters. Refer to the Example Project below for example usage of writing alpha/numeric characters to the LCD.
Note: Every time the digit decoder operates, all 4 pixels connected to Segment 11 get cleared by hardware. The 4 affected pixels are the Degree C, Degree F, Volt, and milliVolt Icons. If the application uses any of these 4 icons, the designer is responsible for re-displaying these icons after every decoder operation. For this example, definitions were set in a header file called “My_Board.h” to specify which icons were used for the application.
When referring to “Display Memory” we really mean the LCD Data Registers 0 through 19 (LCD.DATA19:0). The designer can manually set/clear pixels by directly writing to these registers, but a more convenient way to display data would be to use Control Registers G and H to automatically update Display Memory.
The Low Power Waveform can be used to reduce power consumption by decreasing the number of oscillations on the Segment and Common lines. The Low Power Waveform is enabled in Control Register B. The tradeoff with enabling Low Power Waveform is that it increases the minimum LCD interrupt period.
Download the following zip file and open the Basic LCD Example Project for Xmega128B1 microcontroller. It provides an easy-to-read project with documentation that describes basic functionality of the integrated LCD controller found in Xmega B devices.
The reader is encouraged to read through all of the documentation that is referenced on this page. Was this page helpful? Is there anything you would like to see added to the EEWiki? I hope you enjoy experimenting with the integrated Xmega LCD controller!
A PIC Microcontroller can be easily made to communicate with LCD by using the built in Libraries of MikroC. Interfacing between PIC and LCD can be 4-bit or 8-bit. The difference between 4-bit and 8-bit is how data are send to the LCD. In the 8-bit mode to write an 8-bit character to the LCD module, ASCII data is send through the data lines DB0- DB7 and data strobe is given through the E line.
But 4-bit mode uses only 4 data lines. In this mode the 8-bit ASCII data is divided into 2 parts which are send sequentially through data lines DB4 – DB7 with its own data strobe through the E line. The idea of 4-bit communication is to save as much pins that used to interface with LCD. The 4-bit communication is a bit slower when compared to 8-bit. The speed difference is only minimal, as LCDs are slow speed devices the tiny speed difference between these two modes is not significant. Thus the 4-bit mode data transmission is most commonly used.
The above definitions tells the compiler, how LCD is connected to the microcontroller. The two set of definitions are used to provide Data (PORT) and Direction (TRIS) registers.
This function prints the text (string) in the current cursor position. When we write data to LCD Screen, it automatically increments the cursor position.
16×2 Character LCD is a very basic LCD module which is commonly used in electronics projects and products. It contains 2 rows that can display 16 characters. Each character is displayed using 5×8 or 5×10 dot matrix. It can be easily interfaced with a microcontroller. In this tutorial we will see how to write data to an LCD with PIC Microcontroller using Hi-Tech C Compiler. Hi-Tech C has no built in LCD libraries so we require the hardware knowledge of LCD to control it. Commonly used LCD Displays uses HD44780 compliant controllers.
This is the pin diagram of a 16×2 Character LCD display. As in all devices it also has two inputs to give power Vcc and GND. Voltage at VEE determines the Contrast of the display. A 10K potentiometer whose fixed ends are connected to Vcc, GND and variable end is connected to VEE can be used to adjust contrast. A microcontroller needs to send two informations to operate this LCD module, Data and Commands. Data represents the ASCII value (8 bits) of the character to be displayed and Command determines the other operations of LCD such as position to be displayed. Data and Commands are send through the same data lines, which are multiplexed using the RS (Register Select) input of LCD. When it is HIGH, LCD takes it as data to be displayed and when it is LOW, LCD takes it as a command. Data Strobe is given using E (Enable) input of the LCD. When the E (Enable) is HIGH, LCD takes it as valid data or command. The input signal R/W (Read or Write) determines whether data is written to or read from the LCD. In normal cases we need only writing hence it is tied to GROUND in circuits shown below.
The interface between this LCD and Microcontroller can be 8 bit or 4 bit and the difference between them is in how the data or commands are send to LCD. In the 8 bit mode, 8 bit data and commands are send through the data lines DB0 – DB7 and data strobe is given through E input of the LCD. But 4 bit mode uses only 4 data lines. In this 8 bit data and commands are splitted into 2 parts (4 bits each) and are sent sequentially through data lines DB4 – DB7 with its own data strobe through E input. The idea of 4 bit communication is introduced to save pins of a microcontroller. You may think that 4 bit mode will be slower than 8 bit. But the speed difference is only minimal. As LCDs are slow speed devices, the tiny speed difference between these modes is not significant. Just remember that microcontroller is operating at high speed in the range of MHz and we are viewing LCD with our eyes. Due to Persistence of Vision of our eyes we will not even feel the speed difference.
Hope that you got rough idea about how this LCD Module works. Actually you need to read the datasheet of HD44780 LCD driver used in this LCD Module to write a Hi-Tech C program for PIC. But we solved this problem by creating a header file lcd.h which includes all the commonly used functions. Just include it and enjoy.
Lcd8_Init() & Lcd4_Init() : These functions will initialize the LCD Module connected to the following defined pins in 8 bit and 4 bit mode respectively.
Lcd8_Set_Cursor() & Lcd4_Set_Cursor() : These functions set the row and column of the cursor on the LCD Screen. By using this we can change the position of the character being displayed by the following functions.
Lcd8_Write_Char() & Lcd4_Write_Char() :These functions will write a character to the LCD Screen when interfaced through 8 Bit and 4 Bit mode respectively.
Lcd8_Shift_Left() & Lcd4_Shift_Left() : These functions are used to shift the content on the LCD Display left without changing the data in the display RAM.
Lcd8_Shift_Right() & Lcd4_Shift_Right() : Similar to above functions, these are used to shift the content on the LCD Display right without changing the data in the display RAM.