用MAXQ3212微控制设计数字温度监控器.pdf
Maxim/Dallas App Notes MICROCONTROLLERS Keywords:MAXQ3212,MAXQ3210,microcontroller,reduced instruction set,RISC,low pin count package,LPC,thermostat,digital thermomostat,1-Wire,DS18B20,DS28EA00,ICM7218 Nov 30,2006 APPLICATION NOTE 3965How to Use the MAXQ3212 Microcontroller as a Digital ThermostatThis application note describes a digital thermostat based on the MAXQ3212 RISC microcontroller.This simple system senses the ambient temperature,compares it to a user-input set point,and switches an output relay as a result of this comparison.System components also include a 1-Wire digital thermometer and a 4-digit,7-segment LED display driven by an 8-digit LED display driver.By choosing this microcontroller and the highly integrated display driver,a minimal component count system is achieved.IntroductionThere are situations where temperature regulation and control are desirable.When the ambient temperature is too cold,for example,a light bulb can be used as a heat source in a small enclosure to dry or cure paint or similar material.Temperature sensing,power switching,and a basic heat source thus provide a closed-loop temperature-control system for many situations.This application note describes how to use a MAXQ microcontroller to implement this type of control system,also called a digital thermostat.The MAXQ3212 is chosen as the system controller because of its functionality,ease of use,low cost,and the ready availability of an inexpensive development environment,the MAXQ3210 Evaluation Kit(MAXQ3210-KIT).The complete development environment for this example code is version 1.0 of the MAX-IDE and revision A of the MAXQ3210 kit board.System Implementation DetailsOverviewThe system implementation for this digital thermostat is quite simple.Figure 1 shows a schematic of the circuit.A DS18B20 1-Wire Digital Thermometer measures ambient temperature.The system controller compares the measured ambient temperature to a user-input set point;and a relay is activated if the ambient temperature is below the set point.The system uses a 4-digit LED display to show the ambient temperature in normal operation and the set point when it is input by the user.A MAXQ3212 serves as the systems controller.It performs the user-interface function(set point up and down)through two pushbutton switches connected to the processor through port pins configured as inputs.Another port pin is used as a single output dedicated to controlling the output relay.More detailed image(PDF,36.7kB)Figure 1.Thermostat circuit schematic.For the systems display,a 4-digit,7-segment LED was chosen so the thermostat would be usable in a variety of lighting conditions without additional electronics such as backlighting.To simplify the processors interface to the display and to provide the necessary drive current and multiplexing,a Maxim ICM7218AIPI 8-digit LED driver was selected.This device contains all the circuitry necessary to interface the microcontroller directly to the common-anode,LED display.The LED driver drives up to eight digits,although only four digits are used in this application.This device drives each of the 7 segments directly without external current-limiting resistors.The systems output is switched AC power provided by a standard electromechanical relay which switches AC power to an arbitrary load and can be sized according to the loads power demand.This relay was chosen because it can switch 8 amps and its coil voltage is 5V,which is the systems supply voltage.The relay can,however,operate from any available power source that does not exceed the voltage or current capabilities of the final drive FET(BS170).While it is certainly possible to do so,no attempt was made to model the precise behavior of the system and optimize the closed-loop performance.A simple comparison between the actual and desired temperatures is the only output-switching criterion here.The softwares subroutine Relay_Switch can be modified,as desired,to achieve the desired system behavior.A single DS18B20-PAR measures the ambient temperature.This device has an accuracy of 0.5C over a range of-10C to+85C.In this application,the temperature is expected to range between+12C to+38C(+55F to+100F).The resolution of this device is user-selectable from 9 to 12 bits;the resolution selected for this application is explained below.While it is not used in this application,each DS18B20 contains a unique,64-bit identification number that supports multiple devices on the 1-Wire network.1-Wire InterfaceDallas Semiconductors 1-Wire bus is a unique serial interface that requires a single signal line and ground to perform communications.This interface enables a microcontroller to communicate with a 1-Wire device using a single port pin.This interface also provides power,called parasitic power,over the 1-Wire signal line,which allows the 1-Wire device to operate without a local supply.While not important for this application,this independent operation is required in some remote temperature-measuring situations.Although this application requires only a single 1-Wire device,the 1-Wire bus is designed to operate with a single master and multiple slaves,i.e.,it is multidrop.While this application uses a single DS18B20 device for temperature measurement,there are several devices from Dallas Semiconductor that perform this function.For instance,in applications sensing temperatures in multiple locations,the DS28EA00 is designed specifically for multidrop applications,and it simplifies the detection of the physical sequence of 1-Wire devices in a 1-Wire network environment.In this thermostat application,a single port pin is used to bit-bang and communicate over the 1-Wire bus.The software for this function comes directly from Application Note 3769,Building a 1-Wire Temperature Logger Using the MAXQ3210.This code was written specifically for the MAXQ3210 microcontroller and the DS1822 Econo 1-Wire Digital Thermometer.Only a minor modification was required to change the resolution from nine bits in the original software to 10 bits in this application.Application Note 3769 also provides references for the 1-Wire bus and its applications.MAXQ3212 I/O PinsThe MAXQ3212 has 15 port pins:8 for port zero,and 7 for port one.To interface with the ICM7218 display driver,a minimum of 10 signals(port pins)is required.Four other signals are required to interface with the temperature up switch,the temperature down switch,the relay driver,and the DS18B20.This makes the total requirement 14 port pins.In normal reset default operation,four of the MAXQ3212s 15 port pins are used for the JTAG interface(P1.0,P1.2,P1.3,and P1.4)and one for the reset input(P1.1)function.Although these five signals control debug functions,they are needed for this application.Consequently,some consideration was given to the overall debugging strategy.The default debug functions of these five port pins can be disabled by setting the control bits TAP and RSTD to their appropriate levels(0 and 1,respectively).When disabled,the port pins allocated to these debug functions behave as ordinary port pins,and the debug operations are no longer available.Therefore,the final application software routines debugged were the display operations.Display operations were chosen because they require the largest number of I/O pins,and the functions are relatively uncomplicated and need little debug.The assembly language code has a large delay(approximately 5 seconds)inserted before the instructions to disable these five debug control signals.This delay allows the debugger to gain control of the processor before the debug controls are disabled.In this way,the debugger can regain control of the part if necessary.Display SelectionThe primary requirement for the display options was that the thermostat be used indoors or outdoors,in bright light or darkness.It was decided that an LED display would provide the best performance under these widely varying ambient lighting conditions,except perhaps in very bright light such as a sunny day outdoors.When this device is used in such bright environments,a simple colored lens provides the necessary display contrast and visibility.Finally,the LED display avoids the need for additional circuitry such as a backlight source.Four digits of display are required for this application and there are 7 segments per digit,without multiplexing.Therefore,28 individual signals are required(4 digits x 7 segments/digit),not counting a decimal point.Clearly,the MAXQ3212 has an insufficient number of port pins to manage this interface without additional circuitry.The solution to the problem is the Maxim ICM7218,which provides multiplexing circuitry for up to 8 digits,and is capable of driving the LED segments directly without external current-limiting resistors.A simple 8-bit interface with two control signals(mode and write)interfaces this device to the processor.User Input of the Set PointGiven the number of available port pins on the MAXQ3212,it is not practical to provide a parallel interface for the user to input the desired set point.Therefore,a simple up or down pushbutton interface allows the user to increment or decrement the value.When the system initially powers up,the firmware configures the default set point to+72.5F,and stores this value in the MAXQ3212s EEPROM data memory.If the user increments or decrements the set point,the new value is stored in EEPROM.Since the EEPROM is nonvolatile,it is not erased by a power interruption.When an up or down pushbutton closure is detected,the LED displays the current set point.To help identify that it is the set point being displayed,the microcontroller activates the decimal point of the last digit,which is normally off.If the pushbutton remains pressed for a sufficient period,the set point automatically begins to increment or decrement by 0.5 degrees Fahrenheit approximately every second.When the desired set point is reached,the user releases the pushbutton and the new set point takes effect.The display then returns to displaying the measured ambient temperature,and the last digits decimal point is turned off.Temperature ConversionOne of the more interesting aspects of the software for this application is the temperature conversion from degrees centigrade to degrees Fahrenheit.Upon completion of the DS18B20s Convert Temperature command,the 16-bit sign-extended results are stored in the devices temperature register in the format shown below.Temperature Most Significant Byte Bit 15 Bit 14 Bit 13 Bit 12 Bit 11 Bit 10 Bit 9 Bit 8SignSignSignSignSign262524Temperature Least Significant Byte Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0232221202-12-22-32-4The temperature sensors resolution is user-configurable to 9,10,11,or 12 bits,which correspond to increments of 0.5C,0.25C,0.125C,and 0.0625C,respectively.The DS18B20s default resolution at power-up is 12 bits,but it is initialized to 10 bits for this application.When configured for 10-bit resolution,bits 1 and 0(2-3,2-4)of the results are undefined,and the minimum temperature resolution is 0.25 degrees centigrade.In this application,the temperature is displayed in degrees Fahrenheit.The following equation performs this conversion.Degrees Fahrenheit=(Degrees Centigrade*1.8)+32(Eq.1)Because the temperature is created as a fractional number and the conversion requires a multiplication by 1.8,calculations with binary fractions are required.In performing this multiplication of real numbers,minimizing any calculation errors without taxing the arithmetic resources of the processor is obviously desirable.Since the DS18B20 is initialized to produce the temperature value as a 10-bit number,bits 1 and 0 have no meaning.The remaining two bits(bits 3 and 2)of fractional temperature information provide a 0.25C(0.45F)resolution.As the MAXQ3212s accumulators are 8-bits wide,it is convenient to use a byte as the fractional portion of the calculated temperature data.Therefore,the multiplier can contain six bits of fractional information.The information below shows that six bits of fractional information will approximate the decimal value of 0.8 to 99.61 percent:2-1=0.500000=62.50%of 0.82-2=0.250000+0.50000=0.750000=93.7%of 0.82-5=0.031250+0.0750000=0.781250=97.65%of 0.82-6=0.015625+0.781250=0.796875=99.61%of 0.8Converting to Fahrenheit with this degree of precision is more than sufficient for the resolution of the temperature sensor.As a specific example of this conversion calculation,assume that the temperature measurement produces a value of+24.2510.(018.116)degrees Centigrade.This is represented as follows:Temperature MS Byte Bit 15Bit 14Bit 13Bit 12Bit 11Bit 10 Bit 9 Bit 80(sign)0(sign)0(sign)0(sign)0(sign)001Temperature LS Byte Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0100001xxIllustrated a different way,this is 00000001 1000.01xx2.This value must be multiplied by 1.810.Converting 1.810 to a binary representation truncated to six digits of fractional information,this translates into a constant of 0001.1100112.The conversion calculation follows:0000s0 011000.012=006116x 00000000 01.1100112=007316-00101011.100100112=2B9316+00100000.000000002=32.010-01001011.100100112=4B9316=75.57421910It can be seen that this results in a value of 75.574219 to the precision of the calculation.If this result is truncated to two bits of fractional information,the result is 75.5 degrees Fahrenheit which is within 0.15F of the value arrived at using a calculator and full floating-point arithmetic.This precision was deemed adequate for the accuracy of this application.Dual-FET Coil DriveIn this application,two Field Effect Transistors(FETs)interface the processors output port pin to the electromechanical relay.The reset default condition for the MAXQ3212s port pins is a weak pull-up,high state.Therefore,when the processor powers up,its port pins will be at a high state until the application software changes this.If a single n-channel FET(BS-170)is used to switch power to the relays coil,the reset default polarity of the port pin would cause the relay to activate when the processor powers up.The port pin would remain in that state until the application software changes the pins direction to an output and set its state to zero.This state change is not desirable as it allows a transitional period where the relay is briefly activated and then deactivated.To correct this behavior,two FETs are used in series as shown in the schematic.By using two FETs,the reset default polarity of the port pin does not activate the relay.Example CodeThe software accompanying this application note is available at:ftp:/ file contains the source code files:Thermostat.asm,1-Wire.asm,BCD.asm,ThermDisp.asm,and the header file maxq3210.inc that contains the MAXQ3210/MAXQ3212s register address definitions.Also included in this.zip file is the MAX-IDE project file,Thermostat.prj,and the loadable hex file,Thermostat.hex.By extracting these files to a common directory,the software can be assembled and executed on