728x90

728x90

Friday, September 22, 2023

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

In previous post, I showed about LCD interfacing with PIC16F84A using an 8-bit data mode. For ease of use, most most electronic hobbyists use a 4-bit data mode. It uses only four upper bits D4...7 of the LCD module. The command or data is still 8-bit wide. So the micro-controller needs to send command or data twice. The upper nibble will be transfer first and then the lower nibble.

PIC16F84A And Simple Character LCD Interfacing In 4-Bit Mode Using MPLABX XC8
Simulating Program
 

PIC16F84A And Simple Character LCD Interfacing In 4-Bit Mode Using MPLABX XC8
A low 16x2 cost LCD module (TOP)


PIC16F84A And Simple Character LCD Interfacing In 4-Bit Mode Using MPLABX XC8
A low 16x2 cost LCD module (BOTTOM)

Example 1

In this example I use Port B to interface with this LCD module. Because of PIC micro-controller has a bit addressable instruction, hence we can use a single port interfacing easily. The IDE is MPLABX IDE v1.51 with the XC8 v2.36 compiler.

  1. /*
  2.  * Character LCD Interfacing Using 4-Bit Mode
  3.  * Programming WIth MPLABX XC8
  4.  */
  5.  
  6. #include <xc.h>
  7.  
  8. // PIC16F84A Configuration Bit Settings
  9.  
  10. // CONFIG
  11. #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
  12. #pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
  13. #pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
  14. #pragma config CP = OFF // Code Protection bit (Code protection disabled)
  15.  
  16.  
  17. #define _XTAL_FREQ 4000000UL
  18.  
  19. #define RS RB0
  20. #define EN RB1
  21. #define DATA8 PORTB
  22.  
  23. void lcdCommand(uint8_t cmd){
  24. DATA8=cmd&0xF0;
  25. RS=0;
  26. EN=1;
  27. __delay_us(10);
  28. EN=0;
  29. __delay_us(10);
  30.  
  31. DATA8=cmd<<4;
  32. RS=0;
  33. EN=1;
  34. __delay_us(10);
  35. EN=0;
  36. __delay_us(10);
  37.  
  38. __delay_us(100);
  39. }
  40.  
  41. void lcdData(uint8_t dat){
  42. DATA8=dat&0xF0;
  43. RS=1;
  44. EN=1;
  45. __delay_us(10);
  46. EN=0;
  47. __delay_us(10);
  48.  
  49. DATA8=dat<<4;
  50. RS=1;
  51. EN=1;
  52. __delay_us(10);
  53. EN=0;
  54. __delay_us(10);
  55.  
  56. __delay_us(100);
  57. }
  58.  
  59. void lcdXY(uint8_t x,uint8_t y){
  60. /*16x2 Character LCD*/
  61. uint8_t addr[]={0x80,0xC0};
  62. lcdCommand(addr[y-1]+x-1);
  63. }
  64.  
  65. void lcdString(uint8_t *str){
  66. while(*str) lcdData(*str++);
  67. }
  68.  
  69. void lcdInit(void){
  70. EN=0;
  71. __delay_us(100);
  72. lcdCommand(0x33);
  73. __delay_us(1);
  74. lcdCommand(0x32);
  75. __delay_us(1);
  76. lcdCommand(0x28);
  77. __delay_us(1);
  78. lcdCommand(0x0F);
  79. __delay_us(1);
  80. lcdCommand(0x01);
  81. __delay_ms(5);
  82. lcdCommand(0x06);
  83. }
  84.  
  85. int main(void){
  86. PORTB=0;
  87. TRISB=0;
  88. lcdInit();
  89. lcdXY(3,1);
  90. lcdString("Hello World!");
  91. __delay_ms(1500);
  92. lcdXY(2,2);
  93. lcdString("PIC16F84A LCD");
  94. while(1);
  95. return 0;
  96. }

Click here to download its source file.

Example 2 - Making A Library Function

For convenient we can make a library function for this LCD controller. We need to make a *.h and a *.c file that will include in project as shown in the example below.

  1.  
  2. #include <xc.h>
  3.  
  4. #define _XTAL_FREQ 4000000UL
  5.  
  6. #include "LCD4Bits.h"
  7.  
  8. int main(void){
  9.  
  10. PORTB=0;
  11. TRISB=0;
  12. lcdInit();
  13. __delay_ms(1000);
  14. lcdXY(3,1);
  15. lcdString("Hello World!");
  16. lcdXY(4,2);
  17. lcdString("PIC16F84A");
  18. __delay_ms(1500);
  19. lcdCommand(0x0C);
  20. lcdCommand(CLEAR_SCREEN);
  21. __delay_ms(500);
  22. lcdString("Programming With MPLABX XC8 v2.36");
  23. lcdXY(1,2);
  24. lcdString("16x2 HD44780 Character LCD Module");
  25. __delay_ms(1000);
  26.  
  27. for(uint8_t i=0;i<34;i++){
  28. __delay_ms(500);
  29. lcdCommand(MOVE_LEFT);
  30. }
  31.  
  32. lcdCommand(CLEAR_SCREEN);
  33. __delay_ms(500);
  34. lcdXY(3,1);
  35. lcdString("AKI-Technical");
  36. lcdXY(6,2);
  37. lcdString("BLOGGER");
  38.  
  39. for(uint8_t i=0;i<16;i++) lcdCommand(MOVE_LEFT);
  40. for(uint8_t i=0;i<16;i++){
  41. __delay_ms(500);
  42. lcdCommand(MOVE_RIGHT);
  43. }
  44.  
  45. while(1){}
  46. return 0;
  47. }

This example scrolls the display left and right.

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

PIC16F84A And Simple Character LCD Interfacing In 4-Bit Mode Using MPLABX XC8
Simulating Program #1

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

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90