#include<REG52.H>
#include "delay.h"
#include "lcd.h"
sbit en = P2^2;
sbit rw = P2^1;
sbit rs = P2^0;
sbit d7 = P2^6;
sbit d6 = P2^5;
sbit d5 = P2^4;
sbit d4 = P2^3;
sfr lcd_port = 0xA0;
void lcd_clock(void)
{
en = 1;
delay_us(10);
en = 0;
return;
}
void lcd_init(void)
{
lcd_port=0x00;
delay_ms(10); //power up delay of 10ms
lcd_nibble(0x03,0); //send 0x03 thrice
lcd_clock();
delay_ms(4);
lcd_nibble(0x03,0);
lcd_clock();
delay_us(100);
lcd_nibble(0x03,0);
lcd_clock();
delay_us(100);
lcd_cmd(0x02); //set 4bit mode
delay_us(100);
lcd_cmd(0x28); //4bit mode 5x7 size
delay_us(100);
lcd_cmd(0x10); //cursor shift left
delay_us(100);
lcd_cmd(0x06); //entry mode increment
delay_us(100);
lcd_cmd(0x0C); //display on cursor blink off
delay_us(100);
lcd_cmd(0x01); //clear graphics ram
delay_us(100);
delay_ms(10);
return;
}
void lcd_cmd(unsigned char pdata letter)
{
// Command Function
//The RS is set to 0 to signify this is a command
unsigned char pdata temp; // temp Variable
temp=letter; //move letter to temp
temp=temp>>4; //shift temp to right by 4
lcd_nibble(temp,0); //send out higher nibble
temp=letter; //move letter to temp
temp=temp&0x0F;
lcd_nibble(temp,0); //send out lower nibble
delay_us(12);
return;
}
void lcd_nibble(unsigned char pdata nib,unsigned char pdata reg_sel)
{
unsigned char pdata i,x;
rs=reg_sel;
for(i=0;i<4;i++)
{
if((nib & 0x08) != 0) // AND the nibble to 8
x=1; // If the AND == 1 set x
else
x=0; // If the AND == 0 clear x
if(i==0)
d7=x;
if(i==1)
d6=x;
if(i==2)
d5=x;
if(i==3)
d4=x;
nib=nib<<1; // Shift nibble to the right by 1 (4 times total)
}
lcd_clock(); // Toggle E pin (defined in header file)
return;
}
void lcd_clr_line(unsigned char pdata line)
{
// clear line command.
unsigned char pdata tline; // The line to clear variable
unsigned char pdata x; // loop variable
if(line==1) // Set the variable value based on line
tline=0x80; // 1 = Line 1
if(line==2)
tline=0xC0; // 2 = Line 2
lcd_cmd(tline); // Send command to jump to beggining of line (1/2)
delay_ms(1);
for(x=0x00;x<0x14;x++)
{ // Loop through all 20 chars of line (even tho 16 are viewable)
lcd_char(0x20); // Send Blank Character
delay_ms(1);
}
lcd_cmd(tline); // Go back to beggining of line
delay_ms(1);
return;
}
void lcd_char(unsigned char pdata letter)
{
//TData Function
//The RS is set to 1 to signify this is a command
unsigned char pdata temp; // temp Variable
temp=letter; //move letter to temp
temp=temp>>4; //shift temp to right by 4
lcd_nibble(temp,1); //send out higher nibble
temp=letter; //move letter to temp
temp=temp&0x0F;
lcd_nibble(temp,1); //send out lower nibble
delay_us(20);
return;
}
void lcd_string(unsigned char *senpoint,unsigned char line)
{
if(line==1)
lcd_cmd(0x80);
if(line==2)
lcd_cmd(0xC0);
while(*senpoint != '\0') // While we havent seen a \0 (esc) go on
{
lcd_char(*senpoint); // Send 1st char to our char function
senpoint++; // Send next
}
delay_us(20);
}