View previous topic :: View next topic |
Author |
Message |
snooer Guest
|
DS18B20 and pic16f877a |
Posted: Wed Apr 22, 2009 11:09 am |
|
|
So the problem is that i get 0 in temp3 value all the time any ideas why?
This i my code
Code: | #include <16f877a.h>
#device *=16
#device adc=8
#FUSES NOWDT, HS, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock = 2000000)
#define ONE_WIRE_PIN PIN_A0
#include <lcd.c>
lcd_init();
void onewire_reset() // OK if just using a single permanently connected device
{
output_low(ONE_WIRE_PIN);
delay_us( 500 ); // pull 1-wire low for reset pulse
output_float(ONE_WIRE_PIN); // float 1-wire high
delay_us( 500 ); // wait-out remaining initialisation window.
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 ); // pull 1-wire low to initiate write time-slot.
output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire
delay_us( 60 ); // wait until end of write slot.
output_float(ONE_WIRE_PIN); // set 1-wire high again,
delay_us( 2 ); // for more than 1us minimum.
}
}
int onewire_read()
{
int count, data;
for (count=0; count<8; ++count)
{
output_low(ONE_WIRE_PIN);
delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
output_float(ONE_WIRE_PIN); // now let 1-wire float high,
delay_us( 8 ); // let device state stabilise,
shift_right(&data,1,input(ONE_WIRE_PIN)); // and load result.
delay_us( 120 ); // wait until end of read slot.
}
return( data );
}
float ds1820_read()
{
int8 busy=0, temp1, temp2;
signed int16 temp3,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 = temp3 / 16.0;
delay_ms(200);
return(result);
}
void main()
{
float temperatura;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
Temperatura = ds1820_read();
while(1)
{
Temperatura = ds1820_read();
lcd_init();
lcd_gotoxy(1,1);
printf(lcd_putc "Test: %3.1f", Temperatura);
}
delay_ms(150);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 22, 2009 11:24 am |
|
|
Quote: | #use delay(clock = 2000000) |
Your #use delay() statement is set for 2 MHz. Are you really using
a 2 MHz crystal ? That's very unusual.
If you are actually using a 20 MHz crystal then change the statement
above to be for 20 MHz. |
|
|
snooer
Joined: 22 Apr 2009 Posts: 14
|
|
Posted: Wed Apr 22, 2009 11:26 am |
|
|
It was mistype but it wont change a lot still getting 0 value. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
snooer
Joined: 22 Apr 2009 Posts: 14
|
|
Posted: Wed Apr 22, 2009 11:37 am |
|
|
Yep :( |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 22, 2009 12:51 pm |
|
|
Quote: | float ds1820_read()
{
int8 busy=0, temp1, temp2;
signed int16 temp3,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 = temp3 / 16.0;
delay_ms(200);
return(result);
} |
You have changed the code from the original driver routines:
http://www.ccsinfo.com/forum/viewtopic.php?t=28425
The original driver (in the link) declares 'result' as a 'float'. You changed
it to a 'signed int16'. This means that any 'temp3' value that is less than
16, will cause 'result' to be 0. That's because the floating point number
on the right side of this equation is truncated to be an int16 when it's
put into 'result':
Quote: | result = temp3 / 16.0; |
I suggest that you use the original driver code. |
|
|
snooer
Joined: 22 Apr 2009 Posts: 14
|
|
Posted: Thu Apr 23, 2009 5:13 am |
|
|
Thx for help. I solved the problem if some one needs working code of pic16f877a and ds18b20 pm me
Yea and i changed back to original code. from signed int16 to float for result |
|
|
|