Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Read temp with DS18S20 temperature sensor

Status
Not open for further replies.

Hasher

Junior Member level 1
Joined
Apr 14, 2009
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,458
ds18s20

I'm using PIC18F2420 with DS18S20 temperature sensor.
PIC ==> 10-bit resolution A/D module
TempSensor ==> 9-bit resolution
TempSensor ==> range: -55 C° to 125 C°

I want to read the Temp. Sensor volt as a temperature value.

I did this:

unsigned int volt;
unsigned int temp;

Code:
void main()
{
    while(1)
   { 
        volt = ADC_Read(2);
        temp = 5*volt*/1024;
    }
}

Should I use Op-Amp to increase the o/p analog volt?

Or what is in code is enough?
 

ds18s20 pic

Excuse me, isn't DS18S20 a digital temperature sensor!

Why would you need a ADC when the sensor's output is already binary. Just convert it into decimal for readout.
 

ds1820 temperature sensor

Exactly DS18S20 is a digital sensor which communicates with a 1-wire protocol.
Read about it, implement it (see the www.maxim-ic.com for more info - exactly: **broken link removed**) and it'll work excellent.
 

ds1820 pic

Hi,

Here's an example :

/*
* Project name:
Onewire_Test (Interfacing the DS18x20 temperature sensor - all versions)
* Copyright:
(c) MikroElektronika, 2005-2008.
* Description:
After reset, PIC reads temperature from the sensor and prints it on the Lcd.
The display format of the temperature is 'xxx.xxxx°C'. To obtain correct
results, the 18x20's temperature resolution has to be adjusted (constant
TEMP_RESOLUTION)
* Test configuration:
MCU: PIC16F877A
Dev.Board: EasyPIC4
Oscillator: HS, 08.0000 MHz
Ext. Modules: DS18x20 connected to RE2 pin, LCD_2x16
SW: mikroC v8.0.0.0
* NOTES:
- Pull up and turning off diode on pin used for one wire bus may be required (in this example PORTE)
*/


// Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
// 18S20: 9 (default setting; can be 9,10,11,or 12)
// 18B20: 12
const unsigned short TEMP_RESOLUTION = 9;

char *text = "000.0000";
unsigned temp;

void Display_Temperature(unsigned int temp2write)
{
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp_fraction;

// check if temperature is negative
if (temp2write & 0x8000)
{
text[0] = '-';
temp2write = ~temp2write + 1;
}

// extract temp_whole
temp_whole = temp2write >> RES_SHIFT ;

// convert temp_whole to characters
if (temp_whole/100)
text[0] = temp_whole/100 + 48;

text[1] = (temp_whole/10)%10 + 48; // extract tens digit
text[2] = temp_whole%10 + 48; // extract ones digit

// extract temp_fraction and convert it to unsigned int
temp_fraction = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;

// convert temp_fraction to characters
text[4] = temp_fraction/1000 + 48; // extract thousands digit
text[5] = (temp_fraction/100)%10 + 48; // extract hundreds digit
text[6] = (temp_fraction/10)%10 + 48; // extract tens digit
text[7] = temp_fraction%10 + 48; // extract ones digit

// print temperature on LCD
Lcd_Out(2, 5, text);
}//~

void main()
{
ADCON1 = 7; // Configure AN pins as digital I/O
Lcd_Init(&PORTD); // Lcd_Init_EP4, see Autocomplete
Lcd_Cmd(LCD_CURSOR_OFF);
Lcd_Out(1, 1, " Temperature: ");
// Print degree character, 'C' for Centigrades
Lcd_Chr(2,13,223);
Lcd_Chr(2,14,'C');

//--- main loop
while (1)
{
//--- perform temperature reading
Ow_Reset(&PORTE,2); // Onewire reset signal
Ow_Write(&PORTE,2,0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTE,2,0x44); // Issue command CONVERT_T
Delay_ms(750);

Ow_Reset(&PORTE,2);
Ow_Write(&PORTE,2,0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTE,2,0xBE); // Issue command READ_SCRATCHPAD

temp = Ow_Read(&PORTE,2);
temp = (Ow_Read(&PORTE,2) << 8) + temp;

//--- Format and display result on Lcd
Display_Temperature(temp);

}
}
 

ds1820+pic

Ooooooooh!

Thanks budy!

But I cannot use the mikroC I have to use the Hi-Tech C compiler & it has not the 1 wire library?

????
 

pic ds18s20

It's not that dificult to write 1 wire lib in C...

check this out :

**broken link removed**
 
  • Like
Reactions: volhov

    volhov

    Points: 2
    Helpful Answer Positive Rating
read temp

if you want adc type use LM35 which is also cheaper tan DS1820
 

Re: ds1820 pic

Hi,

Here's an example :

/*
* Project name:
Onewire_Test (Interfacing the DS18x20 temperature sensor - all versions)
* Copyright:
(c) MikroElektronika, 2005-2008.
* Description:
After reset, PIC reads temperature from the sensor and prints it on the Lcd.
The display format of the temperature is 'xxx.xxxx°C'. To obtain correct
results, the 18x20's temperature resolution has to be adjusted (constant
TEMP_RESOLUTION)
* Test configuration:
MCU: PIC16F877A
Dev.Board: EasyPIC4
Oscillator: HS, 08.0000 MHz
Ext. Modules: DS18x20 connected to RE2 pin, LCD_2x16
SW: mikroC v8.0.0.0
* NOTES:
- Pull up and turning off diode on pin used for one wire bus may be required (in this example PORTE)
*/


// Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
// 18S20: 9 (default setting; can be 9,10,11,or 12)
// 18B20: 12
const unsigned short TEMP_RESOLUTION = 9;

char *text = "000.0000";
unsigned temp;

void Display_Temperature(unsigned int temp2write)
{
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp_fraction;

// check if temperature is negative
if (temp2write & 0x8000)
{
text[0] = '-';
temp2write = ~temp2write + 1;
}

// extract temp_whole
temp_whole = temp2write >> RES_SHIFT ;

// convert temp_whole to characters
if (temp_whole/100)
text[0] = temp_whole/100 + 48;

text[1] = (temp_whole/10)%10 + 48; // extract tens digit
text[2] = temp_whole%10 + 48; // extract ones digit

// extract temp_fraction and convert it to unsigned int
temp_fraction = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;

// convert temp_fraction to characters
text[4] = temp_fraction/1000 + 48; // extract thousands digit
text[5] = (temp_fraction/100)%10 + 48; // extract hundreds digit
text[6] = (temp_fraction/10)%10 + 48; // extract tens digit
text[7] = temp_fraction%10 + 48; // extract ones digit

// print temperature on LCD
Lcd_Out(2, 5, text);
}//~

void main()
{
ADCON1 = 7; // Configure AN pins as digital I/O
Lcd_Init(&PORTD); // Lcd_Init_EP4, see Autocomplete
Lcd_Cmd(LCD_CURSOR_OFF);
Lcd_Out(1, 1, " Temperature: ");
// Print degree character, 'C' for Centigrades
Lcd_Chr(2,13,223);
Lcd_Chr(2,14,'C');

//--- main loop
while (1)
{
//--- perform temperature reading
Ow_Reset(&PORTE,2); // Onewire reset signal
Ow_Write(&PORTE,2,0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTE,2,0x44); // Issue command CONVERT_T
Delay_ms(750);

Ow_Reset(&PORTE,2);
Ow_Write(&PORTE,2,0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTE,2,0xBE); // Issue command READ_SCRATCHPAD

temp = Ow_Read(&PORTE,2);
temp = (Ow_Read(&PORTE,2) << 8) + temp;

//--- Format and display result on Lcd
Display_Temperature(temp);

}
}

I'm aware of the fact that I'm quite 'late' for this thread but still hope to get any response. :)

I have been stuck by some 18B20 problems these days and have interest in your implementation.

Could you be so kind to post the Ow_xxxx() routines being invoked by the code above?

Thanks in advance.

Max

---------- Post added at 02:38 ---------- Previous post was at 02:37 ----------

if you want adc type use LM35 which is also cheaper tan DS1820

Thanks for your information.
 

Re: ds1820 pic

I'm aware of the fact that I'm quite 'late' for this thread but still hope to get any response. :)

I have been stuck by some 18B20 problems these days and have interest in your implementation.

Could you be so kind to post the Ow_xxxx() routines being invoked by the code above?

The Ow_xxxx() routines you are referring to are the MikroC Pro Library OneWire Library Routines, unfortunately MikroE does not make the source available for any of their library routines.

Reference: MikroC Pro for PIC Users Manual, pg 384, ONEWIRE LIBRARY:

The OneWire library provides routines for communication via the Dallas OneWire
protocol, for example with DS18x20 digital thermometer. OneWire is a Master/Slave
protocol, and all communication cabling required is a single wire. OneWire enabled
devices should have open collector drivers (with single pull-up resistor) on the
shared data line.
...
...
Library Routines
- Ow_Reset
- Ow_Read
- Ow_Write

The DS1820 requires OneWire routines, while the LM35 requires ADC routines.

Rather than restart this discussion thread, I would suggest you open a new thread with all the relevant information pertaining to your project.

BigDog
 

Re: ds1820 pic

Thanks for your information
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top