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

 

 

 

 

dsPIC30F2010 4-bit HD44780 LCD in MikroC

Introduction 

The HD44780 character LCD controller still popular among micro-controller programmer today even in small electronic control projects. Currently there are a lot of Chinese equivalent LCD controller that is very low cost and low power.

 

dsPIC30F2010 4-bit HD44780 LCD in MikroC
MikroC LCD Example

It has 8-bit data bus with three control signal - Register Select (RS), Read or Write (R/W) and Enable (E). However it operate is a single 8-bit data transfer mode or 4-bit (two times) data transfer mode. Using its 4-bit data transfer mode is very common today. The 8-bit data is divided into two nibbles, higher nibble is transferred first and then the lower nibble.

 

STM32F103R6 MikroC 4-bit HD44780 LCD Example
A 16x4 HD44780 Character LCD

 

MikroC Programming for HD44780 and dsPIC30F2010 

MikroC for dsPIC has an LCD driver for HD44780 that operates in 4-bit data transfer mode with select-able control pins and data pins (D4:D7). The R/W pin must wires to GND as the master MCU only need to send commands or data.

 

dsPIC30F2010 4-bit HD44780 LCD in MikroC
MikroC LCD Example

It does not need a schematic as we can read and understand the C source codes.

 

dsPIC30F2010 4-bit HD44780 LCD in MikroC
Schematic Diagram

 

I don't need to write my codes due to time constraint. I use the MikroC LCD example in its library manual. There are some major routines in MikroC:

Library Routines

 

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. char txt1[] = "mikroElektronika";
  16. char txt2[] = "EasydsPIC4A";
  17. char txt3[] = "Lcd4bit";
  18. char txt4[] = "example";

  19. char i; // Loop variable

  20. void Move_Delay() { // Function used for text moving
  21. Delay_ms(500); // You can change the moving speed here
  22. }

  23. void main(){
  24. ADPCFG = 0xFFFF; // Configure AN pins as digital I/O

  25. Lcd_Init(); // Initialize LCD

  26. Lcd_Cmd(_LCD_CLEAR); // Clear display
  27. Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
  28. Lcd_Out(1,6,txt3); // Write text in first row

  29. Lcd_Out(2,6,txt4); // Write text in second row
  30. Delay_ms(2000);
  31. Lcd_Cmd(_LCD_CLEAR); // Clear display

  32. Lcd_Out(1,1,txt1); // Write text in first row
  33. Lcd_Out(2,5,txt2); // Write text in second row

  34. Delay_ms(2000);

  35. // Moving text
  36. for(i=0; i<4; i++) { // Move text to the right 4 times
  37. Lcd_Cmd(_LCD_SHIFT_RIGHT);
  38. Move_Delay();
  39. }

  40. while(1) { // Endless loop
  41. for(i=0; i<8; i++) { // Move text to the left 7 times
  42. Lcd_Cmd(_LCD_SHIFT_LEFT);
  43. Move_Delay();
  44. }

  45. for(i=0; i<8; i++) { // Move text to the right 7 times
  46. Lcd_Cmd(_LCD_SHIFT_RIGHT);
  47. Move_Delay();
  48. }
  49. }
  50. }


 

 

 

dsPIC30F2010 MikroC I/O Programming

Introduction

The dsPIC30F2010 digital signal controller can be programmed using Assembly language that's free of charge from device's supplier. Currently, using a high level programming language is common for most of modern micro-controllers. Unlike a few decade ago, modern micro-controllers have a large amount of ROM (commonly Flash memory), SRAM, and riches of peripherals and functionalities. Due its larger size in RAM and ROM, using a high level programming language such as C, Basic, Pascal, micro-Python are not a problem anymore. 

For the 16-bit PIC micro-controller families, there are a lot of C compilers including free or paid versions. Microchip Technology offers many C compilers for their 8-bit, 16-bit and 32-bit MCUs. There free and paid version. However, using a free version has some limited functionalities especially memory optimization.

Paid C compilers for this chip are widely available from many companies such as Custom Computer Service (CSS), MikroElectronika MikroC Pro for dsPIC, IAR Embedded Workbench, etc. However MikroC for dsPIC offers a free version for students and hobbyists with a code compiling limits to below 2KB. That's suitable for most of light-weight applications. 

MikroC for dsPIC has a lot of build-in peripheral and hardware libraries. For additional C libraries the user can gets it from LibStocks that contains a lot libraries created by many users. 

 

dsPIC30F2010 LED Blinking in MikroC for dsPIC 

 

Blinking an LED is most common for most of novice embedded system  programmers. In MikroC we can do it from scratch. I use my own dsPIC30F2010/dsPIC30F1010 prototype board to test this program. It has on-board LEDs, push buttons, PICKit2 ICSP and 20MHz crystal oscillator. That's no need for auxiliary components.

 

dsPIC30F2010 MikroC I/O Programming
LED on at RD0

 

In this introductory example, the dsPIC30F2010 toggles its output LED connects to RD0 every one second. The clock frequency is driven from the external crystal oscillator without additional Phase Lock Loop (PLL) enabled. 

Schematic Diagram:

 

dsPIC30F2010 MikroC I/O Programming
Schematic Diagram in Proteus

 

Proteus VSM does not has a model for dsPIC30F series. I need to download this symbol (schematic only) from SnapEDA for free.

 

MikroC Source Code:

 

  1. void main() {
  2. ADPCFG = 0xFFFF; // configure AN pins as digital
  3. TRISD = 0; // set direction to be output
  4. LATD = 0; // turn OFF the PORTD leds
  5. while(1){
  6. LATD=0;
  7. Delay_ms(1000);
  8. LATD=0x01;
  9. Delay_ms(1000);
  10. }
  11. }

 

It will blinks D8 LED connects to RD0 via a DIP switch. 

Don't forget to set some parameters for this projects by going to Project -> Edit Project and select as below.

 

dsPIC30F2010 MikroC I/O Programming
Project Setting

 I use an old 20-year-old DIY PICKit2 Programmer to program this chip. it look likes silly :D.

 

dsPIC30F2010 MikroC I/O Programming
PICKit2 ICSP Programmer

 

In MikroC for dsPIC, programming is quite simple and easier than MPLabX XC16 Compiler. 

 

dsPIC30F2010 MikroC I/O Programming
PICKit2 ICSP Programmer

 The compiler outputs are not only Intel HEX file but also an Assembly code as shown below.

 


  1. _main:
  2. MOV #2048, W15
  3. MOV #6142, W0
  4. MOV WREG, 32
  5. MOV #1, W0
  6. MOV WREG, 52
  7. MOV #4, W0
  8. IOR 68

  9. ;blink_dev.c,1 :: void main() {
  10. ;blink_dev.c,2 :: ADPCFG = 0xFFFF; // configure AN pins as digital
  11. MOV #65535, W0
  12. MOV WREG, ADPCFG
  13. ;blink_dev.c,3 :: TRISD = 0; // set direction to be output
  14. CLR TRISD
  15. ;blink_dev.c,4 :: LATD = 0; // turn OFF the PORTD leds
  16. CLR LATD
  17. ;blink_dev.c,5 :: while(1){
  18. L_main0:
  19. ;blink_dev.c,6 :: LATD=0;
  20. CLR LATD
  21. ;blink_dev.c,7 :: Delay_ms(1000);
  22. MOV #26, W8
  23. MOV #28274, W7
  24. L_main2:
  25. DEC W7
  26. BRA NZ L_main2
  27. DEC W8
  28. BRA NZ L_main2
  29. ;blink_dev.c,8 :: LATD=0x01;
  30. MOV #1, W0
  31. MOV WREG, LATD
  32. ;blink_dev.c,9 :: Delay_ms(1000);
  33. MOV #26, W8
  34. MOV #28274, W7
  35. L_main4:
  36. DEC W7
  37. BRA NZ L_main4
  38. DEC W8
  39. BRA NZ L_main4
  40. ;blink_dev.c,10 :: }
  41. GOTO L_main0
  42. ;blink_dev.c,11 :: }
  43. L_end_main:
  44. L__main_end_loop:
  45. BRA L__main_end_loop
  46. ; end of _main

Now the MCU toggles two LEDs connect to RD0(D8) and RD1(D7) complementary for every one second.

 

  1. void main() {
  2. ADPCFG = 0xFFFF; // configure AN pins as digital
  3. TRISD = 0; // set direction to be output
  4. LATD = 0; // turn OFF the PORTD leds
  5. while(1){
  6. LATD=0x0001;
  7. Delay_ms(1000);
  8. LATD=0x0002;
  9. Delay_ms(1000);
  10. }
  11. }

 

Two LEDs D8 and D7 will toggle alternately on board.

dsPIC30F2010 Button and LED in MikroC for dsPIC 

There are two input buttons - RC13 (CN1) and RC14(CN0). Now the program will read the input pins and it will displays to output LEDs.

MikroC Source Code:


  1. void main() {
  2. ADPCFG = 0xFFFF; // configure AN pins as digital
  3. TRISD = 0; // set direction to be output
  4. LATD = 0; // turn OFF the PORTD leds
  5. TRISC = (1<<13)|(1<<14); // RC13 and RC14 As Inputs
  6. LATC=0; // Clear Buffer
  7. PORTC=0; // Clear Buffer
  8. CNPU1 = (1<<1)|(1<<0); // RC13-CN0 and RC14-CN1 : Enable Pull-Up
  9. while(1){
  10. LATD=PORTC>>13; // Read Input Pins
  11. }
  12. }


The logic state of the output LEDs change upon the input logic triggered by the buttons.

dsPIC30F2010 MikroC I/O Programming
Program Testing on dsPIC30F2010 Prototype Board

 We can use these two buttons to toggle LEDs connects to RD0 and RD1.

MikroC Source Code:


  1. #define SW4 13
  2. #define SW5 14
  3. #define D7 0x02
  4. #define D8 0x01
  5. #define CN0 0
  6. #define CN1 1

  7. void main() {
  8. ADPCFG = 0xFFFF; // configure AN pins as digital
  9. TRISD = 0; // direction to be output
  10. LATD = 0; // turn OFF the PORTD leds
  11. TRISC = (1<<SW5)|(1<<SW4); // RC13 and RC14 As Inputs
  12. LATC=0; // Clear Buffer
  13. PORTC=0; // Clear Buffer
  14. CNPU1 = (1<<CN1)|(1<<CN0); // RC13-CN1 and RC14-CN0 : Enable Pull-Up
  15. while(1){
  16. if((PORTC&(1<<SW4))==0){
  17. LATD^=D8;
  18. Delay_ms(500);
  19. }
  20. if((PORTC&(1<<SW5))==0){
  21. LATD^=D7;
  22. Delay_ms(500);
  23. }
  24. }
  25. }

 

The 16-bit PIC micro-controllers have a lot of feature than the 8-bit PIC micro-controllers. We enable its weak pullup resistors by setting the Input Change Notification Pull-up Enable Register1(CNPU1).

dsPIC30F2010 MikroC I/O Programming
CNPU1: Input Change Notification Pull-up Enable Register1

 



 

 

 

 

 

 

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)