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

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)