728x90

728x90

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

320x50

Search This Blog

tyro-728x90