lcd12864.c
#include "lcd12864.h"
void LcdSt7565_WriteCmd(cmd) //寫入一個命令到12864
{
LCD12864_CS = 0; //chip select勇婴,打開片選
LCD12864_RD = 1; //disable read唇辨,讀使能
LCD12864_RS = 0; //select command鳞贷,選擇命令
LCD12864_RW = 0; //select write状答,選擇寫模式
_nop_();
_nop_();
DATA_PORT = cmd; //put command,寫入命令
_nop_();
_nop_();
LCD12864_RW = 1; //command writing捺萌,寫入命令
}
void LcdSt7565_WriteData(dat) //寫入一個數(shù)據(jù)到12864
{
LCD12864_CS = 0; //chip select脐往,打開片選
LCD12864_RD = 1; //disable read,讀使能
LCD12864_RS = 1; //select data吼渡,選擇數(shù)據(jù)
LCD12864_RW = 0; //select write,選擇寫模式
_nop_();
_nop_();
DATA_PORT = dat; //put data乓序,寫入數(shù)據(jù)
_nop_();
_nop_();
LCD12864_RW = 1; //data writing寺酪,寫入數(shù)據(jù)
}
void Lcd12864_Init()
{
uchar i;
LCD12864_RSET = 0;
for(i=0;i<100;i++);
LCD12864_CS = 0;
LCD12864_RSET = 1;
LcdSt7565_WriteCmd(0xE2);
for(i=0;i<100;i++);
LcdSt7565_WriteCmd(0xa0); //表格第8個命令,0xA0段(左右)方向選擇正常方向(0xA1為反方向)
LcdSt7565_WriteCmd(0xC8); //表格第15個命令替劈,0xC8普通(上下)方向選擇選擇反向寄雀,0xC0為正常方向
LcdSt7565_WriteCmd(0xA6); //-表格第9個命令,0xA6為設(shè)置字體為黑色抬纸,背景為白色. 0xA7為設(shè)置字體為白色咙俩,背景為黑色
//--表格第10個命令耿戚,0xA4像素正常顯示湿故,0xA5像素全開--//
LcdSt7565_WriteCmd(0xA4); //normal display
//--表格第11個命令阿趁,0xA3偏壓為1/7,0xA2偏壓為1/9--//
LcdSt7565_WriteCmd(0xA2); //bias set 1/9
//--表格第19個命令,這個是個雙字節(jié)的命令坛猪,0xF800選擇增壓為4X;--//
//--0xF801,選擇增壓為5X脖阵,其實效果差不多--//
LcdSt7565_WriteCmd(0xF8); //Boost ratio set
LcdSt7565_WriteCmd(0x01); //x4
//--表格第18個命令,這個是個雙字節(jié)命令墅茉,高字節(jié)為0X81命黔,低字節(jié)可以--//
//--選擇從0x00到0X3F。用來設(shè)置背景光對比度就斤。---/
LcdSt7565_WriteCmd(0x81); //V0 a set
LcdSt7565_WriteCmd(0x23);
//--表格第17個命令悍募,選擇調(diào)節(jié)電阻率--//
LcdSt7565_WriteCmd(0x25); //Ra/Rb set
//--表格第16個命令,電源設(shè)置洋机。--//
LcdSt7565_WriteCmd(0x2F);
for (i=0; i<100; i++);
//--表格第2個命令坠宴,設(shè)置顯示開始位置--//
LcdSt7565_WriteCmd(0x40); //start line
//--表格第1個命令,開啟顯示--//
LcdSt7565_WriteCmd(0xAF); // display on
for (i=0; i<100; i++);
}
void Lcd12864_ClearScreen()
{
uchar i,j;
for(i=0;i<8;i++){
//y軸有64個绷旗,一個坐標8位喜鼓,就總共有8個坐標//
//所有我們所使用的坐標是0xb0到0xb7//
LcdSt7565_WriteCmd(0xb0+i); // y軸坐標
//上面48行,當初始化是0xa1時衔肢,x坐標從(0x10,0x04)到(0x18,0x04),一共128位//
//當你的段初始化為0xA0時庄岖,X坐標從(0x10,0x00)到(0x18,0x00),一共128位//
LcdSt7565_WriteCmd(0x10); // 我們初始化是0xa0,設(shè)置x軸坐標
LcdSt7565_WriteCmd(0x00);
for(j=0;j<128;j++){ //x軸有128位,就一共刷128次,x坐標會自動加1角骤,所以不需要再次設(shè)置坐標
LcdSt7565_WriteData(0x00);
}
}
}
#ifdef CHAR_CODE
#include "charcode.h"
uchar Lcd12864_Write16CnCHAR(uchar x,uchar y,uchar *cn)
{
uchar j,x1,x2,wordNum;
if(y > 7){
return 0;
} //Y的坐標只能從0到7隅忿,大于則直接返回
if(x > 128){
return 0;
} //X的坐標只能從0到128,大于則直接返回
y += 0xb0; //得到y(tǒng)坐標的值
LcdSt7565_WriteCmd(y); //設(shè)置Y坐標
while(*cn !='\0')
{
LcdSt7565_WriteCmd(y);
x1 = (x>>4)&0x0f;
x2 = x&0x0f;
LcdSt7565_WriteCmd(0x10+x1);
LcdSt7565_WriteCmd(0x00+x2); //設(shè)置x坐標
for(wordNum=0;wordNum<50;wordNum++) //在字庫里搜索启搂,我們這里假設(shè)有50個漢字
{
if ((CN16CHAR[wordNum].Index[0] == *cn)
&&(CN16CHAR[wordNum].Index[1] == *(cn+1)))
{
for(j=0;j<32;j++) //寫入相應漢字的顯示矩陣共32個數(shù)
{
if(j==16)
{ //漢字的上面一半寫完了硼控,重新定義下面一半的坐標
LcdSt7565_WriteCmd(y+1);
LcdSt7565_WriteCmd(0x10+x1); //x坐標的高4位
LcdSt7565_WriteCmd(0x00+x2); //x坐標的低4位
}
LcdSt7565_WriteData(CN16CHAR[wordNum].Msk[j]); //顯示漢字的上面一半
}
x +=16; //第一個漢字寫完了,坐標右移胳赌,漢字占的是16*16
}//if查字結(jié)束
} //for查字結(jié)束
cn +=2; // 指針加2牢撼,指向第二個數(shù)字
}
return 1;
}
#endif
main.c
#include "reg52.h"
#include "lcd12864.h"
typedef unsigned int u16;
typedef unsigned char u8;
void Delay10ms(uint c)
{
uchar a,b;
for(;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void main()
{
u8 i;
Lcd12864_Init();
Lcd12864_ClearScreen();
while(1){
for(i=0;i<8;i+=2){ //兩行構(gòu)成一個漢字,可寫四行漢字
Lcd12864_ClearScreen();
Lcd12864_Write16CnCHAR(0,i,"歡迎使用--開拓者");
Delay10ms(100);
}
}
}
lcd12864.h
//自定義頭文件名//
#ifndef _lcd12864_h
#define _lcd12864_h
//包含頭文件//
#include<reg52.h>
#include<intrins.h>
#define CHAR_CODE
//快捷定義//
#ifndef uint
#define uint unsigned int
#endif
#ifndef uchar
#define uchar unsigned char
#endif
//引腳//
#define DATA_PORT P0
sbit LCD12864_CS = P3^2;
sbit LCD12864_RD = P2^6;
sbit LCD12864_RW = P2^5;
sbit LCD12864_RSET = P3^3;
sbit LCD12864_RS = P2^7;
//函數(shù)//
void LcdSt7565_WriteCmd(cmd);
void LcdSt7565_WriteData(dat);
void Lcd12864_Init();
void Lcd12864_ClearScreen();
uchar Lcd12864_Write16CnCHAR(uchar x,uchar y,uchar *cn);
#endif
charcode.h
#ifndef __CHARCODE_H
#define __CHARCODE_H
#ifdef CHAR_CODE
// ------------------ 漢字字模的數(shù)據(jù)結(jié)構(gòu)定義 ------------------------ //
struct Cn16CharTypeDef // 漢字字模數(shù)據(jù)結(jié)構(gòu)
{
unsigned char Index[2]; // 漢字內(nèi)碼索引,一個漢字占兩個字節(jié)
unsigned char Msk[32]; // 點陣碼數(shù)據(jù)(16*16有32個數(shù)據(jù))
};
//縱向取模疑苫,字節(jié)倒序, 宋體小四
struct Cn16CharTypeDef code CN16CHAR[]=
{
/*-- 文字: 歡 --*/
/*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
"歡",0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00,
0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00,
/*-- 文字: 迎 --*/
/*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
"迎",0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00,
0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00,
/*-- 文字: 使 --*/
/*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
"使",0x80,0x60,0xF8,0x07,0x04,0xE4,0x24,0x24,0x24,0xFF,0x24,0x24,0x24,0xE4,0x04,0x00,
0x00,0x00,0xFF,0x00,0x80,0x81,0x45,0x29,0x11,0x2F,0x41,0x41,0x81,0x81,0x80,0x00,
/*-- 文字: 用 --*/
/*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
"用",0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,0x00,
0x80,0x60,0x1F,0x02,0x02,0x02,0x02,0x7F,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,0x00,
/*-- 文字: — --*/
/*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
"--",0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*-- 文字: 開 --*/
/*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
"開",0x80,0x82,0x82,0x82,0xFE,0x82,0x82,0x82,0x82,0x82,0xFE,0x82,0x82,0x82,0x80,0x00,
0x00,0x80,0x40,0x30,0x0F,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,
/*-- 文字: 拓 --*/
/*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
"拓",0x10,0x10,0x10,0xFF,0x10,0x90,0x04,0x04,0x84,0xE4,0x9C,0x84,0x84,0x84,0x04,0x00,
0x04,0x44,0x82,0x7F,0x01,0x04,0x02,0x01,0xFF,0x40,0x40,0x40,0x40,0xFF,0x00,0x00,
/*-- 文字: 者 --*/
/*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
"者",0x20,0x20,0x24,0x24,0x24,0x24,0xBF,0x64,0x24,0x34,0x28,0x24,0x22,0x20,0x20,0x00,
0x10,0x08,0x04,0x02,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,0x00,
};
#endif //end of CHAR_CODE
#endif