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.
Program Simulation In Proteus #A |
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.
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.
#include <xc.h> #define _XTAL_FREQ 4000000UL #define RS RA0 #define EN RA1 #define CLEAR_SCREEN 0x01 void lcdCommand(uint8_t cmd){ PORTB=cmd; RS=0; EN=1; __delay_us(10); EN=0; __delay_ms(1); } void lcdData(uint8_t dat){ PORTB=dat; RS=1; EN=1; __delay_us(10); EN=0; __delay_ms(1); } void lcdXY(uint8_t x,uint8_t y){ /*16x2 Character LCD*/ uint8_t addr[]={0x80,0xC0}; lcdCommand(addr[y-1]+x-1); } void lcdString(uint8_t* str){ while(*str) lcdData(*str++); } void lcdInit(void){ EN=0; __delay_us(100); lcdCommand(0x38); lcdCommand(0x0F); lcdCommand(0x01); __delay_ms(1); lcdCommand(0x06); } int main(void){ PORTA=0; TRISA=0; PORTB=0; TRISB=0; lcdInit(); __delay_ms(1000); lcdXY(3,1); lcdString("Hello World!"); lcdXY(4,2); lcdString("PIC16F84A"); __delay_ms(1500); lcdCommand(CLEAR_SCREEN); lcdXY(1,1); lcdString("Programming With"); lcdXY(1,2); lcdString("MPLABX XC8 v2.36"); while(1){ } return 0; }
This simple program need almost a quarter of ROM and RAM of the target PIC16F84A chip.
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.
Library Test |
#include <xc.h> #define _XTAL_FREQ 4000000UL #include "LCD8Bits.h" int main(void){ PORTA=0; TRISA=0; PORTB=0; TRISB=0; lcdInit(); __delay_ms(1000); lcdXY(3,1); lcdString("Hello World!"); lcdXY(4,2); lcdString("PIC16F84A"); __delay_ms(1500); lcdCommand(CLEAR_SCREEN); lcdXY(1,1); lcdString("Programming With MPLABX XC8 v2.36"); lcdXY(1,2); lcdString("16x2 HD44780 Character LCD Example"); __delay_ms(1500); lcdCommand(0x80); for(uint8_t i=0;i<34;i++){ lcdCommand(MOVE_LEFT); __delay_ms(500); } while(1){} return 0; }
Sample #1 |
Sample #2 |
No comments:
Post a Comment