The microcontroller source program is as follows (with detailed comments): #include
#include
#include<18b20.h> #define uchar unsigned char #define uint unsigned int bit flag1s = 0; //1s timing flag unsigned char T0RH = 0; //high byte of T0 reload value unsigned char T0RL = 0; //low byte of T0 reload value void ConfigTimer0(unsigned int ms); unsigned char IntToString(unsigned char *str, int dat); extern bit Start18B20(); extern bit Get18B20Temp(int *temp); extern void InitLcd1602(); extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str); void main() { bit res; int temp; //read current temperature value int intT, decT; //integer and decimal part of temperature value unsigned char len; unsigned char str[12]; EA = 1; //Open the total interrupt ConfigTimer0(10); //T0 timing 10ms Start18B20(); //Start DS18B20 InitLcd1602(); //Initialize LCD while (1) { if (flag1s) //Update the temperature once a second { flag1s = 0; res = Get18B20Temp(&temp); //Read the current temperature if (res) //When the reading is successful, refresh the current temperature display { intT = temp >> 4; //Separate the integer part of the temperature value decT = temp & 0xF; //Separate the decimal part of the temperature value len = IntToString(str, intT); //Convert the integer part to a string str[len++] = '.'; //Add a decimal point decT = (decT*10) / 16; //Convert the decimal part of the binary to 1 decimal place str[len++] = decT + '0'; //Convert the decimal place to ASCII character while (len < 6) //Pad with spaces to 6 characters in length{ str[len++] = ' '; } str[len] = '\0'; //Add string terminator LcdShowStr(0, 0, str); //Display on the LCD screen } else //When reading fails, prompt an error message{ LcdShowStr(0, 0, "error!"); } Start18B20(); //Restart the next conversion } } } /* Convert integer to string, str-string pointer, dat-number to be converted, return value-string length*/ unsigned char IntToString(unsigned char *str, int dat) { signed char i = 0; unsigned char len = 0; unsigned char buf[6]; if (dat < 0) //If it is a negative number, first take the absolute value and add a minus sign to the pointer{ dat = -dat; *str++ = '-'; len++; } do { //First convert to a decimal array with the low order first buf[i++] = dat % 10; dat /= 10; } while (dat > 0); len += i; //The final value of i is the number of valid characterswhile (i-- > 0) //Convert the array value to ASCII code and copy it to the receiving pointer in reverse { *str++ = buf[i] + '0'; } *str = '\0'; //Add the string terminator return len; //Return the string length } /* Configure and start T0, ms-T0 timing time*/ void ConfigTimer0(unsigned int ms) { unsigned long tmp; //Temporary variable tmp = 11059200 / 12; //Timer counting frequency tmp = (tmp * ms) / 1000; //Calculate the required count value tmp = 65536 - tmp; //Calculate the timer reload value tmp = tmp + 2; //Compensate for the error caused by interrupt response delay T0RH = (unsigned char)(tmp>>8); //Split the timer reload value into high and low bytes T0RL = (unsigned char)tmp; TMOD &= 0xF0; //Clear T0 control bit TMOD |= 0x01; //Configure T0 to mode 1 TH0 = T0RH; //Load T0 reload value TL0 = T0RL; ET0 = 1; //Enable T0 interrupt TR0 = 1; //Start T0 }