|
|
View previous topic :: View next topic |
Author |
Message |
JBM Guest
|
One DS18B20 in powered mode |
Posted: Mon May 10, 2004 3:41 pm |
|
|
I've been looking through the forum, and haven't been able to find a set of DS18B20 functions for one sensor in powered mode, i.e. where 1 pin of the PIC connects to the DQ on the sensor, that line is pulled high by a 4k7 resistor and the Vdd and Gnd are connected to +V and 0V.
If anyone has a simple set of functions that work for this scenario, any feedback (other than pointing me to another forum post) would be appreciated.
Thanks,
JBM |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Mon May 10, 2004 5:58 pm |
|
|
It's simpler for one device because the search to find the addresses of all devices on the bus doesn't need to be done.
For the following example, there must be only one device on the bus.
A 4K7 ohm pullup resistor is required on the bus.
Edited:
Code: |
#include <16F88.h>
#fuses XT,NOWDT,NOLVP,PUT,NOPROTECT,NOBROWNOUT,NOWRT,CCPB3
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_B5,rcv=PIN_B2,ERRORS)
#define DQ PIN_B0 // One Wire Bus pin assignment
#include "onewire.c"
void main(void)
{
int8 i;
signed int16 temperature;
int8 scratch[9];
output_float(DQ); // Set as input. 4k7 pullup on bus.
while(1)
{
ow_reset();
write_byte(0xCC); // Skip Rom command
write_byte(0x44); // Temperature Convert command
output_float(DQ);
delay_ms(750); // Max. time for conversion is 750mS
ow_reset();
write_byte(0xCC); // Skip Rom command
write_byte(0xBE); // Read scratch pad command
dowcrc = 0;
// Get the data bytes
for (i=0;i<=7;i++)
{
scratch[i] = read_byte();
ow_crc(scratch[i]);
}
scratch[8] = read_byte(); // Get crc byte
ow_reset();
// If the received crc is same as calculated then data is valid.
// Scale and round to nearest degree C.
// Scaling is 0.0625 (1/16) deg.C/bit with default 12 bit resolution.
// Round by adding half denominator for positive temperatures and
// subtracting half denominator for negative temperatures.
if (scratch[8] == dowcrc)
{
temperature = (signed int16) make16(scratch[1],scratch[0]);
if (temperature >= 0)
temperature = (temperature + 8)/16;
else
temperature = (temperature - 8)/16;
printf("%4LdC \n\r",temperature);
}
else
printf("Error in data\n\r");
}
}
|
Here are the one wire functions. A lot of it is not needed for one device on the bus. There is an example of using it for multiple devices on the bus at
http://www.ccsinfo.com/forum/viewtopic.php?t=29337
Edited:
onewire.c
Code: |
// One Wire bus functions - from Dallas publication AN162
// "Interfacing DS18x20/DS1822 1-wire Temperature Sensor in a Microcontroller
// Environment". Delays calculated from values given for 8051.
// Changed variable name ROM[] to RomBytes[] because ROM is a reserved word
// in version 4 of the CCS compiler.
// Global variables
int8 RomBytes[8];
int8 lastDiscrep = 0;
short doneFlag = 0;
int8 FoundROM[9][8]; // Table of found ROM codes, 8 bytes for each
int8 numROMs;
int8 dowcrc; // crc is accumulated in this variable
// crc lookup table
int8 const dscrc_table[] = {
0,94,188,226,97,63,221,131,194,156,126,32,163,253,31,65,
157,195,33,127,252,162,64,30,95,1,227,189,62,96,130,220,
35,125,159,193,66,28,254,160,225,191,93,3,128,222,60,98,
190,224,2,92,223,129,99,61,124,34,192,158,29,67,161,255,
70,24,250,164,39,121,155,197,132,218,56,102,229,187,89,7,
219,133,103,57,186,228,6,88,25,71,165,251,120,38,196,154,
101,59,217,135,4,90,184,230,167,249,27,69,198,152,122,36,
248,166,68,26,153,199,37,123,58,100,134,216,91,5,231,185,
140,210,48,110,237,179,81,15,78,16,242,172,47,113,147,205,
17,79,173,243,112,46,204,146,211,141,111,49,178,236,14,80,
175,241,19,77,206,144,114,44,109,51,209,143,12,82,176,238,
50,108,142,208,83,13,239,177,240,174,76,18,145,207,45,115,
202,148,118,40,171,245,23,73,8,86,180,234,105,55,213,139,
87,9,235,181,54,104,138,212,149,203,41,119,244,170,72,22,
233,183,85,11,136,214,52,106,43,117,151,201,74,20,246,168,
116,42,200,150,21,75,169,247,182,232,10,84,215,137,107,53
};
// Returns 0 for one wire device presence, 1 for none
int8 ow_reset(void)
{
int8 presence;
output_low(DQ);
delay_us(488); // Min. 480uS
output_float(DQ);
delay_us(72); // Takes 15 to 60uS for devices to respond
presence = input(DQ);
delay_us(424); // Wait for end of timeslot
return(presence);
}
//******************************************************************************
// Read bit on one wire bus
int8 read_bit(void)
{
output_low(DQ);
delay_us(1); // Added, 1uS min. Code relied on 8051 being slow.
output_float(DQ);
delay_us(12); // Read within 15uS from start of time slot
return(input(DQ));
}
//******************************************************************************
void write_bit(int8 bitval)
{
output_low(DQ);
if(bitval == 1) {
delay_us(1); // 1uS min. Code relied on 8051 being slow.
output_float(DQ);
}
delay_us(105); // Wait for end of timeslot
output_float(DQ);
}
//******************************************************************************
int8 read_byte(void)
{
int8 i;
int8 val = 0;
for(i=0;i<8;i++)
{
if(read_bit()) val |= (0x01 << i);
delay_us(120); // To finish time slot
}
return val;
}
//******************************************************************************
void write_byte(int8 val)
{
int8 i;
int8 temp;
for (i=0;i<8;i++)
{
temp = val >> i;
temp &= 0x01;
write_bit(temp);
}
delay_us(105);
}
//******************************************************************************
// One wire crc
int8 ow_crc(int8 x)
{
dowcrc = dscrc_table[dowcrc^x];
return dowcrc;
}
//******************************************************************************
// Searches for the next device on the one wire bus. If there are no more
// devices on the bus then false is returned.
int8 Next(void)
{
int8 m = 1; // ROM Bit index
int8 n = 0; // ROM Byte index
int8 k = 1; // Bit mask
int8 x = 0;
int8 discrepMarker = 0;
int8 g; // Output bit
int8 nxt; // Return value
short flag;
nxt = FALSE; // Reset next flag to false
dowcrc = 0; // Reset the dowcrc
flag = ow_reset();
if (flag||doneFlag) // If no parts return false
{
lastDiscrep = 0; // Reset the search
return FALSE;
}
write_byte(0xF0); // Send SearchROM command
do
{
x = 0;
if (read_bit() == 1) x = 2;
delay_us(120);
if (read_bit() == 1) x |= 1; // And it's complement
if (x == 3) // There are no devices on the one wire bus
break;
else
{
if (x > 0) // All devices coupled have 0 or 1
g = x >> 1; // Bit write value for search
// If this discrepancy is before the last discrepancy on a previous
// Next then pick the same as last time.
else
{
if (m < lastDiscrep)
g = ((RomBytes[n] & k) > 0);
// If equal to last pick 1
else
g = (m == lastDiscrep); // If not then pick 0
// If 0 was picked then record position with mask k
if (g == 0) discrepMarker = m;
}
// Isolate bit in ROM[n] with mask k
if (g == 1) RomBytes[n] |= k;
else RomBytes[n] &= ~k;
write_bit(g); // ROM search write
m++; // Increment bit counter m
k = k << 1; // and shift the bit mask k
// If the mask is 0 then go to new ROM
if (k == 0)
{ // Byte n and reset mask
ow_crc(RomBytes[n]); // Accumulate the crc
n++;
k++;
}
}
} while (n < 8); // Loop through until through all ROM bytes 0-7
if (m < (65||dowcrc)) // If search was unsuccessful then
lastDiscrep = 0; // reset the last Discrepancy to zero
else // Search was successful, so set lastDiscrep, lastOne, nxt
{
lastDiscrep = discrepMarker;
doneFlag = (lastDiscrep == 0);
nxt = TRUE; // Indicates search not yet complete, more parts remain
}
return nxt;
}
//******************************************************************************
// Resets current state of a ROM search and calls Next to find the first device
// on the one wire bus.
int8 First(void)
{
lastDiscrep = 0;
doneFlag = FALSE;
return Next(); // Call Next and return it's return value;
}
//******************************************************************************
void FindDevices(void)
{
int8 m;
if(!ow_reset())
{
if(First()) // Begins when at least one part found
{
numROMs = 0;
do
{
numROMs++;
for (m=0;m<8;m++)
{
FoundROM[numROMs][m] = RomBytes[m];
}
printf("Device No.%u address ",numROMs);
printf("%X%X%X%X%X%X%X%X\n\r",
FoundROM[numROMs][7],FoundROM[numROMs][6],FoundROM[numROMs][5],
FoundROM[numROMs][4],FoundROM[numROMs][3],FoundROM[numROMs][2],
FoundROM[numROMs][1],FoundROM[numROMs][0]);
} while (Next() && (numROMs<10)); // Continues until no additional
// devices found.
}
}
putc('\n'); putc('\r');
}
//******************************************************************************
// Sends Match ROM command to bus then device address
int8 Send_MatchRom(void)
{
int8 i;
if (ow_reset()) return FALSE; // 0 if device present
write_byte(0x55); // Match ROM
for (i=0;i<8;i++)
{
write_byte(FoundRom[numROMs][i]); // Send ROM code
}
return TRUE;
}
|
Last edited by Kenny on Thu Apr 30, 2009 12:44 am; edited 4 times in total |
|
|
JBM Guest
|
|
Posted: Tue May 11, 2004 11:21 am |
|
|
Thanks for the code, Kenny. It compiles fine (with a few moifications) but I get the "xxxx" message. I'm beginning to think my hardware is fussed. I'll have a poke and pry at it and see if I can get if flying. |
|
|
JBM Guest
|
|
Posted: Tue May 11, 2004 1:49 pm |
|
|
As I had suspected, it was my hardware that was letting me down. I had rebuilt my code 3 times becuase of a fried sensor! I tried your code, and it worked. I then tried my 3 previous builds of code and they all worked. AAGH! I just wasted 3 weeks of coding!
It hasn't all been bad though - the rounding algorithm at teh end was very useful. Thanks for your time. |
|
|
SonicPLD Guest
|
|
Posted: Tue Jan 16, 2007 5:52 pm |
|
|
how many sensors can you access through this source?
also - how long coud be 1-wire through PIC ? |
|
|
SerialGmr8 Guest
|
Alarm retrieval |
Posted: Fri Nov 30, 2007 2:15 pm |
|
|
ive been beating the internet trying to get help with identifying a DSB1820 that has given an alarm.
all the ds18b20 pdf's ive read indicate that an alarm flag is raises inside the DS18B20.
My question is how do i get to that flag to initiate some sort of subroutine;
*if(temp_alarm)
{
some subroutine
}
Regards.
SerialGmr8 |
|
|
SerialGmr8
Joined: 30 Nov 2007 Posts: 13
|
|
Posted: Sat Dec 01, 2007 1:37 pm |
|
|
As per Kenny's post :
Quote: | Code: |
write_byte(0xEC); // Send Alarm Search command
do
{
x = 0;
if (read_bit() == 1) x = 2;
delay_us(120);
if (read_bit() == 1) x |= 1; // And it's complement
if (x == 3) // There is no alarm
else
{
...... // alarm found so do something
}
} // end do
|
|
I've tried implementing to solve my problem, but with no success. Does anyone have any ideas?
Im trying to identify a ds18b20 that has issued a temperature alarm. Any aid would be greatly appreciated. |
|
|
Luka Guest
|
|
Posted: Tue Dec 04, 2007 12:52 pm |
|
|
I'm working on ds1820 sensor aswell, but in parasite mode. I'm using at89c51rd2 ucontroller, but cannot get the sensor working. I'm trying the code above (ow_reset), but when i try to compile it with DevMic it outputs some errors like: function 'output_low' implicit declaration, function 'output_float implicit declaration , function 'delay_us' implicit declaration ,
function 'input' implicit declaration !
Any help would be appreciated
thanks in advance Luka |
|
|
frequentguest Guest
|
|
Posted: Tue Dec 04, 2007 2:22 pm |
|
|
output_low(), output_float(), and delay_us() are all built-in functions of the CCS C compiler--the product for which this forum is intended. |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Fri Jan 16, 2009 6:15 am |
|
|
Hello to all!!!
I'm going to use some DS18B20 sensor with a 16f876 and I would like to use the driver you posted here..
I noticed that only the driver takes up to 51% of the MCU's ram!! I'm using PCM compiler version 3.181 ..
I noticed also that there is a crc lookup table.. why do we need that table?
I think that the most of the ram is taken by the table..
Can you explain why there is the lookup table?
thanks!! |
|
|
tesla80
Joined: 23 May 2007 Posts: 81
|
|
Posted: Thu Jan 07, 2010 7:46 pm |
|
|
Hi All,
I'm using the code above for DS18B20 and I always get "85C" message from printf("%4LdC \n\r",temperature); line.
I'm sure that I have a "DS18B20", not another model.
And I've tried to disable all interrupts during reading the temperature.
How to resolve this problem? |
|
|
Ttelmah Guest
|
|
Posted: Fri Jan 08, 2010 5:33 am |
|
|
simons wrote: | Hello to all!!!
I'm going to use some DS18B20 sensor with a 16f876 and I would like to use the driver you posted here..
I noticed that only the driver takes up to 51% of the MCU's ram!! I'm using PCM compiler version 3.181 ..
I noticed also that there is a crc lookup table.. why do we need that table?
I think that the most of the ram is taken by the table..
Can you explain why there is the lookup table?
thanks!! |
The lookup is in ROM, not RAM, so isn't your problem.
The table is the smallest and fastest way of generating the CRC, which is needed for the one wire operations.
Have you got "*DEVICE *=16", otherwise the code can only access the first page of RAM, which on a 16F876, is just 96 bytes of RAM....
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jan 08, 2010 9:51 am |
|
|
tesla80 wrote: | Hi All,
I'm using the code above for DS18B20 and I always get "85C" message from printf("%4LdC \n\r",temperature); line.
I'm sure that I have a "DS18B20", not another model.
And I've tried to disable all interrupts during reading the temperature.
How to resolve this problem? | You will only get this result printed when the received checksum value is correct, so it looks like your communications are working.
What is the temperature value you are expecting to read?
Does the value change when you heat or cool the sensor a bit? Try holding the sensor between your fingers.
What is your compiler version number and which processor are you using? |
|
|
tesla80
Joined: 23 May 2007 Posts: 81
|
|
Posted: Fri Jan 08, 2010 5:11 pm |
|
|
Yes, I always get the same result.
I've tried to heat the sensor but no change.
My compiler version is 4.074 and my mcu is 18F4520.
I use 4k7 pull-up resistor to bus.
I know that, this is the default value in the sensor's register.
I think, the sensor does not refresh this value...
I'll try with another one.
EDIT:
My sensor was broken!!
I publish my very good working code
ds18b20.c
Code: | #ifndef DS1820_C // eray
#define DS1820_C
#ifndef DQ
#define DQ PIN_B1 // One Wire Bus pin assignment
#endif
#define DS1820_CONN_ERROR 1111.0 // error sign
// One Wire bus functions - from Dallas publication AN162
// "Interfacing DS18x20/DS1822 1-wire Temperature Sensor in a Microcontroller
// Environment". Delays calculated from values given for 8051.
// Changed variable name ROM[] to RomBytes[] because ROM is a reserved word
// in version 4 of the CCS compiler.
// Global variables
int8 RomBytes[8];
int8 lastDiscrep = 0;
short doneFlag = 0;
int8 FoundROM[9][8]; // Table of found ROM codes, 8 bytes for each
int8 numROMs;
int8 dowcrc; // crc is accumulated in this variable
// crc lookup table
int8 const dscrc_table[] = {
0,94,188,226,97,63,221,131,194,156,126,32,163,253,31,65,
157,195,33,127,252,162,64,30,95,1,227,189,62,96,130,220,
35,125,159,193,66,28,254,160,225,191,93,3,128,222,60,98,
190,224,2,92,223,129,99,61,124,34,192,158,29,67,161,255,
70,24,250,164,39,121,155,197,132,218,56,102,229,187,89,7,
219,133,103,57,186,228,6,88,25,71,165,251,120,38,196,154,
101,59,217,135,4,90,184,230,167,249,27,69,198,152,122,36,
248,166,68,26,153,199,37,123,58,100,134,216,91,5,231,185,
140,210,48,110,237,179,81,15,78,16,242,172,47,113,147,205,
17,79,173,243,112,46,204,146,211,141,111,49,178,236,14,80,
175,241,19,77,206,144,114,44,109,51,209,143,12,82,176,238,
50,108,142,208,83,13,239,177,240,174,76,18,145,207,45,115,
202,148,118,40,171,245,23,73,8,86,180,234,105,55,213,139,
87,9,235,181,54,104,138,212,149,203,41,119,244,170,72,22,
233,183,85,11,136,214,52,106,43,117,151,201,74,20,246,168,
116,42,200,150,21,75,169,247,182,232,10,84,215,137,107,53
};
// Returns 0 for one wire device presence, 1 for none
int8 ow_reset(void)
{
int8 presence;
output_low(DQ);
delay_us(488); // Min. 480uS
output_float(DQ);
delay_us(72); // Takes 15 to 60uS for devices to respond
presence = input(DQ);
delay_us(424); // Wait for end of timeslot
return(presence);
}
//******************************************************************************
// Read bit on one wire bus
int8 read_bit(void)
{
output_low(DQ);
delay_us(1); // Added, 1uS min. Code relied on 8051 being slow.
output_float(DQ);
delay_us(12); // Read within 15uS from start of time slot
return(input(DQ));
}
//******************************************************************************
void write_bit(int8 bitval)
{
output_low(DQ);
if(bitval == 1) {
delay_us(1); // 1uS min. Code relied on 8051 being slow.
output_float(DQ);
}
delay_us(105); // Wait for end of timeslot
output_float(DQ);
}
//******************************************************************************
int8 read_byte(void)
{
int8 i;
int8 val = 0;
for(i=0;i<8;i++)
{
if(read_bit()) val |= (0x01 << i);
delay_us(120); // To finish time slot
}
return val;
}
//******************************************************************************
void write_byte(int8 val)
{
int8 i;
int8 temp;
for (i=0;i<8;i++)
{
temp = val >> i;
temp &= 0x01;
write_bit(temp);
}
delay_us(105);
}
//******************************************************************************
// One wire crc
int8 ow_crc(int8 x)
{
dowcrc = dscrc_table[dowcrc^x];
return dowcrc;
}
//******************************************************************************
// Searches for the next device on the one wire bus. If there are no more
// devices on the bus then false is returned.
int8 Next(void)
{
int8 m = 1; // ROM Bit index
int8 n = 0; // ROM Byte index
int8 k = 1; // Bit mask
int8 x = 0;
int8 discrepMarker = 0;
int8 g; // Output bit
int8 nxt; // Return value
short flag;
nxt = FALSE; // Reset next flag to false
dowcrc = 0; // Reset the dowcrc
flag = ow_reset();
if (flag||doneFlag) // If no parts return false
{
lastDiscrep = 0; // Reset the search
return FALSE;
}
write_byte(0xF0); // Send SearchROM command
do
{
x = 0;
if (read_bit() == 1) x = 2;
delay_us(120);
if (read_bit() == 1) x |= 1; // And it's complement
if (x == 3) // There are no devices on the one wire bus
break;
else
{
if (x > 0) // All devices coupled have 0 or 1
g = x >> 1; // Bit write value for search
// If this discrepancy is before the last discrepancy on a previous
// Next then pick the same as last time.
else
{
if (m < lastDiscrep)
g = ((RomBytes[n] & k) > 0);
// If equal to last pick 1
else
g = (m == lastDiscrep); // If not then pick 0
// If 0 was picked then record position with mask k
if (g == 0) discrepMarker = m;
}
// Isolate bit in ROM[n] with mask k
if (g == 1) RomBytes[n] |= k;
else RomBytes[n] &= ~k;
write_bit(g); // ROM search write
m++; // Increment bit counter m
k = k << 1; // and shift the bit mask k
// If the mask is 0 then go to new ROM
if (k == 0)
{ // Byte n and reset mask
ow_crc(RomBytes[n]); // Accumulate the crc
n++;
k++;
}
}
} while (n < 8); // Loop through until through all ROM bytes 0-7
if (m < (65||dowcrc)) // If search was unsuccessful then
lastDiscrep = 0; // reset the last Discrepancy to zero
else // Search was successful, so set lastDiscrep, lastOne, nxt
{
lastDiscrep = discrepMarker;
doneFlag = (lastDiscrep == 0);
nxt = TRUE; // Indicates search not yet complete, more parts remain
}
return nxt;
}
//******************************************************************************
// Resets current state of a ROM search and calls Next to find the first device
// on the one wire bus.
int8 First(void)
{
lastDiscrep = 0;
doneFlag = FALSE;
return Next(); // Call Next and return it's return value;
}
//******************************************************************************
void FindDevices(void)
{
int8 m;
if(!ow_reset())
{
if(First()) // Begins when at least one part found
{
numROMs = 0;
do
{
numROMs++;
for (m=0;m<8;m++)
{
FoundROM[numROMs][m] = RomBytes[m];
}
printf("Device No.%u address ",numROMs);
printf("%X%X%X%X%X%X%X%X\n\r",
FoundROM[numROMs][7],FoundROM[numROMs][6],FoundROM[numROMs][5],
FoundROM[numROMs][4],FoundROM[numROMs][3],FoundROM[numROMs][2],
FoundROM[numROMs][1],FoundROM[numROMs][0]);
} while (Next() && (numROMs<10)); // Continues until no additional
// devices found.
}
}
putc('\n'); putc('\r');
}
//******************************************************************************
// Sends Match ROM command to bus then device address
int8 Send_MatchRom(void)
{
int8 i;
if (ow_reset()) return FALSE; // 0 if device present
write_byte(0x55); // Match ROM
for (i=0;i<8;i++)
{
write_byte(FoundRom[numROMs][i]); // Send ROM code
}
return TRUE;
}
float ds1820_read()
{
signed int16 temperature;
int8 i, scratch[9], try=3;
while(try--)
{
output_float(DQ); // Set as input. 4k7 pullup on bus.
ow_reset();
write_byte(0xCC); // Skip Rom command
write_byte(0x44); // Temperature Convert command
output_float(DQ);
delay_ms(750); // Max. time for conversion is 750mS
ow_reset();
write_byte(0xCC); // Skip Rom command
write_byte(0xBE); // Read scratch pad command
dowcrc = 0;
// Get the data bytes
for (i=0;i<=7;i++)
{
scratch[i] = read_byte();
ow_crc(scratch[i]);
}
scratch[8] = read_byte(); // Get crc byte
ow_reset();
// If the received crc is same as calculated then data is valid.
// Scale and round to nearest degree C.
// Scaling is 0.0625 (1/16) deg.C/bit with default 12 bit resolution.
// Round by adding half denominator for positive temperatures and
// subtracting half denominator for negative temperatures.
if (scratch[8] == dowcrc)
{
float t;
temperature = (signed int16) make16(scratch[1],scratch[0]);
if (temperature >= 0)
t = ((float)temperature + 8.0)/16.0;
else
t = ((float)temperature - 8.0)/16.0;
// fprintf(PC, "%4LdC \n\r",temperature);
return (t);
}
// else
// fprintf(PC, "crc error | try=%d \n\r",try);
}
return DS1820_CONN_ERROR;
}
#endif /*DS1820_C*/ |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|