Wednesday, December 20, 2023

PIC16F887 HD44780 4-Bit LCD Example Using XC8

In previous tutorial, I use a PIC16F887 controller to interface with an HD44780 character LCD module in 8-bit mode. However most of this electronics designer use a 4-bit mode operation. Data transferring to this LCD is still 8-bit wide but it sends twice, first the higher nibble then the lower nibble of data byte. This can save the number of microprocessor I/O pins.

PIC16F887 HD44780 4-Bit LCD Example Using XC8
Prototype Board Testing

The programming is no complex. The controller just process the transferring of these two nibbles of data byte. LCD control pins remain the same as per in previous example. We only use the higher nibble (DB4...D7) of LCD for data bus. The lower nibble (DB0...D3) must left blank or connects to ground.

The following example display the system running time since it starts up.

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on December 20, 2023, 7:36 PM
  6.  * HD44780 8-BIT LCD WITH PIC16F887
  7.  * MPLABX IDE v6.15 AND XC8 2.36
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <xc.h>
  12. #include "config.h"
  13.  
  14. #define _XTAL_FREQ 8000000UL
  15.  
  16. #define DB PORTD
  17. #define DR TRISD
  18. #define RS RD0
  19. #define EN RD1
  20.  
  21. /*LCD Command RS=0*/
  22. void lcdCommand(char command){
  23. DB=command&0xF0;
  24. RS=0;
  25. EN=1;
  26. __delay_us(25);
  27. EN=0;
  28. __delay_us(25);
  29.  
  30. DB=command<<4;
  31. RS=0;
  32. EN=1;
  33. __delay_us(25);
  34. EN=0;
  35. __delay_us(250);
  36. }
  37.  
  38. /*LCD Data RS=1*/
  39. void lcdData(char data){
  40. DB=data&0xF0;
  41. RS=1;
  42. EN=1;
  43. __delay_us(25);
  44. EN=0;
  45. __delay_us(25);
  46.  
  47. DB=data<<4;
  48. RS=1;
  49. EN=1;
  50. __delay_us(25);
  51. EN=0;
  52. __delay_us(250);
  53. }
  54.  
  55. void lcdXY(char x, char y){
  56. // 16x2
  57. char addr[]={0x80,0xC0};
  58. lcdCommand(addr[y-1]+x-1);
  59. }
  60.  
  61. void lcdString(char *str){
  62. while(*str) lcdData(*str++);
  63. __delay_us(25);
  64. }
  65.  
  66. void lcdInit(void){
  67. DB=0;
  68. DR=0;
  69. __delay_ms(10);
  70. lcdCommand(0x33);
  71. lcdCommand(0x32);
  72. lcdCommand(0x28);
  73. lcdCommand(0x0C);
  74. lcdCommand(0x01);
  75. __delay_ms(5);
  76. lcdCommand(0x06);
  77. __delay_us(100);
  78. }
  79.  
  80. void main(void) {
  81. char seconds=0,minutes=0,hours=0;
  82. int days=0;
  83. char message[16];
  84. /*Internal 8MHz RC OSC*/
  85. OSCCONbits.IRCF=7;
  86. lcdInit();
  87. lcdString("PIC16F887 4-BIT");
  88. lcdXY(3,2);
  89. lcdString("HD44780 LCD");
  90. __delay_ms(2500);
  91. lcdCommand(0x01);
  92. __delay_ms(5);
  93. lcdXY(4,1);
  94. lcdString("Started Up:");
  95. while(1){
  96. if(seconds>59){seconds=0; minutes++;}
  97. if(minutes>59){minutes=0; hours++;}
  98. if(hours>23){hours=0; days++;}
  99. sprintf(message,"%4d - %2d:%2d:%2d",days,hours,minutes,seconds);
  100. lcdXY(1,2); lcdString(message);
  101. seconds++;
  102. __delay_ms(1000);
  103. }
  104. return;
  105. }
  106.  

Click here to download its source file, or with LCD library.

PIC16F887 HD44780 4-Bit LCD Example Using XC8

MPLABX IDE v6.15 is pretty nice to use. 


PIC16F887 HD44780 4-Bit LCD Example Using XC8

I use my own PIC16F887 prototype board to test this example program.

PIC16F887 HD44780 4-Bit LCD Example Using XC8
Prototype Board Testing

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)