此程序不用人为选择块,只要输入位置就可以直接显示所需内容。控制器是KS0108B/A。
void WriteByte2(unsigned char dat)
{
CheckState2();
PORTA|=(1<<PA0);//SET_RS;//RS = 1;
PORTA&=~(1<<PA1);//CLR_RW;//RW = 0;
DDRB = 0XFF;
Swait();
PORTB = dat;//P2= dat;
Swait();
PORTA|=(1<<PA2);//SET_E;//E = 1;
Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
Swait();
}
void WriteByte1(unsigned char dat)
{
CheckState1();
PORTA|=(1<<PA0);//SET_RS;//RS = 1;
PORTA&=~(1<<PA1);//CLR_RW;//RW = 0;
DDRB = 0XFF;
Swait();
PORTB = dat;//P2= dat;
Swait();
PORTA|=(1<<PA2);//SET_E;//E = 1;
Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
Swait();
}
void WriteByte0(unsigned char dat)
{
CheckState0();
PORTA|=(1<<PA0);//SET_RS;//RS = 1;
PORTA&=~(1<<PA1);//CLR_RW;//RW = 0;
DDRB = 0XFF;
Swait();
PORTB = dat;//P2= dat;
Swait();
PORTA|=(1<<PA2);//SET_E;//E = 1;
Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
Swait();
}
/*-----------------------------------------------------------------------------------------------------*/
//向LCD发送命令
//command :命令
/*-----------------------------------------------------------------------------------------------------*/
void SendCommandToLCD2(unsigned char command)
{
CheckState2();
PORTA&=~(1<<PA1);//CLR_RW;//RW = 0;
DDRB = 0XFF;
Swait();
PORTB = command;//P2 = command;
Swait();
PORTA|=(1<<PA2);//SET_E;//E = 1;
Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
Swait();
}
void SendCommandToLCD1(unsigned char command)
{
CheckState1();
PORTA&=~(1<<PA1);//CLR_RW;//RW = 0;
DDRB = 0XFF;
Swait();
PORTB = command;//P2 = command;
Swait();
PORTA|=(1<<PA2);//SET_E;//E = 1;
Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
Swait();
}
void SendCommandToLCD0(unsigned char command)
{
CheckState0();
PORTA&=~(1<<PA1);//CLR_RW;//RW = 0;
DDRB = 0XFF;
Swait();
PORTB = command;//P2 = command;
Swait();
PORTA|=(1<<PA2);//SET_E;//E = 1;
Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
Swait();
}
/*----------------------------------------------------------------------------------------------------*/
//设定行地址(页)--X 0-7
/*----------------------------------------------------------------------------------------------------*/
void SetLine0(unsigned char line){
line = line & 0x07; // 0<=line<=7
Swait();
line = line | 0xb8; //1011 1xxx
Swait();
SendCommandToLCD0(line);
// SendCommandToLCD1(line);
// SendCommandToLCD2(line);
}
void SetLine1(unsigned char line){
line = line & 0x07; // 0<=line<=7
Swait();
line = line | 0xb8; //1011 1xxx
Swait();
SendCommandToLCD1(line);
// SendCommandToLCD1(line);
// SendCommandToLCD2(line);
}
void SetLine2(unsigned char line){
line = line & 0x07; // 0<=line<=7
Swait();
line = line | 0xb8; //1011 1xxx
Swait();
SendCommandToLCD2(line);
// SendCommandToLCD1(line);
// SendCommandToLCD2(line);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//设定列地址--Y 0-63
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SetColumn0(unsigned char column){
column = column & 0x3f; // 0=<column<=63
Swait();
column = column | 0x40; //01xx xxxx
Swait();
SendCommandToLCD0(column);
}
void SetColumn1(unsigned char column){
column = column & 0x3f; // 0=<column<=63
Swait();
column = column | 0x40; //01xx xxxx
Swait();
SendCommandToLCD1(column);
}
void SetColumn2(unsigned char column){
column = column & 0x3f; // 0=<column<=63
Swait();
column = column | 0x40; //01xx xxxx
Swait();
SendCommandToLCD2(column);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//设定显示开始行--XX
void SetStartLine(unsigned char startline) //0--63
{
startline=startline & 0x07;
Swait();
startline=startline | 0xc0; //1100 0000
Swait();
SendCommandToLCD0(startline);
SendCommandToLCD1(startline);
SendCommandToLCD2(startline);
}
//开关显示
void SetOnOff(unsigned char onoff)
{
onoff=0x3e | onoff; //0011 111x
Swait();
SendCommandToLCD0(onoff);
SendCommandToLCD1(onoff);
SendCommandToLCD2(onoff);
}
/*---------------------------------------------------------------------------------------------------*/
//选择屏幕
//screen: 0-全屏,1-左屏,2-右屏
void SelectScreen(unsigned char screen) //负有效 cs1: 0--右; cs2: 0--左
{
if(screen == 2)
{
PORTA|=(1<<PA3);//CLR_CS1;//CSA = 0;//右屏
Swait();
PORTA|=(1<<PA4);//SET_CS2;//CSB = 1;
Swait();
PORTA&=~(1<<PA5);//SET_CS2;//CSB = 1;
Swait();
}
if (screen == 1)
{
PORTA|=(1<<PA3);//CLR_CS2;//CSB = 0;
Swait();
PORTA&=~(1<<PA4);//SET_CS1;//CSA = 1;//左屏
Swait();
PORTA|=(1<<PA5);//SET_CS2;//CSB = 1;
Swait();
}
if (screen == 0)
{
PORTA&=~(1<<PA3);//SET_CS1;//CSA = 1;//左屏
Swait();
PORTA|=(1<<PA4);//SET_CS1;//CSA = 1;//左屏
Swait();
PORTA|=(1<<PA5);//SET_CS1;//CSA = 1;//左屏
Swait();
}
}
/*---------------------------------------------------------------------------------------------------*/
//清屏
//screen: 0-全屏,1-左屏,2-右
void ClearScreen(unsigned char screen)
{
unsigned char i,j;
// uchar k;
SelectScreen(screen);
if(screen==0)
{ for(i=0;i<8;i++)
{
SetLine0(i);
for(j=0;j<64;j++)
{
SetColumn0(j);
Swait();
WriteByte0(0x00);
Swait();
}
}
}
if(screen==1)
{ for(i=0;i<8;i++)
{
SetLine1(i);
for(j=0;j<64;j++)
{
SetColumn1(j);
Swait();
WriteByte1(0x00);
Swait();
}
}
}
if(screen==2)
{ for(i=0;i<8;i++)
{
SetLine2(i);
for(j=0;j<64;j++)
{
SetColumn2(j);
Swait();
WriteByte2(0x00);
Swait();
}
}
}
}
void ClearScreenAll()
{
SelectScreen(0);
SelectScreen(1);
SelectScreen(2);
}
/*--------------------------------------------------------------------------------------------------
//读显示数据
unsigned char ReadByte()
{
// uint i;
uchar dat;
CheckState();
RS = 1;
RW = 1;
Swait();
P2 = 0xff;
Swait();
E = 1;
Swait();
dat = P0;
Swait();
E = 0;
return(dat);
}
---------------------------------------------------------------------------------------------------*/
void InitLcd() //初始化LCD 不显示的问题
{
unsigned char i=250; //延时
while(i--);
SetOnOff(0); //关显示
//SelectScreen(2);
//SetStartLine(0); //开始行:0
//SelectScreen(1);
//SetStartLine(0); //开始行:0
//SetColumn(0);
// ClearScreen(1);//清屏
// ClearScreen(2);
//SelectScreen(0);
SetOnOff(1); //开显示
//SelectScreen(0);
//SetStartLine(0); //开始行:0
SelectScreen(2);
SetStartLine(0); //开始行:0
SelectScreen(1);
SetStartLine(0); //开始行:0
SelectScreen(0);
SetStartLine(0);
ClearScreen(1);//清屏
ClearScreen(2);
ClearScreen(0);
}
void CheckState2()
{
SelectScreen(2);
uchar temp; //汇编中,这之前有个片选
do{
PORTA|=(1<<PA1);//SET_RW;//RW = 1;
PORTA&=~(1<<PA0);//CLR_RS;//RS = 0;在这一句后面,汇编里面把数据口都置为1了,我没有改,看效果
Swait();
//P2 = 0XFF;
PORTA|=(1<<PA2);//SET_E;//E = 1;
//PORTB = 0XFF;
DDRB = 0X00;
Swait();
temp = PINB;//temp = P2;
templong = temp;
//Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
temp = 0x80 & temp; //当第7bit为0时可以操作
}while(!(temp == 0));
}
void CheckState1()
{
SelectScreen(1);
uchar temp; //汇编中,这之前有个片选
do{
PORTA|=(1<<PA1);//SET_RW;//RW = 1;
PORTA&=~(1<<PA0);//CLR_RS;//RS = 0;在这一句后面,汇编里面把数据口都置为1了,我没有改,看效果
Swait();
//P2 = 0XFF;
PORTA|=(1<<PA2);//SET_E;//E = 1;
//PORTB = 0XFF;
DDRB = 0X00;
Swait();
temp = PINB;//temp = P2;
templong = temp;
//Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
temp = 0x80 & temp; //当第7bit为0时可以操作
}while(!(temp == 0));
}
void CheckState0()
{
SelectScreen(0);
uchar temp; //汇编中,这之前有个片选
do{
PORTA|=(1<<PA1);//SET_RW;//RW = 1;
PORTA&=~(1<<PA0);//CLR_RS;//RS = 0;在这一句后面,汇编里面把数据口都置为1了,我没有改,看效果
Swait();
//P2 = 0XFF;
PORTA|=(1<<PA2);//SET_E;//E = 1;
//PORTB = 0XFF;
DDRB = 0X00;
Swait();
temp = PINB;//temp = P2;
templong = temp;
//Swait();
PORTA&=~(1<<PA2);//CLR_E;//E = 0;
temp = 0x80 & temp; //当第7bit为0时可以操作
}while(!(temp == 0));
}
void Show(uchar lin,uchar column,uchar linechar,uchar columnchar,const uchar *p,uchar num)
{
uchar i,j,k,screenbz;
const uchar *p1;
p1=p + 32 * num;
k=column;
screenbz=0;
if(k>192) {return;}
if(k<64)
{
SelectScreen(0); //如果列数<8(0,1,2,3,4,5,6,7)则写在第一屏上
SetColumn0(k);
screenbz=0;
}
else if(k<128)
{
SelectScreen(1); //否则 (8,9,10,11,12,13,14,15)写在第二屏上
//k=k-64; 修改20090410--1 这一句在这里有点多余,而且在后面判断的时候会引发错误
SetColumn1(k);
screenbz=1;
}
else
{
SelectScreen(2);
//k=k-128; //这里同上,没有必要做减法
SetColumn2(k);
screenbz=2;
}
SetLine0(lin);
SetLine1(lin);
SetLine2(lin);
//SetColumn0(k);
for(i=lin;i<linechar+lin;i++)
{
if(screenbz==0)
{
for(j=k;j<k+columnchar;j++)
{
if(j>=64)
{
SelectScreen(1);
SetColumn1(0);
SetLine1(i);
for(j=64;j<k+columnchar;j++)//这里应该是从64开始往上加,但是总的上界不边,原来从K开始加的话,就又回去了
{
// if(j==128)
// {
// SelectScreen(2);
// SetColumn2(0);
// SetLine2(i);
// for(j=128;j<k+columnchar;j++)
// {
// WriteByte2(pgm_read_byte(p1));
// p1++;
// }
// }
WriteByte1(pgm_read_byte(p1));
p1++;
}
}
//if(j<64)
else
{
WriteByte0(pgm_read_byte(p1));
p1++;
}
}
SelectScreen(0);
SetLine0(i+1);
SetColumn0(k);
}
if(screenbz==1)
{
for(j=k;j<k+columnchar;j++)
{
if(j>=128) //对应前面的 修改20090410--1 ,如果前面不修改,这里的K根本到不了128,也就是到64的时候,事实上屏幕已经到边了,但
是数据还继续往里面写
{
SelectScreen(2);
SetColumn2(0);
SetLine2(i);
for(j=128;j<k+columnchar;j++)
{
WriteByte2(pgm_read_byte(p1));
p1++;
}
}
//if(j<128)
else
{
WriteByte1(pgm_read_byte(p1));
p1++;
}
}
SelectScreen(1);
SetLine1(i+1);
SetColumn1(k);
}
if(screenbz==2)
{
for(j=k;j<k+columnchar;j++)
{
WriteByte2(pgm_read_byte(p1));
p1++;
}
SelectScreen(2);
SetLine2(i+1);
SetColumn2(k);
}
}
}
void ShowChar(uchar lin,uchar column,uchar linechar,uchar columnchar,const uchar *p,uchar num)
{
uchar i,j,k,screenbz;
const uchar *p1;
p1=p + 16 * num;
k=column;
screenbz=0;
if(k>192) {return;}
if(k<64)
{
SelectScreen(0); //如果列数<8(0,1,2,3,4,5,6,7)则写在第一屏上
screenbz=0;
SetColumn0(k);
}
else if(k<128)
{
SelectScreen(1); //如果列数<8(0,1,2,3,4,5,6,7)则写在第一屏上
k=k-64;
SetColumn1(k);
screenbz=1;
}
else
{
SelectScreen(2); //否则 (8,9,10,11,12,13,14,15)写在第二屏上
k=k-128;
SetColumn2(k);
screenbz=2;
}
SetLine0(lin);
SetLine1(lin);
SetLine2(lin);
//SetColumn0(k);
for(i=lin;i<linechar+lin;i++)
{
if(screenbz==0)
{
for(j=k;j<k+columnchar;j++)
{
if(j>=64)
{
SelectScreen(1);
SetColumn1(0);
SetLine1(i);
for(j=64;j<k+columnchar;j++)//这里应该是从64开始往上加,但是总的上界不边,原来从K开始加的话,就又回去了
{
// if(j==128)
// {
// SelectScreen(2);
// SetColumn2(0);
// SetLine2(i);
// for(j=128;j<k+columnchar;j++)
// {
// WriteByte2(pgm_read_byte(p1));
// p1++;
// }
// }
WriteByte1(pgm_read_byte(p1));
p1++;
}
}
//if(j<64)
else
{
WriteByte0(pgm_read_byte(p1));
p1++;
}
}
SelectScreen(0);
SetLine0(i+1);
SetColumn0(k);
}
if(screenbz==1)
{
for(j=k;j<k+columnchar;j++)
{
if(j>=128) //对应前面的 修改20090410--1 ,如果前面不修改,这里的K根本到不了128,也就是到64的时候,事实上屏幕已经到边了,但
是数据还继续往里面写
{
SelectScreen(2);
SetColumn2(0);
SetLine2(i);
for(j=128;j<k+columnchar;j++)
{
WriteByte2(pgm_read_byte(p1));
p1++;
}
}
//if(j<128)
else
{
WriteByte1(pgm_read_byte(p1));
p1++;
}
}
SelectScreen(1);
SetLine1(i+1);
SetColumn1(k);
}
if(screenbz==2)
{
for(j=k;j<k+columnchar;j++)
{
WriteByte2(pgm_read_byte(p1));
p1++;
}
SelectScreen(2);
SetLine2(i+1);
SetColumn2(k);
}
}
}
void ShowCharLittle(uchar lin,uchar column,uchar linechar,uchar columnchar,const uchar *p,uchar num)
{
uchar i,j,k,screenbz;
const uchar *p1;
p1=p + 6 * num;
k=column;
screenbz=0;
if(k>192) {return;}
if(k<64)
{
SelectScreen(0); //如果列数<8(0,1,2,3,4,5,6,7)则写在第一屏上
screenbz=0;
SetColumn0(k);
}
if(k>63&&k<128)
{
SelectScreen(1); //如果列数<8(0,1,2,3,4,5,6,7)则写在第一屏上
k=k-64;
SetColumn1(k);
screenbz=1;
}
else
{
SelectScreen(2); //否则 (8,9,10,11,12,13,14,15)写在第二屏上
k=k-128;
SetColumn2(k);
screenbz=2;
}
SetLine0(lin);
SetLine1(lin);
SetLine2(lin);
//SetColumn0(k);
for(i=lin;i<linechar+lin;i++)
{
if(screenbz==0)
{
for(j=k;j<k+columnchar;j++)
{
if(j==64)
{
SelectScreen(1);
SetColumn1(0);
SetLine1(i);
for(j=k;j<k+columnchar;j++)
{
if(j==128)
{
SelectScreen(2);
SetColumn2(0);
SetLine2(i);
for(j=k;j<k+columnchar;j++)
{
WriteByte2(pgm_read_byte(p1));
p1++;
}
}
WriteByte1(pgm_read_byte(p1));
p1++;
}
}
WriteByte0(pgm_read_byte(p1));
p1++;
}
SelectScreen(1);
SetLine0(i+1);
SetColumn0(k);
}
if(screenbz==1)
{
for(j=k;j<k+columnchar;j++)
{
if(j==128)
{
SelectScreen(2);
SetColumn2(0);
SetLine2(i);
for(j=k;j<k+columnchar;j++)
{
WriteByte2(pgm_read_byte(p1));
p1++;
}
}
WriteByte1(pgm_read_byte(p1));
p1++;
}
SelectScreen(1);
SetLine1(i+1);
SetColumn1(k);
}
if(screenbz==2)
{
for(j=k;j<k+columnchar;j++)
{
WriteByte2(pgm_read_byte(p1));
p1++;
}
SelectScreen(2);
SetLine2(i+1);
SetColumn1(k);
}
}
}
|