Sie sind auf Seite 1von 3

A Digital Thermometer Using the

AT89C2051 Microcontroller

Introduction and handles the newline character by
The system presented in this application
advancing the cursor to the beginning of
the next line.
note implements a simple digital ther-
mometer that includes a built-in LCD and PositionLcd simply sets the cursor Microcontroller
RS-485 communication port. It is address to the value specified by the
designed around Atmels AT89C2051 caller. The ClearLcd function is used to
processor, a DS1620 digital thermome- clear the entire LCD and home the cur-
ter/thermostat from Dallas Semiconduc- sor. Application
tor, a small 8 X 2 LED backlit LCD, and The functions that follow concern them-
an RS485 line interface. The system, selves with actual physical communica- Note
shown in Figure 1, can be used as the tion to the LCD. Since there is not a
basis for developing custom solutions for direct correspondence between the
networked and stand alone data collec- LSIs data RAM and the LCDs physical
tion and control equipment. It can be mapping, it is necessary to keep watch
centrally powered due to its low current for certain boundary conditions. When a
requirement and its small size allows it to boundary is encountered, the cursor
be placed almost anywhere. must be repositioned in order to keep the
output contiguous. Since all displayable
data must pass through DataWr, it
Software makes sense to contain the corrective
The LCD driver is written entirely in C actions here. To handle this problem,
and compiles under Micro-C (from Dun- keep track of the logical cursor position
field Development Systems) using the and invoke a remedial maneuver when-
tiny memory model. Although a canoni- ever discontinuity may occur. There are
cal stack-based implementation, Micro-C two ways you can accomplish this:
includes a number of special features
Read the LCDs status register (to get
that make it quite suitable for generating
the cursor address) or
ROMable code for small systems. The
overhead incurred performing stack Keep a local copy just for your
manipulations is made up by the library reference.
functions that are all hand coded in an Not wanting to waste a pin to control the
highly optimized assembler. As an LCDs read/write line (it runs in write-only
added benefit, Micro-C comes with fully mode), the latter approach was adopted.
documented library source code so spe- Here, the global register (IRAM actually)
cial modifications can be made as cir- variable Cursor is used for this purpose.
cumstances arise. Cursor is consulted prior to any data
The first few functions contained in the write operation. If a correction is neces-
LCD driver module are conventional C sary, a new cursor address is generated
library implementations. PutString dis- and dispatched to the LCD control regis-
plays a null terminated string by merely ter via CommandWr.
passing characters off to PutChar until a Following this, DataWr splits the data
null byte is encountered. PutChar out- byte into nibbles (remember the LCD
puts a character at a time to the LCD operates using a 4-bit bus) and falls

through to handle the actual physical transfer. Using Micro- nated by pulling \RST low which forces DQ into a high
Cs extended preprocessor allows bit manipulation macros impedance state and concludes the transfer. Temperature
that expand directly to 8051 SETB and CLR instructions. data is transmitted over the 3-wire bus in lsb first format. A
Here, clearing DRS selects the LCDs data register and total of nine bits are transmitted where the most significant
DEN is toggled to generate the data strobe. CommandWr bit is the sign bit. If all nine bits are not of interest, the trans-
operates similarly but does not have to deal with any cursor fer can be terminated at any time by asserting \RST.
entanglements. It selects the command register as its desti- The DS1620 support routines are coded in assembly. The
nation by setting DRS high prior to clocking the nibbles DS1620 also has nonvolatile EEPROM configuration regis-
across the interface. ters that hold thermostatic and operational control informa-
The initialization function InitLcd begins at a more rudimen- tion. TempConfig is hardcoded to set the mode for opera-
tary nibble oriented level since no assumption can be made tion under CPU control and continuous temperature con-
as to the operational status of the LCD at this time. The first version. Once in continuous conversion mode, the actual
three sequences ensure that the transfer mode is set to conversion process is started by issuing the start conver-
operate over a 4-bit bus. Repeating the sequence three sion command through TempConvert. Now the DS1620
times ensures that the command will be recognized regard- can be read at any time and the last temperature conver-
less of the operational mode of the LSI. (It is wise to make sion that was performed will be returned. This is accom-
no assumptions when performing any low-level initializa- plished by calling TempRead. The result is returned in the
tion.) Following this, the actual operating parameters are 16 bit accumulator as defined by Micro-C consisting of the
transferred to the LCD using the standard CommandWr B (msb) and ACC (lsb) registers.
function. Software for this application note may be down-
loaded from Atmels Web Site or BBS.
Mainline Glue
Coordination of the support drivers is managed by the main
Digital Temperature module. This module takes control after the Micro-C startup
Temperature acquisition is handled using the DS1620 ther- routine finishes. On entry, the code initializes the serial
mometer/thermostat IC from Dallas Semiconductor. The port, instructs the DS1620 to start performing temperature
DS1620 contains all temperature measurement and signal conversions, initializes the LCD, displays a log on mes-
conditioning circuitry on-chip and presents the processor sage, and enters into an endless loop. This loop continu-
with a 3-wire digital interface composed of a bi-directional ously reads the DS1620, performs a Celsius to Fahrenheit
data line DQ, a reset input \RST, and a clock input CLK. conversion, translates the resulting binary number to an
The temperature reading is provided in a 9 bit, twos com- ASCII string, and displays the conversion result on the
plement format. The measurement range spans from -55C LCD. Periodically the code falls through and toggles an
to +125C in .5C increments. LED and transmits the temperature data serially.
Data transfers into and out of the DS1620 are initiated by The Celsius to Fahrenheit conversion is performed using
driving \RST high. Once the DS1620s reset is released, a the familiar equation: F = C * 9/5 + 32. Since the DS1620
series of clock pulses is emitted by the processor to actu- returns temperature in .5C increments the value is first
ally transfer the data. For transmission to the DS1620, data divided by 2. Unlike the often impenetrable gyrations that
must be valid during the rising edge of the clock pulse. result when working with numbers in assembler, the C rep-
Data bits received by the processor are output on the fall- resentation of this calculation is perfectly clear in intent and
ing edge of the clock and remain valid through the rising function. A short sequence of divisions, modulos, and logi-
edge. Taking the clock high results in DQ assuming a high cal OR operations results in decimal ASCII values that
impedance state. The sequence can be immediately termi- make up the output string.

5-60 Microcontroller