DS18B20 pic
DS18B20.
DS18B20 is 1-Wire interface digital thermometer that require one port pin (and ground) for communication, has a unique 64-bit serial code stored in an onboard ROM, can measure temperatures from -55C to +125 C (-67F to +257F),and user-selectable resolution from 9 to 12 bits.
Pin assignment, pin description and block diagram of DS18B20 shown below. The DS18B20 can be powered by an external supply on the Vdd pin, or powered by the DQ pin (parasite power mode). Stealing power from DQ pin saves a wire but comunication with it is more complicate than using external power supply. For more detail about powering the DS18B20 can be found in the powering the DS18B20 section of its datasheet.
DS18B20 Memory.
Scratchpad is 9 bytes of SRAM that organized as figer shown below. The first two bytes (byte 0 and byte 1) are read-only memory that contain the LSB and the MSB of the temperature register. Bytes 2 and 3 provide access to TH and TL registers. Byte 4 is a configuration register. Bytes 5,6 and 7 are reserved. Byte 8 is read-only and contains CRC code (cyclic redundancy check) for byte 0 through byte 7 of the scratchpad.
The output temperature data from DS18B20 is calibrated in degree centigrade and the default resolurion at power up is 12-bit. The temperature register format shown below, where as sign bits (S) indicate if the temperature is positive (S=0) or negative (S=1).
To access the DS18B20's data, 3 steps sequence as follows is need.
1. Initalization. All transections on the 1-wire bus begin with an initalization sequence. It consists of a reset pulse transmitted by the bus master followed by presence pulse transmitted by the slave. Timing for the reset and presence pulse is shown below.
2. Issue a ROM command after the bus master has detected a presence pulse. ROM commands are : Search ROM[F0h], Read ROM[33h], Match ROM[55h], Skip ROM[CCh] and Alarm Search[ECh].
3. Issue a DS18B20 function command after a ROM command. A ROM command is to select which DS18B20 that the master want to communicate with. A function command allows the master to read and to write from the DS18B20's scratchpad, etc. DS18B20 function commands are: Convert T[44h], Write Scratchpad[4Eh], Read Scratchpad[BEh], Copy Scratchpad[48h], Recall E2[B8h], and Read Power Supply[B4h].
It is very important to follow this sequence every time the DS18B20 is accessed. Exceptions to this rule are Search ROM[F0] and Alarm Search[EC] commands. The master must return to step 1 after issue either of those ROM commands.
Read/Write signaling.
All data and commands are transmitted least significant bit first over the 1-Wire bus. The figure shown below is Read/Write timing diagram. All Read/Write time slots must be 60 usec in duration with a minimum of a 1 usec recovery time between individual read/write slots. To write "1" the bus master pull low and release within 15 usec. To write "0", after pulling the bus low, the bus master must continue stay low for 60 usec minimum then release the bus. To read logical from DS18B20, the bus master must pull low for at least 1 usec then release the bus then master must sample the signal within 15 usec from the start of the slot.
1-wire digital thermometer .
The schematic is shown below. Input has a DS18B20 and a 4.7K pull-up resister. For output, I have my own I2C 7 segment display moduel. You can change to a serial LCD display or something else.
The code.
Although Micko C has built-in 1-wire library but my goal for this project is to get and display temperature from DS18B20 with my own function routine.
My code is to display only positive temperature. You need extra work for negative temperature.
/*
* Project name:
DS18B20 1-wire temperature
* Copyright:
Nicholas Sirirak
* Description:
* Test configuration:
MCU: PIC16F886
Dev.Board: -
Oscillator: HS, 4.0000 MHz
Ext. Modules: -
SW: mikroC v8.2.0.0
* NOTES:
HW connection
MCU DS18B20
RB0/INT <--------> QD W/ 4.7K Ohm pull up
MCU I2C display module
SCL(RC3) <--------> SCL W/ 4.7K Ohm pull up
SDA(RC4) <--------> SDA W/ 4.7K Ohm pull up
*/
#define Skip_ROM 0xCC
#define Convert_T 0x44
#define Read_scratchpad 0xBE
#define Port_18B20 PORTB.F0
#define Tx_18B20 TRISB.F0 = 0
#define Rx_18B20 TRISB.F0 = 1
unsigned temp;
unsigned short tempL, tempH, fraction;
void delay480() {
delay_us(480);
}
char Reset_18B20() {
Tx_18B20; // Tris = 0 (output)
Port_18B20 = 0; // set pin# to low (0)
delay480(); // 1 wire require time delay
Rx_18B20; // Tris = 1 (input)
delay_us(60); // 1 wire require time delay
if (Port_18B20 == 0) { // if there is a presence pluse
delay480();
return 0; // return 0 ( 1-wire is presence)
} else {
delay480();
return 1; // return 1 ( 1-wire is NOT presence)
}
}
void Write_18B20 (char Cmd){
char i;
Rx_18B20; // set pin# to input (1)
for(i = 0; i < 8; i++){
if((Cmd & (1<<i))!= 0) {
// write 1
Tx_18B20; // set pin# to output (0)
Port_18B20 = 0; // set pin# to low (0)
delay_us(1); // 1 wire require time delay
Rx_18B20; // set pin# to input (release the bus)
delay_us(60); // 1 wire require time delay
} else {
//write 0
Tx_18B20; // set pin# to output (0)
Port_18B20 = 0; // set pin# to low (0)
delay_us(60); // 1 wire require time delay
Rx_18B20; // set pin# to input (release the bus)
}
}
}
char Read_18B20 (){
char i,result = 0;
Rx_18B20; // TRIS is input(1)
for(i = 0; i < 8; i++){
Tx_18B20; // TRIS is output(0)
Port_18B20 = 0; // genarate low pluse for 2us
delay_us(2);
Rx_18B20; // TRIS is input(1) release the bus
if(Port_18B20 != 0) result |= 1<<i;
delay_us(60); // wait for recovery time
}
return result;
}
void main(){
TRISB = 0;
ANSELH = 0;
I2C_Init(100000); //initial I2C
delay_ms(1000);
while(1){
if(!Reset_18B20()){
Write_18B20(Skip_ROM);
Write_18B20(Convert_T);
delay_ms(750);
Reset_18B20();
Write_18B20(Skip_ROM);
Write_18B20(Read_scratchpad);
tempL = Read_18B20();
tempH = Read_18B20();
if(tempL.F3) fraction = 1;
else fraction = 0;
tempL >>= 4;
tempH <<= 4;
tempH += tempL;
tempL = Dec2Bcd(tempH);
I2C_Start(); //issue start signal
I2C_Wr(0x68); //send slave address and write signal
I2C_Wr(0x40);
I2C_Wr(tempL);
if(fraction) I2C_Wr(0x5C);
else I2C_Wr(0x0C);
I2C_Stop();
}
}
}
Note.
--
With Regards,
s.m.sethupathy,
sms communication,
Tanjore -1.
mobile :9944 186 173
www.questionpaperlink.co.cc
www.sethu-panguvarthagam.blogspot.com
Currently have 0 comments: