keil sample
Is this any use, actually for AT89s8252/3. should not require many changes. For 1x16 display, electrically 2x8.
P1.0 = RS
P1.1 = E
P1.2 = D4
P1.3 = D5
P1.4 = D6
P1.5 = D7
Ground Pin 3 on LCD
Delay shown is "Delay.h"
Delay.h
#define XTAL_FREQ 12
#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)*((XTAL_FREQ)/12); \
while(--_dcnt != 0) \
continue; }
extern void DelayMs(unsigned char);
LCD.h
/*
* LCD interface header file
* See lcd.c for more info
*/
/* write a byte to the LCD in 4 bit mode */
extern void lcd_write(unsigned char);
/* Clear and home the LCD */
extern void lcd_clear(void);
/* write a string of characters to the LCD */
extern void lcd_puts(const char * s);
/* Go to the specified position */
extern void lcd_goto(unsigned char pos);
/* intialize the LCD - call before anything else */
extern void lcd_init(void);
extern void lcd_putch(char);
/* Set the cursor position */
#define lcd_cursor(x) lcd_write(((x)&0x7F)|0x80)
LCD.c
#include <reg51.h>
#include "delay.h"
sbit LCD_RS =P1^0; // Register select
sbit LCD_E =P1^1; // Enable
#define LCD_STROBE ((LCD_E = 1),(LCD_E=0))
// Time in Milliseconds
void DelayMs(unsigned char cnt)
{ cnt=2*cnt;
do
{DelayUs(500);}
while(--cnt != 0);
}
// Send to Display
void lcd_write(unsigned char c)
{
unsigned char Movit;
Movit = (c >> 2);
P1 = (P1 & 0xC3) | (Movit & 0x3C);
LCD_STROBE;
Movit = (c << 2);
P1 = (P1 & 0xC3) | (Movit & 0x3C);
LCD_STROBE;
DelayUs(60);
}
// Clear and home the LCD
void lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x80);
DelayMs(2);
}
// Position the Cursor
void lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write (0x80+pos);
}
// Send Character String
void lcd_puts(const char * s)
{
unsigned char cursor = 0x00;
while(*s)
{
LCD_RS = 1;
lcd_write(*s);
s++;
cursor++;
if (cursor==0x08)lcd_goto(0x40); // Change to 0x80 for simulation
if (cursor==0x10){cursor=0;lcd_goto(0x00);}
}
}
// Initialise the LCD - mode 4 bits
void lcd_init(void)
{
LCD_RS = 0;
DelayMs(40); // power on delay
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
LCD_STROBE;
DelayMs(5);
LCD_STROBE;
DelayMs(5);
LCD_STROBE;
lcd_write(0x28);
lcd_write(0x08); // display off
lcd_write(0x0C); // display on, cursor off
lcd_write(0x06); // entry mode
}