您的位置 首页 IC

根据MSP430F149的KS0108(LCD)驱动程序

//MSP430与12864连接驱动程序(KS0108)#includemsp430x14x.h//StandardEquations#includemath.h#includestdlib.h#in

//MSP43012864衔接驱动程序(KS0108)

#include “msp430x14x.h” // Standard Equations
#include “math.h”
#include “stdlib.h”
#include “stdio.h”
#include “string.h”
#include “ctype.h”
//#include “HZTable.h”
//#include “ASCII816.h”
#define uint unsigned int
#define uchar unsigned char
//**********************************************
/*#define RS BIT0
#define RW BIT1
#define E BIT2
#define CS1 BIT3
#define CS2 BIT4
#define RST BIT5
#define EL BIT6 //背光*/

#define RS 0x01//寄存器挑选信号(0–Command, 1–data) ks0108()
#define RW 0x02//读/写挑选信号
#define E 0x04//使能信号
#define CS1 0x02// 左屏
#define CS2 0x04// 右屏
#define RST 0x01

//数据线:p4口 ,操控线: p5口
//**********************************************
uchar cradd1,cradd2;

uchar item1[5]={0,1,2,3,50}; //欢迎运用
uchar item2[5]={4,5,6,7,50}; //请等候……

unsigned char hzbei[32]={
0x04,0x04,0x04,0xE4,0x24,0x24,0x25,0x26,0x24,0x24,0x24,0xE4,0x04,0x06,0x04,0x00,
0x00,0x40,0x20,0x13,0x1A,0x42,0x82,0x7E,0x02,0x02,0x0A,0x13,0x30,0x60,0x00,0x00
};

//=======================================================
void short_delay(uint i)
{ uint j;
for(j=0;j<=i;j++);
}
void delayms(uchar tickms)
{ uchar count,i;
_NOP();
_NOP();
_NOP();

for(i=tickms;i>0;i–)
{ for(count=0;count<=58;count++) {;}
_NOP();
}
}
//**************************************************
void prl0(uchar com1)// 写指令代码子程序(左)
{
P3DIR =0X7F; //P3为输出口
P1DIR =0X7F; //P3为输出口
P1OUT |=CS1; //csa=1
P1OUT&=~CS2; //csb=0
P3OUT&=~RS;
P3OUT |=RW; //RS=0,R/W=1,以便读液晶状况
P2DIR=0x00; //P2口为输进口
do
{
P3OUT |= E; //E=1
cradd1=P2IN;
P3OUT&=~E; //E=0
}
while((cradd1&0x80)!=0);
P3OUT&=~RW; //rw=0
P2DIR=0xff; //P2口为输出口
P2OUT=com1;
P3OUT |=E; //E=1
P3OUT&=~E; //E=0
}
//**************************************************
void prl1(uchar dat1)// 写显现数据子程序(左)
{
P3DIR =0X7F; //P3为输出口
P1DIR =0X7F; //P3为输出口
P1OUT |=CS1; //csa=1
P1OUT&=~CS2; //csb=0
P3OUT&=~RS;
P3OUT |=RW; //RS=0,R/W=1,以便读液晶状况
P2DIR=0x00; //P2口为输进口
do
{
P3OUT |=E; //E=1
cradd1=P2IN;
P3OUT&=~E; //E=0
}
while((cradd1&0x80)!=0);
P3OUT |=RS; //rs=1
P3OUT&=~RW; //rw=0
P2DIR=0xff; //P2口为输出口
P2OUT=dat1;
P3OUT|=E; //E=1
P3OUT&=~E; //E=0
}
//**************************************************
void prr0(uchar com2)// 写指令代码子程序(右)
{
P3DIR =0X7F; //P3为输出口
P1DIR =0X7F; //P3为输出口
P1OUT&=~CS1; //csa=0
P1OUT |=CS2; //csb=1
P3OUT&=~RS;
P3OUT |=RW; //RS=0,R/W=1,以便读液晶状况
P2DIR=0x00; //P2口为输进口
do
{
P3OUT |= E; //E=1
cradd1=P2IN;
P3OUT&=~E; //E=0
}
while((cradd1&0x80)!=0);
P3OUT&=~RW; //rw=0
P2DIR=0xff; //P2口为输出口
P2OUT=com2;
P3OUT |=E; //E=1
P3OUT&=~E; //E=0
}
//*************************************************
void prr1(uchar dat2)// 写显现数据子程序(右)
{
P3DIR =0X7F; //P3为输出口
P1DIR =0X7F; //P3为输出口
P1OUT&=~CS1; //csa=0
P1OUT |=CS2; //csb=1
P3OUT&=~RS;
P3OUT |=RW; //RS=0,R/W=1,以便读液晶状况
P2DIR=0x00; //P2口为输进口
do
{
P3OUT |=E; //E=1
cradd1=P2IN;
P3OUT&=~E; //E=0
}
while((cradd1&0x80)!=0);
P3OUT |=RS; //rs=1
P3OUT&=~RW; //rw=0
P2DIR=0xff; //P2口为输出口
P2OUT=dat2;
P3OUT |=E; //E=1
P3OUT&=~E; //E=0
}
//****************************************************
//读显现数据子程序 (左屏)
//****************************************************
uchar ReadDatal(void)
{
uchar Rdata;
P3DIR =0X7F; //P3为输出口
P1DIR =0X7F; //P3为输出口
P1OUT |=CS1; //csa=1
P1OUT&=~CS2; //csb=0
P3OUT&=~RS;
P3OUT |=RW; //RS=0,R/W=1,以便读液晶状况
P2DIR=0x00; //P2口为输进口
do
{
P3OUT |=E; //E=1
cradd1=P2IN;
P3OUT&=~E; //E=0
}
while((cradd1&0x80)!=0);

P3OUT |=RS; //RS=1
//P3OUT =RW; //R/W=1
P3OUT |=E; //E=1
Rdata=P2IN;
P3OUT&=~E; //E=0
return Rdata;
}
//****************************************************
//读显现数据子程序 (右屏)
//****************************************************
uchar ReadDatar(void)
{
uchar Rdata;
P3DIR =0X7F; //P3为输出口
P1DIR =0X7F; //P3为输出口
P1OUT&=~CS1; //csa=0
P1OUT |=CS2; //csb=1
P3OUT&=~RS;
P3OUT |=RW; //RS=0,R/W=1,以便读液晶状况
P2DIR=0x00; //P2口为输进口
do
{
P3OUT |=E; //E=1
cradd1=P2IN;
P3OUT&=~E; //E=0
}
while((cradd1&0x80)!=0);

P3OUT |=RS; //RS=1
//P3OUT =RW; //R/W=1
P3OUT |=E; //E=1
Rdata=P2IN;
P3OUT&=~E; //E=0
return Rdata;
}
//**********************************************
//**********************************************
void clsr(void) //清屏
{ uchar i,j;
for(i=0;i<8;i++)
{ prl0(i|0xb8); //设置页地址
prr0(i|0xb8);
prl0(0x40);
prr0(0x40);
for(j=0;j<64;j++) //设置列地址
{ prl1(0x00); //写0x00
prr1(0x00);
}
}
}
//**********************************************
void allon(void) //满屏
{ uchar i,j;
for(i=0;i<8;i++)
{ prl0(i| 0xb8);
prr0(i| 0xb8);
prl0(0x40);
prr0(0x40);
for(j=0;j<64;j++)
{ prl1(0xff); //写0xff
prr1(0xff);
}
}
}
//***********************************************
void stripe(void)
{ uchar i,j;
for(i=0;i<8;i++)
{ prl0(i| 0xb8);
prr0(i| 0xb8);
prl0(0x40);
prr0(0x40);
for(j=0;j<64;j++)
{ prl1(0xaa);
prr1(0xaa); // 写0xaa
}
}
}
//***********************************************
void stripe1(void)
{ uchar i,j;
for(i=0;i<8;i++)
{ prl0(i| 0xb8);
prr0(i| 0xb8);
prl0(0x40);
prr0(0x40);
for(j=0;j<64;j++)
{ prl1(0x55); // 写0x55
prr1(0x55);
}
}
}

//***************************************************

//***************************************************
void hzw_pr(uchar colum2,uchar page2)// 写汉字
//page2:页 colum2:列 code2:代码 code2=0,1,2,3……表明第几个汉字
{ uchar i,j,colum;
uchar *hzp;

uchar flag;

hzp=hzbei;

for(j=0;j<2;j++)
{ prl0(page2 |0xb8); //页
prr0(page2 |0xb8);
colum=colum2;
if(colum2>63) //右屏
{ colum=colum-64;
prr0(colum |0x40);
flag=1;
}
else //左屏
{prl0(colum| 0x40);
flag=0;
}

for(i=0;i<16;i++)
{
if(flag==0)
{
prl1(*hzp);
}
else
{prr1(*hzp);}
hzp++;
if(colum==64) //列=64
{
colum=0;
if(flag==0)
{
flag=1;
prr0(colum |0x40);
}
else{break;}
}
colum++;
}
page2++;
}
}
//***********************************************
/*void w_hzstr(uchar colum4,uchar page4,uchar *str1)// 写汉字字符串
{
while(*str1!=50)
{
hzw_pr( colum4, page4,*str1) ;
colum4=colum4+16;
str1++;
}
}*/
//***************************************************
//init_xt2; 初始化XT2 晶振为4MHZ
//*****************************************************
void init_xt2(void)
{
unsigned int iq0;

//运用XT2振荡器
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_1; //MCLK 2分频SMCLK=MCLK=2MHZ
}

void InitMcu430(void)
{
unsigned char ti;

//_BIS_SR(OSCOFF); //Disable LFXT1
BCSCTL1&=~XT2OFF; // XT2OFF=0
//BCSCTL1|=XTS;
BCSCTL2|=SELS+SELM1;

do{
IFG1&=~OFIFG; //Clear OscFault flag
for(ti=255;ti>1;ti–);
}while((IFG1 & OFIFG)==OFIFG);
for(ti=255;ti>1;ti–);
IFG1&=~OFIFG;

//SCTL1&=~XTS;
//BCSCTL1|=0x30; //8分频
BCSCTL1&=0x8F; //1分频, ACLK=32768HZ

/* ————————Setup Timer_A———————- */
TACTL=0x0004; /*Timer_A操控寄存器:接连形式,输入时钟不分频,时钟源为TACLK,接连形式*/

/* ————————Setup ADC12 —————– */

//WDTCTL = WDTPW+0x0019; //500ms timer,MCLK

//WDTCTL =WDT_ADLY_250; //ACLK
//WDTCTL =WDTPW+BIT7;

P1DIR=0x7F; //set P1 input
P2DIR=0xFF; //set P2,P3,P5 is output
P3DIR|=0x7F;
P4DIR=0xFF;
P5DIR=0xFF;

P1SEL=0x00;
P2SEL=0x00;
P3SEL=0x00;
P4SEL=0x00;
P5SEL=0x00;
//P6SEL=0xFF; //P6 外部模块功用
//P1SEL|=0x08; //P1.3 外部模块功用

}
//***********************************************
//主程序
//***********************************************

void main(void)
{
uchar a;
// uchar i,j;
WDTCTL=WDTPW+WDTHOLD;
InitMcu430();
//init_xt2();
P1DIR =0X7F; //P3为输出口
//P1OUT&=~RST; //rst=0
//delayms(50); //1ms
//P1OUT |=RST; //rst=1
//P3OUT&=~EL; //EL=0 背光=0

clsr(); // clear screen
do
{
prl0(0xc0);prr0(0xc0); //显现开始行
prl0(0x3f);prr0(0x3f); //开显现
//P3OUT&=~EL; //EL=0 背光=0
//******************************************************
clsr(); // clear screen
//写汉字字符串1 欢迎运用
//写汉字字符串2 请等候…
//w_hzstr(32,2,item1);
//w_hzstr(32,5,item2);
hzw_pr(32,5);
delayms(100);
for(a=0;a<250;a++)
{ delayms(100);}

delayms(100);
for(a=0;a<250;a++)
{ delayms(100);}

prl0(0xc0);prr0(0xc0);

//stripe();
//clsr();

allon(); // fill screen

delayms(100);
for(a=0;a<250;a++)
{ delayms(100);}
prl0(0xc0);prr0(0xc0);
clsr(); // clear screen
delayms(100);
for(a=0;a<250;a++)
{ delayms(100);}

// stripe screen
prl0(0xc0);prr0(0xc0);
stripe();
delayms(100);
for(a=0;a<250;a++)
{ delayms(100);}
prl0(0xc0);prr0(0xc0);
stripe1();
delayms(100);
for(a=0;a<250;a++)
{ delayms(100);}

}while(1);}
//****************************************************

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/273052.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部