十年单片机开发方案公司深圳英锐恩分享PIC单片机实例七:SED1565型液晶串行驱动。
程序(PICC8.05)
/***********************************************
* 标题:SED1565图形LCD模块驱动 *
* 作者:Flashbird *
* 日期:2008.03.16 *
* 版本:ZHX1.0 *
* 说明:所用单片机为PIC16F877A *
***********************************************/
#include
#define NOP() asm("nop")
#define SI RB0
#define SCL RB1
#define A0 RB2
#define CS1 RB3
#define uch unsigned char
/**********************************************
* 函数:SerialInputByte(uch dat) *
* 描述:从SI口串行输入8位数据 *
**********************************************/
void SerialInputByte(uch dat)
{
uch i;
for(i=0;i<8;i++)
{
if(dat&0x80)SI=1;
else SI=0;
SCL=0; //上升沿有效
NOP();
SCL=1;
dat=dat<<1;
}
}
/**********************************************
* 函数:WriteData(uch dat) *
* 描述:从SI口串行输入要显示的数据 *
**********************************************/
void WriteData(uch dat)
{
CS1=0;
A0=1; //写数据
SerialInputByte(dat);
CS1=1;
}
/**********************************************
* 函数:WriteCommand(uch cmd) *
* 描述:从SI口串行输入命令 *
**********************************************/
void WriteCommand(uch cmd)
{
CS1=0;
A0=0; //写命令
SerialInputByte(cmd);
CS1=1;
}
/**********************************************
* 函数:ClearScreen() *
* 描述:清屏 *
**********************************************/
void ClearScreen()
{
uch i,j;
for(i=0;i<8;i++)
{
WriteCommand(0xb0+i);
WriteCommand(0x10);
WriteCommand(0x0);
for(j=0;j<132;j++)
{
WriteData(0x00);
}
}
}
/**********************************************
* 函数:SetLine(uch line) *
* 描述:设定页地址----X:0--7 *
**********************************************/
void SetLine(uch line)
{
line=line&0x07; //0<=line<=7
line=line|0xb0;
WriteCommand(line);
}
/**********************************************
* 函数:SetColumn(uch column) *
* 描述:设定列地址----Y:0--127 *
**********************************************/
void SetColumn(uch column)
{
uch column_h,column_l;
column=column&0x7f; //0<=column<=127
column_h=column&0x0f0; //列地址高四位
column_h=column>>4;
column_l=column&0x0f; //列地址低四位
column_h=column_h|0x10;
column_l=column_l|0x00;
WriteCommand(column_h);
WriteCommand(column_l);
}
/**********************************************
* 函数:SetOnOff(uch state) *
* 描述:开关显示 1:开显示 0:关显示 *
**********************************************/
void SetOnOff(uch state)
{
state=state|0xae;
WriteCommand(state);
}
/*****************************************************
* 函数:Show16X16(uch line,uch comlon,const uch *pt) *
* 描述:显示一个16X16的汉字 *
* line:显示的页(0--7) *
* column:显示的列(0--127) *
* *pt:显示字的首地址 *
*****************************************************/
void Show16X16(uch line,uch column,const uch *pt)
{
uch i,column_temp;
WriteCommand(0xa1); //反向显示(即从左到右显示)
column_temp=column;
SetColumn(column); //设置起始列
SetLine(line); //设置行
for(i=0;i<16;i++)
{
WriteData(*pt); //写数据
*pt++;//
column++;
}
SetLine(line+1); //设置汉字下半部分显示页
column=column_temp;
SetColumn(column);
for(i=0;i<16;i++)
{
WriteData(*pt);
*pt++;
column++;
}
}
//以下为测试程序
/**********************************************
* 函数:Init() *
* 描述:端口初始化 *
**********************************************/
void init()
{
TRISB=0;
PORTB=0;
}
//字库
const uch huan[]=
{0x14,0x24,0x44,0x84,0x64,0x1C,0x20,0x18,0x0F,0xE8,0x08,0x08,0x28,0x18,0x08,0x00,
0x20,0x10,0x4C,0x43,0x43,0x2C,0x20,0x10,0x0C,0x03,0x06,0x18,0x30,0x60,0x20,0x00};/*"欢",0*/
const uch ying[]=
{0x40,0x41,0xCE,0x04,0x00,0xFC,0x04,0x02,0x02,0xFC,0x04,0x04,0x04,0xFC,0x00,0x00,
0x40,0x20,0x1F,0x20,0x40,0x47,0x42,0x41,0x40,0x5F,0x40,0x42,0x44,0x43,0x40,0x00};/*"迎",1*/
const uch fang[]=
{0x40,0x41,0x4E,0xC4,0x00,0x08,0x08,0x08,0x09,0xFE,0x4A,0x48,0x48,0xC8,0x08,0x00,
0x00,0x00,0x00,0x3F,0x10,0x48,0x20,0x18,0x07,0x00,0x20,0x60,0x20,0x1F,0x00,0x00};/*"访",2*/
const uch wen[]=
{0x00,0x00,0xF8,0x01,0x06,0xE0,0x22,0x22,0x22,0x22,0xE2,0x02,0x02,0xFE,0x00,0x00,
0x00,0x00,0xFF,0x00,0x00,0x03,0x02,0x02,0x02,0x02,0x03,0x40,0x80,0x7F,0x00,0x00};/*"问",3*/
//滚动显示“欢迎访问”
void test()
{
int i;
uch set=0;
uch column=32;
while(1)
{
Show16X16(3,column,huan);
Show16X16(3,column+16,ying);
Show16X16(3,column+32,fang);
Show16X16(3,column+48,wen);
for(i=0;i<4000;i++);
if(set==0)column--;
if(set==1)
{
column++;
SetColumn(column-1);
SetLine(3);
WriteData(0); //擦除前面显示的数据
SetColumn(column-1);
SetLine(4);
WriteData(0); //擦除前面显示的数据
}
if(column==0)set=1; //如果到最左,就往右移
if((column+48)==111)set=0; //如果到最右,就往左移
}
}
void main()
{
(文源网络,侵删)