CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

DS18B20 request

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
José
Guest







DS18B20 request
PostPosted: Mon Jun 27, 2005 4:08 am     Reply with quote

Hi,

How can i use the DS18B20 (12bit) ???????

can i use the reset, read_byte, write_byte, read_bit, write_bit , from the DS1820??????

How implement a DS18B20 temp. mes.?????

Tkn´s in advance.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 27, 2005 5:53 am     Reply with quote

First you read the datasheet. Then you write a driver. Maxim already gives source code for that

http://dbserv.maxim-ic.com/quick_view2.cfm?qv_pk=2812
José
Guest







PostPosted: Mon Jun 27, 2005 7:01 am     Reply with quote

Quote:
First you read the datasheet. Then you write a driver. Maxim already gives source code for that


Hi,

I already read this, and the sample code is in asm, but i like to use the ccs(but i´m begginer), and the reason for this post is , I need help for CCS code.

And as I said, all the information is for the DS1820, and not for the 18B20.

Note: 0% -> 100%, i´m in 5%.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 27, 2005 7:23 am     Reply with quote

Well my brain must be dead then cause this sure looks like C to me
Code:

//individual Serial #s and readings
#define DEVICES 4
int ROMS[DEVICES][8];
int TEMPS[DEVICES];

int GetTemperatures(int BASE)
{
  // init. the 1-Wire Master
  Initialize(BASE);
  // exit if no devices can be found
  if(Reset(BASE)) return(1);

  // find all individual Serial#s
  FindROMs(BASE);

  // Convert temp. and read devices
  ConvertT(BASE);
  ReadTemps(BASE);

  return(0);
}
void Initialize(int BASE)
{
  // Divide the clock
  outp(BASE+4,0x0F);

  // Generate INTs on reset and send
  outp(BASE+3,0x09);
}

int Reset(int BASE)
{
  outp(BASE,0x02);  // send reset
  WaitforInterrupt();

  if(inp(BASE+2) & 0x02)
    return(1);   //no presence found
  else
    return(0);   //presence found
}

int FindROMs(int BASE)
{
  int loop;
  int dev;
  int TData[16];
  int RData[16];

  // reset RecoverROM and generate the
  // starting TData
  RecoverROM(NULL,TData,NULL);

  //run once for each device
  for(dev=0;dev<4;dev++)
  {
    outp(BASE,0x01);   // send reset
    WaitforInterrupt();
    outp(BASE+1,0xF0);   // send SeachROM
    WaitforInterrupt();

    // enter Accelerator mode
    outp(BASE,0x02);

    // transmit the TDATA and receive
    // the RDATA.
    for(loop=0;loop<16;loop++)
    {
      outp(BASE+1,TData[loop]);
      WaitforInterrupt();
      inp(BASE+1,RData[loop]);
    }

    //decode recovered ROM and generate
    //next Search value
    RecoverROM(RDATA,TData,ROMS[dev]);
  }
}

int ConvertT(int BASE)
{
  outp(BASE,0x01);   // send reset
  WaitforInterrupt();

  outp(BASE+1,0xCC);   // skip ROM
  WaitforInterrupt();

  outp(BASE+1,0x44);   // convert Temp.
  WaitforInterrupt();

int ReadTemps(int BASE)
{
  int dev,loop;
  int LSB,MSB;

  for(dev=0;dev<4;dev++)
  {
    outp(BASE,0x01);   // send reset
    WaitforInterrupt();
    outp(BASE+1,0x55);   // match ROM
    WaitforInterrupt();

    // send 8 bytes of ROM code
    for(loop=0;loop<8;loop++)
    {
      outp(BASE+1,ROMS[dev][loop]);
      WaitforInterrupt();
    }

    outp(BASE+1,0xBE);   // read memory
    WaitforInterrupt();

    outp(BASE+1,0xFF);   // read LSB
    WaitforInterrupt();
    LSB = inp(BASE+1);

    outp(BASE+1,0xFF);   // read MSB
    WaitforInterrupt();
    MSB = inp(BASE+1);

    TEMPS[dev] = MSB<<8 + LSB;
  }
}

/////////////////////////////////////////////////////////////////////////////////
// RecoverROM performs two functions. Given 16 bytes of receive data taken from
// the 1-Wire Master during a Search ROM function, it will extract the ROM code
// found into an 8 byte array and it will generate the next 16 bytes to be trans-
// mitted during the next Search ROM.
// RecoverROM must be initialized by sending a NULL pointer in ReceivedData. It
// will write 16 bytes of zeros into TransmitData and clear the discrepancy tree.
// The discrepancy tree keeps track of which ROM discrepancies have already been
// explored.
// RecoverROM also returns a value telling whether there are any more ROM codes to
// be found. If a zero is returned, there are still discrepancies. If a one is
// returned all ROMs on the bus have been found. Running RecoverROM again in this
// case will result in repeating ROM codes already found
////////////////////////////////////////////////////////////////////////////////

int RecoverROM(int* ReceiveData, int* TransmitData, int* ROMCode)
{
  int loop;
  int result;
  int TROM[64];   // the transmit value being generated
  int RROM[64];   // the ROM recovered from the received data
  int RDIS[64];   // the discrepancy bits in the received data

  static int TREE[64];   // used to keep track of which discrepancy bits have
                         // already been flipped.

  // If receivedata is NULL, this is the first run. Transmit data should be all
  // zeros, and the discrepancy tree must also be reset.

  if(ReceiveData == NULL)
  {
    for(loop = 0; loop < 64; loop++) TREE[loop] = 0;
    for(loop = 0; loop < 16; loop++) TransmitData[loop] = 0;
    return 1;
  }
  // de-interleave the received data into the new ROM code and the discrepancy bits
  for(loop = 0; loop < 16; loop++)
  {
    if((ReceiveData[loop] & 0x02) == 0x00) RROM[loop*4] = 0; else RROM[loop*4 ] = 1;
    if((ReceiveData[loop] & 0x08) == 0x00) RROM[loop*4+1] = 0; else RROM[loop*4+1] = 1;
    if((ReceiveData[loop] & 0x20) == 0x00) RROM[loop*4+2] = 0; else RROM[loop*4+2] = 1;
    if((ReceiveData[loop] & 0x80) == 0x00) RROM[loop*4+3] = 0; else RROM[loop*4+3] = 1;

    if((ReceiveData[loop] & 0x01) == 0x00) RDIS[loop*4] = 0; else RDIS[loop*4 ] = 1;
    if((ReceiveData[loop] & 0x04) == 0x00) RDIS[loop*4+1] = 0; else RDIS[loop*4+1] = 1;
    if((ReceiveData[loop] & 0x10) == 0x00) RDIS[loop*4+2] = 0; else RDIS[loop*4+2] = 1;
    if((ReceiveData[loop] & 0x40) == 0x00) RDIS[loop*4+3] = 0; else RDIS[loop*4+3] = 1;
  }

  // initialize the transmit ROM to the recovered ROM

  for(loop = 0; loop < 64; loop++) TROM[loop] = RROM[loop];

  // work through the new transmit ROM backwards setting every bit to 0 until the
  // most significant discrepancy bit which has not yet been flipped is found.
  // The transmit ROM bit at that location must be flipped.

  for(loop = 63; loop >= 0; loop--)
  {
    // This is a new discrepancy bit. Set the indicator in the tree, flip the
    // transmit bit, and then break from the loop.

    if((TREE[loop] == 0) && (RDIS[loop] == 1) && (TROM[loop] == 0))
    {
      TREE[loop] = 1;
      TROM[loop] = 1;
      break;
    }
    if((TREE[loop] == 0) && (RDIS[loop] == 1) && (TROM[loop] == 1))
    {
      TREE[loop] = 1;
      TROM[loop] = 0;
      break;
    }

    // This bit has already been flipped, remove it from the tree and continue
    // setting the transmit bits to zero.

    if((TREE[loop] == 1) && (RDIS[loop] == 1)) TREE[loop] = 0;
    TROM[loop] = 0;
  }
  result = loop;   // if loop made it to -1, there are no more discrepancy bits
                   // and the search can end.

  // Convert the individual transmit ROM bit into a 16 byte format
  // every other bit is don't care.

  for(loop = 0; loop < 16; loop++)
  {
    TransmitData[loop] = (TROM[loop*4]<<1) +
                         (TROM[loop*4+1]<<3) +
                         (TROM[loop*4+2]<<5) +
                         (TROM[loop*4+3]<<7);
  }

  // Convert the individual recovered ROM bits into an 8 byte format

  for(loop = 0; loop < 8; loop++)
  {
    ROMCode[loop] = (RROM[loop*8]) +
                    (RROM[loop*8+1]<<1) +
                    (RROM[loop*8+2]<<2) +
                    (RROM[loop*8+3]<<3) +
                    (RROM[loop*8+4]<<4) +
                    (RROM[loop*8+5]<<5) +
                    (RROM[loop*8+6]<<6) +
                    (RROM[loop*8+7]<<7);
  }
  if(result == -1) return 1;   // There are no DIS bits that haven't been flipped
                               // Tell the main loop the seach is over
  return 0;   // else continue
}




Its not in CCS C but shouldn't be a problem to convert it.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 27, 2005 7:25 am     Reply with quote

Oh yeah, you can also use the search feature and use the code already posted here!

http://www.ccsinfo.com/forum/viewtopic.php?t=19255
http://www.ccsinfo.com/forum/viewtopic.php?t=12576&highlight=ds18b20
jds-pic



Joined: 17 Sep 2003
Posts: 205

View user's profile Send private message

PostPosted: Mon Jun 27, 2005 7:28 am     Reply with quote

José wrote:

I need help for CCS code.


90% of what you need is right here;
http://www.ccsinfo.com/forum/viewtopic.php?t=19520

José wrote:

And as I said, all the information is for the DS1820, and not for the 18B20.


and this is life for an embedded systems programmer: borrow, adapt, improvise, overcome.

see also
http://www.ccsinfo.com/forum/viewtopic.php?p=37998
and
http://www.ccsinfo.com/forum/viewtopic.php?t=19795

how did i find these? by using the search feature.

jds-pic
Guest








PostPosted: Mon Jun 27, 2005 8:37 am     Reply with quote

Who does not know is as a blind person, and I am starting to see, and open my eyes.

So thanks for all info´s.

(in the search I had an error in the words, thought that it got a better result if it is looked for 1820 or 18b20, but not. But as it said I am to start to open the eyes.)
I wait in a next time not to commit as many basic errors.

Debtor to all the involved ones.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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