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

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

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

Добавлен: 15.06.2025

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

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

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

Displays

Interfacing

Full control of an LCD module requires 8 bidirectional lines for reading and writing data and 3 outputs for the control signals. To save four lines, you can use the 4-bit data interface described later. Also, the ability to read the display and the busy flag at D7 are optional. If you give these up, you can use outputs (such as the 74HC374’s) instead of bidirectional port bits for D0-D7, and eliminate one of the control lines by tying R/W low.

Figure 8-9 shows an LCD module connected to an 82(C)55, using an 8-bit bidirectional interface. The interface uses Port A and three bits of Port C on an 8255. You can use any of the 8255’s port bits, if you write your program to match.

On the LCD module, pins 1-3 connect to ground, +5V, and a contrast potentiometer. For maximum contrast, connect pin 3 directly to ground. Pins 4-6 are the control signals for the LCD module. These connect to three outputs on Port C. The eight data bits, pins 7-14 on the LCD module, connect to Port A.

Listing 8-4 is a BASIC-52 program that initializes a 2-line display and writes LINE 1 and LINE 2 to the matching lines.

Initializing the module. On power up, the LCD module must initialize properly. If power-up is clean, with the supply voltage rising from 0.2V to 4.5V in 10 milliseconds or less, the module initializes automatically. But, if power-up doesn’t meet this requirement, your program has to provide the initialization routine. It’s a good idea to always include an initialization routine in your program, since it does no harm, and if the module doesn’t initialize properly, it won’t respond correctly or at all.

Table 8-3 summarizes the initialization procedure. In short, the module must first receive three identical commands selecting an 8-bit interface. BASIC-52 easily provides the necessary delays between the commands. To begin the initializing, you must send the instruction to select an 8-bit interface, even if your interface is four bits.

Once this is done, the instructions for Function Set, Display On, Display Clear, and Entry Mode Set tell the controller the configuration you desire. The automatic power-on initialization routine turns the display off, so if you use it, you have to turn the display on by writing 0Ch to the instruction register. When initializing is complete, you can control the display as you wish, though you can’t change the number of display lines unless you reinitialize from the beginning.

Listing 8-4 has two subroutines, one for writing characters to the display and one for writing instructions. To write a character, set D equal to the character’s code, and call subroutine 800, which sets RS, writes the character to the display, and toggles E. To write an instruction,

The Microcontroller Idea Book

145

Chapter 8

Listing 8-4 (page 1 of 2). Initializes a 2-line LCD module and displays a message on each line.

10

REM address of 8255, Port A

20

A=0FC00H

30

REM address of 8255, Port C

40

C=A+2

50

REM address of 8255, Control port

60

X=A+3

70

REM Control word for Enable (PC.5)

80

E=0AH

90

REM Control word for RW (PC.6)

100

RW=0CH

110

REM Control word for RS (PC.4)

120

RS=8

130

REM Initialize LCD module

140

REM initial values

150

XBY(X)=80H: REM Ports A,B,&C are outputs

160

XBY(X)=E : REM E=1

170

XBY(X)=RW : REM RW=1

180

XBY(X)=RS : REM RS=1

190

REM function set: 8-bit interface, 3 times

200

REM toggle E after each instruction

210

XBY(A)=30H

220

XBY(X)=E+1:XBY(X)=E

230

XBY(A)=30H

240

XBY(X)=E+1:XBY(X)=E

250

XBY(A)=30H

260

XBY(X)=E+1:XBY(X)=E

270

REM function set to match module

280

XBY(A)=38H

290

XBY(X)=E+1:XBY(X)=E

300

REM display on

310

XBY(A)=0CH

320

XBY(X)=E+1:XBY(X)=E

330

REM clear display

340

XBY(A)=01H

350

XBY(X)=E+1:XBY(X)=E

360

REM entry mode set

370

XBY(A)=06H

380

XBY(X)=E+1:XBY(X)=E

146

The Microcontroller Idea Book


Displays

Listing 8-4 (page 2 0f 2).

390 REM display “LINE 1", ”LINE 2"

400 D=ASC(L):GOSUB 800

410 D=ASC(I):GOSUB 800

420 D=ASC(N):GOSUB 800

430 D=ASC(E):GOSUB 800

440 D=20H:GOSUB 800

450 D=ASC(1):GOSUB 800

460 I=0C0H

470 GOSUB 900

480 D=ASC(L):GOSUB 800

490 D=ASC(I):GOSUB 800

500 D=ASC(N):GOSUB 800

510 D=ASC(E):GOSUB 800

520 D=20H:GOSUB 800

530 D=ASC(2):GOSUB 800

600 END

790 REM write data to the display

800 XBY(X)=RS+1

810 XBY(X)=RW

820 XBY(A)=D

830 XBY(X)=E+1:XBY(X)=E

840 RETURN

890 REM write an instruction to the display

900 XBY(X)=RS

910 XBY(A)=I

920 XBY(X)=E+1:XBY(X)=E

930 RETURN

The Microcontroller Idea Book

147

Chapter 8

Table 8-3. Initialization procedure for LCD modules using HD44780 controller.

Power on

Wait 15 milliseconds after V+ = 4.5V

Function set = 30h

Wait 4.1 milliseconds

Function set = 30h

Wait 100 microseconds

Function set = 30h

Function set to match display module

Display on

Display clear

Entry mode set

set I equal to the instruction and call subroutine 900, which clears RS, writes the instruction to the display, and toggles E.

Using the example program as a model, you can experiment with your own messages by adapting the code in lines 400-530.

Listing 8-5 is another test program that displays a prompt on the host’s screen and then displays the character you type at the keyboard both on the host’s screen and on the LCD module. To use this program, you must add lines 10 through 380 of Listing 8-4 to initialize the module and variables.

The 4-bit Interface

The HD44780’s 4-bit data interface can be convenient if you don’t have a lot of port bits to spare. The minimum interface requires just 6 outputs, to D4-D7, RS, and E. The drawback is that the 4-bit interface is slower in operation and more complicated to program.

To send an instruction using a 4-bit interface, you send half at a time over D4-D7, along with the appropriate RS and R/W signals. D0-D3 are unused. For example, with an 8-bit interface, writing Z (5Ah) to the display requires the following operations:

clear R/W set RS

write 5Ah to D0-D7 bring E high, then low

148

The Microcontroller Idea Book

Displays

Listing 8-5. Displays key presses on the host computer’s screen and on an LCD module.

1REM reserve space for 1 string variable,

2REM 1 character in length

3STRING 3,1

4REM You must add lines 10 through 380 of listing 8-4

5REM to this program

400 Z=0

410 DO

420 INPUT “Press a key: ”,$(0)

430 PRINT $(0)

440 REM reset display after 8 characters

450 IF Z=8 THEN Z=0:RW=0:I=1:GOSUB 900

460 REM keep track of how many characters are displayed 470 Z=Z+1

480 REM display the character matching the key press

490 D=ASC($(0),1):GOSUB 800

500 WHILE 1=1

600 END

790 REM write data to the display

800 XBY(X)=RS+1

810 XBY(X)=RW

820 XBY(A)=D

830 XBY(X)=E+1:XBY(X)=E

840 RETURN

890 REM write an instruction to the display

900 XBY(X)=RS

910 XBY(A)=I

920 XBY(X)=E+1:XBY(X)=E

930 RETURN

The Microcontroller Idea Book

149


Chapter 8

Listing 8-6 (page 1 of 2). Creates and displays a custom character (upside-down question mark) on an LCD module.

1REM You must add lines 5-380 from Listing 8-4 to this

2REM program.

400 REM R1-R8 store row patterns for custom character

401R1=4

402R2=0

403R3=4

404R4=8

405R5=10H

406R6=11H

407R7=0BH

408R8=0

410 REM custom character number

420 CC=0

430 XBY(X)=RS

440 XBY(X)=RW

450 REM display clear

460 XBY(A)=1

470 XBY(X)=E+1:XBY(X)=E

480 REM set CG RAM address to 0

490 XBY(A)=40H

500 XBY(X)=E+1:XBY(X)=E

510 REM store R1-R8 in CG RAM

520 D=(CC)+R1

530 GOSUB 800

540 D=(CC)+R2

550 GOSUB 800

560 D=(CC)+R3

570 GOSUB 800

580 D=(CC)+R4

590 GOSUB 800

600 D=(CC)+R5

610 GOSUB 800

620 D=(CC)+R6

630 GOSUB 800

640 D=(CC)+R7

650 GOSUB 800

660 D=(CC)+R8

670 GOSUB 800

150

The Microcontroller Idea Book


Displays

Listing 8-6 (page 2 of 2).

680 XBY(X)=RS

690 XBY(X)=RW

700 REM set DD RAM address to 0

710 XBY(A)=80H

720 XBY(X)=E+1:XBY(X)=E

730 XBY(X)=RS+1

740 REM write custom character 0 to display

750 XBY(A)=0

760 XBY(X)=E+1:XBY(X)=E

770 END

790 REM write data to display

800 XBY(X)=RS+1

805 XBY(X)=RW

810 XBY(A)=D

820 XBY(X)=E+1:XBY(X)=E

840 RETURN

890 REM write an instruction to the display

900 XBY(X)=RS

910 XBY(A)=I

920 XBY(X)=E+1:XBY(X)=E

930 RETURN

With a 4-bit interface, you have two extra steps:

clear R/W set RS

write 5h to D4-D7 bring E high, then low write 0Ah to D4-D7 bring E high, then low

Custom Characters

If the 192 characters provided in the CG ROM aren’t enough, you can create your own. To design a character, draw a 5 x 7 matrix and fill it with 1s where you want dots, and 0s where you want nothing. Figure 8-10 illustrates, with an example of an upside-down question mark for Spanish-language messages. Listing 8-6 creates the character and stores and displays it.

The Microcontroller Idea Book

151

Chapter 8

Figure 8-10. You can create custom characters with the HD44780 controller.

The CG RAM stores the bit values for each row in the character.

For your own designs, change the values of R1-R8 in lines 401-408 to match the symbol you want.

Mounting Displays in an Enclosure

Mounting a display in an enclosure for a finished project usually involves cutting or drilling the enclosure and wiring the display to the control circuits.

For individual LEDs, you can buy inexpensive mounting rings, and drill matching holes in the enclosure’s front panel.

Seven-segment modules often mount on separate circuit boards that fit over an opening cut into the enclosure. For a more finished appearance, you can buy bezels with matching sheets of clear or tinted plastic to cover the displays and mounting hole.

Most character-based LCD modules have a mounting hole in each corner of the circuit board. You’ll need to drill matching holes in the enclosure, and cut a hole for the display to show through. Some displays have a ribbon cable attached; others have 14 holes on 0.1" centers, to which you can solder a ribbon cable or a header into which a cable plugs.

Be sure to mount your displays so they will be visible from the expected viewing angle. If necessary, tilt the display slightly in its mounting.

152

The Microcontroller Idea Book

Using Sensors to Detect and Measure

9

Using Sensors to Detect and Measure

With your 8052-BASIC system and some sensors, you can detect and measure properties such as temperature, light, chemical composition, motion, and more. This chapter focuses on how to use sensors in an 8052-BASIC or other microcontroller system.

Sensor Basics

A sensor is a device that responds to a physical property or condition. Other terms for sensor are detector and transducer. Sensors enable a circuit to learn about the world outside of itself, much as humans use the senses of sight, hearing, touch, smell, and taste.

A sensor may respond in any of a number of ways. For example, litmus paper is a sensor that responds to acidity by changing color. For interfacing to the 8052-BASIC, we’re interested in sensors that respond electrically, by varying in voltage, current, or resistance, since these are easily interfaced to electronic circuits.

One obvious use for sensors is in environmental monitoring, including detecting and measuring temperature, light, wind speed and direction, humidity, and so on. But all kinds of electronic devices use sensors, even when sensing isn’t the primary purpose. For example, computer printers have sensors that detect when the printer is out of paper. Many cameras

The Microcontroller Idea Book

153


Chapter 9

can sense light level and distance. And modern automobiles contain all kinds of sensors, including ones to measure engine temperature, composition of exhaust emissions, oil pressure, engine speed, and whether or not the seatbelts are fastened.

You can find a sensor to detect and measure just about any property. Some sensors are readily available from suppliers of other electronic components. These include photodiodes and solar cells, which respond to light, and semiconductors that respond to changes in temperature.

Surplus catalogs sometimes have good deals on sensors from failed or obsolete products— for example, dollar-bill sensors from vending machines and motion detectors from security systems.

Sometimes you can make your own sensors from everyday materials. The conductive foam commonly used to hold CMOS components can double as a simple pressure sensor, since its top-to-bottom resistance decreases as the foam is pressed. A popular homemade moisture detector is a printed-circuit board with two interleaved but untouching copper traces. When the board is wet, water shorts the traces together and changes the resistance between them from very high to a few hundred ohms.

Some projects call for a specialized sensor that you just won’t find in the usual sources. A good resource is the Sensors Buyer’s Guide, published annually by Sensors magazine. The guide lists over 1200 companies involved with sensors, and indexes them according to property sensed, technology used, manufacturer, and related products and services. From the list of properties sensed, you can select the category that interests you and consult a list of companies that offer products in that area. Most companies are happy to provide product information and applications hints.

Choosing Sensors

To pick the right sensor for a job, you first need to specify what you want the sensor to do. Below are some of the questions to ask about your desired sensor. The example answers describe a temperature sensor intended for use in a controller used in processing photographic film:

What property do I want to measure? (temperature)

What range of inputs do I need to measure? (60-110 degrees Fahrenheit)

What resolution and accuracy do I need? (accurate to within 0.5 degree Fahrenheit)

How fast must it respond to input changes? (quick response not critical for this application)

What kind of output do I need (analog, digital, voltage, current,...)? (8-bit digital output would be ideal, but analog voltage or current output is OK)

154

The Microcontroller Idea Book

Using Sensors to Detect and Measure

• What power supplies are available to power the sensor? (+12V, +5V)

The answers to these questions will help you narrow your choices as you research what’s available.

On/off Sensors

Sometimes, all you need to detect is the presence or absence of the sensed property. Some simple sensors act like switches, with a low resistance in the presence of the sensed property, and a high resistance in its absence.

There are many types of sensors that you can use in this way. A magnetic proximity sensor responds to the physical separation of the items connected to each of the switch elements. A vibration sensor responds to rapid motion. Both of these are often marketed as home-se- curity devices for use on doors or windows, but you might come up with other uses for them. Another example is a mercury tilt switch, which uses a ball of liquid mercury as a conductor. The switch contacts open or close when the switch tilts and the mercury rolls to the opposite end of the switch. Figure 9-1 illustrates.

Figure 9-2 shows two ways to detect the state of on/off sensors like these.

Figure 9-2A is an unlatched input. When the resistance across the sensor is high, the pull-up resistor brings the input voltage high. When the sensor’s resistance is low, the input goes low.

You can connect this circuit to any unused pin on an input port. If you use the 8052-BASIC’s INT1 input, you can use an ONEX1 statement to trigger a subroutine whenever the sensor detects the property in question. If you use an ordinary port input, reading the port bit will tell you the current state of the sensor.

In Figure 9-2B, when the sensor switches from high to low resistance, a 74LS73 JK flip-flop stores the information as a high Q output, which your program can read at its leisure. After reading the input, strobing the CLR input low brings Q low again, until the next sensing event. The flip-flop “remembers” past events, so you don’t have to detect or respond to events as they happen.

Figure 9-1. The tilt, or physical angle, of the mercury switch determines which of its three terminals connect.

The Microcontroller Idea Book

155