Wednesday, December 20, 2023

PIC16F887 HD44780 8-Bit LCD Example Using XC8

Overview

A character LCD is a good replacement for a conventional dot matrix display. It has a build-in LCD controller especially the HITACHI HD44780 chip. Characters are send once consuming a little microprocessor executing time. 

PIC16F887 HD44780 8-Bit LCD Example Using XC8

The HD44780 is very popular among electronics hobbyists and small electronics digital control project. It use an 8-bit or even 4-bit data bus with a few control pins. It has various optional outputs, 8x2, 16x1, 16x2, 20x2, 16x4, etc. It can show ASCII, Japanese, and even any custom characters.

PIC16F887 HD44780 8-Bit LCD Example Using XC8

Most of Chinese LCD parts use Chip-On-Board assembling with ceramic coating. So we can not see its controller chip. The picture above is an JHD1602A LCD module.

PIC16F887 HD44780 8-Bit LCD Example Using XC8

Using an 8-bit data transfer mode is very simple. We just need send command and data to this display module.

PIC16F887 HD44780 8-Bit LCD Example Using XC8

 
PIC16F887 HD44780 8-Bit LCD Example Using XC8

Register Select (RS) allow us to choose between command (RS=0) and data (RS=1). Read or Write (R/W) must be wired to GND because we just need to send command or data to the LCD controller. Enable (E) is active high allowing the command or data latched into the LCD controller via its 8-bit data bus (DB) port.

Interfacing and Programming Using MPLABX IDE

I use the current version of MPLABX IDE of v6.15 and XC8 compiler v2.36. This C compiler is much easier to use than PIC Assembly language. I just use free version without code optimization.

PIC16F887 HD44780 8-Bit LCD Example Using XC8

In this example, the LCD show operating time since it started up.

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on December 20, 2023, 2:18 PM
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <xc.h>
  10. #include "config.h"
  11.  
  12. #define _XTAL_FREQ 8000000UL
  13.  
  14. #define RS RD0
  15. #define EN RD1
  16. #define CB PORTD
  17. #define CR TRISD
  18. #define DB PORTC
  19. #define DR TRISC
  20.  
  21. void lcdCommand(char command){
  22. DB=command;
  23. RS=0;
  24. EN=1;
  25. __delay_us(25);
  26. EN=0;
  27. __delay_us(25);
  28. }
  29.  
  30. void lcdData(char data){
  31. DB=data;
  32. RS=1;
  33. EN=1;
  34. __delay_us(25);
  35. EN=0;
  36. __delay_us(25);
  37. }
  38.  
  39. void lcdString(char *myText){
  40. while(*myText) lcdData(*myText++);
  41. __delay_us(25);
  42. }
  43.  
  44. void lcdXY(char x,char y){
  45. // 16x2
  46. char addr[]={0x80,0xC0};
  47. lcdCommand(addr[y-1]+x-1);
  48. }
  49.  
  50. void lcdInit(void){
  51. DB=0;
  52. DR=0;
  53. CB=0;
  54. CR=0;
  55. __delay_ms(10);
  56. lcdCommand(0x38);
  57. lcdCommand(0x0C);
  58. lcdCommand(0x01);
  59. __delay_ms(5);
  60. lcdCommand(0x06);
  61. __delay_ms(5);
  62. }
  63. void main(void) {
  64. OSCCONbits.IRCF=7;
  65. unsigned char seconds=0,minutes=0,hours=0,days=0;
  66. char message[16];
  67. lcdInit();
  68. lcdString("PIC16F887 XC8");
  69. lcdXY(1,2);
  70. lcdString("LCD Programming");
  71. __delay_ms(2500);
  72. lcdCommand(0x01);
  73. __delay_ms(5);
  74. lcdXY(5,1);
  75. lcdString("Up Time:");
  76. while(1){
  77. if(seconds>59){seconds=0; minutes++;}
  78. if(minutes>59){minutes=0; hours++;}
  79. if(hours>23){hours=0; days++;}
  80. sprintf(message,"%2d - %2d:%2d:%2d",days,hours,minutes,seconds);
  81. lcdXY(1,2); lcdString(message);
  82. seconds++;
  83. __delay_ms(1000);
  84. }
  85. return;
  86. }
  87.  
  88.  

 

I use its 8MHz internal RC oscillator. Click here to download its source file.


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)