Philips p89lpc917 MCU takes temperature notes on the temperature chip DS18B20

Source: Internet
Author: User

Hardware environment: p89lpc917, DS18B20 temperature chip, VDD = 3.3 V, debugging serial port;
Software environment: Keil uvision2, serial communication controller (used to receive serial data)

Process:
After reading the data, it has many examples of operating the DS18B20 on the C51 microcontroller, but its crystal oscillator frequency is based on the standard 51 chip, 11.059 MHz. the p89lpc917 crystal oscillator is 7372800hz. because a single bus is very rigorous in timing, and it is very short-lived, Its latency is delayed by loop code. The key is how to implement the latency program under the 7372800hz crystal oscillator?

I learned about the clock cycle, machine cycle, and instruction cycle. I plan to calculate a precise latency program. However, I wrote it in C language, which is really hard to calculate. finally, I plan to use an oscilloscope to measure the latency. after some tossing, I occasionally come to the following conclusion:

Latency code:
// Delay-with an 7.3728 MHz Crystal
// Calling the routine takes about 4μs, and then
// Each count takes another 3.2 μs
Void delay (uint16 us)
{
Volatile uint16 S;
For (S = 0; S <us; s ++ );
}
Conclusion:
The time used to call a cyclic function is 4us.
The time for adding a loop is not increased.
Time required for a value assignment code: 280ns

Then, refer to the Data Manual and strictly follow the read/write sequence to get the correct temperature value. (Note that the temperature increases by 0.5 degrees, after the obtained value is converted to decimal, dividing by 2 is the true temperature value)

The code is now published below:
/*
**************************************** **************************************** *************************
* ###### Company Limited
* Technical R & D department
*
* (C) Copyright 2005-2006, kmajian
* All Rights Reserved
*
* Obtain the temperature program from the DS18B20 through a single bus protocol
*
* File: gettmp. h
* By: kmajian
* Date: 2006-5-15
**************************************** **************************************** *************************
*/
# Include "config. H"

Sbit DQ = P0 ^ 1;

Void gettempinit (void)
{
P0m1 = 0x00; // set p0.1 to a quasi-bidirectional Port
P0m2 = 0x00;
}

// Delay-with an 7.3728 MHz Crystal
// Calling the routine takes about 4μs, and then
// Each count takes another 3.2 μs
Void delay (uint16 us)
{
Volatile uint16 S;
For (S = 0; S <us; s ++ );
}
// Reset
Uint8 ow_reset (void)
{
Unsigned char presence;
DQ = 0; // pull DQ line low
Delay (141); // leave it low for 480 μs
DQ = 1; // allow line to return high
Delay (14); // wait for presence
Presence = DQ; // get presence Signal
Delay (110); // wait for end of timeslot
Return (presence); // presence signal returned
} // Presence = 0, no part = 1

// Read bit
Uint8 read_bit (void)
{
DQ = 0; // pull DQ low to start timeslot
DQ = 0;
DQ = 0;
DQ = 0;
DQ = 1; // then return high
Delay (3); // delay 15 μs from start of timeslot
Return (DQ); // return value of DQ line
}

// Write bit
Void write_bit (int8 bitval)
{
DQ = 0; // pull DQ low to start timeslot
DQ = 0; // At least 1us
DQ = 0;
DQ = 0;
If (bitval = 1)
DQ = 1; // return DQ high if write 1
Delay (18); // hold value for remainder of timeslot, 60us
DQ = 1;
}

// Read byte
Uint8 read_byte (void)
{
Uint8 I;
Uint8 value = 0;
For (I = 0; I <8; I ++)
{
If (read_bit ())
Value | = 0x01 <I; // reads byte in, one byte at a time and then
// Shifts it left
Delay (18); // wait for rest of timeslot
}
Return (value );
}

// Write bytes
Void write_byte (int8 Val)
{
Uint8 I;
Uint8 temp;
For (I = 0; I <8; I ++) // writes byte, one bit at a time
{
Temp = Val> I; // shifts Val right 'I' Spaces
Temp & = 0x01; // copy that bit to temp
Write_bit (temp); // write bit in temp
}
Delay (18 );
}

// Read Temperature
Uint8 gettmp (uint8 * sym)
{
Uint8 val1 = 0, val2 = 0;

Ow_reset (); // Reset
Write_byte (0xcc); // skip the ROM command
Write_byte (0x44); // convert t command
Tdelay (100); // The delay is one second. The conversion takes enough time.
Ow_reset (); // Reset
Write_byte (0xcc); // skip the ROM command
Write_byte (0xbe); // sends the READ command
Val1 = read_byte (); // read low bytes
Val2 = read_byte (); // read high bytes
If (val2> 0) // The temperature is negative.
{
* Sym = 1; // indicates a negative number.
Val1 = ~ Val1 + 1; // obtain its complement
}
Else
{
* Sym = 0;
}

Return val1;
}

/*
**************************************** **************************************** **************************
* End
**************************************** **************************************** *************************
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.