Menu

Adskeeper - header widge

728x90

Search This Blog

468x60

Navbar

AdsKeeper - IAB Display Standard Ad Unit

Saturday, October 10, 2026

dsPIC30F2010 ds18B20 HD44780 LCD in MikroC

Introduction

The ds18S20/ds18B20 is one-wire precision digital temperature sensor. It implements only one data cable for command and data. The resolution of the ds18S20 is 9-bit while the ds18B20 is 12-bit higher resolution with floating point numbers.

dsPIC30F2010 ds18B20 HD44780 LCD in MikroC
A DS18B20 Sample from Ali Express

 

To interface with this sensor the master MCU program commonly uses the high speed Assembly language due to its fast response and its one-by-one instruction executions. However the manufactures of this chip (DALLAS) provide some sample programs written in C for the 8051-base devices.

Today using the Arduino is very common and ease of use for most of electronic hobbyists and engineers. There a lot of libraries and demo programs using this sensors. 

dsPIC30F2010 and ds18B20 Programming in MikroC

MikroC supports this one-wire digital temperature sensor for both the ds18S20 and the ds18B20. Unlike the Arduino libraries, using the MikroC One-Wire library, the master MCU need to process many steps such as writing command, reset, reading from scratch-pad, etc.

 

dsPIC30F2010 ds18B20 HD44780 LCD in MikroC
Schematic Diagram


In this example, the dsPIC30F2010 read the temperature data from a connected ds18B20 and it will be displayed on a character LCD. Both LCD and the sensor can be connect to any pins of an MCU upon the programming settings.

dsPIC30F2010 ds18B20 HD44780 LCD in MikroC
Edit Project

 

Timing is important for this one-wire sensor incorrect timing could cause command and data reading errors. The clock source is directly driven from an external 20MHz crystal oscillator without using Phase Lock Loop (PLL).

MikroC Source Code:

 

  1. // LCD module connections
  2. sbit LCD_RS at LATE0_bit;
  3. sbit LCD_EN at LATE1_bit;
  4. sbit LCD_D4 at LATE2_bit;
  5. sbit LCD_D5 at LATE3_bit;
  6. sbit LCD_D6 at LATE4_bit;
  7. sbit LCD_D7 at LATE5_bit;

  8. sbit LCD_RS_Direction at TRISE0_bit;
  9. sbit LCD_EN_Direction at TRISE1_bit;
  10. sbit LCD_D4_Direction at TRISE2_bit;
  11. sbit LCD_D5_Direction at TRISE3_bit;
  12. sbit LCD_D6_Direction at TRISE4_bit;
  13. sbit LCD_D7_Direction at TRISE5_bit;
  14. // End LCD module connections

  15. // Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
  16. // 18S20: 9 (default setting; can be 9,10,11,or 12)
  17. // 18B20: 12
  18. const unsigned short TEMP_RESOLUTION = 12;

  19. char *text = "000.0000";
  20. unsigned temp;

  21. void Display_Temperature(unsigned int temp2write) {
  22. const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  23. char temp_whole;
  24. unsigned int temp_fraction;

  25. // check if temperature is negative
  26. if (temp2write & 0x8000) {
  27. text[0] = '-';
  28. temp2write = ~temp2write + 1;
  29. }

  30. // extract temp_whole
  31. temp_whole = temp2write >> RES_SHIFT ;

  32. // convert temp_whole to characters
  33. if (temp_whole/100)
  34. text[0] = temp_whole/100 + 48;
  35. else
  36. text[0] = '0';

  37. text[1] = (temp_whole/10)%10 + 48; // Extract tens digit
  38. text[2] = temp_whole%10 + 48; // Extract ones digit

  39. // extract temp_fraction and convert it to unsigned int
  40. temp_fraction = temp2write << (4-RES_SHIFT);
  41. temp_fraction &= 0x000F;
  42. temp_fraction *= 625;

  43. // convert temp_fraction to characters
  44. text[4] = temp_fraction/1000 + 48; // Extract thousands digit
  45. text[5] = (temp_fraction/100)%10 + 48; // Extract hundreds digit
  46. text[6] = (temp_fraction/10)%10 + 48; // Extract tens digit
  47. text[7] = temp_fraction%10 + 48; // Extract ones digit

  48. // print temperature on LCD
  49. Lcd_Out(2, 5, text);
  50. }

  51. void main() {

  52. ADPCFG = 0xFFFF; // Configure AN pins as digital

  53. Lcd_Init(); // Initialize LCD
  54. Lcd_Cmd(_LCD_CLEAR); // Clear LCD
  55. Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
  56. Lcd_Out(1, 1, " Temperature: ");
  57. // Print degree character, 'C' for Centigrades
  58. Lcd_Chr(2,13,223); // different LCD displays have different char code for degree
  59. // if you see greek alpha letter try typing 178 instead of 223

  60. Lcd_Chr(2,14,'C');

  61. //--- main loop
  62. do {
  63. //--- perform temperature reading
  64. Ow_Reset(&PORTC, 14); // Onewire reset signal
  65. Ow_Write(&PORTC, 14, 0xCC); // Issue command SKIP_ROM
  66. Ow_Write(&PORTC, 14, 0x44); // Issue command CONVERT_T
  67. Delay_us(120);

  68. Ow_Reset(&PORTC, 14);
  69. Ow_Write(&PORTC, 14, 0xCC); // Issue command SKIP_ROM
  70. Ow_Write(&PORTC, 14, 0xBE); // Issue command READ_SCRATCHPAD
  71. Delay_ms(400);

  72. temp = Ow_Read(&PORTC, 14);
  73. temp = (Ow_Read(&PORTC, 14) << 8) + temp;

  74. //--- Format and display result on Lcd
  75. Display_Temperature(temp);

  76. Delay_ms(500);
  77. } while (1);
  78. }

 

LCD and One-Wire libraries are not include in project by default. So we need select these two drivers.

 

dsPIC30F2010 ds18B20 HD44780 LCD in MikroC
Go to View -> Library Manager

 

If we temperature reading program please check connection or set its correct resolutions - 9-bit for ds18S20 and 12-bit for ds18B20.

 

dsPIC30F2010 ds18B20 HD44780 LCD in MikroC
Program Testing on my DIY dsPIC30F2010 Prototype Board

 

 

 

 

No comments:

Post a Comment

Adskeep- Standard Ads

336x280

DIY Electronic Projects And Instruments

AdsKeeper - 2091654

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (36) Analog Comparator (1) Arduino (16) ARM (32) AT89C52 (7) ATMega32 (59) ATMega644P (27) AVR (88) AVR PCB (2) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (5) Display (151) Distance Sensor (3) dot matrix (1) ds1307 (10) DS18B20 (6) ds3231 (1) ds3232 (2) dsPIC (7) dsPIC30F1010 (3) dsPIC30F2010 (8) EEPROM (5) Environment Sensor (6) ESP32 (1) esp8266 (1) Graphical LCD (3) HD44780 (7) I2C (38) ILI9341 (1) Input/Output (82) Interrupt (22) IoT PCB (1) Keil (5) Keypad (16) KS0108 (3) LCD (81) LM35 (4) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) MikroC ARM (1) MikroC dsPIC (3) Module PCB (2) Motor (15) MPLABX (73) Nokia 5110 LCD (4) OLED (2) One-Wire (7) Oscillator (8) PCB (13) PCD8544 (3) PCF8574 (10) PIC (111) PIC12F (3) PIC16 (105) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (12) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PWM (12) RTC (12) SBN0064G (1) Sensor (15) SH1106 (3) Shift Register (14) Shift Registers (16) SN74HC595N (4) Software TWI (2) SPI (41) ST7735 (1) STM32 (29) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (23) STM32CubeIDE (32) STM32F103C8T6 (9) STM32F103R6 (18) STM32F401CCU6 (1) SysTick (7) temperature sensor (15) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (10) UART (10) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)