Overview
A character LCD is a good replacement for a conventional dot matrix display. It has a build-in LCD controller especially the HITACHI HD44780 chip. Characters are send once consuming a little microprocessor executing time.
The HD44780 is very popular among electronics hobbyists and small electronics digital control project. It use an 8-bit or even 4-bit data bus with a few control pins. It has various optional outputs, 8x2, 16x1, 16x2, 20x2, 16x4, etc. It can show ASCII, Japanese, and even any custom characters.
Most of Chinese LCD parts use Chip-On-Board assembling with ceramic coating. So we can not see its controller chip. The picture above is an JHD1602A LCD module.
Using an 8-bit data transfer mode is very simple. We just need send command and data to this display module.
Register Select (RS) allow us to choose between command (RS=0) and data (RS=1). Read or Write (R/W) must be wired to GND because we just need to send command or data to the LCD controller. Enable (E) is active high allowing the command or data latched into the LCD controller via its 8-bit data bus (DB) port.
Interfacing and Programming Using MPLABX IDE
I use the current version of MPLABX IDE of v6.15 and XC8 compiler v2.36. This C compiler is much easier to use than PIC Assembly language. I just use free version without code optimization.
In this example, the LCD show operating time since it started up.
/* * File: main.c * Author: Admin * * Created on December 20, 2023, 2:18 PM */ #include <stdio.h> #include <xc.h> #include "config.h" #define _XTAL_FREQ 8000000UL #define RS RD0 #define EN RD1 #define CB PORTD #define CR TRISD #define DB PORTC #define DR TRISC void lcdCommand(char command){ DB=command; RS=0; EN=1; __delay_us(25); EN=0; __delay_us(25); } void lcdData(char data){ DB=data; RS=1; EN=1; __delay_us(25); EN=0; __delay_us(25); } void lcdString(char *myText){ while(*myText) lcdData(*myText++); __delay_us(25); } void lcdXY(char x,char y){ // 16x2 char addr[]={0x80,0xC0}; lcdCommand(addr[y-1]+x-1); } void lcdInit(void){ DB=0; DR=0; CB=0; CR=0; __delay_ms(10); lcdCommand(0x38); lcdCommand(0x0C); lcdCommand(0x01); __delay_ms(5); lcdCommand(0x06); __delay_ms(5); } void main(void) { OSCCONbits.IRCF=7; unsigned char seconds=0,minutes=0,hours=0,days=0; char message[16]; lcdInit(); lcdString("PIC16F887 XC8"); lcdXY(1,2); lcdString("LCD Programming"); __delay_ms(2500); lcdCommand(0x01); __delay_ms(5); lcdXY(5,1); lcdString("Up Time:"); while(1){ if(seconds>59){seconds=0; minutes++;} if(minutes>59){minutes=0; hours++;} if(hours>23){hours=0; days++;} sprintf(message,"%2d - %2d:%2d:%2d",days,hours,minutes,seconds); lcdXY(1,2); lcdString(message); seconds++; __delay_ms(1000); } return; }
I use its 8MHz internal RC oscillator. Click here to download its source file.
No comments:
Post a Comment