• AVR Freaks

Helpful ReplyHot!DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem

Author
ceejay
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2018/10/25 23:12:35
  • Location: 0
  • Status: offline
2019/01/27 12:50:58 (permalink)
0

DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem

Hello! I am new to PIC programming and I decided to interface waterproof digital temperature sensor to PIC 16f18426. I read some posts in the web, including threads here about this sensor and other pic microcontrollers. The problem is: first, the reading in lcd is one line full of block lines. Second, I used the debug tool in mplab, inserting a breakpoint after the read_byte() command in main.c. However, results show my variables are null. The code that I am using is attached to this post. I hope you can help me. Thank you.
post edited by ceejay - 2019/01/31 20:43:33
#1
Mysil
Super Member
  • Total Posts : 3324
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/27 17:38:15 (permalink) ☄ Helpfulby ceejay 2019/01/27 23:51:18
+2 (2)
Hi,
The problem example code in message #1 is incomplete, and cannot be compiled.
There are 2 include files referenced, but not provided.
 
LCD character display that show line full of blocks, may be because of wrong LCD voltage,
or not successfully initialized.
Delays used during LCD initialization are far too short,
and delays used for toggling enable signal during data transfer, are longer than needed.
See Datasheet for Hitachi HD44780 display controller.
The 'for...'  loop used for delays in LCD routines, will depend on system clock frequency.
 
The Dallas one wire temperature sensor depend on specific timing for communication to work.
Do you have som way to verify that signal and timing are as expected?
Some kind of oscilloscope or logic analyzer will be helpful when trying to understand if signaling work as expected.
 
    Mysil
 
#2
ceejay
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2018/10/25 23:12:35
  • Location: 0
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/27 20:45:27 (permalink)
0
Hi Mysil! Thanks for the reply! I forgot to include the header file. I'll try to verify the timing using oscilloscope. I'll get back to you on this. Thanks.
#3
Mysil
Super Member
  • Total Posts : 3324
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/27 21:39:30 (permalink) ☄ Helpfulby ceejay 2019/01/27 23:51:11
+1 (1)
Hi,
There is a mistake in setting up ANSEL control.
Ansel bit should be Cleared for pin RA2 to enable digital input.
 
    Mysil
#4
ceejay
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2018/10/25 23:12:35
  • Location: 0
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/28 20:48:51 (permalink)
0
Hi Mysil! the reading error of my sensor has now been resolved. When I used the debug tool, the microcontroller can read the temperature. The problem is the setting up of ANSEL control. I interchanged the definition, that's why it's wrong. Now, I'll have to work on my LCD. You said that "delays used during LCD initialization are far too short, and delays used for toggling enable signal during data transfer, are longer than needed. " The data sheet showed the initialization part, but I am having problem during the data transfer. Are you suggesting that I remove the for loop for delays? What will be my basis for the delays during data transfer? 
Thanks
 
 
#5
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/28 20:56:46 (permalink)
+1 (1)
The data sheet for your LCD should list all the required delays.
As mentioned, it's a good idea to make the delays bigger than the minimum specified during initialisation, but you can just use the minimum required once it is initialised.
 

Nearly there...
#6
ceejay
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2018/10/25 23:12:35
  • Location: 0
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/30 21:46:44 (permalink)
0
Hello all, I have addressed the problem of LCD. However, I think the reading is wrong. It reads 0 or 8. I think the temperature is around 24 degree celcius. I changed my code to this, just like one of the posts here in microchip:
 
onewire_reset();
write_byte(write_scratchpad);
write_byte(resolution_12bit);
onewire_reset();
write_byte(skip_rom);
write_byte(convert_temp);
__delay_ms(750);
onewire_reset();
write_byte(skip_rom);
write_byte(read_scratchpad);

for (k=0;k<9;k++)
{
getBit[k] = read_byte();
}

temp_msb = getBit[1];
temp_lsb = getBit[0];

temp = (temp_msb << 8) + temp_lsb;
temp = temp >> 4;

sprintf(buf3, "%d", temp);

Lcd_Set_Cursor(1,1);
Lcd_Write_String("Temperature");
Lcd_Set_Cursor(2,1);
Lcd_Write_String(buf3);
__delay_ms(1000);
post edited by ceejay - 2019/01/30 21:50:32
#7
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/30 22:12:18 (permalink)
+1 (1)
Hard to comment when you only post a snippet of your code, missing all the variable and constant definitions.
 

Nearly there...
#8
ceejay
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2018/10/25 23:12:35
  • Location: 0
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/31 03:41:09 (permalink)
0
Sorry sir/ma'am. I can't upload the file here, just comment.
#9
Mysil
Super Member
  • Total Posts : 3324
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/31 05:59:03 (permalink)
+2 (2)
Hi,
You were able to upload the .c  source files.
There are restrictions in forum software or firewall, so .h  header files are not allowed,
but it is possible to make a copy, rename it to a .txt  file.
Another possibility is to use the 'Package' facility in MPLAB X to collect all files needed to restore a project.
 
    Mysil
#10
ceejay
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2018/10/25 23:12:35
  • Location: 0
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/01/31 20:45:43 (permalink)
0
Hi! I decided to reupload the files in the original post. As I have said in the previous comment, the reading of my pic microcontroller is wrong, and I can't find the error in this. 
 
Thank you. 
#11
Mysil
Super Member
  • Total Posts : 3324
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/02/01 02:54:11 (permalink)
+1 (1)
Hi,
When making completely different code, why replacing code attached the original message, with something completely different?
Following progression of a thread, is easier if revised code is attached with the current message in the thread.
 
Then, if you are finding or borrowing code written by someone else, why aren't there any comments with reference to origin or author?
 
Include files are still missing.
 
    Mysil
#12
ceejay
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2018/10/25 23:12:35
  • Location: 0
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/02/03 20:43:52 (permalink)
0
Hi Mysil!
 
I humbly apologize for my shortcomings and thank you for pointing it out. I did that because I couldn't upload the files here in the comment section of the thread. Also, I will include the reference of the codes that I used.
#13
qhb
Superb Member
  • Total Posts : 9998
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/02/03 20:48:47 (permalink)
+1 (1)
ceejay
...
I did that because I couldn't upload the files here in the comment section of the thread.

You need to click on "Open Full Version" just above the "Quick Reply" window to get the upload button etc.
 

Nearly there...
#14
ceejay
New Member
  • Total Posts : 30
  • Reward points : 0
  • Joined: 2018/10/25 23:12:35
  • Location: 0
  • Status: offline
Re: DS18B20 (Digital Temperature Sensor) with PIC microcontroller: reading problem 2019/02/26 19:57:56 (permalink)
#15
Jump to:
© 2019 APG vNext Commercial Version 4.5