View previous topic :: View next topic |
Author |
Message |
bolke
Joined: 16 Apr 2009 Posts: 5
|
DS18B20 initializes but doesn't measure. |
Posted: Thu Apr 16, 2009 5:58 am |
|
|
I'm using pic18f2420 in combination with a DS18B20.
I've searched the forums and I've found a whole lot of
examples, except they don't work. Probably a small problem,
but I'm not seeing it.
The DS18B20 is connected in normally powered mode.
I'm able to read the scratchpad and write a configuration
value in it.
Except when I want to read a temprature value, it doesn't
work. I'm only getting the default 0x50 and 0x05 values that
are in the MSB and LSB of the temprature. So it isn't a formatting
error.
It seems like the chip is skipping the temprature convert command, not pulling the line low while reading temprature.
The pin used is PIN_B7. This one is #defined as TEMP.
Ccs version: 4.068
Fuses:
Code: |
#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 BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES PUT //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 IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
|
The code:
Code: |
void tmp_reset(){
output_low(TEMP);
delay_us(480);
output_float(TEMP);
delay_us(480);
}
void write1(){
output_low(TEMP);
delay_us(2);
output_float(TEMP);
delay_us(62);
}
void write0(){
output_low(TEMP);
delay_us(60);
output_float(TEMP);
delay_us(2);
}
void tmp_write(int8 value){
int i;
for(i = 0; i < 8; i++){
if (shift_right(&value,1,0))
write1();
else
write0();
}
}
int8 tmp_read(){
int i;
int result = 0;
for(i = 0; i < 8; i++){
output_low(TEMP);
delay_us(2);
output_float(TEMP);
delay_us(8);
shift_right(&result,1,input(TEMP));
delay_us(120);
}
return result;
}
float tmp_read_temp()
{
int i;
int busy=0, temp1, temp2;
signed int16 temp3;
float result;
output_float(TEMP);
tmp_reset();
tmp_write(0xCC); // skip rom
tmp_write(0x44); // get temprature;
output_float(TEMP);
while(!tmp_read()); // wait till pullup
tmp_reset();
tmp_write(0xCC); // skip rom
tmp_write(0xBE); // read sketchpad
for(i = 0; i < 9; i++){
printf("*1value1:%u*\r\n",tmp_read()); // print it
}
tmp_reset();
return 0;
}
|
And a main.
Code: |
void init(){
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF); setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_XMIT_L_TO_H|SPI_CLK_DIV_16);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_8 );
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);
set_timer1(25000);
enable_interrupts(INT_RDA);
disable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
}
void main()
{
init();
while(true){
tmp_read_temp();
delay_ms(10);
}
}
|
Last edited by bolke on Fri Apr 17, 2009 1:25 am; edited 4 times in total |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Apr 16, 2009 6:27 am |
|
|
For start I'd say clean up your code. There is so much temporary test code present that I don't want to spend time looking into it. For example: Code: | void tmp_reset(){
output_low(TEMP);
delay_us(480);
output_float(TEMP);
delay_us(60);
//printf("#low[%u]*\r\n",input(TEMP));
delay_us(15);
output_float(TEMP);
delay_us(180);
output_float(TEMP);
} | is equivalent to: Code: | void tmp_reset(){
output_low(TEMP);
delay_us(480);
output_float(TEMP);
delay_us(255);
} |
Post a cleaned up and complete test program including main and #fuses.
Post your compiler version number. |
|
![](templates/subSilver/images/spacer.gif) |
bolke
Joined: 16 Apr 2009 Posts: 5
|
|
Posted: Thu Apr 16, 2009 9:09 am |
|
|
Code has been cleaned, and information added, as per request. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Apr 16, 2009 12:00 pm |
|
|
I'm not at home so can't run the code in a simulator but I don't see any obvious errors.
The code as posted contains a syntax error making it not the exact same code as you have tested with...
Double check you are using the DS18B20 and not the DS18B20P. The Parasitic power version apparently gives the same results you are seeing.
http://www.ccsinfo.com/forum/viewtopic.php?t=28425&start=17
Note: are you sure to need the LVP fuse? 99% of the programmers are high voltage types. If you are not using an LVP-programmer set the fuse to NOLVP to avoid problems with the processor resetting on voltage spikes on pin B5. |
|
![](templates/subSilver/images/spacer.gif) |
bolke
Joined: 16 Apr 2009 Posts: 5
|
|
Posted: Fri Apr 17, 2009 2:25 am |
|
|
It's a DS18B20 , at least as far as I can see. On the chip it
says: DALLAS DS18B20 0838C3 +161AE.
A normal DS18B20 it seems.
I've removed the fuse, I'm not using it indeed.
Syntax error also removed. A bit of an copy paste error. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Apr 17, 2009 9:37 am |
|
|
The DS18B20-PAR is clearly marked as a DS18B20P. You have the normal DS18B20.
Don't remove the LVP fuse but replace it by NOLVP. I'm not sure what the default value is so I always like to make these settings specific.
Try replacing the line: Code: | while(!tmp_read()); // wait till pullup | by Code: | while (tmp_read() == 0xFF); // wait till pullup |
This is the opposite of most example code I see here in the forum. Maybe someone else can confirm this to be correct? In the Proteus simulator the posted code works with this change. |
|
![](templates/subSilver/images/spacer.gif) |
bolke
Joined: 16 Apr 2009 Posts: 5
|
|
Posted: Mon Apr 20, 2009 1:17 am |
|
|
Nope, not working.
The chip still returns the 0x50 / 0x05 values.
I'll see if I can get another chip, maybe that this
one's half-dead.
If there's anyone out there witha ds18b20, could
you confirm that the code above works? (as in
reads a temprature, not compile works ) ... |
|
![](templates/subSilver/images/spacer.gif) |
|