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

three DS18B20...
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

three DS18B20...
PostPosted: Tue Feb 10, 2009 4:26 pm     Reply with quote

Hi.
I have 3 DS18B20, pic16F887 and LCD.
I managed to use one sensor, and it's show ok.
But now, I want to use 3 sensors, on a single pic pin.
Can somebody help me with some sources (all code)?
I tried Kenny code http://www.ccsinfo.com/forum/viewtopic.php?t=19255 , but I have many errors.
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

PostPosted: Tue Feb 10, 2009 7:01 pm     Reply with quote

It's Dallas code translated to CCS C.

Did you try it like this?
http://www.ccsinfo.com/forum/viewtopic.php?t=29337&highlight=
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Feb 11, 2009 2:20 am     Reply with quote

Thanks for reply. Wink
In http://www.ccsinfo.com/forum/viewtopic.php?t=29337 , there is only the main function. I put the rest of the program from here ( http://www.ccsinfo.com/forum/viewtopic.php?t=19255 ), and I have few errors, I don't know how to resolve them.
I will send you my code, to see what I tried.
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Feb 12, 2009 12:51 pm     Reply with quote

Here is the code:
(Kenny code)
main.c:

Code:

#include <16F887.H>
#device *=16               // FULL RAM ACCESS
#device adc = 10            // ADC de 10 bits
#include <math.h>

//#fuses
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Brownout reset
#FUSES PUT                      // Power Up Timer
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock = 20000000)

#include "Flex_LCD420.c"

//header
#include "DS18B20.h"

//source
#include "DS18B20.c"
void init_adc()
{
   //Set up RA0 as the only analog input
   setup_adc_ports(sAN0);
   //Turn on ADC and use Fosc/2? as the conversion clock
   //Fosc=20Mhz->Fosc/2=100ns        ----       Fosc=8Mhz->Fosc/2=250ns
   //Fosc=20Mhz->Fosc/8=400ns        ----       Fosc=8Mhz->Fosc/8=2.0us
   //Fosc=20Mhz->Fosc/32=1.6us        ----       Fosc=8Mhz->Fosc/32=8.0us
   setup_adc(ADC_CLOCK_DIV_2);
   //Set channel for conversion to AN0 (on RA0)
   set_adc_channel(0);
   //delay 10 uS
   delay_ms(10);
}

float voltmetru()
{
   int16 adc_value;
   float volts;
   
   adc_value = read_adc();
   delay_ms(10);
   volts = (float)(adc_value *5)/1023.0;
   
   return volts;
}
void init_io()
{
    // set up PORTA so RA0, RA1 is an input for the ADC, RA2-RA7 are outputs
    SET_TRIS_A(0x01);   // bit 0 = output, 1 = input
   
    // set up PORTB so RB0 is an input for the switch SW1, RB1-RB7 are outputs
    SET_TRIS_B(0x01);   // bit 0 = output, 1 = input

    // set up remaining ports to be all outputs
    OUTPUT_C(0x00);
    OUTPUT_D(0x00);
    OUTPUT_E(0x00);
}
//main function
void main()
{
   float temperature=0;
   float volts=0;

   int8 i;
   signed int16 stemp16;    // Raw temperature data in 2's complement
   int8 scratch[10];       // Lowest two bytes are for temperature
   
   // Set up MCU
    init_io();
   
   //Set up the ADC
    init_adc();

   //init LCD
   lcd_init();
   
   // Clear the LCD.
   printf(lcd_putc, "\f");

   output_float(DQ);       // 4k7 pullup on bus
   FindDevices();          // Find all owb devices get their codes, and output them
   ow_reset();
   //init timers
   init_timers();

   //for 10 bit resolution mod
   //onewire_write(0xCC);
   //onewire_write(0x4E);

   //onewire_write(125);
   //onewire_write(-55); //this should be done for proper working of DS18B20
   //onewire_write(127);

   //onewire_reset();
   //onewire_write(0xCC);
   //onewire_write(0x48);
   delay_ms(15);



   while (1)
   {
      //read temperature from PIN_A1 - DS18B20 sensor
      //temperature = ds1820_read();
      //read volts from PIN_A0
      volts = voltmetru();
      
      //reading temps from 3 DS18B20 sensors
      // If not using parasitic power can send the temperature convert
      // command to all DS18B20 and/or DS1822 devices on the bus.
      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().  Scale to deg. C, scaling is
            // 0.0625 (1/16) deg. C/bit with default 12 bit res. Could reduce res.
            // and therefore shorten conversion time if necessary.
            // Output the temperatures in whole degrees, apply rounding by adding
            // half denom.
   
            for (numRoms=1;numRoms<=8;numRoms++)
            {
               if (Send_MatchRom())
               {
                  write_byte(0xBE); // Read scratch pad command
                  dowcrc = 0;       // Reset the crc to start a new calculation
      
                  for (i=0;i<=7;i++)
                  {
                      scratch[i] = read_byte();
                      ow_crc(scratch[i]);     // Accumulate the crc
                  }
      
                  scratch[8] = read_byte();   // Received crc byte
                  ow_reset();
      
                  // If calculated crc from incoming bytes equal to crc byte
                  // then data is valid. Calculate and output temperature.
                  
                  if (scratch[8] == dowcrc)
                  {
                     stemp16 = (signed int16) make16(scratch[1],scratch[0]);
                     stemp16 = (stemp16 + 8)/16;   
                     //printf("%4ld ",stemp16);
                 printf(lcd_putc, "%4ld ",stemp16);
delay_ms(500);
                  }
                  else
               {
                  //printf("xxx");       // There was an error in the data
                  printf(lcd_putc,"\nThere was an error in the data");
delay_ms(500);
               }
               }
            }
   
         //putc('\n'); putc('\r');
      }


      //print to LCD
      printf(lcd_putc,"\fTemp= %3.1f \uC", temperature);
      printf(lcd_putc, "\nV= %3.1f", volts);
      
      delay_ms(100);
   }


}


Last edited by pyu on Thu Feb 12, 2009 12:52 pm; edited 1 time in total
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Feb 12, 2009 12:51 pm     Reply with quote

DS18B20.h:
Code:

#define DQ PIN_A1

// Global variables
int8 Rom_Bit[8]; // Rom_Bit 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);

// Read bit on one wire bus
int8 read_bit(void);

void write_bit(int8 bitval) ;

int8 read_byte(void) ;

void write_byte(int8 val) ;

// One wire crc
int8 ow_crc(int8 x) ;

// 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);

// Resets current state of a ROM search and calls Next to find the first device
// on the one wire bus.
int8 First(void);

void FindDevices(void);

// Sends Match ROM command to bus then device address
int8 Send_MatchRom(void);
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Feb 12, 2009 12:52 pm     Reply with quote

and DS18B20.C
Code:



// 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

   printf(lcd_putc, "\fPresence= %i", presence);

delay_ms(500);
   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_Bit[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_Bit[n] with mask k
         if (g == 1)
         Rom_Bit[n] |= k;
         else
         Rom_Bit[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_Bit[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_Bit[m];   // Identifies ROM no. on device
            }

            //printf("Device No.%u address ",numROMs);
         printf(lcd_putc, "\f%1i ",numROMs);
         delay_ms(1000);

            //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]);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][7]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][6]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][5]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][4]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][3]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][2]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][1]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][0]);
         delay_ms(500);

         } while (Next() && (numROMs<10));   // Continues until no additional
                                             // devices found.
      }
      else
         {
            printf(lcd_putc, "\fno ROM found");
         delay_ms(500);
         }
   }
   else
         {
            printf(lcd_putc, "\fno ROM found2");
         delay_ms(500);
         }
   //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;

/******************************************************************************/
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Feb 12, 2009 12:57 pm     Reply with quote

the result:
Code:
printf(lcd_putc, "\fPresence= %i", presence);

presence = 1
and the next thing that is show:
Code:
printf(lcd_putc, "\fno ROM found2");


What is wrong in my program?
The ds18B20 sensor is ok, if I run the project only for one sensor, it display the correct temp.
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

PostPosted: Thu Feb 12, 2009 4:31 pm     Reply with quote

pyu wrote:
Here is the code:
(Kenny code)
main.c:


It's your code, not mine Smile

All I suggested in reply to your PM was to change the variable name ROM[] to something else because in version 4 of the compiler ROM is a reserved word. You have done that.

I suggest for testing that you strip everything off not relevant to the problem, eg voltage measurement, setting tris, etc., and start again with the code I posted.

Sorry, can't help any further.
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Feb 14, 2009 7:25 am     Reply with quote

I wanted to say that is based on your sample code, with my modifications Smile

I have been corected and now, my program looks like this:

main.c:
Code:
#include <16F887.H>
//#device *=16               // FULL RAM ACCESS
//#device adc = 10            // ADC de 10 bits
#include <math.h>

//#fuses
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Brownout reset
#FUSES PUT                      // Power Up Timer
#FUSES NOLVP                    //No low voltage prgming
#FUSES NOCPD                    //No EE protection

#use delay(clock = 20000000)

#include "Flex_LCD420.c"

#define DQ PIN_A1 // One Wire Bus pin assignment

//header
#include "DS18B20.h"

//source
#include "DS18B20.c"


void main()
{
   int8 i;
   signed int16 stemp16;   // Raw temperature data in 2's complement
   int8 scratch[10];       // Lowest two bytes are for temperature

   output_float(DQ);       // 4k7 pullup on bus
   FindDevices();          // Find all owb devices get their codes, and output them
   ow_reset();
   lcd_init();

   while(1)
   {
      // If not using parasitic power can send the temperature convert
      // command to all DS18B20 and/or DS1822 devices on the bus.
      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().  Scale to deg. C, scaling is
         // 0.0625 (1/16) deg. C/bit with default 12 bit res. Could reduce res.
         // and therefore shorten conversion time if necessary.
         // Output the temperatures in whole degrees, apply rounding by adding
         // half denom.
   
         for (numRoms=1;numRoms<=8;numRoms++)
         {
            if (Send_MatchRom())
            {
               write_byte(0xBE); // Read scratch pad command
               dowcrc = 0;       // Reset the crc to start a new calculation
   
               for (i=0;i<=7;i++)
               {
                   scratch[i] = read_byte();
                   ow_crc(scratch[i]);     // Accumulate the crc
               }
   
               scratch[8] = read_byte();   // Received crc byte
               ow_reset();
   
               // If calculated crc from incoming bytes equal to crc byte
               // then data is valid. Calculate and output temperature.
               
               if (scratch[8] == dowcrc)
               {
                  stemp16 = (signed int16) make16(scratch[1],scratch[0]);
                  stemp16 = (stemp16 + 8)/16;   
                  printf(lcd_putc, "%4ld ",stemp16);
               }
               else printf(lcd_putc,"\f XXX");       // There was an error in the data
            }
         }
   
         printf(lcd_putc,"\f aaa");
      delay_ms(200);
      }
      else
      {
         printf(lcd_putc,"\f !ow_reset - false");
         delay_ms(200);
      }
   }   
}


DS18B20.h:
Code:


// Global variables
int8 Rom_Bit[8]; // Rom_Bit 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);

// Read bit on one wire bus
int8 read_bit(void);

void write_bit(int8 bitval) ;

int8 read_byte(void) ;

void write_byte(int8 val) ;

// One wire crc
int8 ow_crc(int8 x) ;

// 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);

// Resets current state of a ROM search and calls Next to find the first device
// on the one wire bus.
int8 First(void);

void FindDevices(void);

// Sends Match ROM command to bus then device address
int8 Send_MatchRom(void);



DS18B20.c:
Code:



// 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

   //printf(lcd_putc, "\fPresence= %i", presence);

//delay_ms(500);
   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_Bit[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_Bit[n] with mask k
         if (g == 1)
         Rom_Bit[n] |= k;
         else
         Rom_Bit[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_Bit[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_Bit[m];   // Identifies ROM no. on device
            }

            //printf("Device No.%u address ",numROMs);
         printf(lcd_putc, "\f%1i ",numROMs);
         delay_ms(1000);

            //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]);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][7]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][6]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][5]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][4]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][3]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][2]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][1]);
         delay_ms(500);
         printf(lcd_putc, "\f%1x ",FoundROM[numROMs][0]);
         delay_ms(500);

         } while (Next() && (numROMs<10));   // Continues until no additional
                                             // devices found.
      }
      else
         {
            printf(lcd_putc, "\fno ROM found");
         //delay_ms(500);
         }
   }
   else
         {
            printf(lcd_putc, "\fno ROM found2");
         //delay_ms(500);
         }
   //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;

/******************************************************************************/


The result is not what I expected :(
On the lcd:
Code:
!ow_reset - false

Something is still wrong on my program.
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

PostPosted: Sat Feb 14, 2009 3:38 pm     Reply with quote

Very good Smile

I will test it on hardware at work.
The lcd_init() statement should be before FindDevices() though.
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Feb 15, 2009 9:15 am     Reply with quote

Ok, thanks.
I use CCS C Compiler version 4.0.13.
Ttelmah
Guest







PostPosted: Sun Feb 15, 2009 9:27 am     Reply with quote

Ouch....
You might want to read the 'sticky thread' at the top of this forum about the early V4 releases. 4.013, is very likely to give problems, if you try to use functions much beyond 'basic' operations. You might want to see if you can upgrade to save yourself a lot of grief.

Best Wishes
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Feb 15, 2009 12:13 pm     Reply with quote

I have now installed the version 4.084.
The result is the same :(
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Feb 15, 2009 1:19 pm     Reply with quote

I just tested your code using four DS18B20s after making three changes:

1. I removed the LCD related stuff (i.e lcd_putc) and printed just to an
RS232 terminal connection.
2. I changed the processor to an 18F4520 since this is what I already had
set up to test.
3. I used E2 as the DQ pin rather than A1

Once I made those changes, your code basically worked correctly and it
properly enumerated the multiple DS18B20s in the circuit. The temps for
all the units displayed correctly and the temps went up when I was
touching them. This proves the core code is good.

I would recommend for you to do the same thing, get rid of the extra stuff
and concentrate on getting the basic code working then add a piece at
a time.

Using printfs I displayed the interim data for the first two DS18B20s so
you can see the results. The incoming data is good, you have some "math" issues left to resolve.

Quote:

stemp16 (hex) result: 0154 Your math result (degrees C): 21 The CRC is: fd
stemp16 (hex) result: 0147 Your math result (degrees C): 20 The CRC is: 93


.
pyu



Joined: 04 Feb 2009
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Feb 15, 2009 2:52 pm     Reply with quote

What version of compiler did you used?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
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