Sunday, December 24, 2023

PIC16F887 DS18B20 LCD Example Using XC8

Overview

For a micro-controller that doesn't have a analog to digital converter module, the designer can choose any others temperature sensor that use any communication method, I2C, SPI, or even 1-Wire serial communication interface. A 1-Wire digital thermometer is very popular due to its low cost, multiples device communication, low pins count (a few wires).

PIC16F887 DS18B20 LCD Example Using XC8
Prototype Board Test

DS18X20 temperature sensor is in common now. Using only one data pin the controller able to control up to 127 DS18X20 devices on a single bus with distinct device unique ID (ROM). Here I use a DS18B20 temperature sensor to interface with a Microchip PIC16F887 micro-controller. I don't write any more details here you check it in previous post.

PIC16F887 DS18B20 LCD Example Using XC8
DS18B20 Pin Configurations

This device requires only three wires GND, DQ, and VDD, or ever two wires mode (parasitic power supply). These three wires will share with other 1-Wire devices for multiple device connections. 

MPLABX IDE and XC8 Programming

Arduino platform has various libraries for this sensor. Other C compilers such as MikroC, CCS PICC also has their own libraries for this device. However I write my own libraries follow the original application note created by the device manufacture. 

MPLABX IDE and XC8 C compiler for 8-bit PIC micro-controller is free to use (without code optimization). 

In this example, I use my own 8-bit PIC Prototyping Board to test this program. It has an on-board 16x2 LCD module (TINSHARP TC1604A-01).

PIC16F887 DS18B20 LCD Example Using XC8
TC1604A-01 Front

PIC16F887 DS18B20 LCD Example Using XC8
TC1604A-01 Back View

PIC16F887 doesn't require an external crystal oscillator. It's operated by its internal RC oscillator with a selected 8MHz clock frequency.

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on December 24, 2023, 5:56 PM
  6.  * PIC16F887 DS18B20 and LCD Example
  7.  * MPLABX IDE v6.15
  8.  * XC8 v2.36
  9.  */
  10.  
  11.  
  12. #include <xc.h>
  13. #include "config.h"
  14. #include "lcd.h"
  15. #include "ds18b20.h"
  16.  
  17. #define _XTAL_FREQ 8000000UL
  18.  
  19. unsigned char byte[10];
  20. unsigned char TH=0,TL=0;
  21.  
  22. void sensorRead(void){
  23. ow_reset();
  24. writeByte(0xCC);
  25. writeByte(0x44);
  26. __delay_us(104);
  27. ow_reset();
  28. writeByte(0xCC);
  29. writeByte(0xBE);
  30. for(int i=0;i<9;i++) byte[i]=readByte();
  31. TH=byte[1];
  32. TL=byte[0];
  33. __delay_ms(10);
  34. }
  35.  
  36. void main(void) {
  37. OSCCONbits.IRCF=7;
  38. PORTD=0;
  39. TRISD=0;
  40. lcdInit(); lcdString("PIC16F887 LCD");
  41. lcdXY(1,2); lcdString("DS18B20 1-Wire");
  42. lcdXY(1,3); lcdString("Programming With");
  43. lcdXY(1,4); lcdString("MPLABX IDE XC8");
  44. __delay_ms(2500);
  45. lcdCommand(0x0C);
  46. lcdCommand(0x01);
  47. __delay_ms(5);
  48. lcdXY(3,1); lcdString("Temperature:");
  49. lcdXY(1,3); lcdString("DS18B20 Connects");
  50. lcdXY(1,4); lcdString("Pin RD7.");
  51. int temp=0, fraction=0;
  52. while(1){
  53. sensorRead();
  54. temp=(TH<<8)+TL;
  55. TH=temp>>4;
  56. lcdXY(1,2); lcdString("1Wire: ");
  57. if(temp&0x8000){
  58. temp=~temp+1;
  59. TH=temp>>4;
  60. TH&=0x7F;
  61. lcdData('-');
  62. lcdData(48+TH/10);
  63. lcdData(48+TH%10);
  64. }else{
  65. lcdData((TH>=100)?(48+TH/100):' ');
  66. lcdData((TH>=10)?(48+(TH%100)/10):' ');
  67. lcdData(48+TH%10);
  68. }
  69. fraction=temp&0x000F;
  70. fraction*=625;
  71. lcdData('.');
  72. lcdData(48+fraction/1000);
  73. lcdData(48+(fraction%1000)/100);
  74. lcdData(223);
  75. lcdData('C');
  76. __delay_ms(500);
  77. }
  78. return;
  79. }
  80.  

Without using C floating point and sprintf() function, it saves ROM and RAM usage. 

PIC16F887 DS18B20 LCD Example Using XC8
MPLABX IDE Dashboard
 

Proteus can be used for schematic design, or even circuit simulation.

PIC16F887 DS18B20 LCD Example Using XC8
Initialization

 
PIC16F887 DS18B20 LCD Example Using XC8
Temperature Reading In Simulator

Click here to download its source file. I use MPLABX IDE v6.15 and XC8 C compiler v2.36 free edition.

PIC16F887 DS18B20 LCD Example Using XC8

 
PIC16F887 DS18B20 LCD Example Using XC8
Temperature Reading

This old DIY prototyping board works well easing component placement board breadboard.




No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)