LCD interface
to STM32F0 discovery embedded board
In most of the embedded systems applications, the need for
a display is must. If any text /numeric data and graphics/image
need to be displayed, use of LCD display panel is a must.
The basics of LCD display is already explained in the module17.
In this module we will explain both hardware connection
(wiring) and sample program/code in C language to display
alphanumeric text on a simple LCD display panel.
The LCD display panel we have selected is STN type LCD
panel with 16 characters in 2 rows (16x2). The specifications
and the datasheet for LCD display called JHD162A is available
at http://www.hantronix.com/files/data/1278554733char-comm.pdf
and http://www.itron.com.cn/PDF_file/JHD162A%20SERIES.pdf.
16x2 JHD162A LCD display panel is easily available in most
of the electronics component shops in India and other places
in the world. You should buy the LCD panel fitted on a PCB
as shown in the picture below:
Hardware Interface:
Components/parts required:
1. 32 bit MCU based STM32F0 discovery embedded board
2. LCD Panel: JHD 162A
3. Trim pot 10 Kilo ohms
4. 220 Ohms resistor
5. General purpose PCB.
6. External +5V power supply
LCD pin details:
Summary on LCD Panel: JHD162A LCD module powers
up in 8-bit mode. Additional commands are required to put
the module into 4-bit mode. In this example, we are using
LCD panel in 8-bit mode. 16x2 means there are 16 columns
and 2 rows of alphanumeric ASCII blocks on the LCD.
control signals:
R/W: When this signal is '1' = Reads data from the
LCD RAM. When this signal is '0' = Writes data on LCD RAM.
EN: Is basically a Latch signal. You have to send
'1' first and then '0' signal with a particular delay to
latch the data.
RS: Is a Register Select Control signal. When this
signal is '1' = It accepts data to be displayed. When this
signal is '0' = It accepts instructions for the LCD like
setting font, cursor position etc.
Databus (D0 to D7): Is 8-bit Databus. It is used
to send both data as well as Instructions to the LCD based
upon control signals.
Backlight + and Backlight GND: Turns on Backlight
of that LCD so that you can see the words correctly.
VEE: is a contrast voltage. Using a trim pot you
can adjust the contrast of the LCD. More voltage more the
contrast and vice versa(voltage should never exceed VCC
= +5 volts).
Circuit diagram(below): Connect STM32F0-DISCOVERY
embedded board to the LCD panel.
LCD Initialization
1. Initializations for LCD Communications: First
set RS= 0, R/W=0, EN=1; And give 10ms delay, then write
the hex code (0X0001 & latch, 0X0038 & latch , 0X000E
& latch,0X0010 & latch, 0X0006 & latch) on data
bus(PA0-PA7). The C code below has all this taken care.
Table below: Code of various
display commands
Command |
Code |
Clear Display, Cursor to Home |
0x0001 |
Cursor to Home |
0x0002 |
Entry Mode: |
|
Cursor Decrement, Shift off |
0x0004 |
Cursor Decrement, Shift on |
0x0005 |
Cursor Increment, Shift off |
0x0006 |
Cursor Increment, Shift on
|
0x0007 |
Display Control: |
|
Display, Cursor, and Cursor Blink off
|
0x0008 |
Display on, Cursor and Cursor Blink off
|
0x000C |
Display and Cursor on, Cursor Blink off
|
0x000E |
Display, Cursor, and Cursor Blink on
|
0x000F |
Cursor / Display Shift: (nondestructive
move) |
|
Cursor shift left |
0x0010 |
Cursor shift right |
0x0014 |
Display shift left |
0x0018 |
Display shift right |
0x001C |
Display Function (2 rows for 4-bit data;
big) |
0x002C |
Display Function (2 rows for 4-bit data;
small)) |
0x0028 |
Display Function (1 row for 4-bit data;
big) |
0x0024 |
Display Function (1 row for 4-bit data;
small) |
0x0020 |
Display Function (2 rows for 8-bit data;
big) |
0x003C |
Display Function (2 rows for 8-bit data;
small) |
0x0038 |
Display Function (1 row for 8-bit data;
big) |
0x0034 |
Display Function (1 row for 8-bit data;
small) |
0x0030 |
Move cursor to beginning of second row
|
0x00C0 |
2.Ready to Write Data to LCD Data
You have to Change RS=1 after executing above instructions.
Table below: Codes for displaying different ASCII
and other characters on LCD panel
Example: To display character 'P' is 0101 0000 = 0x0050
on this LCD Character RAM.
Below is the code written to display character 'www.eeherald.com'
on JHD 162A.
#include "main.h"
#include "stm32f0xx_conf.h"
uint32_t TickValue=0;
#define RS GPIO_Pin_13 // RS is named
as Port 13
#define RW GPIO_Pin_14 // RW is named as Port 14
#define EN GPIO_Pin_15 // EN is named as Port 15
//------------------------------------------------------------------------------
// Function Name : delay_ms
// Description : delay for some time in ms unit(accurate)
// Input : n_ms is how many ms of time to delay
//------------------------------------------------------------------------------
void TimingDelay_Decrement(void)
{
TickValue--;
}
void delay_ms(uint32_t n_ms)
{
SysTick_Config(8000*PLL_MUL_X - 30);
TickValue = n_ms;
while(TickValue == n_ms)
;
SysTick_Config(8000*PLL_MUL_X);
while(TickValue != 0)
;
}
//------------------------------------------------------------------------------
// Function Name : Init GPIO
// Description : pins ,port clock & mode initialization.
//------------------------------------------------------------------------------
void initgpio()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC,
ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14
| GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 |
GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6
| GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//------------------------------------------------------------------------------
// Function Name : s_init
// Description : Send Instruction Function (RS=0 & RW=0)
//------------------------------------------------------------------------------
void s_init()
{
GPIOC->BRR=RS;
GPIOC->BRR=RW;
}
//------------------------------------------------------------------------------
// Function Name : s_data
// Description : Send Data Select routine(RS=1 & RW=0)
//------------------------------------------------------------------------------
void s_data()
{
GPIOC->BSRR=RS;
GPIOC->BRR=RW;
}
//------------------------------------------------------------------------------
// Function Name : s_latch
// Description : Latch Data/Instruction on LCD Databus.
//------------------------------------------------------------------------------
void s_latch()
{
GPIOC->BSRR=EN;
delay_ms(10);
GPIOC->BRR=EN;
delay_ms(10);
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
*******************************************************************************/
int main(void) //Main function
{
initgpio();
int k=0;
char a[]="WWW.EEHERALD.COM";
char b[]="EMBEDDED SYSTEMS";
GPIOC->BRR=RS; //Initialize RS=0 for selecting instruction
Send
GPIOC->BRR=RW; // Select RW=0 to write Instruction/data
on LCD
GPIOC->BSRR=EN; // EN=1 for unlatch. (used at initial
condition)
delay_ms(10);
s_init(); //Call Instruction Select routine
GPIOA->ODR=0x0001; // Clear Display, Cursor to Home
s_latch(); //Latch the above instruction
GPIOA->ODR=0x0038; // Display Function (2 rows for 8-bit
data; small)
s_latch(); //Latch this above instruction 4 times
s_latch();
s_latch();
s_latch();
GPIOA->ODR=0x000E; // Display and Cursor on, Cursor Blink
off
s_latch(); //Latch the above instruction
GPIOA->ODR=0x0010; // Cursor shift left
s_latch(); //Latch the above instruction
GPIOA->ODR=0x0006; // Cursor Increment, Shift off
s_data(); //Change the input type to Data.(before it was
instruction input)
s_latch(); //Latch the above instruction
for(k=0;a[k];k++)
{
GPIOA->ODR=a[k]; //It will send a[0]='P' as = '0x0050'
on Port A.
s_latch(); //Latch the above instruction only once. Or it
will clone each character twice if you latch twice.
}
GPIOC->BRR=RS; //Initialize RS=0 for selecting instruction
Send
GPIOC->BRR=RW; // Select RW=0 to write Instruction/data
on LCD
GPIOC->BSRR=EN; // EN=1 for unlatch. (used at initial
condition)
delay_ms(10);
GPIOA->ODR=0x00C0; // Move cursor to beginning of second
row
s_latch(); //Latch the above instruction
s_data(); //Change the input type to Data.(before it was
instruction input)
for(k=0;b[k];k++)
{
GPIOA->ODR=b[k]; //It will send b[0]='E' as = '0x0044'
on Port A.
s_latch();//Latch the above instruction only once. Or it
will clone each character twice if you latch twice.
}
s_init();
}
This module is part of complete embedded systems course
available at www.eeherald.com. The other modules of the
course based on diffrent kits used are listed in the table
below.