Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8541

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

LCD Interfacing and Programming

285

Function set

Function set sets the parallel interface data length, the number of display lines, and the character font. If B4 (DL) bit is set, then the interface is set to eight bits. Otherwise it is set to four bits. If B3 (N) bit is zero, the display is initialized for 1/8 or 1/11 duty cycle. When the N bit is set, the display is set to 1/16 duty cycle. Displays with multiple lines typically use the 1/16 duty cycle. The 1/16 duty cycle on a one-line display appears as if it were a two-line display, that is, the line consists of two separate address groups (see Table 13.2).

If B2 (F) bit is set then the display resolution is 5 x 10 pixels. Otherwise the resolution is 5 x 8 pixels. This bit is not significant when the 1/16 duty cycle is selected; that is, when the N bit is set.

The function set instruction should be issued during controller initialization. No other instruction can be executed before this one, except for changing the interface data length.

Set CGRAM address

Set CGRAM address sets the CGRAM (character generator RAM) address to which data is sent or received after this operation. The CGRAM address is a six-bit field in the range 0 to 64 decimal. Once a value is entered in the CGRAM Address register, data can be read or written from CGRAM.

Set DDRAM address

Set DDRAM address sets the DDRAM (display data RAM) address to which data is sent or received after this operation. The DDRAM address is a seven-bit field in the range 0 to 127 decimal. Once a value is entered in the DDRAM Address register, data can be read or written from CGRAM. DDRAM address mapping is discussed in Section 13.1.4.

Read busy flag and Address register

Read busy flag and Address register reads the busy flag to determine if an internal operation is in progress and reads the address counter content. The value in the Address register is reported in bits 0 to 6. Bit 7 (BF) is the busy flag bit. This bit is read only. The address counter is incremented or decremented by 1 (according to the mode set) after the execution of a data write or read instruction.

Write data

Write data writes eight data bits to CGRAM or DDRAM. Before data is written to either controller RAM area, software must first issue a set DDRAM address or set CGRAM address instruction (described previously). These two instructions not only set the next valid address in the Address register, but also select either CGRAM or DDRAM for writing operations. What other actions take place as data is written to the controller depends on the settings selected by the entry mode set instruction. If the direction of cursor movement or data shift is in the increment mode, then the data write operation adds one to the value in the Address register. If the cursor movement is enabled, then the cursor is moved accordingly after data write takes place. If the display shift mode is active, then the displayed characters are shifted either right or left.

286

Chapter 13

Read data

Read data reads eight data bits to CGRAM or DDRAM. Before data is read from either controller RAM area, software must first issue a set DDRAM address or set CGRAM address instruction. These instructions not only set the next valid address in the Address register, but also select either CGRAM or DDRAM for writing operations. Failing to set the corresponding RAM area results in reading invalid data.

What other actions take place as data is read from the controller RAM depends on the settings selected by the entry mode set instruction. If the direction of cursor movement or data shift is in the increment mode, then the data read operation adds one to the value in the Address register. However, display is no shifted by a read operation even if the display shift is active.

The cursor shift instruction has the effect of changing the content of the Address register. So if a cursor shift precedes a data read instruction, there is not need to reset the address by means of an address set command.

13.2.2 A 16F84 8-bit Data Mode Circuit

The first circuit presented in this chapter is experimental. Its purpose is to exercises LCD display functions in the simplest forms. Therefore, the circuit uses 8-bit parallel data transmission interfacing with a 16F84 microcontroller. The circuit is shown in Figure 13-4.

E R/W RS

HD44780 pin out

1

GND

2

DC +5v

3

Contrast adjust

4

RS (register select)

HD44780

5

R/W (read/write select)

1

6

E (signal enable)

7-14 Data bits 0 to 7

14

+5 V

BF

4 MHz

Osc

E

+5 V

RS

18

17

16

15

14

13

12

11

10

RA1

RA0

OSC1 OSC2

Vdd

RB7

RB6

RB5

RB4

+5 V

16F84

RA4/

RA2

RA3

T0Tkl

MCLR Vss

RB0/INT RB1

RB2

RB3

10 K

1

2

3

4

5

6

7

8

9

RESET

R/W

100 Ohms

Figure 13-4 16F84 to LCD 8-bit Mode Circuit


LCD Interfacing and Programming

287

In the circuit of Figure 13-4, three control lines are wired between the microcontroller and the LCD. The line designations are shown inside ovals. The R/W line is not necessary, since it is possible to devise a system that does not read LCD data. In spite of this, the R/W line is not included since it allows reading the busy flag in synchronizing operations. Table 13.5 shows the control and data connections for the circuit in Figure 13-4.

Table 13.5

Connections for 16F84/LCD 8-bit Data Mode Circuit

16F84

LCD

LINE

PIN

PORTBIT

PIN

NAME

FUNCTION

1

A2

4

RS

Select instruction/

data register

2

A3

5

R/W

Read/write select

18

A1

6

E

Enable signal

13

B7

14

before

Busy flag.

6-13

B0-B7

7-14

Data

Data lines

13.3 LCD Programming

LCD programming is usually device-specific. Before attempting to write code, the programmer should become familiar with the circuit wiring diagram, the set up parameters, and the specific hardware requirements. It is risky to make assumptions that a specific device conforms exactly to the HD44780 interface since often a style sheet contains specifications that are not in strict conformance with the standard. In addition to the PIC set up and initialization functions, code to display a simple text message on the LCD screen consists of the following display-related functions:

1.Define the required constants, variables, and buffers.

2.Set up and initialize ports used by the LCD.

3.Initialize the LCD to circuit and software specifications.

4.Store text in PIC text buffer.

5.Select DDRAM start address on LCD.

6.Display text by transferring characters in PIC text buffer to LCD DDRAM.

If the LCD display consists of multiple lines, then the previous steps 4, 5, and 6 are repeated for each line. LCD initialization and display operations vary according to whether the interface is 4- or 8-bits and whether the code uses delay loops or busy flag monitoring to synchronize operations. All of these variations are considered in the examples in this chapter.

13.3.1 Defining Constants and Variables

In any program, defining and documenting constants and fixed parameters should be done centrally, rather than hard-coded through the code. Centralizing the elements that are variable under different circumstances makes it possible to adapt code to circuit and hardware changes.


288

Chapter 13

Two common ways are available for defining constants: the C-like #define directive and the equ (equate) directive. In most cases, it is a matter of personal preference which is used, but a general guideline is to use the #define statement to create literal constants; that is, constants that are not associated with program registers or variables. The equ directive is then used to define registers, flags, and local variables.

According to this scheme, an LCD display driver program could use #define statements to create literals that are related to the wiring diagram or the specific LCD values obtained from the data sheet, such as the DDRAM addresses for each display line, as in the following code fragment:

;=====================================================

;

constant definitions

;

for PIC-to-LCD pin wiring and LCD line addresses

;=====================================================

#define E_line 1

;|

#define RS_line 2

;| — from wiring diagram

#define RW_line 3

;|

; LCD line addresses (from LCD data sheet)

#define LCD_1

0x80

; First LCD line constant

#define LCD_2

0xc0

; Second LCD line constant

By the same token, the values associated with PIC register addresses and bit flags are defined using equ, as follows:

;=====================================================

; PIC register equates

;=====================================================

porta

equ

0x05

portb

equ

0x06

fsr

equ

0x04

status

equ

0x03

indf

equ

0x00

z

equ

2

One advantage of this scheme is that constants are easier to locate, since they are grouped by device. Those for the LCD are in #define directives area and those for the PIC hardware in an area of equ directives.

There are also drawbacks to this approach, since symbols created in #define directive are not available for viewing in the MPLAB debuggers. However, if the use of the #define directive is restriced to literal constants, then their viewing during a debugging session is not essential.

MPLAB also supports the constant directive for creating a constant symbol. Its use is identical to the equ directive but the latter is more commonly found in code.


LCD Interfacing and Programming

289

Using MPLAB Data Directives

Often a program needs to define a block of sequential symbols and assign to each one a corresponding name. In the PIC 16f84, the address space allocated to general purpose registers allocated by the user is of 68 bytes, starting at address 0x0c. One possible way of allocating user-defined registers is to use the equ directive to assign addresses in the PIC SRAM space:

Var1

equ

0x0c

Var2

equ

0x0d

Var3

equ

0x0e

Buf1

equ

0x0f

;

10-byte buffer space

Var4

equ

0x19

;

Next variable

Although this method is functional, it depends on the programmer calculating the location of each variable in the PIC’s available SRAM space. Alternatively, MPLAP provides a cblock directive that allows defining a group of consecutive sequential symbols while referring only to the address of the first element in the group. If no address is entered in cblock, then the assembler assigns the address. This address is one higher than the final address in the previous cblock. Each cblock ends with the endc directive. The following code fragment showing the use of the cblock directive is from one of the sample programs for this chapter.

;=====================================================

;

variables in PIC RAM

;=====================================================

;Reserve 16 bytes for string buffer cblock 0x0c

strData

endc

;Leave 16 bytes and continue with local variables

cblock 0x1d

; Start of block

count1

; Counter # 1

count2

; Counter # 2

count3

; Counter # 3

pic_ad

; Storage for start of text area

; (labeled strData) in PIC RAM

J

; counter J

K

; counter K

index

; Index into text table

endc

Note in the preceding code fragment, the allocation for the 16-byte buffer space named strData is ensured by entering the corresponding start address in the second cblock. The PIC microcontrollers do not contain a directive for reserving memory areas inside cblock, although the res directive can be used to reserve memory for individual variables.