#include <intrins.h>
#define dataport P1
sbit RS=P2^ 6
sbit RW=P2^5
sbit EN=P2^4
//========================
//=========================
void waitfor() //检测忙信号函数
{
dataport=0xff
RS=0RW=1_nop_() //选择指令寄存器 读 *** 作
EN=1_nop_() //使能 *** 作
while(dataport&0x80) //如果最高位是1 表示1602正忙 原地踏步 忙完后芯片会将高位拉低
EN=0
}
//======================
void writedata(unsigned char dataw) //写数据到lcm
{
waitfor() //测忙
RS=1RW=0_nop_()//选择数据寄存器 写 *** 作
dataport=dataw_nop_()//将数据送到数据口
EN=1_nop_()_nop_()EN=0 //使能
}
//==========================
void writecmd(unsigned char cmd) //写命令到lcm
{
waitfor()
RS=0RW=0_nop_()
dataport=cmd_nop_()
EN=1_nop_()_nop_()EN=0
}
//===========================
void init(void) // 初始化函数
{
writecmd(0x38)//功能设定 8位数据传输 双行显示
writecmd(0x0c)//显示器开关
writecmd(0x01)//清屏
writecmd(0x06)//字符进入模式 每进入一个字符光标向右移动一格 原有字符不动
//我在刚开始学的时候不知道下一个字符显示在哪 是和AC值有关还是和光标位置有关?
//最后摸索出来是只和光标定位有关 现在还是不知道Ac值有什么用
}
//=========================
void location(unsigned char x,unsigned char y) //确实坐标函数
{
unsigned char temp
temp=x&0x0f //只要x数据的后四位
if(y){temp=temp|0x40} //第一行为0 第二行为1 如果y=1则地址加0x40
temp|=0x80//DDRAM地址的命令DB7为一
writecmd(temp)
}
//==============================
void displyonechar(unsigned char x,unsigned char y,unsigned char dataw) //显示一个字符函数
{
location(x,y)
writedata(dataw)
}
//=======================================
void displylistchar(unsigned char x,unsigned char y,unsigned char *p) //显示字符串
{
while(*p) //当一个字符型数组读完时*P指的为零
{
displyonechar(x,y,*(p++))
x++
}
}
//=====================================================
void writecgram(unsigned char address,unsigned char *p)//写CGRAM的数据
{
unsigned char i=8
writecmd(address) //CGRAM里的地址 初始值0x40 每次加0x80
while(i--)
{
writedata(*p)
p++
}
}
//=====================================================
void displyonecharacter(unsigned char x,unsigned char y,unsigned char address,unsigned char *p) //显示一个自定义字符
{
unsigned char i=8
writecmd(address) //CGRAM里的地址 初始值0x40 每次加0x08
while(i--)
{
writedata(*p)
p++
}
//============================================================
location(x,y) //设定要显示的位置
writedata((address&=0x3f)/0x08)//要从CGRAM中读出数据在1602上显示 搞了半天发现CGRAM里的地址
} //和DDRAM里的地址有上面的转换关系
//========================================================
void displynumber(unsigned char x,unsigned char y,unsigned long num) //显示一个整数
{
unsigned int number[8]
int k,gh
for(k=0k++)
{
*(number+k)=(unsigned int)(num%10)//强制类型转换
num=num/10
if(num==0)break
}
for(gh=kgh>=0gh--)
{
displyonechar(x,y,(*(number+gh)+48))
x++
}
}
//字型码
uchar code nin[]={0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02}// "年"
uchar code yue[]={0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11}// "月"
uchar code ri[]={0x1f,0x11,0x11,0x1f,0x11,0x11,0x11,0x1f}// "日"
显示汉字
displyonecharacter(0,0,0x40,nin)
displyonecharacter(1,0,0x80,yue)
displyonecharacter(1,0,0xc0,ri)
/*本文件为12×12点阵中文字库文件,字的横向8点构成一字节,左边点在字节的高位,字符点阵四角按左上角→右上角→左下角→右下角取字*//*如你想在以后继续用HZDotReader打开文件,追加汉字点阵数据,请不要修改或增删所有注释*/
unsigned char code hzdot[90240] = {
/*啊 CB0A1 */
0x1D,0xE0,0xF4,0x20,0xB4,0x20,0xBB,0xA0,
0xB6,0xA0,0xB6,0xA0,0xF7,0xA0,0x1C,0x20,
0x10,0x20,0x10,0x20,0x10,0xE0,0x00,0x00,
/*阿 CB0A2 */
0xF7,0xE0,0x90,0x40,0xA0,0x40,0xC7,0x40,
0xA5,0x40,0x95,0x40,0x97,0x40,0xF0,0x40,
0x80,0x40,0x80,0x40,0x81,0xC0,0x00,0x00,
/*埃 CB0A3 */
0x22,0x00,0x24,0x40,0x2F,0xE0,0xF0,0x00,
0x24,0x00,0x27,0xC0,0x29,0x00,0x3F,0xE0,
0xC2,0x80,0x04,0x40,0x18,0x20,0x00,0x00,
/*挨 CB0A4 */
0x21,0x00,0x22,0x40,0xF7,0xE0,0x20,0x20,
0x24,0x00,0x37,0xC0,0xE9,0x00,0x3F,0xE0,
0x22,0x80,0x24,0x40,0x78,0x20,0x00,0x00,
/*哎 CB0A5 */
0x04,0x80,0xEF,0xE0,0xA4,0x80,0xA0,0x00,
0xA4,0x80,0xA4,0x80,0xA4,0x80,0xE3,0x00,
0x03,0x00,0x04,0x80,0x38,0x60,0x00,0x00,
/*唉 CB0A6 */
0x02,0x80,0xE4,0x40,0xAF,0xE0,0xA4,0x00,
0xA7,0xC0,0xA9,0x00,0xA9,0x00,0xFF,0xE0,
0x02,0x80,0x04,0x40,0x18,0x20,0x00,0x00,
/*哀 CB0A7 */
0x04,0x00,0xFF,0xE0,0x00,0x00,0x3F,0x80,
0x20,0x80,0x3F,0x80,0x0C,0x40,0x12,0x80,
0x31,0x00,0xD4,0x80,0x18,0x60,0x00,0x00,
/*皑 CB0A8 */
0x21,0x00,0x45,0x20,0xF7,0xE0,0x90,0x00,
0x97,0xC0,0xF0,0x40,0x97,0xC0,0x94,0x00,
0xF4,0x20,0x94,0x20,0x07,0xE0,0x00,0x00,
/*癌 CB0A9 */
0x02,0x00,0x7F,0xE0,0x48,0x40,0xCF,0xC0,
0x40,0x00,0x5D,0xE0,0xD5,0x20,0x5D,0xE0,
0x42,0x00,0x92,0x40,0x9F,0xC0,0x00,0x00,
/*蔼 CB0AA */
0x12,0x00,0xFF,0xE0,0x48,0x40,0x2F,0xC0,
0x08,0x40,0xCF,0xC0,0x48,0x60,0x59,0x20,
0x4A,0xA0,0x6F,0xA0,0x00,0xC0,0x00,0x00,
/*矮 CB0AB */
0x41,0xC0,0x4F,0x00,0xF1,0x00,0xAF,0xE0,
0x25,0x40,0xF9,0x20,0x22,0x00,0x2F,0xE0,
0x54,0x80,0x43,0x80,0x9C,0x60,0x00,0x00,
/*艾 CB0AC */
0x09,0x00,0xFF,0xE0,0x09,0x00,0x09,0x00,
0x10,0x80,0x10,0x80,0x09,0x00,0x0A,0x00,
0x06,0x00,0x19,0x00,0xE0,0xE0,0x00,0x00,
/*碍 CB0AD */
0xF7,0xC0,0x24,0x40,0x27,0xC0,0x44,0x40,
0x77,0xC0,0xD0,0x00,0x57,0xE0,0x50,0x40,
0x77,0xE0,0x52,0x40,0x01,0xC0,0x00,0x00,
/*爱 CB0AE */
0x03,0xC0,0x7C,0x80,0x15,0x00,0xFF,0xE0,
0x88,0x20,0x7F,0xC0,0x08,0x00,0x0F,0x80,
0x19,0x00,0x26,0x00,0xD9,0xE0,0x00,0x00,
/*隘 CB0AF */
0xE8,0x40,0xA4,0x80,0xBF,0xE0,0xA2,0x80,
0xE4,0x40,0x98,0x20,0x9F,0xC0,0xF5,0x40,
0x95,0x40,0x95,0x40,0xBF,0xE0,0x00,0x00,
/*鞍 CB0B0 */
0x51,0x00,0xFF,0xE0,0x54,0x20,0x71,0x00,
0x21,0x00,0xFF,0xE0,0xAA,0x40,0xFA,0x40,
0x23,0x80,0xF9,0x40,0x26,0x20,0x00,0x00,
/*氨 CB0B1 */
0x20,0x00,0x7F,0xE0,0x40,0x00,0xBF,0x80,
0x08,0x80,0x7F,0x80,0x51,0x80,0xFE,0x80,
0x24,0xA0,0x1E,0x60,0xE3,0x20,0x00,0x00,
/*安 CB0B2 */
……
……
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)