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.

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)