📄 lcd12864r.c
字号:
//12864LCD带中文字库
//编程让12864LCD显示公司名称“北京太格公司”,
//硬件设置
#include <stdio.h>
#include <string.h>
#include <REG52.H>
#include <LCD12864R.h>
//芯片配置字,看门狗关,上电延时开,掉电检测关,低压编程关,加密,4M晶体HS振荡
sbit rs=P1^0; //定义LCD的数据/命令控制口
sbit rst=P1^1; //定义复位口
sbit e=P1^2; //定义LCD的使能口
sbit rw=P1^3; //定义LCD的读/写控制口
sbit psb=P1^4; //定义通信方式控制口
const unsigned char TAB1A[ ]={0xB1,0xB1,0xBE,0xA9,0XCC,0xAB,0xB8,0xF1,0XCA,0XB1,0XB4,0XFA,0XB9,0XAB,0XCB,0XBE};
//显示"北京太格时代公司"
const unsigned char TAB1B[ ]={' ', 'w', 'w', 'w', '.', 't', 'o', 'g', 'e','s','t','.', 'c', 'o', 'm', ' '};
//显示"www.togest.com"
const unsigned char TAB1C[ ]={'T', 'E', 'L' ,' ' ,'0', '1' ,'0','-', '6', '3','3', '5' ,'6','5' ,'2','1'};
//显示"TEL 010-63356521"
const unsigned char TAB1D[ ]={'F', 'A', 'X', ' ', '0', '1', '0', '-','6', '3','3', '5', '6', '5', '2', '1'};
//显示"FAX 010-63356521"
//unsigned int lcd_x=0; //定义LCD页地址寄存器
//unsigned int lcd_y=0; //定义LCD列地址寄存器
bit busy; //定义LCD忙标志位
void MCU_Init(); //申明I/O口设置函数
void lcd_init(); //申明LCD初始化函数
void clear_p(); //申明清屏函数
void han_wr2a(); //申明显示公司名称函数
void han_wr2b(); //申明显示公司web函数
void han_wr2c(); //申明显示公司tel函数
void han_wr2d(); //申明显示公司fax函数
//void wr_zb(); //申明有关显示设置函数
void flash(); //申明设置LCD显示闪烁函数
void qushu(int counts,const unsigned char *ps); //申明查表获取显示数据
void send_d(unsigned char x); //申明送一字节数据显示函数
void send_i(unsigned char x); //申明送一字节控制命令函数
void chk_busy(); //申明检测LCD是否工作繁忙函数
void delay(); //申明延时函数1
void delay1(); //申明延时函数2,用以决定显示闪烁快慢
void Serial(void); //串口中断函数
void Mdelay(void);
//-------------------------------------------
//主程序
void main()
{
while(1)
{
//printf("$#");
MCU_Init(); //调用I/O口设置函数
lcd_init(); //调用LCD初始化函数
clear_p(); //调用清屏函数
delay();
han_wr2a(); //调用显示公司名称函数
han_wr2b(); //调用显示公司web函数
han_wr2c(); //调用显示公司tel函数
han_wr2d(); //申明显示公司fax函数
delay(); //延长显示一段时间
flash(); //调用显示闪烁函数
clear_p(); //调用清屏函数
}
}
//单片机初始化函数
void MCU_Init(void)
{
istart=0;
iend=0;
ostart=0;
oend=0;
sendactive=0;
sendfull=0;
PCON=0X80;
TMOD=0X20;
TH1=(unsigned char)(256-(XTAL/(16L*12L*baudrate)));
TL1=(unsigned char)(256-(XTAL/(16L*12L*baudrate)));
TR1=1;
SCON=0X50;
EA = 1;
ES = 1;
}
//LCD初始化函数
void lcd_init()
{
rst=0; //复位LCD
delay(); //保证复位所需要的时间
//printf("*****");
rst=1; //恢复LCD正常工作
Mdelay();
psb=1; //设置LCD为8位并口通信
send_i(0x30); //基本指令操作
send_i(0x01); //清除显示
send_i(0x06); //指定在写入或读取时,光标的移动方向
send_i(0x0c); //开显示,关光标,不闪烁
}
//-------------------------------------------
//显示公司名称函数
void han_wr2a()
{
send_i(0x80); //设置显示位置:第一行
qushu(0x10,TAB1A); //调用取数函数,共14个数据,保存在数组
}
//-------------------------------------------
//显示公司web函数
void han_wr2b()
{
send_i(0x90); //设置显示位置:第二行
qushu(0x10,TAB1B); //调用取数函数,共16个数据,保存在数组TAB1B
}
//-------------------------------------------
//显示公司tel函数
void han_wr2c()
{
send_i(0x88); //设置显示位置:第三行
qushu(0X10,TAB1C); //调用取数函数,共16个数据,保存在数组TAB1C里
}
//-------------------------------------------
//显示公司fax函数
void han_wr2d()
{
send_i(0x98); //设置显示位置:第四行
qushu(0X10,TAB1D); //调用取数函数,共16个数据,保存在数组TAB1D里
}
//有关显示设置函数
/*
void wr_zb()
{
send_i(lcd_y);
send_i(lcd_x);
}
*/
//-------------------------------------------
//显示闪烁函数
void flash()
{
send_i(0x08); //关显示
delay1(); //延长一定时间
send_i(0x0c); //开显示
delay1();
delay1(); //延长关显示两倍的时间
send_i(0x08); //关显示
delay1();
send_i(0x0c); //开显示
delay1();
delay1();
send_i(0x08); //关显示
delay1();
send_i(0x0c); //开显示
delay1();
delay1();
}
//-------------------------------------------
//清屏函数
void clear_p()
{
send_i(0x1); //清除所有显示
send_i(0x34); //扩展指令操作
send_i(0x30); //基本指令操作
}
//------------------------------------------
//查表函数
void qushu(int counts,const unsigned char *ps)
{
int i; //定义循环变量
for(i=counts;i>0;i--) //循环counts次
{
send_d(*ps); //查表取数并调用显示一个字节数据函数送显示
delay(); //延长一定时间,确保能看到数据一个个的显示出来
ps++; //取下一个数据
}
}
//-------------------------------------------
//显示一字节数据函数
void send_d(unsigned char x)
{
chk_busy(); //检测LCD是否工作繁忙
rs=1; //设置该字节数据是显示数据
rw=0; //设置该次操作为写
P0=x; //送数据口PORTD
e=1; //使能
Mdelay();
Mdelay();
Mdelay();
e=0; //禁止
}
//--------------------------------------------
//送一字节命令代码函数
void send_i(unsigned char x)
{
chk_busy(); //检测LCD是否工作繁忙
rs=0; //设置该字节数据为控制命令
rw=0; //设置此次操作为写
P0=x; //送数据口PORTD
e=1; //使能
Mdelay();
Mdelay();
e=0; //禁止
}
//-------------------------------------------
//检测LCD是否工作繁忙
void chk_busy()
{
busy=1; //先置位繁忙标志位
rs=0; //设置该字节数据为命令代码
rw=1; //设置此次操作为读
while(busy)
{
Mdelay();
Mdelay();
e=1; //使能
Mdelay();
Mdelay();
if(!P0^7)
busy=0; //检测LCD是否工作繁忙
Mdelay();
Mdelay();
e=0; //禁止
}
e=0; //禁止
}
//-------------------------------------------
//延时函数
void delay()
{
int i;
for(i=0;i<5000;i++)
{;}
}
//-------------------------------------------
//延时函数1
void delay1()
{
int i;
for(i=0;i<10;i++)
{
delay(); //调用延时函数
}
}
void Mdelay()
{
}
/***************************************串口驱动函数***************************/
void Serial()interrupt 4 using 2
{
char c;
if(RI!=0)
{
c=SBUF;
RI=0;
//snd_flag=1;
if(istart+ILEN!=iend)
{
inbuf[iend++&(ILEN-1)]=c;
}
}
if(TI!=0)
{
TI=0;
if(ostart!=oend)
{
SBUF=outbuf[ostart++&(OLEN-1)];
sendfull=0;
}
else
{
sendactive=0;
}
}
}
void putbuf(char c)
{
if(!sendfull)
{
if(!sendactive)
{
sendactive=1;
SBUF=c;
}
else
{
ES=0;
outbuf[oend++&(OLEN-1)]=c;
if(((oend^ostart)&(OLEN-1))==0)
{
sendfull=1;
}
ES=1;
}
}
}
/*********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -