View previous topic :: View next topic |
Author |
Message |
40inD Guest
|
multiple 18B20 - need working example - i'm stupid in 1-wire |
Posted: Mon Jan 01, 2007 8:24 am |
|
|
Does anybody have a working example how to select and read one of 18B20's on bus?
I've read this ccsinfo.com/forum/viewtopic.php?t=19255 but i do not understand how to assemble this in a whole program. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1937 Location: Norman, OK
|
|
|
40ind Guest
|
|
Posted: Mon Jan 01, 2007 9:48 am |
|
|
Quote: | This code will not work with 2 or more DS1820 sharing the same 1-wire bus.
|
|
|
|
Assen
Joined: 18 Oct 2006 Posts: 8
|
|
Posted: Mon Jan 01, 2007 11:50 am |
|
|
Check this driver: DS2432.c
It is written for DS2432 but the function search_rom can be used with any 1Wire chip. It is very simple to find all available 1Wire chips using that function and then select one of them using MATCH_ROM_CMD. I'll try to make an example if you cannot do it yourself. |
|
|
40inD Guest
|
|
Posted: Mon Jan 01, 2007 1:32 pm |
|
|
Assen, I need to get temperature from 3 18B20. Could you explain me the algorythm? Which function follows after which? |
|
|
Assen
Joined: 18 Oct 2006 Posts: 8
|
|
Posted: Mon Jan 01, 2007 2:19 pm |
|
|
I'm not using DS1820 and your reference code (ccsinfo.com/forum/viewtopic.php?t=19255) but from what I see it must be like this:
Use the function FindDevices to create your own search/read function.
Insert after the last printf a call to Send_MatchRom and if it returns TRUE then do whatever you want with the selected chip - you can read the temperature for example. That's all. Using FindDevices you will have access to your 3 chips once at time. |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Mon Jan 01, 2007 5:25 pm |
|
|
Yep, that's pretty much it. Here's an example for testing.
Edited:
onewire.c is here
http://www.ccsinfo.com/forum/viewtopic.php?t=19255
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.
FindDevices();
while(1)
{
if (!ow_reset()) // If a device is present
{
write_byte(0xCC); // Skip Rom command
write_byte(0x44); // Temperature convert command
output_float(DQ);
delay_ms(750); // Max. conv. time is 750mS for 12 bit
ow_reset();
// Now get the device raw temperature data using Match ROM with the
// addresses obtained with FindDevices().
// 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.
for (numRoms=1;numRoms<=8;numRoms++)
{
if (Send_MatchRom())
{
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 calculated crc from incoming bytes equal to crc byte
// then data is valid.
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("%4Ld ",temperature);
}
else
printf("Error in data\n\r");
}
}
putc('\n'); putc('\r');
}
}
}
|
Here's the output with four DS18B20 and four DS1822 on the bus.
Code: |
Device No.1 address B400000056245628 DS18B20 family code 28
Device No.2 address 8500000042A0AE28 " " " "
Device No.3 address 8300000042521128 " " " "
Device No.4 address 8A00000042889128 " " " "
Device No.5 address 920000000232B022 DS1822 family code 22
Device No.6 address 3C0000000227B022 " " " "
Device No.7 address 870000000235C422 " " " "
Device No.8 address EE00000002311222 " " " "
25 25 25 25 25 25 24 25
25 25 25 25 25 25 24 25
25 25 25 25 25 25 24 25
etc.
|
Last edited by Kenny on Thu Apr 30, 2009 12:23 am; edited 2 times in total |
|
|
Guest
|
|
Posted: Sun Apr 01, 2007 1:37 pm |
|
|
Quote: | // include one wire functions here |
Which OneWire functions are to be included here? From one of the CCS example programs or some other ones? Can you post the specific ones? Thanks. |
|
|
Guest
|
|
Posted: Mon Apr 02, 2007 2:31 am |
|
|
The ones from the link in the first post in this thread. |
|
|
Keith1130
Joined: 31 Oct 2009 Posts: 6
|
|
Posted: Sat Nov 07, 2009 11:08 am |
|
|
Hi, Kenny, how can you show the outputs in computer? I am using one DS1822 communicated with PIC18F2480. I have finished the driver code, but when I try to debug it, I don't know how to get the outputs like yours. Can you show me how to do it in MPLAB? |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
|
Posted: Sat Nov 07, 2009 3:04 pm |
|
|
For one device the address is not required. Either use the code in the link, or the code in the code library section of this forum. However, the code above should work with just one device connected.
I use the CCS IDE. |
|
|
|