MSP430驱动128643图形点阵液晶KS0108.docx
《MSP430驱动128643图形点阵液晶KS0108.docx》由会员分享,可在线阅读,更多相关《MSP430驱动128643图形点阵液晶KS0108.docx(13页珍藏版)》请在冰豆网上搜索。
MSP430驱动128643图形点阵液晶KS0108
MSP430驱动12864-3图形点阵液晶(KS0108)
作者:
jdzj868来源:
机电之家下载站录入:
jdzj868更新时间:
2009-12-716:
44:
28点击数:
0
【字体:
】
#include
//***************数据类型定义****************************
#defineuintunsignedint
#defineucharunsignedchar
//***************液晶控制线定义****************************
#defineRSBIT1//P3.1
#defineRWBIT0//P3.0
#defineEBIT7//P2.7
#defineCS1BIT3//P3.3
#defineCS2BIT2//P3.2
#defineRSTBIT1//P2.1
//数据线:
p4口
//**************汉字定义为16*16,纵向,大小10,D0--D7,字体TimesNewRoman**************
constunsignedcharcode1[]={64,66,66,66,254,66,66,66,66,254,66,67,98,64,0,0,32,32,16,8,7,0,0,0,0,63,0,0,0,0,0,0};//开
constunsignedcharcode2[]={8,8,248,15,8,248,32,48,44,35,32,40,48,96,0,0,32,17,10,4,10,17,0,63,17,17,17,17,63,0,0,0};//始
constunsignedcharcode3[]={8,8,136,255,72,72,4,4,4,4,252,4,6,4,0,0,1,17,32,31,0,0,0,0,16,32,31,0,0,0,0,0};//打
constunsignedcharcode4[]={0,252,68,68,66,67,2,0,252,4,4,4,252,0,0,0,0,15,8,4,4,2,2,0,63,0,2,4,3,0,0,0};//印
constunsignedcharcode5[]={4,4,132,196,52,12,23,20,20,212,52,20,6,4,0,0,2,1,0,63,0,1,1,17,33,31,1,1,1,1,0,0};//存
constunsignedcharcode6[]={64,32,248,7,64,204,32,40,168,255,168,176,172,32,0,0,0,0,63,0,0,31,10,1,63,20,20,20,63,0,0,0};//储
constunsignedcharcode7[]={16,97,134,64,254,2,250,2,254,0,252,0,255,0,0,0,2,62,1,32,19,8,7,8,51,0,19,32,31,0,0,0};//测
constunsignedcharcode8[]={0,32,32,190,170,170,170,170,170,170,170,191,34,32,0,0,0,32,40,47,42,42,42,63,42,42,42,47,40,32,0,0};//量
constunsignedcharcode9[]={32,33,230,0,128,136,136,255,136,136,255,136,136,136,0,0,16,8,7,8,16,40,36,35,32,32,47,32,32,32,0,0};//进
constunsignedcharcode10[]={16,136,68,227,24,32,34,34,34,34,226,35,50,32,0,0,1,0,0,63,0,0,0,0,16,32,31,0,0,0,0,0};//行
constunsignedcharcode11[]={0,248,8,8,8,8,255,8,8,8,8,252,8,0,0,0,0,1,1,1,1,1,63,1,1,1,1,1,0,0,0,0};//中
constunsignedcharcode12[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,8,0,0,0,8,0,0,0,0,0,0};//点
constunsignedcharcode100[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32};//线
//******************函数声明*******************************
voidInit_Clock(void);
voidInit_IO(void);
voidInit_LCD(void);
voiddelay(uintus);
voiddelayl(uintms);
voidWriteCommand_L(ucharcom1);
voidWriteCommand_R(ucharcom2);
voidWriteData_L(uchardat1);
voidWriteData_R(uchardat2);
voidClearScreen(void);
voidAllScreen(void);
voidStateCheck(void);
voidWriteWord(ucharcolum,ucharpage,constuchar*code);
voidDrawLine(void);
//*************系统主程序**********************************
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//关闭看门狗
Init_Clock();
Init_IO();
Init_LCD();
WriteWord(0,6,code1);
WriteWord(16,6,code2);
WriteWord(48,6,code3);
WriteWord(64,6,code4);
WriteWord(96,6,code5);
WriteWord(112,6,code6);
WriteWord(16,2,code7);
WriteWord(32,2,code8);
WriteWord(48,2,code9);
WriteWord(64,2,code10);
WriteWord(80,2,code11);
WriteWord(96,2,code12);
DrawLine();
}
//**********************IO初始化***************************
voidInit_IO(void)
{
P1DIR=0;//设置为输入
P1SEL=0;//设置为一般IO
P1OUT=0X00;
P2DIR=0;
P2SEL=0;
P2OUT=0X00;
P3DIR=0;
P3SEL=0;
P3OUT=0X00;
P4DIR=0;
P4SEL=0;
P4OUT=0X00;
P5DIR=0;
P5SEL=0;
P5OUT=0X00;
P6DIR=0;
P6SEL=0;
P6OUT=0X00;
}
//*****************高频时钟初始化**************************
voidInit_Clock(void)
{
//UTCTL1=SSEL0;
//BCSCTL1=0X00;
//BCSCTL2=0X00;
unsignedintiq0;
BCSCTL1&=~XT2OFF;//打开XT2振荡器
do
{
IFG1&=~OFIFG;//清除振荡器失效标志
for(iq0=0xFF;iq0>0;iq0--);//延时,等待XT2起振
}
while((IFG1&OFIFG)!
=0);//判断XT2是否起振
BCSCTL2=SELM_2+SELS;//选择MCLK、SMCLK为XT2,
BCSCTL2|=DIVM_2;//MCLK4分频MCLK=2MHZ
BCSCTL2|=DIVS_2;//SMCLK4分频SMCLK=2MHZ
}
//***************延时子程序******************************
voiddelay(uintus)
{
while(us--);
}
voiddelayl(uintms)
{
uinti,j;
for(i=0;ifor(j=0;j<1000;j++)
;
}
//*****************状态检查*********************************
voidStateCheck(void)
{ucharcradd1;
P3OUT&=~RS;
P3OUT|=RW;//RS=0,R/W=1,以便读液晶状态
P4DIR=0x00;//P4口为输入口
do
{
P2OUT|=E;//E=1
cradd1=P4IN;
P2OUT&=~E;//E=0
}
while((cradd1&0x80)!
=0);
}
//***************写指令代码子程序(左)*******************
voidWriteCommand_L(ucharcom1)
{P3OUT&=~CS1;//cs1=0
P3OUT|=CS2;//cs2=1选择左半屏
StateCheck();//检查是否忙
P3OUT&=~RW;//rw=0
P4DIR=0xff;//P4口为输出口
P4OUT=com1;
P2OUT|=E;//E=1
P2OUT&=~E;//E=0
}
//*****************写指令代码子程序(右)*******************
voidWriteCommand_R(ucharcom2)
{P3OUT|=CS1;//cs1=1
P3OUT&=~CS2;//cs2=0
StateCheck();//检查是否忙
P3OUT&=~RW;//rw=0
P4DIR=0xff;//P4口为输出口
P4OUT=com2;
P2OUT|=E;//E=1
P2OUT&=~E;//E=0
}
//****************写显示数据子程序(左)**********************
voidWriteData_L(uchardat1)
{
P3OUT&=~CS1;//cs1=0
P3OUT|=CS2;//cs2=1
StateCheck();//检查是否忙
P3OUT|=RS;//rs=1
P3OUT&=~RW;//rw=0
P4DIR=0xff;//P4口为输出口
P4OUT=dat1;
P2OUT|=E;//E=1
P2OUT&=~E;//E=0
}
//****************写显示数据子程序(右)**********************
voidWriteData_R(uchardat2)
{
P3OUT|=CS1;//cs1=1
P3OUT&=~CS2;//cs2=0
StateCheck();//检查是否忙
P3OUT|=RS;//rs=1
P3OUT&=~RW;//rw=0
P4DIR=0xff;//P4口为输出口
P4OUT=dat2;
P2OUT|=E;//E=1
P2OUT&=~E;//E=0
}
//******************清屏************************
voidClearScreen(void)
{uchari,j;
for(i=0;i<8;i++)
{WriteCommand_L(i|0xb8);//设置页地址
WriteCommand_R(i|0xb8);
WriteCommand_L(0x40);//设置列地址
WriteCommand_R(0x40);
for(j=0;j<64;j++)//写0x00
{WriteData_L(0x00);
WriteData_R(0x00);
}
}
}
//******************满屏************************
voidAllScreen(void)
{uchari,j;
for(i=0;i<8;i++)
{WriteCommand_L(i|0xb8);//设置页地址
WriteCommand_R(i|0xb8);
WriteCommand_L(0x40);//设置列地址
WriteCommand_R(0x40);
for(j=0;j<64;j++)//写0xff
{WriteData_L(0xff);
WriteData_R(0xff);
}
}
}
//****************LCD初始化******************************
voidInit_LCD(void)
{
P2DIR|=BIT1;//P2.7为输出口
P2OUT&=~RST;//RST=0,LCD复位
delayl(50);//延时
P2OUT|=RST;//rst=1
delayl(50);
P3DIR|=BIT3;//P3.3为输出口
P3OUT&=~CS1;//CS1=0
P3DIR|=BIT2;//P3.2为输出口
P3OUT&=~CS2;//CS2=0
P3DIR|=BIT1;//P3.1为输出口
P3OUT&=~RS;//RS=0
P3DIR|=BIT0;//P3.0为输出口
P3OUT&=~RW;//RW=0
P2DIR|=BIT7;//P2.7为输出口
P2OUT&=~E;//E=0
WriteCommand_L(0xc0);WriteCommand_R(0xc0);//显示开始行
WriteCommand_L(0x3f);WriteCommand_R(0x3f);//开显示
ClearScreen();
}
//***********************写汉字*********************************
voidWriteWord(ucharcolum,ucharpage,constuchar*code)//page2:
页colum2:
列code2:
字代码
{
uchari,j,colum0;
ucharflag;
for(j=0;j<2;j++)
{WriteCommand_L(page|0xb8);//页
WriteCommand_R(page|0xb8);
colum0=colum;
if(colum0>63)//右屏
{colum0=colum0-64;
WriteCommand_R(colum0|0x40);
flag=1;
}
else//左屏
{
WriteCommand_L(colum0|0x40);
flag=0;
}
//*********************
for(i=0;i<16;i++)
{
if(flag==0)
{
WriteData_L(*code);
}
else
{
WriteData_R(*code);
}
code++;
if(colum0==64)//列=64
{
colum0=0;
if(flag==0)
{
flag=1;
WriteCommand_R(colum0|0x40);
}
else
{
break;
}
}
//colum++;
}
page++;
}
}
//*******************画线*********************************
voidDrawLine(void)
{
uchari,j=0;
for(i=0;i<8;i++)
{
WriteWord(j,4,code100);
j+=16;
}
}
调试日记:
8月19日;
出现问题:
液晶无法正常显示。
找到原因:
1、12864-3(KS0108)出厂设置为5V供电,可3V供电,需改焊控制板上的两个焊盘。
2、外接10K可调电阻(用于背光调节)没有正确调节。
解决办法:
1、更改液晶板上的供电系统,将电源端由5V改焊到3V的通路上(此为改更液晶背面的控制板,而非单片机电路。
)
2、调节10K可调电阻,使1、2端之间电阻(即接入电路中的电阻)大约在8K左右,这时对比度正常。