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

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

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

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

Добавлен: 14.06.2025

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

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

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

LCD Interfacing and Programming

301

retlw

‘a’

retlw

0

; terminator character

The auxiliary procedure named msg1, listed in the preceding code fragment, performs the character-generator function by producing each of the ASCII characters in the message string. Since a retlw instruction is necessary for each character, one instruction space in program memory is used for each character generated, plus a final binary zero for the string terminator.

Displaying the Text String

Once the string is stored in a local buffer, it is displayed by moving each ASCII code from the buffer into LCD DDRAM. Here again, we assume that the LCD has previously been set to the auto increment mode and that the Address register has been properly initialized with the corresponding DDRAM address. The following procedure demonstrates initialization of the DDRAM Address register to the value defined in the constant named LCD_1:

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

;Set Address register

;to LCD line 1 ;========================

;ON ENTRY:

;Address of LCD line 1 in constant LCD_1

line1:

bcf

porta,E_line

; E line low

bcf

porta,RS_line

; RS line low, set up for

; control

call

delay_125

; delay 125 microseconds

; Set to second display line

Movlw

LCD_1

; Address and command bit

movwf

portb

call

pulseE

; Pulse and delay

; Set RS line for data

bsf

porta,RS_line

; Set up for data

call

delay_125mics

; Delay

return

Once the Address register has been set up, the display operation consists of transferring characters from the PIC RAM buffer into LCD DDRAM. The following procedure can be used for this:

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

;LCD display procedure ;=============================

;Sends 16 characters from PIC buffer, with address stored

;in variable pic_ad, to LCD line previously selected display16:

;Set up for data

bcf

porta,E_line

; E line low


302

Chapter 13

bsf

porta,RS_line

; RS line low for control

call

delay_125

; Delay

; Set up counter for 16 characters

movlw

D’16’

; Counter = 16

movwf

count3

; Get display address from local variable pic_ad

movf

pic_ad,w ; First display RAM address to W

movwf

fsr

; W to FSR

getchar:

movf

indf,w

; get character from display RAM

; location pointed to by file select

; register

movwf

portb

call

pulseE

;send data to display

; Test for 16 characters displayed

decfsz

count3,f ; Decrement counter

goto

nextchar ; Skipped if done

return

nextchar:

incf

fsr,f

; Bump pointer

goto

getchar

Note the procedure display16, previously listed, assumes that the address of the local buffer is stored in a variable name pic_ad. This allows reusing the procedure to display text stored at other locations in PIC RAM.

The previously listed procedures demonstrate just one of many possible variations on this technique. Another approach is to store the characters directly in DDRAM memory as they are produced by the message-returning routine, thus avoiding the display procedure entirely. In this last case, the programming saves some data memory space at the expense of having to generate the message characters each time they are needed. Which approach is the most suitable one depends on the application.

13.3.5 Data Compression Techniques

Circuits based on the parallel data transfer of eight data bits require eight devoted port lines. Assuming that three other lines are required for LCD commands and interfacing (RS, E, and R/W lines), then 11 PIC-to-LCD lines are needed, leaving two free port lines at the most, on an 16f84 microcontroller. Not many useful devices can make do with just two port lines. Several possible solutions allow compressing the data transfer function. The most obvious one is to use the 4-bit data transfer mode to free four port lines. Other solutions are based on dedicating logic components to the LCD function.

4-bit Data Transfer Mode

One possible solution is to use the capability of the Hitachi 44780 controller that allows a parallel interface using just four data paths instead of eight. The objections are that programming in 4-bit mode is slightly more convoluted and there is a very minor performance penalty. In 4-bit mode, data must be sent one nibble at a time, so execu-


LCD Interfacing and Programming

303

tion is slower. Since the delay is required only after the second nibble, the execution time penalty for 4-bit transfers is not very large.

Many of the previously developed routines for 8-bit data mode can be reused without modification in the 4-bit mode. Others require minor changes, and there is one specific display procedure that must be developed ad hoc. The first required change is in the LCD initialization since bit 4 in the Function Set command must be clear for a 4-bit interface. The remaining initialization commands should require no further change, although it is a good idea to consult the data sheet for the LCD hardware in use.

Displaying data using a 4-bit interface consists of sending the high-order nibble followed by the low-order nibble, through the LCD 4-high-order data lines, usually labeled DB5 to DB7. The pulsing of line E follows the last nibble sent. It is usually the case in the 16f84 PIC that circuit wiring in the 4-bit mode uses four of five lines in Port-A, or four of eight lines in port B. Software must provide a way of reading and writing to the appropriate port lines, the ones used in the data transfer, without altering the value stored in the port bits dedicated to other uses. Bit merging routines, discussed in Section 13.3, are quite suitable for the purpose at hand.

The following procedures are designed to send the two nibbles of a data byte through the four high-order lines in port B. The auxiliary procedure named merge4 performs the bit-merging operation while the procedure named send8 does the actual write operation:

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

;send 2 nibbles in

;4-bit mode ;========================

;Procedure to send two 4-bit values to port B lines

;7, 6, 5, and 4. High-order nibble is sent first

;ON ENTRY:

;w register holds 8-bit value to send

send8:

movwf

store1

; Save

original value

call

merge4

; Merge with port B

; Now w has merged byte

movwf

portb

; w to

port B

call

pulseE

; Send

data to LCD

; High nibble is sent

movf

store1,w

; Recover byte into w

swapf

store1,w

; Swap

nibbles in w

call

merge4

movwf

portb

call

pulseE

; Send

data to LCD

call

delay_125

return

;================= ; merge bits


304

Chapter 13

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

;Routine to merge the 4 high-order bits of the

;value to send with the contents of port B

;so as to preserve the 4 low-bits in port B

;Logic:

;AND value with 1111 0000 mask

;AND port B with 0000 1111 mask

;Now low nibble in value and high nibble in

;port B are all 0 bits:

;

value = vvvv 0000

;

port B = 0000 bbbb

;OR value and port B resulting in:

;

vvvv bbbb

;ON ENTRY:

;w contain value bits

;ON EXIT:

;w contains merged bits

merge4:

andlw

b’11110000’

; ANDing with

0 clears

the

; bit. ANDing

with 1 preserves

; the original value

movwf

store2

; Save result

in variable

movf

portb,w

; port B to w

register

andlw

b’00001111’

; Clear high nibble in

port b

; and preserve low nibble

iorwf

store2,w

; OR two operands in w

return

The program named LCDTest3 in the book’s online software package is a demonstration using the 4-bit interface mode. Figure 13-6 shows a PIC/LCD circuit that is wired for the 4-bit data transfer mode.

Note in the circuit of Figure 13-6 that a total of six port lines remain unused. Two of these lines are in Port-A and four in Port-B.

Master/Slave Systems

To this point we have assumed that driving the LCD is one of the functions performed by the PIC microcontroller, which also executes the other circuit functions. In practice, such a scheme is rarely viable for two reasons: the number of interface lines required and the amount of PIC code space used up by the LCD driver routines. A more efficient approach is to dedicate a PIC exclusively to controlling the LCD hardware, while one or more other microcontrollers perform the main circuit functions. In this scheme, the PIC devoted to the LCD function is referred to as a slave while the one that sends the display commands is called the master.


LCD Interfacing and Programming

305

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)

6

E (signal enable)

1

11-14 Data bits 4 to 7

14

+5 V

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

2

3

4

5

6

7

8

9

1

R/W

RESET

100 Ohms

Figure 13-6 PIC/LCD Circuit for 4-bit Data Mode

When sufficient numbers of interface lines are available, the connection between master and slave can be simplified by using a parallel interface. For example, if four port lines are used to interconnect the two PICs, then 16 different command codes can be sent to the slave. The slave reads the communications lines much like it would read a multiple toggle switch. A simple protocol can be devised so that the slave uses these same interface lines to provide feedback to the master. For example, the slave sets all four lines low to indicate that it is ready for the next command, and sets them high to indicate that command execution is in progress and that no new commands can be received. The master, in turn, reads the communications lines to determine when it can send another command to the slave.

But using parallel communications between master and slave can be a self-defeating proposition, since it requires at least seven interface lines to be able