#include <pic18.h> //调用头文件,可以去PICC18软件下去查找PIC18FXX2.H
__CONFIG(1,XT) ; //晶振为外部4M
__CONFIG(2,WDTDIS) ; //看门狗关闭
__CONFIG(4,LVPDIS) ; //禁止低电压编程
#define RSPIN RB5 //0 input instrument;1 input data
#define RWPIN RB4 //0 give LCD write instrument or data;1 read LCD information
#define EPIN RB3 //1 read infor;1-0 action instrument
//名称: 1602检测到忙
void lcd_wait_busy(void)//RS R/W DB7 DB6~DB0
{//读取忙信号或AC地址信号:0 1 FB AC内容7位
//BF=1 表示液晶显示忙,暂无法接受单片机数据或指令
TRISD7=1;//为读状态做准备
RSPIN=0;//选择指令寄存器
RWPIN=1;//选择读
EPIN=1; //使能电平,1,读信息
while(RD7==1);//,BF=1表示液晶显示器忙
EPIN=0;
TRISD7=0;
}
//名称: 1602写命令函数(带忙检测)
void lcd_write_com_busy(unsigned char combuf)
{
lcd_wait_busy(); //调用忙检测函数
RSPIN=0;//input instrument
RWPIN=0;//give LCD writing instrument or data
PORTD=combuf;//command to PD
EPIN=1;
asm("NOP");
EPIN=0;//1 to 0 action instrument
}
//名称: 1602写数据函数(带忙检测)
void lcd_write_data_busy(unsigned char combuf)
{
lcd_wait_busy(); //调用忙检测函数
RSPIN=1;
RWPIN=0;
PORTD=combuf;
EPIN=1;
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)//送地址的时候应该是0x80+Address
lcd_write_com_busy(x|0x80); //第一行的列地址写入
else//second row 0x40
lcd_write_com_busy((x+0x40)|0x80); //第二行的列地址写入
}
//名称: 指定地址写入函数
void lcd_write_char(unsigned char x,unsigned char y,unsigned char buf)
{
lcd_write_address(x,y); //写入地址
lcd_write_data_busy(buf); //写入显示数据
}
//初始化开始
void lcdreset(void)
{
lcd_write_com_busy(0x38); //功能设定指令,设定数据总线位数、显示的行数及字型。
lcd_write_com_busy(0x08); //显示开关控制指令,显示功能关,无光标
lcd_write_com_busy(0x01); //清屏指令
lcd_write_com_busy(0x06); //进入模式,每次定入1位数据后光标右移,屏不移动
lcd_write_com_busy(0x14); //设定显示屏或光标移动方向指令,写入新的数据后,光标右移,显示屏不移动
lcd_write_com_busy(0x0c); //显示功能开,无光标,不闪烁
}
void main()
{
ADCON1=0X06; //所有IO均为数字口,模拟量输入禁止
TRISB=0B11000111; //RB3-5设置为输出
TRISD=0B00000000; //RD设置为输出
lcdreset();
lcd_write_char(0,0,0x41); //A
lcd_write_char(1,0,0x42); //B
lcd_write_char(2,0,0x43); //C
lcd_write_char(3,0,0x44); //D
lcd_write_char(4,0,0x45); //E
lcd_write_char(5,0,0x46); //F
lcd_write_char(6,0,0x47); //G
lcd_write_char(7,0,0x48); //H
lcd_write_char(8,0,0x49); //I
lcd_write_char(9,0,0x4A); //J
lcd_write_char(10,0,0x4B); //K
lcd_write_char(11,0,0x4C); //L
lcd_write_char(12,0,0x4D); //M
lcd_write_char(13,0,0x4E); //N
while(1)
{
}
}
|