当前位置:
首页 >
单片机技术文章 >
MSP430单片机 > 详细内容
- MSP430驱动12864-3图形点阵液晶(KS0108)
- 发布时间:2009/7/27 阅读次数:934 字体大小: 【小】 【中】【大】
#include <MSP430x14x.h>
//***************数据类型定义****************************
#define uint unsigned int
#define uchar unsigned char
//***************液晶控制线定义****************************
#define RS BIT1 //P3.1
#define RW BIT0 //P3.0
#define E BIT7 //P2.7
#define CS1 BIT3 //P3.3
#define CS2 BIT2 //P3.2
#define RST BIT1 //P2.1
//数据线:p4口
//**************汉字定义为16*16,纵向,大小10,D0--D7 ,字体 Times NewRoman**************
const unsigned char code1[]={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};//开
const unsigned char code2[]={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};//始
const unsigned char code3[]={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};//打
const unsigned char code4[]={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};//印
const unsigned char code5[]={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};//存
const unsigned char code6[]={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};//储
const unsigned char code7[]={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};//测
const unsigned char code8[]={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};//量
const unsigned char code9[]={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};//进
const unsigned char code10[]={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};//行
const unsigned char code11[]= {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};//中
const unsigned char code12[]={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};//点
const unsigned char code100[]={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};//线
//******************函数声明*******************************
void Init_Clock(void);
void Init_IO(void);
void Init_LCD (void);
void delay (uint us);
void delayl(uint ms);
void WriteCommand_L (uchar com1);
void WriteCommand_R (uchar com2);
void WriteData_L (uchar dat1);
void WriteData_R(uchar dat2);
void ClearScreen(void);
void AllScreen(void);
void StateCheck(void);
void WriteWord(uchar colum,uchar page, const uchar *code);
void DrawLine(void);
//*************系统主程序**********************************
void main( 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初始化***************************
void Init_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;
}
//*****************高频时钟初始化**************************
void Init_Clock(void)
{
//UTCTL1 = SSEL0;
// BCSCTL1=0X00;
//BCSCTL2=0X00;
unsigned int iq0;
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; //MCLK 4分频 MCLK=2MHZ
BCSCTL2|=DIVS_2; //SMCLK 4分频 SMCLK=2MHZ
}
//***************延时子程序******************************
void delay (uint us)
{
while(us--);
}
void delayl (uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<1000;j++)
;
}
//*****************状态检查*********************************
void StateCheck(void)
{ uchar cradd1;
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);
}
//***************写指令代码子程序(左)*******************
void WriteCommand_L (uchar com1)
{ 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
}
//*****************写指令代码子程序(右)*******************
void WriteCommand_R (uchar com2)
{ 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
}
//****************写显示数据子程序(左)**********************
void WriteData_L (uchar dat1)
{
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
}
//****************写显示数据子程序(右)**********************
void WriteData_R(uchar dat2)
{
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
}
//******************清屏 ************************
void ClearScreen(void)
{ uchar i,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);
}
}
}
//******************满屏 ************************
void AllScreen(void)
{ uchar i,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初始化******************************
void Init_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();
}
//***********************写汉字*********************************
void WriteWord (uchar colum,uchar page, const uchar *code)//page2:页 colum2:列 code2:字代码
{
uchar i,j,colum0;
uchar flag;
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++;
}
}
//*******************画线*********************************
void DrawLine(void)
{
uchar i,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左右,这时对比度正常。