Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8575
Скачиваний: 0
290 |
Chapter 13 |
13.3.2 LCD Initialization
LCD initialization depends on the specific hardware in use and on the circuit wiring. Information about the specific LCD can be obtained from the device’s data sheet. Sometimes, the data sheet includes examples of initialization values for different conditions and even code listings. The information is usually sufficient to ensure correct initialization.
A word of warning: the popular LCD literature available online often contains initialization “myths” for specific components requiring that a certain mystery code be used for no documented reason, or that a certain function be repeated a given number of times. The programmer should make sure that the code is rational and that every operation is actually required and documented.
Before the LCD initialization commands are used it is necessary to set the communications lines correctly. The E line should be low, the RS line should be low for command, and the R/W line (if connected) should be low for write mode. After the lines are set accordingly, there should be a 125ms delay. Note that at this point, the LCD busy flag is not yet reliable. The following code fragment shows the processing:
bcf |
porta,E_line |
; E line low |
||
bcf |
porta,RS_line |
; RS line low for command |
||
bcf |
porta,RW_line |
; |
Write |
mode |
call |
delay_125 |
; |
delay |
125 microseconds |
The procedure delay_125 in the previous code fragment is described later in this chapter.
Function Set Command
Function set is the first initialization command sent to the LCD. The command determines whether the display font consists of 5 x 10 or 5 x 7 pixels. The latter is by far the more common. It determines the duty cycle, which is typically 1/8 or 1/11 for sin- gle-line displays and 1/16 for multiple lines. The interface width is also determined in the Function Set command. It is 4-bits or 8-bits. The following code fragment shows the commented code for the Function Set command:
;***********************|
; |
Function Set |
| |
||||||
;***********************| |
||||||||
movlw |
0x38 |
; 0 0 1 1 1 0 0 0 (FUNCTION SET) |
||||||
; |
| | | |__ font select: |
|||||||
; |
| | | |
1 |
= 5x10 |
in 1/8 or 1/11 |
||||
; |
| | | |
0 |
= 1/16 |
dc |
||||
; |
| | |___ |
Duty cycle select |
||||||
; |
| | |
0 |
= 1/8 or 1/11 |
|||||
; |
| | |
1 |
= 1/16 |
(multiple ines) |
||||
; |
| |___ Interface width |
|||||||
; |
| |
0 |
= 4 bits |
|||||
; |
| |
1 |
= 8 bits |
|||||
; |
|___ FUNCTION SET |
COMMAND |
||||||
LCD Interfacing and Programming |
291 |
|
movwf |
portb |
|
call |
pulseE ;pulse E line to force LCD command |
|
In the preceding code fragment, the LCD is initialized to multiple lines, 5 x 7 font, and 8-bit interface, as in the program LCDTest1 found in the book’s online software package.
The procedure named pulseE sets the E line bit off and on to force command recognition by the LCD. The procedure is listed and described later in the chapter.
Display Off
Some initialization routines in LCD documentation and data sheets require that the display be turned off following the Function Set command. If so, the Display Off command is executed as follows:
;***********************|
; |
Display Off |
| |
||||
;***********************| |
||||||
movlw |
0x08 |
; 0 0 0 0 1 0 0 |
0 (DISPLAY ON/OFF) |
|||
; |
| | | |
|___ Blink character at |
||||
; |
| | | |
| |
Cursor |
|||
; |
| | | |
1 = on, 0 = off |
||||
; |
| | |___ Curson on/off |
|||||
; |
| | |
1 = on, 0 = off |
||||
; |
| |____ Display on/off |
|||||
; |
| |
1 = on, 0 = off |
||||
; |
|____ |
COMMAND BIT |
||||
movwf |
portb |
|||||
call |
pulseE |
; pulse E line to force LCD command |
||||
Display and Cursor On
Whether or not the display is turned off, it must be turned on first. Also code must select if the cursor is on or off, and whether the character at the cursor position is to blink. The following command sets the cursor and the display on and the character blink off:
;***********************|
; Display and Cursor On |
;***********************|
movlw |
0x0e |
; 0 0 0 0 1 1 1 |
0 (DISPLAY ON/OFF) |
||
; |
| | | |
|___ |
Blink character at |
||
; |
| | | |
| |
cursor |
||
; |
| | | |
1 = on, 0 = off |
|||
; |
| | |___ Curson on/off |
||||
; |
| | |
1 |
= on, 0 = off |
||
; |
| |____ Display on/off |
||||
; |
| |
1 = on, 0 = off |
|||
; |
|____ |
COMMAND BIT |
|||
292 |
Chapter 13 |
movwf |
portb |
call |
pulseE ; pulse E line to force LCD command |
Set Entry Mode
The Entry Mode Command sets the direction of cursor movement or display shift mode. Normally, the display is set to the increment mode when writing in the Western European languages. The Entry Mode command controls display shift. If enabled, the displayed characters appear to scroll. This mode is used to simulate an electronic billboard effect by storing more than one line of characters in DDRAM and then scrolling the characters left-to-right. The following code sets entry mode to increment mode and no shift:
;***********************|
; |
Set Entry Mode |
| |
|||
;***********************| |
|||||
movlw |
0x06 |
; 0 0 0 0 0 1 1 0 (ENTRY MODE SET) |
|||
; |
| | |___ display shift |
||||
; |
| | |
1 = shift |
|||
; |
| | |
0 = no shift |
|||
; |
| |____ cursor increment |
||||
; |
| |
mode |
|||
; |
| |
1 = left-to-right |
|||
; |
| |
0 = right-to-left |
|||
; |
|___ COMMAND BIT |
||||
movwf |
portb |
;00000110 |
|||
call |
pulseE |
||||
Operations that read or write to CGRAM and operations that read DDRAM do not shift the display.
Cursor and Display Shift
These commands determine whether the cursor or the display shift according to the selected mode. Shifting the cursor or the display provides a software mechanism for making DDRAM corrections or for retrieving display data at specific DDRAM locations. The four available options appear in Table 13.4 previously in this chapter. The following instructions set the cursor to shift right and disable display shift:
;***********************|
; Cursor/Display Shift |
;***********************|
movlw |
0x14 |
; 0 0 0 1 0 1 0 |
0 (CURSOR/DISPLAY |
||||
; |
| | | | |
| |
SHIFT) |
||||
; |
| | | |_|___ |
don’t care |
|||||
; |
| |_|__ |
cursor/display shift |
|||||
; |
| |
00 |
= |
cursor shift |
left |
||
; |
| |
01 |
= |
cursor shift |
right |
||
; |
| |
10 |
= |
cursor and display |
|||
; |
| |
shifted left |
|||||
LCD Interfacing and Programming |
293 |
||
; |
| |
11 = |
cursor and display |
; |
| |
shifted right |
|
; |
|___ COMMAND |
BIT |
|
movwf portb ;0001 1111
call pulseE
Clear Display
The final initialization command is usually one to clear the display. It is entered as follows:
;***********************| |
||
; |
Clear Display |
| |
;***********************| |
||
movlw |
0x01 |
; 0 0 |
0 0 0 0 0 1 (CLEAR DISPLAY) |
; |
|___ COMMAND BIT |
||
movwf |
portb |
;0000 |
0001 |
call |
pulseE |
||
call |
delay_5 |
;delay 5 milliseconds after init |
|
Note that the last command is followed by a 5ms delay. The delay procedure delay_5 is listed and described later in this chapter.
13.3.3 Auxiliary Operations
Several support routines are required for effective text display in LCD devices. These include time delay routines for timed access, a routine to pulse the E line in order to force the LCD to execute a command or to read or write text data, routines to read the busy flag when this is the method used for processor/LCD synchronization, and routines to merge data with port bits so as to preserve the status of port lines not being addressed by code.
Time Delay Routine
There are several ways of producing time delays in PIC microcontroller. The Bibliography lists a title by David Benson devoted almost entirely to timing and counting routines. The present concern is quite simple: to develop a software routine that ensures the time delay that must take place in LCD programming, as shown in Table 13.3.
One mechanism for producing time delays in PIC programming is by means of the TIMER0 module, a built-in 8-bit timer counter. Once enabled, Port-A pin 4, labeled the TOCKI bit and associated with file register 01 (TMR0), is used to time processor operations. In the particular case of LCD timing routines, using the TIMER0 module seems somewhat of an overkill, in addition to the fact that it requires the use of a Port-A line which is often required for other purposes.
Alternatively, timing routines that serve the purpose at hand can be developed using simple delay loops. In this case, no port line is sacrificed and coding is considerably simplified. These routines are generically labeled software timers, in contrast with the hardware timers that depend on the PIC timer/counter device described previously. Software timers provide the necessary delay by means of program loops;
294 |
Chapter 13 |
that is, by wasting time. The length of delay provided by the routine depends on the execution time of each instruction and on the number of repeated instructions.
Instructions on the PIC 16f84 consume four clock cycles. If the processor clock is running at 4 MHz, then one fourth of 4 MHz is the execution time for each instruction, which is 1 µs. So if each instructions requires 1 µs, repeating 1000 instructions produces a delay of 1 ms. The following routines provide convenient delays for LCD interfacing:
;=======================
;Procedure to delay
;125 microseconds ;======================= delay_125mics:
movlw |
D’42’ |
; Repeat |
42 machine |
cycles |
movwf |
count1 |
; Store value in counter |
||
repeat: |
||||
decfsz |
count1,f |
; Decrement counter |
(1 cycle) |
|
goto |
repeat |
; Continue if not 0 |
(2 cycles) |
|
; 42 * 3 |
= 126 |
|||
return |
; End of |
delay |
||
;======================= |
||||
;Procedure to delay
;5 milliseconds ;======================= delay_5ms:
movlw |
D’41’ |
; Counter = 41 |
|
movwf |
count2 |
; Store in |
variable |
delay: |
|||
call |
delay_125mics |
; Delay 41 |
microseconds |
decfsz |
count2,f |
; 41 times |
125 = 5125 ms. |
; or approximately 5 ms |
|||
goto |
delay |
||
return |
; End of delay |
||
Actually, the delay loop of the procedure named delay_5ms is not exactly the product of 41 iterations times 125 µs, since the instruction to decrement the counter and the goto to the label delay are also inside the loop. Three instruction cycles must be added to those consumed by the delay_125mics procedure. This results in a total of 41 * 3 or 123 instruction cycles that must be added to the 5,125 consumed by delay_125mics. In fact, there are several other minor delays by the instructions to initialize the counters that are not included in the calculation. In reality, the delay loops required for LCD interfacing need not be exact, as long as they are not shorter than the recommended minimums.
For calculating software delays in the 16f84, the instruction execution time is determined by an external clock either in the form of an oscillator crystal, a resonator, or an RC oscillator furnished in the circuit. The PIC 16f84A is available in various processor speeds, from 4MHz to 20MHz. These speeds describe the maximum ca-