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 support@ccsinfo.com

one wire search rom problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JULIEN LAFFITTE



Joined: 28 Mar 2007
Posts: 20

View user's profile Send private message

one wire search rom problem
PostPosted: Thu Jan 17, 2008 11:34 am     Reply with quote

Hello
I m trying to implement the SEARCH ROM command on my application.
But when I compile a problem appear on this line, in the include for one wire device:
int8 ROM[8]; // ROM bit

I don't think the error was in the main file or in the include for ds18b20/ds1822 but was in the variable declaration of the one wire device include file.

Main file:
Code:

#include <18F2523.H>
#fuses   HS,NOPROTECT,NOBROWNOUT,NOWDT,WDT4096,PUT,NOCPD,NOSTVREN,NODEBUG,NOLVP,NOWRT,NOFCMEN//4s: WDT1024
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define DQ PIN_B1
#include <string.h>
#include <stdlib.h>
#include <owire_v2.c>
#include <ds18b20.c>

void main()
{
float temperature_c;

printf("connecting to DS1820 -----\r\n");
while (TRUE){
delay_ms(2000);
if (!ow_reset()) {
  // set the number of bits of resolution on the ds1822.
  // note that this affects the conversion time,
  // RTFM and see the called function.
  onewire_ds1822_set_temperature_resolution(12);

  // read the temperature from the ds1822.
  // note that the returned value is in deg C,
  // and the "lite" function returns only positive temps.
  temperature_c = onewire_ds1822_read_temp_c_lite();
  printf( "Temperature C= %10.2f degrees C \r\n", temperature_c ); // print temperature C
  delay_ms (1000);

  }
else
  printf("onewire device not found\r\n");

}//end while(1)
}// end of main()


include for ds18b20 or ds1822:
Code:

// 16F877A Pin 33 (RB0) connected to DS1820 Pin2 (DQ).
// 16F877A Pin 33 (RB0) also connect to 4.7K resistor pulled up to +5V.
// 16F877A Pin 25 (RC6/Tx) connected to MAX232 which is then connected to PC serial Port.

// ds1822 scratchpad registers
#define DS1822_SP_TLSB  0
#define DS1822_SP_TMSB  1
#define DS1822_SP_HLIM  2
#define DS1822_SP_LLIM  3
#define DS1822_SP_CFG   4
#define DS1822_SP_RES0  5
#define DS1822_SP_RES1  6
#define DS1822_SP_RES2  7
#define DS1822_SP_CRC   8

// ds1822 rom registers
#define DS1822_ROM_DEVTYPE  0
#define DS1822_ROM_SERIAL1  1
#define DS1822_ROM_SERIAL2  2
#define DS1822_ROM_SERIAL3  3
#define DS1822_ROM_SERIAL4  4
#define DS1822_ROM_SERIAL5  5
#define DS1822_ROM_SERIAL6  6
#define DS1822_ROM_CRC      7

// ds1822 command set
#define DS1822_CMD_READROM           0x33
#define DS1822_CMD_SKIPROM           0xCC
#define DS1822_CMD_CONVERTTEMP       0x44
#define DS1822_CMD_WRITESCRATCHPAD   0x4E
#define DS1822_CMD_READSCRATCHPAD    0xBE
#define DS1822_CMD_COPYSCRATCHPAD    0x48

// note that not all applications need to disable interrupts when
// performing onewire transactions.  but, if unmasked interrupts
// cause onewire timing violations, returned data will be suspect
// or there may be other, hard-to-reproduce problems.

int onewire_ds1822_read_scratchpad(int field) { /* returns config bitfield */
   int data[9];

   ow_reset();
   write_byte(DS1822_CMD_SKIPROM);
   write_byte(DS1822_CMD_READSCRATCHPAD);

   data[DS1822_SP_TLSB]=read_byte();    // 0 Tlsb
   data[DS1822_SP_TMSB]=read_byte();    // 1 Tmsb
   data[DS1822_SP_HLIM]=read_byte();    // 2 Thlim
   data[DS1822_SP_LLIM]=read_byte();    // 3 Tllim
   data[DS1822_SP_CFG]=read_byte();     // 4 Config
   data[DS1822_SP_RES0]=read_byte();    // 5 RES0
   data[DS1822_SP_RES1]=read_byte();    // 6 RES1
   data[DS1822_SP_RES2]=read_byte();    // 7 RES2
   data[DS1822_SP_CRC]=read_byte();     // 8 CRC

   if (field > 8) {
      //printf(debug_putc,"ERR! >scratchpad field 0x%x out of range (ds1822)\n\r",field);
      return(0);
      }
   else {
      //printf(debug_putc,"0x%x >scratchpad field 0x%x (ds1822)\n\r",data[field],field);
      return(data[field]);
      }
}

int onewire_ds1822_read_rom(int field) { /* rtns ROM info, one byte at a time */
   int data[8];

   if (!ow_reset())
     return (0);
   write_byte(DS1822_CMD_READROM);

   data[DS1822_ROM_DEVTYPE]=read_byte();   // 0 family code
   data[DS1822_ROM_SERIAL1]=read_byte();   // 1 serial number LSB
   data[DS1822_ROM_SERIAL2]=read_byte();   // 2 serial number
   data[DS1822_ROM_SERIAL3]=read_byte();   // 3 serial number
   data[DS1822_ROM_SERIAL4]=read_byte();   // 4 serial number
   data[DS1822_ROM_SERIAL5]=read_byte();   // 5 serial number
   data[DS1822_ROM_SERIAL6]=read_byte();   // 6 serial number MSB
   data[DS1822_ROM_CRC]=read_byte();       // 7 CRC

   if (field > 7) {
      //printf(debug_putc,"ERR! >rom field 0x%x out of range (ds1822)\n\r",field);
      return(0);
      }
   else {
      //printf(debug_putc,"0x%x >rom field 0x%x (ds1822)\n\r",data[field],field);
      return(data[field]);
      }
}

int onewire_ds1822_set_temperature_resolution(int resolution) { /* set up for nbit resolution */
   int resolution_cfgfield;

   if ((resolution < 9) || (resolution > 12))
      resolution=9;

   resolution_cfgfield=((resolution-9)<<5);  // see DS1822 datasheet page 7

   if (ow_reset())
     return (0);
   write_byte(DS1822_CMD_SKIPROM);
   write_byte(DS1822_CMD_WRITESCRATCHPAD);

   write_byte(0b01111101);   // set max TH threshold (125'C)
   write_byte(0b11001001);   // set min TL threshold (-55'C)
   write_byte(resolution_cfgfield);    // Temp resolution, set to nbit

   ow_reset(); // reset
   write_byte(DS1822_CMD_SKIPROM);
   write_byte(DS1822_CMD_COPYSCRATCHPAD);
   delay_ms(15); // allow time for flash memory write.

   return( (onewire_ds1822_read_scratchpad(DS1822_SP_CFG) & 0b01100000)>>5 );
}

float onewire_ds1822_read_temp_c_lite() {
   int temperatureLSB, temperatureMSB, config, delaymult;
   signed int16 temp16;
   float temperature;
   config=((onewire_ds1822_read_scratchpad(DS1822_SP_CFG) && 0b01100000)>>5);
      // each addn'l resolution bit needs twice the base conversion time!
   if (ow_reset())
     return (0);
   write_byte(DS1822_CMD_SKIPROM);
   write_byte(DS1822_CMD_CONVERTTEMP);

   delaymult=1<<config;
      //printf(debug_putc,"0x%x >delay_mult (ds1822)\n\r",delaymult);

   while (delaymult--)
      delay_ms(100); // allow worst case time for temp. conversion.

   temperatureLSB = onewire_ds1822_read_scratchpad(DS1822_SP_TLSB);
   temperatureMSB = onewire_ds1822_read_scratchpad(DS1822_SP_TMSB);
   temp16= make16(temperatureMSB, temperatureLSB);
   temperature = (float) temp16 / 16.0;
   return(temperature);
}


include for one wire device:
Code:

// One Wire bus functions - from Dallas publication AN162
// "Interfacing DS18x20/DS1822 1-wire Temperature Sensor in a uC Environment"
// Delays calculated from values given for 8051 in source code

// Global variables

int8 ROM[8];            // ROM bit
int8 lastDiscrep = 0;
short doneFlag = 0;
int8 FoundROM[7][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);         // 1uS min. Original code relied on 8051 being slow
   output_float(DQ);
   delay_us(20);        // Wait at least 15mS from start of time slot
   return(input(DQ));   // Delay to finish time slot (total 60 to 120uS)
}                       // must be done next.
/******************************************************************************/
void write_bit(int8 bitval)
{
   output_low(DQ);

   if(bitval == 1) {
      delay_us(1);      // 1uS min. Original 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 = ((ROM[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) ROM[n] |= k;
         else ROM[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(ROM[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] = ROM[m];   // Identifies ROM no. on device
            }

            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;
}
/******************************************************************************/


I m using PCWH 4.064
All my code is on different post of the forum...
please help me ;(
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Jan 17, 2008 2:09 pm     Reply with quote

Quote:
I don't think the error was in the main file or in the include for ds18b20/ds1822 but was in the variable declaration of the one wire device include file.
Don't assume but test!

I created the following test program
Code:
#include <18F2523.H>
#fuses   HS,NOPROTECT,NOBROWNOUT,NOWDT,WDT4096,PUT,NOCPD,NOSTVREN,NODEBUG,NOLVP,NOWRT,NOFCMEN//4s: WDT1024
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


int8 ROM[8];            // ROM bit


void main()
{
   while (TRUE) ;
}

Please note that we do like the short test programs very much. A large piece of code as you have posted takes way too much of our time to read, so most people will skip it and you don't get an answer...


Surprise!! I got an error on the same line of code as you did. I just get the strange feeling you could have performed the same test and now I'm doing your work...

Answer: Since compiler version 4 the ROM keyword is reserved and should not be used for variable names. It is a bad variable name anyway as it is not very specific about what data it contains.

More info on the ROM keyword from page 43 of the CCS manual:
Quote:
A special method allows the use of pointers to ROM. This method does not contain extra code at
the start of the structure as does constant..
For example:
char rom commands[] = {“put|get|status|shutdown”};
JULIEN LAFFITTE



Joined: 28 Mar 2007
Posts: 20

View user's profile Send private message

PostPosted: Fri Jan 18, 2008 2:25 am     Reply with quote

Thank you Exclamation
I'm sorry to have take your time for that Embarassed , but now I know Idea
Thanks again!
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