When using DS18B20, be sure to pay attention to not insert the wrong direction. Otherwise, the device may burn directly. . . DS18B20 is currently about 20 yuan. . . 12bit is designed for general temperature, enough.
The following is what I wrote early, the program is not very good. Because I use a float type directly; only for reference learning. . . The float type is very in-memory, which is very valuable for the MMS.
#include
/ *
Author: Li Jinhao
QQ: 67260745
E-mail: ltsoft918@163.com
---------------------------
DS18B20 The default is set to 12 digits. Actual temperature can be obtained by Taking 0.0625
Development Environment: 6MHz, SST89C58 in Keil C 7.01 Mon, Platform: Hengjian HJ51 / AVR Development System
Disconnect the VCC pin of the DS18B20, the temperature is 85 ° C can be used to test whether it is normal
* /
#define DSDIP P3_2 // P3 ^ 2 as a 1-wire acquisition port
Unsigned CHAR C1, C10, C100, CDEC, Mark; // Define global variables to return, 10, hundred, symbolic
// 0 ~ 9, if the decimal point is small, the high level is 8, that is, 8 * 16
The data in the // LED is a common anode display code unsigned char ledchar [] = {0xc0, 0xF9, 0xA4, 0X82, 0X99, 0X92, 0X82, 0xF8, 0X80, 0X90, 0X40, 0X79, 0X24, 0X30, 0X19, 0X12, 0x02, 0x78, 0x00, 0x10}; // LED co-cathode display / * const char LEDchar [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0xBF, 0x86, 0xDB, 0xcf, 0xe6, 0xed, 0xFD, 0x87, 0xFF, 0xEF}; * //////// 1 ~ 8 LED data tube address number code unsigned char led [] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; void display (unsigned char showbit, lednum); Void Shownum (unsigned char number, Num1, Num10, Num100, Mark); // ----------------- -------------------------- // 6MHz / * delay (1); // 60us delay (2); // 106US DELAY (11); // 526us void delay8us (void); void delay34us (void); // void delay14us (void); * /// ------------------- ----------------------------------------------------- ----------------------------- void delay (unsigned char Times) {// does not accurately delay, large error unsigned char i For (; Times> 0; Times -) for (i = 0; i <255; i -);} // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------- ----------------------------- void delay34us (void) {UNSIGNED Char i; for (i = 0; i <200; i -);
/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The DS18B20 function is partially passed through C1, C10, C100, CDEC, Mark; global variables return, ten, hundred, symbolic unsigned char restage; // Reset unsigned char reads (void); // Read 1 word Void Write (unsigned char val) // Write 1 byte float readtemp (void) // read temperature ------------------------ ------------------------ * / / * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Imaginary reset ------------------------- * / unsigned char restage (void) {unsigned char ingXist; dsdip = 0; DELAY (11); // 526US DSDIP = 1; // Wait 15 ~ 60us delay34us (); ISEXIST = DSDIP; // Get DS18B20 if there is DELAY (// 106us return (ISEXIST);} / * --- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------- * / unsigned char readds (void) {unsigned char i, t, value = 0; for (i = 8; i> 0; I -) {value> > = 1; DSDIP = 0; T ; // 2us DSDIP = 1; T ; // 2us T ; // 2us if (dsdip) value | = 0x80; T ; // 2us; // Read time gap, 1us} return (value);
/ * ---------------------------- Write a byte ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------- * / void writeds {unsigned char i; for (i = 8; i> 0; I -) {dsdip = 0; dsdip = Val & 0x01; // Write 1 bit each time, remove DELAY34US () (// delay 34us (15 ~ 60us sampling time) DSDIP = 1; // Release bus VAL >> = 1; // Calculate It will produce a 2US time gap} // delay8us (); // 2 time a time gap is greater than 1us} / * ---------------------------------------------------------------------------------------------------------------------------------------- ----------- Read DS18B20 ID --------------------------------- * / Unsigned char readid (void) {unsigned char id; rest 4; // Reset DS18B20 WRITEDs (0x33); // Read ROM: 0x33 instruction id = reads (); return (ID);} / * ----- ---------------------------------------------- DS18B20 read Temperature passes C1, C10, C100, CDEC, Mark; global variables return, ten, hundred, symbol bit functions itself returns a FLOAT type DTEMP temperature value --------------- ----------------------------------- * / float readtemp (void) {unsigned int temp; unsigned char lowbit , highbit DTEMP = 0; RESTDS (); // Reset DS18B20 WRITEDs (0xcc); // Skip ROM WRITEDs (0x44); // Send Temperature Conversion Command // ------ RESTDS (); Write (0xcc); // Skip ROM WRITEDs (0xbe); // Read the temporary storage memory Lowbit = reads (); // get low 8-bit highbit = read DS (); // Get high 8-bit TEMP = highbit << 8; Temp = Temp Lowbit; // Synthetic 16 binary number / * Judgment 5 bits, whether: 1: Negota 0: Positive * / Mark = 1; // 1: Representation is positive temperature 0: negative temperature (Mark is global variable) IF ((highbit >> 3)! = 0) {// is negative for anti- 1 TEMP = ~ TEMP 1; Mark = 0;} // --------- DTEMP = Temp * 0.0625; // Get the actual temperature TEMP = DTEMP; // Temp is int type, DTEMP is Float type, forced to remove the decimal / * The global variable returns, ten, hundred, symbolic bit * / c100 = TEMP / 100; // Get 100 c10 = (TEMP-C100 * 100) / 10; // Get ten c1 = TEMP-C100 * 100 -C10 * 10; // Get a bit CDEC = (DTEMP-TEMP) * 10; // Get a decimal return (DTEMP);} // ----------------- --Main () ----------------------- void main (void) {unsigned char dsid; while (1) {dsid = readid (); readTemp (); // Get the DS18B20 temperature Shownum (Mark, C100, C10, C1, CDEC);