Menu

Adskeeper - header widge

728x90

Search This Blog

468x60

Navbar

AdsKeeper - IAB Display Standard Ad Unit

Thursday, September 21, 2023

PIC16F84A And Simple Character LCD Interfacing In 8-Bit Mode Using MPLABX XC8

The HD44780-base character LCD is easy to interface with. Electronic technician can test it manually using a simple logic circuit, digital IC circuit, or even using a small micro-controller. Using a micro-controller to interface with this LCD module is very straight forward. The controller only need its 8-bit data port, a Register Select (RS) pin, and Enable (E) pin for the LCD module. The LCD module just need to receive data. So its Read or Write (R/W) is connects to logic 0 or GND. For more information of using this character LCD module using the 8-bit interfacing mode, please see this post.

PIC16F84A And Simple Character LCD Interfacing In 8-Bit Mode Using MPLABX XC8
Program Simulation In Proteus #A

PIC16F84A And Simple Character LCD Interfacing In 8-Bit Mode Using MPLABX XC8
Program Simulation In Proteus #B

 

In this example, I use a simple 8-bit micro-controller, PIC16F84A to interface with this LCD module. It has two general-purpose I/O ports, Port A and Port B. Its total I/O is 11. Port B connects to the 8-bit LCD data bus, while Port A RA0 and RA1 connects to LCD RS and E respectively. The LCD E pin connects to GND as it just need to accept input data or command.

PIC16F84A And Simple Character LCD Interfacing In 8-Bit Mode Using MPLABX XC8

A 16x2 Character LCD Module

Example 1

I wrote this program using MPLABX IDE and v1.51 and XC8 v2.36 (free version). It's very light-weight and simple to use. Using the Assembly language could be effective in built firmware but it could require more lines of written codes.

  1. #include <xc.h>
  2.  
  3. #define _XTAL_FREQ 4000000UL
  4.  
  5. #define RS RA0
  6. #define EN RA1
  7.  
  8. #define CLEAR_SCREEN 0x01
  9.  
  10. void lcdCommand(uint8_t cmd){
  11. PORTB=cmd;
  12. RS=0;
  13. EN=1;
  14. __delay_us(10);
  15. EN=0;
  16. __delay_ms(1);
  17. }
  18.  
  19. void lcdData(uint8_t dat){
  20. PORTB=dat;
  21. RS=1;
  22. EN=1;
  23. __delay_us(10);
  24. EN=0;
  25. __delay_ms(1);
  26. }
  27.  
  28. void lcdXY(uint8_t x,uint8_t y){
  29. /*16x2 Character LCD*/
  30. uint8_t addr[]={0x80,0xC0};
  31. lcdCommand(addr[y-1]+x-1);
  32. }
  33.  
  34. void lcdString(uint8_t* str){
  35. while(*str) lcdData(*str++);
  36. }
  37. void lcdInit(void){
  38. EN=0;
  39. __delay_us(100);
  40. lcdCommand(0x38);
  41. lcdCommand(0x0F);
  42. lcdCommand(0x01);
  43. __delay_ms(1);
  44. lcdCommand(0x06);
  45. }
  46.  
  47. int main(void){
  48. PORTA=0;
  49. TRISA=0;
  50. PORTB=0;
  51. TRISB=0;
  52. lcdInit();
  53. __delay_ms(1000);
  54. lcdXY(3,1);
  55. lcdString("Hello World!");
  56. lcdXY(4,2);
  57. lcdString("PIC16F84A");
  58.  
  59. __delay_ms(1500);
  60. lcdCommand(CLEAR_SCREEN);
  61. lcdXY(1,1);
  62. lcdString("Programming With");
  63. lcdXY(1,2);
  64. lcdString("MPLABX XC8 v2.36");
  65.  
  66. while(1){
  67.  
  68. }
  69. return 0;
  70. }
  71.  

This simple program need almost a quarter of ROM and RAM of the target PIC16F84A chip.

PIC16F84A And Simple Character LCD Interfacing In 8-Bit Mode Using MPLABX XC8
Memory Resource Usage Of PIC16F84

Click here to download its source file

Example 2

Now let make a library for this 8-bit LCD. It requires a pair of a *.h and a *.c file. We need to include these files in project folder before we can call its functions.

PIC16F84A And Simple Character LCD Interfacing In 8-Bit Mode Using MPLABX XC8
Library Test
We can test this library as below.

  1.  
  2. #include <xc.h>
  3.  
  4. #define _XTAL_FREQ 4000000UL
  5.  
  6. #include "LCD8Bits.h"
  7.  
  8. int main(void){
  9. PORTA=0;
  10. TRISA=0;
  11. PORTB=0;
  12. TRISB=0;
  13. lcdInit();
  14. __delay_ms(1000);
  15. lcdXY(3,1);
  16. lcdString("Hello World!");
  17. lcdXY(4,2);
  18. lcdString("PIC16F84A");
  19.  
  20. __delay_ms(1500);
  21. lcdCommand(CLEAR_SCREEN);
  22. lcdXY(1,1);
  23. lcdString("Programming With MPLABX XC8 v2.36");
  24. lcdXY(1,2);
  25. lcdString("16x2 HD44780 Character LCD Example");
  26. __delay_ms(1500);
  27. lcdCommand(0x80);
  28. for(uint8_t i=0;i<34;i++){
  29. lcdCommand(MOVE_LEFT);
  30. __delay_ms(500);
  31. }
  32. while(1){}
  33. return 0;
  34. }
  35.  
  36.  

PIC16F84A And Simple Character LCD Interfacing In 8-Bit Mode Using MPLABX XC8
Sample #1

PIC16F84A And Simple Character LCD Interfacing In 8-Bit Mode Using MPLABX XC8
Sample #2
 Click here to download its source file.

1 comment:

  1. Delivers crisp visuals and smooth performance—ideal for modern embedded interfaces and compact display needs.
    lcd graphic

    ReplyDelete

Adskeep- Standard Ads

Electronics and Programming Tutorials

336x280

DIY Electronic Projects And Instruments

AdsKeeper - 2091654

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (40) 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 (157) Distance Sensor (3) dot matrix (1) ds1307 (10) DS18B20 (6) ds3231 (2) ds3232 (2) dsPIC (16) dsPIC30F1010 (3) dsPIC30F2010 (15) EEPROM (5) Environment Sensor (6) ESP32 (1) esp8266 (1) Graphical LCD (3) HD44780 (12) I2C (40) ILI9341 (1) Input/Output (84) Interrupt (22) IoT PCB (1) Keil (5) Keypad (16) KS0108 (3) LCD (87) LM35 (4) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) MikroC ARM (1) MikroC dsPIC (11) MikroC PIC (1) Module PCB (2) Motor (15) MPLABX (73) Nokia 5110 LCD (5) OLED (2) One-Wire (7) Oscillator (8) PCB (13) PCD8544 (3) PCF8574 (13) PIC (121) 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 (13) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PLL (1) PWM (12) RTC (13) SBN0064G (1) Sensor (15) SH1106 (3) Shift Register (15) Shift Registers (19) SN74HC595N (9) Software SPI (1) Software TWI (3) SPI (44) sprinf (3) 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 (11) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)