# Define gpgcon (* (volatile unsigned *) 0x56000060) // 18b20 register settings
# Define gpgdat (* (volatile unsigned *) 0x56000064)
# Define gpgup (* (volatile unsigned *) 0x56000068)
# Define ufcon0 (* (volatile unsigned *) 0x50000008) // serial register settings
# Define umcon0 (* (volatile unsigned *) 0x5001_c)
# Define ulcon0 (* (volatile unsigned *) 0x50000000)
# Define ucon0 (x (volatile unsigned *) 0x50000004)
# Define ubrdiv0 (* (volatile unsigned X) 0x50000028)
# Define utrstat0 (* (volatile unsigned *) 0x50000010)
# Define utxh0 (* (volatile unsigned *) 0x50000020)
# Define urxh0 (* (volatile unsigned *) 0x50000024)
Unsigned char WD [4];
Unsigned int sdata; // The integer portion of the temperature measured
Unsigned char xiaoshu1; // The first decimal place
Unsigned char xiaoshu2; // second decimal place
Unsigned char Xiaoshu; // two decimal places
Void ZH (void );
Void delay (unsigned int X );
Void UART (void );
Void DS18B20 Pro (void );
Void dmsec (unsigned int t );
Void tmreset (void );
Unsigned char tmrbit (void );
Unsigned char tmrbyte (void );
Void tmwbyte (unsigned char dat );
Void tmstart (void );
Void tmrtemp (void );
Int main (void) // continuously checks the temperature value and converts it to ASC | code, which is passed through the serial port
{
// Gpgup & = 0xffffff1f;
While (1)
{
DS18B20 ();
// Sdata = 39.20;
En ();
UART ();
Delay (30 );
}
Return (0 );
}
Void dmsec (unsigned int t) // precise latency Function
{
Unsigned int I;
Unsigned Int J;
J = 1 * t;
For (I = 0; I <j; I ++ );
}
Void tmreset (void) // 18b20 Initialization
{
Unsigned int I;
Gpgcon & = 0 xfffffffc; // set the Register to write to 18b20
Gpgcon | = 0x01;
Gpgdat | = 0x01;
Dmsec (100 );
Gpgdat & = 0 xfffe;
Dmsec (600 );
Gpgdat | = 0x01;
Dmsec (100 );
Gpgcon & = 0 xfffffffc; // set the Register to read 18b20
I = gpgdat;
}
Unsigned char tmrbyte (void) // read a byte Function
{
Unsigned Int J;
Unsigned char I, u = 0;
For (I = 1; I <= 8; I ++)
{
Gpgcon & = 0 xfffffffc;
Gpgcon | = 0x01; // set gpg0 to output
Gpgdat & = 0 xfffe;
// Delay( 120 );
U> = 1;
// Gpgdat | = 0x01;
Gpgcon & = 0 xfffffffc; // set as the input port
J = gpgdat;
If (J & 0x01) u | = 0x80;
Dmsec (46 );
Gpgdat | = 0x01;
}
Return (U );
}
Void tmwbyte (unsigned char dat) // write a byte Function
{
Unsigned char J;
Gpgcon & = 0 xfffffffc;
Gpgcon | = 0x01;
For (j = 1; j <= 8; j ++)
{
Gpgdat & = 0 xfffe;
Dmsec (1 );
Gpgdat | = (DAT & 0x01 );
Dmsec (47 );
Gpgdat | = 0x01;
Dat = dat> 1;
}
}
Void tmstart (void) // send DS18B20 to start Conversion
{
Tmreset (); // Reset
Dmsec (120); // latency
Tmwbyte (0xcc); // skip the serial number command
Tmwbyte (0x44); // sends the conversion command 44 h,
}
Void tmrtemp (void) // read Temperature
{
Unsigned char A, B;
Tmreset (); // Reset
Dmsec (2000); // latency
Tmwbyte (0xcc); // skip the serial number command
Tmwbyte (0xbe); // sends the READ command
A = tmrbyte (); // read Low Temperature
B = tmrbyte (); // read high temperature
Sdata = A/16 + B * 16; // integer
Xiaoshu1 = (A & 0x0f) * 10/16; // The first decimal place
Xiaoshu2 = (A & 0x0f) * 100/16% 10; // second decimal place
Xiaoshu = xiaoshu1 * 10 + xiaoshu2; // two decimal places
}
Void ZH () // convert the temperature value to an ASCII Value
{
Unsigned int wdata;
Wdata = sdata;
WD [0] = wdata/10 + 0x30;
// If (WD [0]> 0x33)
// While (1 );
WD [1] = wdata % 10 + 0x30;
WD [2] = Xiaoshu/10 + 0x30;
WD [3] = Xiaoshu % 10 + 0x30;
}
Void DS18B20 Pro (void)
{
Tmstart (); // UART (10 );
Dmsec (5); // It can be read continuously without delay //
Tmrtemp (); // read temperature. The temperature will be stored in TMP after execution //
}
Void UART () // serial port function used to send temperature values
{
Ufcon0 = 0x0;
Umcon0 = 0x0;
Ulcon0 = 0x3;
Ucon0 = 0x245;
Ubrdiv0 = (INT) (52000000/16/115200 + 0.5)-1); // 115200
While (! (Utrstat0 & 0x2 ));
Utxh0 = '/N ';
While (! (Utrstat0 & 0x2 ));
Utxh0 = WD [0];
While (! (Utrstat0 & 0x2 ));
Utxh0 = WD [1];
While (! (Utrstat0 & 0x2 ));
Utxh0 = 46;
While (! (Utrstat0 & 0x2 ));
Utxh0 = WD [2];
While (! (Utrstat0 & 0x2 ));
Utxh0 = WD [3];
While (! (Utrstat0 & 0x2 ));
Utxh0 = 39;
While (! (Utrstat0 & 0x2 ));
Utxh0 = 67;
}
Void delay (unsigned int X)
{
Unsigned int I, J, K;
For (I = 0; I <= x; I ++)
For (j = 0; j <0xff; j ++)
For (k = 0; k <0xff; k ++ );
}