#include <pic18.h> //调用头文件,可以去PICC18软件下去查找PIC18FXX2.H
__CONFIG(1,XT) ; //晶振为外部4M
__CONFIG(2,WDTDIS) ; //看门狗关闭
__CONFIG(4,LVPDIS) ; //禁止低电压编程
#define RSPIN RB5 //Data or Instrument Select
#define RWPIN RB4 //Write or Read
#define EPIN RB3 //6800 mode Enable single
unsigned char table[]="LCD check ok"; //要显示的内容1放入数组tablel
//此函数定义八个字符分别写入CGRAM的八个地址
unsigned char pic[]={
0x03,0x07,0x0f,0x1f,0x1f,0x1f,0x1f,0x1f,
0x18,0x1E,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
0x07,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
0x10,0x18,0x1c,0x1E,0x1E,0x1E,0x1E,0x1E,
0x0f,0x07,0x03,0x01,0x00,0x00,0x00,0x00,
0x1f,0x1f,0x1f,0x1f,0x1f,0x0f,0x07,0x01,
0x1f,0x1f,0x1f,0x1f,0x1f,0x1c,0x18,0x00,
0x1c,0x18,0x10,0x00,0x00,0x00,0x00,0x00
};
///---------------------------------------
//名称: 延时函数
//-----------------------------------------
void delay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
{
for(j=0;j<10;j++);
}
}
//---------------------------------------
//名称: 1602忙检测函数
//-----------------------------------------
void lcd_wait_busy(void)
{
TRISD7=1; //为读状态做准备
RSPIN=0; //选择指令寄存器
RWPIN=1; //选择读
EPIN=1; //使能线电平变化
while(RD7==1); //读忙状态,不忙时退出
EPIN=0; //恢复使能线电平
TRISD7=0;
}
//---------------------------------------
//名称: 1602写命令函数
//-----------------------------------------
void lcd_write_com(unsigned char combuf)
{
RSPIN=0; //选择指令寄存器
RWPIN=0; //选择写
PORTD=combuf; //把命令字送入RD
EPIN=1; //使能线电平变化,命令送入1602的8位数据口
asm("NOP");
EPIN=0; //恢复使能线电平
}
//---------------------------------------
//名称: 1602写命令函数(带忙检测)
//-----------------------------------------
void lcd_write_com_busy(unsigned char combuf)
{
lcd_wait_busy(); //调用忙检测函数
lcd_write_com(combuf); //调用写命令函数
}
//---------------------------------------
//名称: 1602写数据函数(带忙检测)
//-----------------------------------------
void lcd_write_data(unsigned char databuf)
{
lcd_wait_busy(); //调用忙检测函数
RSPIN=1; //选择数据寄存器
RWPIN=0; //选择写
PORTD=databuf; //把数据字送入P2
EPIN=1; //使能线电平变化,命令送入1602的8位数据口
asm("NOP");
EPIN=0; //恢复使能线电平
}
//---------------------------------------
//名称: 1602显示地址写函数
//-----------------------------------------
void lcd_write_address(unsigned char x,unsigned char y)
{
x&=0x0f; //列地址限制在0-15
y&=0x01; //行地址限制在0-1
if(y==0x00)
lcd_write_com_busy(x|0x80); //第一行的列地址写入
else
lcd_write_com_busy((x+0x40)|0x80); //第二行的列地址写入
}
//---------------------------------------
//名称: 1602初始化函数
//-----------------------------------------
void lcdreset(void)
{
lcd_write_com_busy(0x38); //8位数据,双列,5*7字形
lcd_write_com_busy(0x08); //显示功能关,无光标
lcd_write_com_busy(0x01); //清屏指令
lcd_write_com_busy(0x06); //写入新的数据后,光标右移,显示屏不移动
lcd_write_com_busy(0x0c); //显示功能开,无光标,
}
//---------------------------------------
//名称: 指定地址写入函数
//-----------------------------------------
void lcd_write_char(unsigned char x,unsigned char y,unsigned char buf)
{
lcd_write_address(x,y); //写入地址
lcd_write_data(buf); //写入显示数据
}
//显示字符串 ,还不能准确应用
void lcd1602_write_character(unsigned char add,unsigned char *p)
{
lcd_write_com_busy(add);
while (*p!='\0')
{
lcd_write_data(*p++);
}
}
//将自定义字符写入CGRAM
//add是CGRAM 的地址(0~7)
//*pic_num是指向一个8位数组的首地址
void lcd1602_write_pic(unsigned char add,unsigned char pic_num)
{
unsigned char i;
add=add<<3;
for(i=0;i<8;i++)
{
lcd_write_com_busy(0x40|add+i); //设定行地址,DB6=1,所以地址可定要加上0X40;
lcd_write_data(pic_num);
}
}
//---------------------------------------
//名称: 主函数
//-----------------------------------------
void main(void)
{
char i,m;
ADCON1=0X06; //所有IO均为数字口,模拟量输入禁止
TRISB=0B11000111; //RB3-5设置为输出
TRISD=0B00000000; //RD设置为输出
lcdreset(); //复位1602
lcd_write_char(6,0,0x31); //1
lcd_write_char(7,0,0x36); //6
lcd_write_char(8,0,0x30); //0
lcd_write_char(9,0,0x32); //2
lcd_write_char(10,0,0x35); //5
lcd_write_char(11,0,'g'); //g
lcd_write_char(12,0,'o'); //o
lcd_write_char(13,0,'o'); //o
lcd_write_char(14,0,'d'); //d
lcd_write_com_busy(0x80+0x44);//8.重新设定显示地址为0xc0,即下排第0位
for(m=0;m<12;m++) //将table1[]中的数据依次写入1602显示
{
lcd_write_data(table[m]);
}
//写八个自定义字符到CGRAM
lcd_write_com_busy(0x40); //7.设定CGRAM地址0100; 0x06
for(i=0;i<64;i++) //将心型代码写入CGRAM中
lcd_write_data(pic[i]);//lcd1602_write_pic(i,pic[i]);
//显示八个自定义字符
lcd_write_com_busy(0x80);//设定上排的显示位置
for(i=0;i<0x04;i++)//显示心型图案的上半部分
lcd_write_data(i);
lcd_write_com_busy(0xc0);////将显示坐标转移到下排和上排相对应的地方
for(i=4;i<0x08;i++)
lcd_write_data(i);
while(1)
{
}
}