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

I need help for DS18B20

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



Joined: 20 Apr 2008
Posts: 6

View user's profile Send private message

I need help for DS18B20
PostPosted: Sun Apr 20, 2008 12:38 pm     Reply with quote

Hi all,

I am using Pic16f877A and I wanna use DS18B20 sensor with it. I have been searcing about this for a long time I had many codes about that but I havent managed to work any of them yet.

here my last sample codes are;

1- for reading DS18B20;

int get_temp(void)
{
touch_present();
touch_write_byte(0xCC);
touch_write_byte(0x44);
delay_ms(0.4);
touch_present();
touch_write_byte(0xCC);
touch_write_byte(0xBE);
return touch_read_byte();
}


2- one wire communication

#ifndef TOUCH_PIN
#define TOUCH_PIN PIN_C0
#if defined(__PCH__)
#bit TOUCH_PIN_BIT = 0xF8C.0
#else
#bit TOUCH_PIN_BIT = 6.0
#endif
#endif


BYTE touch_read_byte() {
BYTE i,data;

for(i=1;i<=8;++i) {
output_low(TOUCH_PIN);
delay_us(14);
output_float(TOUCH_PIN);
delay_us(5);
shift_right(&data,1,input(TOUCH_PIN));
delay_us(100);
}
return(data);
}

BYTE touch_write_byte(BYTE data) {
BYTE i;

for(i=1;i<=8;++i) {
output_low(TOUCH_PIN);
delay_us(10);
if(shift_right(&data,1,0)) {
output_high(TOUCH_PIN);
delay_us(10);
if(!TOUCH_PIN_BIT)
return(0);
} else {
output_low(TOUCH_PIN);
delay_us(10);
if(TOUCH_PIN_BIT)
return(0);
}
delay_us(50);
output_high(TOUCH_PIN);
delay_us(50);
}
return(TRUE);
}

BYTE touch_present() {
BOOLEAN present;

output_low(TOUCH_PIN);
delay_us(500);
output_float(TOUCH_PIN);

delay_us(5);

if(!input(TOUCH_PIN))
return(FALSE);
delay_us(65);
present=!input(TOUCH_PIN);
delay_us(240);
if(present)
return(TRUE);
else
return(FALSE);
}


I ll be so happy ıf anyone can say where my error is or send me a worked one.
thanks very much in advance..
ckielstra



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

View user's profile Send private message

PostPosted: Sun Apr 20, 2008 2:30 pm     Reply with quote

When posting code please use the 'code' buttons to preserve the layout of your program. Easier reading of your program will get you quicker and better answers.

You are not allowed to post the code of the CCS drivers in this forum. We all have these drivers so it will do when you post the name of the driver you used.


Code:
delay_ms(0.4);
Float values are not accepted as a parameter and this will be truncated to 0ms, so not waiting at all.
Better is to not have a fixed delay time but to read the DS18B20 status until it signals the conversion to be ready.

Another problem is that the DS18B20 starts up in 12-bit mode while you are only reading a single byte.

Code:
signed int16 get_temp(void)
{
  int8 msb, lsb;
 
  // Start temperature conversion
  touch_present();
  touch_write_byte(0xCC);
  touch_write_byte(0x44);
 
  // Wait for temperature conversion to complete.
  while (touch_read_byte() == 0xFF);

  // Read temperature
  touch_present();
  touch_write_byte(0xCC);
  touch_write_byte(0xBE);
 
  lsb = touch_read_byte();
  msb = touch_read_byte();

  return make16(msb, lsb);
}


The returned value is in 0,125C steps, so to print the value you have to convert it to float first:
Code:
float Temp;

Temp = 0.125 * get_temp();
printf("Temperature = %f\n", Temp);
or if you want to avoid the large overhead involved with float calculations you can use:
Code:
signed int16 Temp16;
int8 TempDegrees, TempDecimals;

Temp16 = get_temp();
TempDegrees = Temp16 >> 4;
TempDecimals = ((Temp16 & 0x0F) * 10) / 16;
printf("Temperature = %d.%d\n", TempDegrees, TempDecimals);


Also check out the other recent thread about the DS18B20: http://www.ccsinfo.com/forum/viewtopic.php?t=34372&postdays=0&postorder=asc&start=15

Edit 21-Apr-2008: changed int16 to signed int16 in example code.


Last edited by ckielstra on Mon Apr 21, 2008 7:30 am; edited 1 time in total
electro22



Joined: 20 Apr 2008
Posts: 6

View user's profile Send private message

Thanks
PostPosted: Mon Apr 21, 2008 4:02 am     Reply with quote

Thanks for your advice I am new here thats why ı dont know well the rules..
by the way I did not manage to work the codes which u sent me..It gave errors again. I dont know what ı can do more. Crying or Very sad
ckielstra



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

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 7:28 am     Reply with quote

What is the error text you get?
Post a complete but short program showing your problems.
electro22



Joined: 20 Apr 2008
Posts: 6

View user's profile Send private message

error message
PostPosted: Mon Apr 21, 2008 8:06 am     Reply with quote

the error message: STDOUT not defined maybe missing #use RS232 in call one


I cant understand that because I am not using RS232 in anywhere in my code.

By the way thanks again
ckielstra



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

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 8:35 am     Reply with quote

In my examples I used the printf command. If you don't specify a destination then printf will send the data to the STDOUT port, this defaults to RS232. If you don't have RS232, and you didn't specify another port for STDOUT, than this explains your error.

Just wondering, how are you going to present the temperature to the user?
electro22



Joined: 20 Apr 2008
Posts: 6

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 8:49 am     Reply with quote

I am sending data to LCD display and LCD is connecting to B port. Sorry maybe I am little stupid at this but still I cant get how I work that out?
ckielstra



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

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 9:46 am     Reply with quote

Are you using lcd.c as the LCD-driver?

If yes, than you can show the temperature with:
Code:
float Temp;

Temp = 0.125 * get_temp();
printf(lcd_putc, "\fCurrent Temp: %f", Temp);


Note the '\f' which has a special meaning in the LCD-driver: it clears the display.
electro22



Joined: 20 Apr 2008
Posts: 6

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 10:05 am     Reply with quote

yes I am using LCD.C at the begining. I did all things you said. It doesnt give any error But in Proteus, still LCD is not showing any temperature.
I used also the different one wire code, Ds18B20 and main code but they show only one value and it is wrong it is not the right temperature. I got these codes from Code library.

What u think Can ı manage to work the code what we have been talking or Do ı have to change them?
ckielstra



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

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 10:19 am     Reply with quote

Many things can be wrong, without code example I can't help you any further.
Post your main.c and the code for reading the DS18B20.

What is your compiler version number?
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