Sunteți pe pagina 1din 7

Sample C program: LCD interface to ARM Cortex M0 MCU

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:
E

le c tro

n ic s E

n g in

e e rin

g He

ra ld

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.
E

le c

tro n ic

s E

ng

in e

e rin g

He

ra ld

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.
Stream-Discovery

Stream-LPCXpresso

Stream-AME-51

Module 3b: Installation of ARM


Module 3a: Installation of ARM
Module 3: Installation of OKI's
Cortex M0 based STM32F0 kit from Cortex M0 based LPCXpresso kit
ST Microelectronics and sample codefrom NXP Semiconductor and sample ARM7 KIT(old module, suggest to
skip this module)
Module 4: Sample programs -1 code
Module 4: Sample programs -1
for ARM Cortex M0 based on
Module 4: Sample programs -1
for ARM7 TDMI MCU
STM32F0
for ARM Cortex M0 based
LPCXpress
Module 5: OKI ARM-7
Module 5b: ARM Cortex M0
Processor Architecture.
Architecture.
Module 5a: ARM Cortex M0
Architecture.
Module 6b: Sample program-2
Module 6: Sample programs -2
(interfacing keypad)
Module 6a: Sample program-2
for ARM7 TDMI MCU
(interfacing keypad)
Module 7: Serial communication
Module 7: Serial communication
concepts -1
concepts -1
Module 7: Serial communication
Module 8: RS-232 Basics
concepts -1
Module 8: RS-232 Basics
Module 9: Controller Area
Module 8: RS-232 Basics
Module 9: Controller Area
Networking (CAN)
Networking (CAN)
Module 9: Controller Area
Module 10: LIN
Networking (CAN)
Module 10: LIN
Module 11: I2C Bus Interface
Module 10: LIN
Module 11: I2C Bus Interface
Module 12: SPI Bus Interface
Module 11: I2C Bus Interface
Module 12: SPI Bus Interface
Module14: USB Interface
Module 12: SPI Bus Interface
Module14: USB Interface
Module15: SRAM memory
Module14: USB Interface
Module15: SRAM memory
interface
interface
Module15: SRAM memory
Module16: Flash memory
interface
Module16: Flash memory
interface
interface
Module16: Flash memory
Module17: LCD display panel
interface
Module17: LCD display panel
interface
interface
Module17: LCD display panel
Module18: Touch pane interface
interface
Module18: Touch pane interface
Module19: Audio/video
Module18: Touch pane interface
Module19: Audio/video
interface
interface
Module19: Audio/video interface

S-ar putea să vă placă și