DataSheet: JM12864M.rar
I/O接口与LCD引脚的连接详见源代码注释.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
12864源代码
//带中文字库的12864LCD显示程序
#include <reg52.H>
#include <intrins.H>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#define uchar unsigned char
#define uint unsigned int
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
sbit dula = P2^6;
sbit wela = P2^7;
//SCM12864-05端口定义
#define LCD_DATA P0 //数据口
sbit RS = P3^5; //并行的指令/数据选择信号, H数据, L命令
sbit RW = P3^6; //并行读写选择信号, H读, L写
sbit E = P3^4; //并行使能端, H有效, L无效
sbit PSB = P3^7; //并/串接口选择, H并,L串
sbit RET = P3^3; //复位, L有效
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
uchar code datas1[] =
{"广东工业大学"};
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
uchar code datas2[] =
{"自动化学院2004级"};
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
uchar code datas3[] =
{"网络工程 (4)班"};
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
uchar code datas4[] =
{"fengmk2@gmail.com"};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//延时约2us
void delayUs()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
_nop_();_nop_();
}
//延时 a * 1ms
void delayMs(uint a)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
uint i, j;
for(i = a; i > 0; i--)
for(j = 100; j > 0; j--);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//检测LCD是否处于忙状态, 若忙返回1, 空闲返回0
bit checkBusy()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
bit busy;
RS = 0;
RW = 1;
E = 1;
delayUs();
busy = (bit)(LCD_DATA&0x80);
E = 0;
return busy;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//等待LCD到空闲
void wait()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
while(checkBusy());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//写命令
void writeCmd(uchar cmd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
wait();
RS = 0;
RW = 0;
E = 0;
delayUs();
LCD_DATA = cmd;
delayUs();
E = 1;
delayUs();
E = 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//写数据
void writeData(uchar dat)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
wait();
RS = 1;
RW = 0;
E = 0;
delayUs();
LCD_DATA = dat;
delayUs();
E = 1;
delayUs();
E = 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//初始化LCD
void init()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
PSB = 1; //并口方式
writeCmd(0x30); //基本指令, 扩充指令为34H
delayMs(10);
writeCmd(0x0c); //显示开, 关光标
delayMs(10);
writeCmd(0x01); //清屏
delayMs(10);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void setPosition(uchar x, uchar y)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
uchar p;
switch(x%4)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 0: p = 0x80; break; //第一行开始地址
case 1: p = 0x90; break; //第二行
case 2: p = 0x88; break; //第三行
case 3: p = 0x98; break; //第四行
}
p += y;
writeCmd(p);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void writeString(uchar * str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
uchar i = 0;
while(str[i] != '\0')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
writeData(str[i++]);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//***************************LCD12864***************************************/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//先关闭数码管, 使两个锁存器锁存, 因开了数码管, USB电压驱动不够, 将无法使LCD正常工作
dula = wela = 0;
init();
setPosition(0, 0);
writeString(datas1);
setPosition(1, 0);
writeString(datas2);
setPosition(2, 0);
writeString(datas3);
setPosition(3, 0);
writeString(datas4);
while(1);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
效果图: