CONTROLLING THE OUTPUT AND READING THE INPUT:THE LCD DISPLAY

THE LCD DISPLAY

This section describes the use of and interactions with existing hardware connections as they come with the LAB-X1 module. Other wiring schemes can be used with ease as defined in the compiler manual.

The LCD is controlled from PORTD, and all eight bits of this port are connected to the LCD. You therefore have the choice of using only the four high bits as a 4-bit data path for the LCD or using all eight bits. The entire port is also connected to eight of the LEDs on the 10-light LED bar graph. (The two leftmost LEDs in the bar graph are used to indicate that the power to the LAB-X1 is on.) The four high bits, bits D4 to D7, cannot be used for any other purpose if the LCD is being used. The software does not release these four bits automatically after using them to transfer information to the LCD, but you do have the option of saving the value of PORTD before using the LCD and then restoring this value after the LCD has been written to. The complication, of course, is that there will be a short glitch when the LCD is written to, and the use you make of PORTD has to tolerate this discontinuity.

PORTE, which has only three external lines, is dedicated to controlling the information transfer to the LCD. These lines can be used for other purposes (analog or digital) if the LCD is not being used.

The LCD provided on the LAB-X1 allows us to display two lines of 20 characters each. Its connections to the microcontroller are shown on the schematic provided with the LAB-X1 and are as shown in Figure 5.3.

RUNNING SMALL MOTORS-0023

Figure 5.3 is an easy to comprehend schematic diagram that shows the lines between the microcontroller and the display module. The other wiring is still in place, but it has been suppressed so we can concentrate on the LCD connections.

In Figure 5.3 we see that the LCD uses all the lines available on ports D and E. All of PORTD is used as the port the data will be put on, and PORTE, which has only three lines, is used to control data transfer to the LCD. We also know from looking at the full schematics provided with the LAB-X1 that all of PORTD is also connected to the LED bar graph. This does not affect the programming of the LCD and we will ignore this for now. You will, however, notice that the LEDs in the bar graph go on and off as programs run because we will be manipulating the data on these lines (D0 to D7). It is also possible to control the LCD with just the four high bits of PORTD, and we will use the scheme for most of the programs in this book. See the PBP manual for more information on how this is done.

Let us write the ubiquitous “Hello World” program for the LCD as our first exercise in programming the LCD. Once we know how to do that, we can basically write what- ever we want to the LCD display and whenever we want to.

Before we can write to the LCD we have to define how the LCD is connected to the MCU. Also, since the 16F877A has some analog capabilities, it always starts up and resets in its analog mode, and it has to be put into digital mode for (at least) PORTE before we can use any of the digital properties of the affected ports (A and E).

The compiler manual says that we have to specify the location of both the LCD data and the LCD control lines that connect it to the system so that the compiler can address the device properly. Doing so allows us to place the LCD where convenient for us, in memory (the I/O lines), when we design our own devices, and the compiler will be able to address the LCD. The ports and lines used are specified in DEFINE statements that must be executed early in the program before the LCD is addressed.

Note When you are designing your own devices it will be an advantage to place your LCD at the same memory locations used by the LAB-X1 so that your programs will run on the LAB-X1 for testing purposes, should you get into trouble. Being able to run the program on the LAB-X1 will let you know if it is a hardware or a software problem. All the devices I built used the same addresses as the LAB-X1 for the LCD, and this is reflected in the programs listings throughout this book.

For the LCD display registers, on the LAB-X1, the DEFINE statements are as indi- cated in Program 5.4.

RUNNING SMALL MOTORS-0024

RUNNING SMALL MOTORS-0025

Program 5.4 demonstrates the most elementary control over output to the LCD display. Variations of these lines of code will be used to write to the LCD in all our programs. (We will always use these addresses but when the reader writes his or her programs they can be at any suitable address.) Be sure to include the PAUSE 500 instruction in all your programs to allow the LCD enough time to initialize.

Not all the preceding DEFINE statements are needed on the LAB-X1, and you will notice this in some of the sample programs in this book, but when you build your own devices, you will need to include them all to make sure that nothing has been omitted.

ADCON1 = Analog to Digital CONtrol register #1.

The ADCON1=%00000111 statement, or one like it, is needed for our use of the 16F877A because any PIC MCU processor that has any analog capabilities comes up in the analog mode on reset and startup. In the analog mode all the lines of the PIC that have analog capabilities are set to the analog mode. This particular instruc- tion puts all the analog pins on ports A and E into the digital mode. Since we need only PORTE and PORTD for controlling the LCD, none of PORTA needs to be in digital mode. I am showing %00000111 because all the examples provided by MicroEngineering Labs use this value. See the data sheet for more detailed informa- tion. (The use of this register is explained in Table 9-8 on using the LCD.) If you want to turn just the three available lines on PORTE to digital mode, you can use any binary value from 010 to 111 inclusive.

The control of the A to D conversion capability is managed by the four low bits of ADCON1. For our purposes, bit 0 and bit 3 are not relevant.

Note Much of the information in this chapter can be found on page 126 in Section 11 Analog to Digital Converter (A/D) Module of the data sheet.

The following lists how the four least significant bits in register ADCON1 are used to manage the A to D setting of the three bits of PORTE and five relevant bits of PORTA. (We are setting them all except PORTA.4 to digital.) Bit 0 is not relevant to the LCD operation (it is a “don’t care” bit).

N Bit 1 and 2 must be set to 1 to make the two ports (A and E) digital

N Bit 3 is not relevant to the LCD operation (it too is a “don’t care” bit.)

RUNNING SMALL MOTORS-0026

Leave a comment

Your email address will not be published. Required fields are marked *