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

[RESOLVED]18f4550 and ds18x20

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



Joined: 24 May 2010
Posts: 10

View user's profile Send private message

[RESOLVED]18f4550 and ds18x20
PostPosted: Mon May 24, 2010 7:28 pm     Reply with quote

Hi folks,

I'm trying to communicate ds18s20/18b20 with pic18f4550 but I didn't implement. I've already searched all CCS forum pages but my issue is still alive.
When I start the ISIS simulation I'm just seeing -0.5 degree in LCD panel.

My sources as below;

Code:

/*///////////////////////////////////
main.c
////////////////////////////////////*/

#include <18F4550.h>

#device  ADC=10
#FUSES NOWDT                    //No Watch Dog Timer
//#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#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 NOMCLR                   //Master Clear pin disabled
//#fuses   NOWDT, INTRC_IO, MCLR, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD
#use     delay(clock=48000000)
#define use_portb_lcd True

#include <lcd.c>

#include "ds18b20.c"


void main(void)
{
   float temperature;
   lcd_init();

   while(1)
   {
      temperature = ds1820_read();
      printf(lcd_putc,"\f%3.1f%cC", temperature,223);


   }
 
}




Code:

/*///////////////////////////////////
ds18b20.c
////////////////////////////////////*/

#define ONE_WIRE_PIN PIN_A1

void onewire_reset()
{
   output_low(ONE_WIRE_PIN);
   delay_us( 500 ); //1-wire resetleme için sıfıra çekilir
   output_float(ONE_WIRE_PIN); //1-wire bir yapılır
   delay_us( 500 ); // sensörün hazırlanması için beklenir.
   output_float(ONE_WIRE_PIN);
}

void onewire_write(int data)
{
   int count;
   for (count = 0; count < 8; count++)
   {
      output_low(ONE_WIRE_PIN);
      delay_us( 2 ); // Sensöre yazma işlemine başlamak için  1-wire sıfıra çekilir.
      output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // yazılacak bilgi 1-wire'da
      delay_us( 60 ); // Yazma işlemi zamanı doldurulur.
      output_float(ONE_WIRE_PIN); // 1-wire bir yapılır,
      delay_us( 2 ); // 1us'den fazla beklenir.
   }
}

int onewire_read()
{
   int count, data;
   for (count = 0; count < 8; count++)
   {
      output_low(ONE_WIRE_PIN);
      delay_us(2); //Sensördem okuma işlemi içinl 1-wire sıfıra çekilir.
      output_float(ONE_WIRE_PIN); //1-wire bir yapılır,
      delay_us(8); // Sensörün kendine gelmesi beklenir,
      shift_right(&data, 1, input(ONE_WIRE_PIN)); // sonuc bilgisi alınır.
      delay_us(120); //Okuma işlemi zamanı doldurulur.
}
return( data );
}

float ds1820_read()
{
   int8 busy = 0, temp1, temp2;
   signed int16 temp3;   
   float result;
   
   onewire_reset();
   onewire_write(0xCC);
   onewire_write(0x44);
   
   while (busy == 0)
      busy = onewire_read();
     
   onewire_reset();
   onewire_write(0xCC);
   onewire_write(0xBE);
   
   temp1 = onewire_read();
   temp2 = onewire_read();
   temp3 = make16(temp2, temp1);
   
   result = (float) temp3 / 2.0;
   delay_ms(20);
   
   return result;
}


Any idea or help would be more than welcome

thanks & regards


Last edited by blackdragon on Tue May 25, 2010 3:37 pm; edited 2 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 24, 2010 8:04 pm     Reply with quote

You got that driver from the Code Library. It has a 3-page thread on
the ds18B20. If you read page 3, then you will see how it explains
about your problem with Proteus.

Read ishmael's post:
http://www.ccsinfo.com/forum/viewtopic.php?t=28425&start=34
Then read a little farther down, and he explains how to solve it.
blackdragon



Joined: 24 May 2010
Posts: 10

View user's profile Send private message

PostPosted: Mon May 24, 2010 11:54 pm     Reply with quote

thanks for reply PCM programmer, but same problem still counting

any idea?
blackdragon



Joined: 24 May 2010
Posts: 10

View user's profile Send private message

PostPosted: Tue May 25, 2010 12:40 pm     Reply with quote

folks I solved my problem so I changed some code blocks now it works.

::::::::::::::::: ds18b20 :::::::::::::::::::::::::
Code:


#define DS1820_WIRE_PIN                PIN_A1   //ds1820 data bacağı
//sensör için  sabitler
#define DS1820_SKIP_ROM                0xCC
#define DS1820_READ_SCRATCHPAD         0xBE
#define DS1820_CONVERT_T               0x44


static char g_shiftBit, g_dataOut;
static long g_temperature, g_dataInput;


void resetDS1820(void)
{
   output_low(DS1820_WIRE_PIN);     //1-wire resetleme için sıfıra çekilir
   delay_us(500);                   // sensörün hazırlanması için beklenir.
   output_float(DS1820_WIRE_PIN);   //1-wire bir yapılır
   delay_us(500);                   // sensörün hazırlanması için beklenir.

}

void writeDS1820(void)
{
   for (g_shiftBit = 1; g_shiftBit <= 8; ++g_shiftBit)
   {
      output_low(DS1820_WIRE_PIN);                                    // Sensöre yazma işlemine başlamak için  1-wire sıfıra çekilir.
      delay_us(2);
      output_bit(DS1820_WIRE_PIN, shift_right(&g_dataOut, 1, 0));    // yazılacak bilgi 1-wire'da
      delay_us(60);                                                  // Yazma işlemi zamanı doldurulur.
      output_float(DS1820_WIRE_PIN);                                 // 1-wire bir yapılır,
      delay_us(2);                                                   // 1us'den fazla beklenir.
   }
}

void readDS1820(void)
{
   g_dataInput = 0;
   for ( g_shiftBit = 1; g_shiftBit <= 16; ++g_shiftBit )
   {
      output_low(DS1820_WIRE_PIN);                                   //Sensörden okuma işlemi içinl 1-wire sıfıra çekilir.
      delay_us(2);                                                   
      output_float(DS1820_WIRE_PIN);                                //1-wire(data) bir yapılır,
      delay_us(8);                                                  // Sensörün kendine gelmesi beklenir,
      shift_right ( &g_dataInput, 2, input ( DS1820_WIRE_PIN ));   // sonuc bilgisi alınır
      delay_us(120);                                              //Okuma işlemi zamanı doldurulur.
   }
 
}
void waitForConversion (void)             
{
    while(TRUE)
    {
       output_low(DS1820_WIRE_PIN);
       delay_us(5);
       output_float(DS1820_WIRE_PIN);
       delay_us(5);
       if (input(DS1820_WIRE_PIN) == 1)   
       {
           break;
       }
       delay_us(55);
     }
}


long ds1820_read()
{
   
    resetDS1820();
    g_dataOut = DS1820_SKIP_ROM;
    writeDS1820();
    g_dataOut = DS1820_CONVERT_T;
    writeDS1820();
    waitForConversion();

    resetDS1820();
    g_dataOut = DS1820_SKIP_ROM;
    writeDS1820();
    g_dataOut = DS1820_READ_SCRATCHPAD;
    writeDS1820();
    readDS1820();
    g_temperature = g_dataInput / 16; //ds18s20 için bu değer 2 olacaktır

   
   return g_temperature;
}


::::::::::::::: main.c :::::::::::::::::::
Code:

/*///////////////////////////////////
main.c

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

#include <18F4550.h>

#FUSES  NOPROTECT, NOPUT, NOWDT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NOMCLR

#use   delay(clock=4000000)
#define use_portb_lcd True

#include <lcd.c>
#include "ds18b20.c"


void main(void)
{
   float temperature;
   lcd_init();
     printf(lcd_putc,"test");
     
   while(1)
   {
      temperature = ds1820_read();
      printf(lcd_putc,"\f%3.1f%cC", temperature, 223);
   }

}
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