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

<DS1820 work> but <DS18S20 not work>!!

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







<DS1820 work> but <DS18S20 not work>!!
PostPosted: Sat Oct 25, 2008 4:36 am     Reply with quote

Hi all,

I have 4 sensor :

2- DS1820 (old sensor)
2- DS18S20 (new sensor)

The program work because i read 64 bit rom, code very well .
The difference is in read temperature :
DS1820 read real temperature.
DS18S20 read "SCRATCHPAD" Temperature LSB = 255 (0xFF) but when I friger the sensor I read the real temperature!!!! and when the sensor return to room temperature above 20°C the "SCRATCHPAD" Temperature LSB = 255!!!!
--
And two all kinds of sensor I write correctly in "SCRATCHPAD" .

post the code:


Code:

#include "C:\Documents and Settings\Michele\Desktop\test_DS18S20\main.h"
#include "flex_lcd.c"

//#use fast_io (C)
#use fast_io (B)


//ds18b20 pin
#define DS18B20_IO PIN_E0

int1 ds18b20_initialization(void);
void ds18b20_write_bit(int8 b);
void ds18b20_write_byte(int8 B);
int1 ds18b20_read_bit(void);
int8 ds18b20_read_byte(void);
void ResetDS1820 ( void );


void main()
{
   signed int8 i;
   int8 scratchpad_data[9],busy;
   char buffer[20];       
   signed int16 temp=0x0000;
   float temperature,temperatura;

   float average_temp[6],temp16_1,temp16_2,temp16_3,temp16_4,temp16_5,temp_avg;
   int8 sample_count;


   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!
   output_float(DS18B20_IO);



   lcd_init();

   lcd_gotoxy(2,1);
   lcd_putc("Temp: ");


delay_ms(1000);

                     //Leggi Temperatura
      for(i=0; i<9; i++)
         scratchpad_data[i]=0x00;   
      if(ds18b20_initialization()){
         ds18b20_write_byte(0x33);  //skip ROM command
     
         for(i=0; i<8; i++){
            scratchpad_data[i]=ds18b20_read_byte();
         }
   }

   
      if(ds18b20_initialization()){

         ds18b20_write_byte(0xCC);  //skip ROM command
         ds18b20_write_byte(0x4E); 

         ds18b20_write_byte(125);
         ds18b20_write_byte(-55); 
         ds18b20_write_byte(127); 

   }
   delay_ms(15);
      if(ds18b20_initialization()){

         ds18b20_write_byte(0xCC);  //skip ROM command
         ds18b20_write_byte(0x48); 

   }


#asm
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
#endasm




                       
   while(1){                       
           
                     //Leggi Temperatura
      for(i=0; i<9; i++)
         scratchpad_data[i]=0x00;   
         
      if(ds18b20_initialization()){
         ds18b20_write_byte(0xCC);  //skip ROM command
         ds18b20_write_byte(0x44);  //convert temperature command

         delay_ms(850);             //max conversion time for 12bit resolution=750ms

         ds18b20_initialization();
         ds18b20_write_byte(0xCC);  //skip ROM command
         ds18b20_write_byte(0xBE);  //read scratch pad command
     
         for(i=0; i<8; i++){
            scratchpad_data[i]=ds18b20_read_byte();
         }


      //low precision
/*
         temp=make16(scratchpad_data[1], scratchpad_data[0]);

       i = scratchpad_data[0] >> 1;
       temperature = i;
       i = scratchpad_data[0] & 0x01;
       temperature = (float) temperature + (i * 0.5);
       i= scratchpad_data[1] & 0x01;  //segno
       if (i==1){
          temperature = temperature * -1;
       }

*/


      //hight precisione temp

      i = scratchpad_data[0];


       temperature = (float)(scratchpad_data[7] - scratchpad_data[6]) / scratchpad_data[7];      

      if(i<0){
          i = (signed)scratchpad_data[0]/2;
      //       i &= 0x7F;
      // i = i* -1;   

      }
      else{
       i = scratchpad_data[0] >> 1;
          i &= 0x7F;
      }

       temperatura = ((float)i - 0.25);
       temperatura = temperatura + temperature;    
         temperature = temperatura;

      
       //Media temperatura


      average_temp[sample_count] = temperature;
         //Calcola average
     
   temp16_1 = average_temp[0];
   temp16_2 = average_temp[1];
   temp16_3 = average_temp[2];
   temp16_4 = average_temp[3];
   temp16_5 = average_temp[4];


   temp16_1 = (temp16_1 + temp16_2 + temp16_3 + temp16_4 + temp16_5);

    temp_avg = temp16_1/5;      //Media della temperatura
    sample_count++;
    sample_count %= 5;



   printf("%2.1f - %2.1f\n\r",temperature,temp_avg);


//      lcd_putc("\f");                   //Cancello display
      lcd_gotoxy(7,1);               //Mi posiziono alla spam 1,1
      sprintf(buffer,"%2.1f°C   ",temp_avg);//converto numero in stringa
      lcd_puts(buffer);             //invio stringa sul display


//      delay_ms(250);

    #asm
      nop
      nop
      nop
      nop
      nop
      nop
      nop
      nop
      nop
      nop
    #endasm










      }
         else{

         temperatura = -100;
           // printf("DS18B20 failed to initialize\r\n");
      }
    #asm
      nop
      nop
      nop
      nop
      nop
      nop
      nop
      nop
      nop
      nop
    #endasm


   }



}




void ResetDS1820 ( void )
    {
    output_low ( DS18B20_IO );         
    delay_us ( 480 );                               
    output_float ( DS18B20_IO );         
    delay_us ( 480 );                               
    }



int1 ds18b20_initialization(void){
   int1 ready;
   
   output_low(DS18B20_IO);
   delay_us(488);          //min 480us
   output_float(DS18B20_IO);
   delay_us(72);           //15 - 60us for device to respond
   ready=!input(DS18B20_IO);
   delay_us(424);
   return ready;
}

void ds18b20_write_bit(int8 b){
   output_low(DS18B20_IO);
   if(b == 1){
      delay_us(2);         //min 1
      output_float(DS18B20_IO);
   }
   delay_us(125);
   output_float(DS18B20_IO);
}

void ds18b20_write_byte(int8 B){
   int8 i, aux;
   
   for(i=0; i<8; i++){
      aux = B >> i;     //aux equals B shifted i times to the right
      aux &= 0x01;      //least significant bit survives
      ds18b20_write_bit(aux);
   }
   delay_us(120);
}

int1 ds18b20_read_bit(void){
   output_low(DS18B20_IO);
   delay_us(2);         //min 1us
   output_float(DS18B20_IO);
   delay_us(8);
   return(input(DS18B20_IO)); //next a delay of 60 to 120us must be done!
}

int8 ds18b20_read_byte(void){
   int8 i, result=0x00;
   
   for(i=0; i<8; i++){
      if(ds18b20_read_bit())
         result |= (0x01 << i);
      delay_us(125);
   }
   return result;
}





Code:


#include <18F4620.h>
#device ICD=TRUE
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV28                   //Brownout reset at 2.8V
#FUSES PUT                      //Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES DEBUG                    //Debug mode for use with ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled

#use delay(clock=10000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

micman2



Joined: 12 Mar 2007
Posts: 5

View user's profile Send private message

PostPosted: Sat Oct 25, 2008 6:29 am     Reply with quote

Debug sensor DS1820

Read 64bit ROM ......... OK!




-------------------------------------------------------------------

Debug sensor DS1820

Read Temp and SCRATCHPAD memory .........Ok



Last edited by micman2 on Sat Oct 25, 2008 6:34 am; edited 2 times in total
micman2



Joined: 12 Mar 2007
Posts: 5

View user's profile Send private message

PostPosted: Sat Oct 25, 2008 6:32 am     Reply with quote

Debug sensor DS18S20

Read 64bit ROM .............OK!




-------------------------------------------------------------------

Debug sensor DS1820

Read Temp and SCRATCHPAD memory..... Error temp LSB = 255 !!!!!


[/u]
ckielstra



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

View user's profile Send private message

PostPosted: Sat Oct 25, 2008 8:29 am     Reply with quote

Do I understand it correctly you have an DS1820 and a DS18S20 but your code is using the functions named for the DS18B20? This implies you copied the code from somewhere else.... not giving credits to the original author and you don't know what the limitations of the code are.

Code:
#use fast_io (B)

//ds18b20 pin
#define DS18B20_IO PIN_E0
You set fast_io for PortB but this port is never used in your code. Setting it for portE would have made sense but then you have to set the TRIS register on every I/O operation. Not recommended.

Code:
         ds18b20_write_byte(0x33);  //skip ROM command
0x33 is the command for reading the ROM code, not skipping. Try to write 'self documenting code', this will make your code easier to read and causes less of these copy/paste errors. For example you could have written this as:
Code:
#define CMD_READ_ROM    0x33
#define CMD_SKIP_ROM    0xCC

ds18b20_write_byte(CMD_READ_ROM);


Code:
         ds18b20_write_byte(0x4E);

         ds18b20_write_byte(125);
         ds18b20_write_byte(-55);
         ds18b20_write_byte(127);
This is code for the DS18B20 where you have to write 3 bytes. In your DS18S20 you should only write two bytes.

I haven't checked the rest of your code but as you copied the code from a 12-bit DS18B20 I suggest you check all commands to be valid for your 9-bit DS18S20.

Is your DS18S20 powered directly from 5V or is it powered using the 'parasitic' feature on the data line?
Also mention your compiler version number.
micman2



Joined: 12 Mar 2007
Posts: 5

View user's profile Send private message

PostPosted: Sat Oct 25, 2008 12:34 pm     Reply with quote

Hi,

The sensor is power with 5V No in parasite mode but standard mode.

I find this note in maxim-ic : difference on DS1820 and 18s20





there is no difference. but I doubt, if I send below 0 ° C sensor 18s20 this work.

May be faulty? I take in sample
ckielstra



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

View user's profile Send private message

PostPosted: Sat Oct 25, 2008 1:07 pm     Reply with quote

micman2 wrote:
I find this note in maxim-ic : difference on DS1820 and 18s20

<picture>

there is no difference.
The DS1820 and DS18S20 are equal, but my point is that (part) of your code is written for the DS18B20 which _is_ different.

Again, post your compiler version number.
micman2



Joined: 12 Mar 2007
Posts: 5

View user's profile Send private message

PostPosted: Sun Oct 26, 2008 2:31 am     Reply with quote

compiler ver is 4.074
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