Anda di halaman 1dari 2

1

5. Alphanumeric LCD
It is often necessary to present results of calculations to user. A liquid crystal display (LCD) may
come handy to do it. There are many different LCDs available; a simple LCD capable of presenting
two lines with 16 characters in each line is planned to be used here. It implements a standard bus
andrelateddatasheetandexamplesofdrivingsoftwarecaneasilybefoundontheweb.TheLCDis
connected to the microcontroller using a fourbit data bus and twobit control bus. Additionally,
power supply and contrast setting lines increase the number of wires at the connector to 10, all is
availableatK350,seeFig.1.
LowerfourbitsofportC(PC00toPC03)areusedtodrivethefourbitdatabus,andnexttwobits
ofportC(PC04,PC05)areusedtodrivethecontrollinesnamedRegisterSelect(RS)andEnable(E).
SinceitisnotplannedtoreadfromtheLCDthecontrollineRead/Write(RW),usedtodistinguishthe
direction of data transfer, is permanently
connected to GND allowing only writing into the
LCD.
The contrast of the characters displayed at
the LCD depends of the position of trimmer
potentiometer P350. The use of the LCD in a
program starts by the initialization, which must
be performed at the beginning of the program.
Later the LCD can be used to display any
alphanumerical symbols, strings of symbols or
integer numbers of different lengths. A set of
functionsisavailable,itconsistsof:
LCD_init:ThisfunctioniscalledatthebeginningoftheprogramtoinitializetheportC,lower
six bits and the LCD using a fourbit interface. The function requires no parameters and
returnsnovalues.
LCD_string:Thisfunctiondisplaysastringofcharacters(maximum16charactersinlength)at
theLCDatthedesiredposition.Thefunctionacceptstwoparameters.Thefirstisthestringto
be written (for instance ABCD), the second is the position at the LCD where the string
shouldstart.Thefirstpositionintheupperlineisindexedas0x00,thelastpositioninthefirst
lineisindexedas0x0f.Thefirstpositioninthesecondlineisindexedas0x40,thelastposition
inthesecondlineisindexedas0x4f.
LCD_uInt16:Thefunctionisusedtodisplaya16bitunsignedintegernumberattheLCD.The
function requires three parameters. The first parameter is the unsigned integer itself, the
second parameter is the position on the LCD (as for the LCD_string), the third parameter is

Figure1:Theconnectionoftheliquidcrystaldisplay
usingafourbutdatabusandtwobitcontrolbus
5
4
PC05
6
PC04
8
+5V
2
PC00
9
K350
3
GND
7
VLCD
1
10
3
E
7
VLCD
1
10
D1
5
4
RS
+5V
6
RW
8
D3
LCD 2x16
GND
2
D0
9
D2
PC01
PC02
PC03
11
12
13
14
D7
D6
D4
D5
GND
PlayingwithSTM32F407testboardAlphanumericLCD
2

either 0 or 1. If 0 the number is shown including leading zeros, if 1 the leading zeros are
erased.
LCD_sInt16:ThesameasLCD_uInt16,butcapableofdisplayinga16bitsignedinteger(15bits
&sign).
LCD_uInt32: The same as LCD_uINT16 but capable of displaying a 32bit unsigned integer
number.Alsorequiresthreeparameters(maybenotcomplete32bits,checkthesoftware).
LCD_sInt32:ThesameasLCD_uInt32,butcapableofdisplayinga32bitsignedinteger(31bits
&sign,maybenotcomplete31bits,checkthesoftware).
Please note that these functions are combined into a file that must be included into the user
sourcecode.Thisisdonebya#includeLCD2x16.cstatementatthebeginningofeveryprogram
utilizingtheLCD.Inallintegerdisplayingfunctionsaconversionfrombinarytobinarycodeddecimal
in performed following the wellknown algorithm from the web (search keyword: shift and add 3
algorithm).
The demo program to write a simple message (imitation of the measurement result) is given in
Fig.2,andthecorrespondingphotooftheLCDisgiveninFig.3.

#i ncl ude " st m32f 4xx. h"


#i ncl ude " LCD2x16. c" / / i ncl ude f i l e wi t h LCD f unct i ons

i nt mai n ( ) {
unsi gned i nt i = 0; / / decl ar e & i ni t a var i abl e i

LCD_i ni t ( ) ; / / i ni t i al i ze t he LCD

LCD_st r i ng( " Demo" , 0x03) ; / / wr i t e a t ext i nt o t op l i ne
LCD_st r i ng( " x=" , 0x41) ; / / wr i t e a t ext i nt o bot t oml i ne
LCD_st r i ng( " mV" , 0x48) ; / / wr i t e a t ext i nt o bot t oml i ne

whi l e ( 1) {
LCD_uI nt 16( i ++, 0x43, 1) ; / / wr i t e a number i nt o bot t oml i ne
i &= 0x7f f f ; / / l i mi t t he val ue of t he number
};
}
Figure4:AlistingoftheprogramtowritearesultinthesecondlineontheLCD
Figure5:ThephotooftheLCDasaresultofrunningtheprograminFig.4

Anda mungkin juga menyukai