#include <msp430x14x.h>
const unsigned char CharMode[10][16];
/****************************定义管脚*************************/
#define DB0_DB7OUT P4OUT //定DB0_DB7为P4
#define DB0_DB7DIR P4DIR
#define DB0_DB7IN P4IN
//定CS1为P2.4
static void CS1_1(void) //CS1输出为高电平
{
P2OUT |= BIT4;
_NOP();
_NOP();
}
static void CS1_0(void) //CS1输出为低电平
{
P2OUT &= ~(BIT4);
_NOP();
_NOP();
}
//定CS2为P2.3
static void CS2_1(void) //CS2输出为高电平
{
P2OUT |= BIT3;
_NOP();
_NOP();
}
static void CS2_0(void) //CS2输出为低电平
{
P2OUT &= ~(BIT3);
_NOP();
_NOP();
}
//定CS3为P2.1
static void CS3_1(void) //CS3输出为高电平
{
P2OUT |= BIT1;
_NOP();
_NOP();
}
static void CS3_0(void) //CS3输出为低电平
{
P2OUT &= ~(BIT1);
_NOP();
_NOP();
}
//定LED为P2.0
extern void LED_1(void) //LED输出为高电平,开背光
{
P2OUT |= BIT0;
_NOP();
_NOP();
}
extern void LED_0(void) //LED输出为低电平,关背光
{
P2OUT &= ~(BIT0);
_NOP();
_NOP();
}
//定义RS为P5.3
static void RS_1(void) //RS输出为高电平
{
P5OUT |= BIT3;
_NOP();
_NOP();
}
static void RS_0(void) //RS输出为低电平
{
P5OUT &= ~(BIT3);
_NOP();
_NOP();
}
//定义RW为P5.2
static void RW_1(void) //RW输出为高电平
{
P5OUT |= BIT2;
_NOP();
_NOP();
}
static void RW_0(void) //RW输出为低电平
{
P5OUT &= ~(BIT2);
_NOP();
_NOP();
}
//定义E为P5.1
static void E_1(void) //E输出为高电平
{
P5OUT |= BIT1;
_NOP();
_NOP();
}
static void E_0(void) //E输出为低电平
{
P5OUT &= ~(BIT1);
_NOP();
_NOP();
}
//定义RES为P5.0
static void RES_1(void) //RES输出为高电平
{
P5OUT |= BIT0;
_NOP();
_NOP();
}
static void RES_0(void) //RES输出为低电平
{
P5OUT &= ~(BIT0);
_NOP();
_NOP();
}
/*************************************************************/
static void delay(int n) //1毫秒延时程序
{
int q,m,p=500;
for(m=0;m<n;m++)
{
for(q=0;q<p;q++);
}
}
static void RESET() //复位
{
RES_1();
delay(5);
RES_0();
delay(5);
}
static unsigned char FZ_Data(unsigned char tem1) //数据高低为翻转
{
unsigned char i,tem2=0;
for(i=0;i<8;i++)
{
tem2=tem2>>1;
tem2=tem2|(tem1&0x80);
tem1=tem1<<1;
}
return tem2;
}
static unsigned char R_Data()
{
DB0_DB7DIR=0;
return(FZ_Data(DB0_DB7IN));
}
static void W_Data(unsigned char LCD_Data)
{
DB0_DB7DIR=255;
DB0_DB7OUT=FZ_Data(LCD_Data);
}
static void WaitIdle() //等待空闲状态
{
int i=1000;
unsigned char LCD_Data;
do
{
RS_0();
RW_1();
E_1();
LCD_Data=R_Data();
E_0();
}
while((LCD_Data&0x6f!=0)&&(i--)!=0);
}
static void SendCommand(unsigned char LCD_Cmd) //发送命令
{
WaitIdle();
RS_0();
RW_0();
E_1();
W_Data(LCD_Cmd);
E_0();
}
static void SendData(unsigned char LCD_Data) //发送数据
{
WaitIdle();
RS_1();
RW_0();
E_1();
W_Data(LCD_Data);
E_0();
}
/*
static unsigned char ReceiveData() //接收数据
{
unsigned char LCD_Data;
WaitIdle();
RS_1();
RW_1();
E_1();
LCD_Data=R_Data();
E_0();
return(LCD_Data);
}
*/
static void LCD_CS(unsigned char n) //LCD显示屏幕选择
{
switch(n)
{
case 0:CS1_0();CS2_1();CS3_1();break;
case 1:CS1_1();CS2_0();CS3_1();break;
case 2:CS1_1();CS2_1();CS3_0();break;
default:
CS1_0();CS2_1();CS3_1();
}
}
static unsigned char LCD_Row(int n) //列值转换
{
LCD_CS(n/64);
return((unsigned char)(n%64));
}
extern void DisplayOn()//显示开
{
SendCommand(0x3f);
}
extern void DisplayOff()//显示关
{
SendCommand(0x3e);
}
extern void ClearScreen() //清屏
{
unsigned char i,j;
CS1_0();
CS2_0();
CS3_0();
for(i=0xb8;i<=0xbf;i++)
{
SendCommand(i);
SendCommand(0x40);
for(j=0;j<64;j++)
SendData(0x00);
}
}
extern void FillScreen() //黑屏
{
unsigned char i,j;
CS1_0();
CS2_0();
CS3_0();
for(i=0xb8;i<=0xbf;i++)
{
SendCommand(i);
SendCommand(0x40);
for(j=0;j<64;j++)
SendData(0xff);
}
}
extern void LcdDisplayInit() //显示初始化
{
P2DIR|=BIT0+BIT1+BIT3+BIT4;
P5DIR|=BIT0+BIT1+BIT2+BIT3;
RESET();
CS1_0();
CS2_0();
CS3_0();
DisplayOn();//开显示
delay(1000);
SendCommand(0xc0);//显示从零行开始
}
static void WriteLine(unsigned char line,int row,int n,const unsigned char *str,unsigned char fb) //写一整行
{
int row1,i;
row1=LCD_Row(row);
SendCommand(0xb8+line);
SendCommand(0x40+row1);
for(i=0;i<n;i++)
{
if(row%64==0)
{
row1=LCD_Row(row);
SendCommand(0xb8+line);
SendCommand(0x40+row1);
}
if(fb==1) SendData(~str[i]);
else SendData(str[i]);
row++;
}
}
extern int LcdDisplayWord(unsigned char page,int row,const unsigned char *str,unsigned char fb)//显示汉字
{
//unsigned char str=(unsigned char)st;
WriteLine(page,row,16,str,fb);
WriteLine(page+1,row,16,str+16,fb);
return(row+16);
}
extern int LcdDisplayNum(unsigned char page,int row,unsigned char x,unsigned char fb)//显示数字
{
unsigned char *str=(unsigned char*)CharMode;
WriteLine(page,row,8,str+x*16,fb);
WriteLine(page+1,row,8,(str+8)+x*16,fb);
return(row+8);
}
extern int LcdDisplayChar(unsigned char page,int row,const unsigned char *str,unsigned char fb)//显示字符
{
WriteLine(page,row,8,str,fb);
WriteLine(page+1,row,8,str+8,fb);
return(row+8);
}
extern int LcdDisplayPIC(unsigned char Addresspage,int Addressrow,int Sizerow,int Sizeline,const unsigned char *str,unsigned char fb)//显示图片
{
unsigned char i;
for(i=0;i<Sizeline/8;i++)
WriteLine(Addresspage+i,Addressrow,Sizerow,str+Sizerow*i,fb);
return(Addressrow+Sizerow);
}
extern int LcdClear(unsigned char Addresspage,int Addressrow,int Sizerow,int Sizepage,unsigned char fb)
{
int i;
unsigned char str[192]={0};
for(i=0;i<Sizepage;i++)
{
WriteLine(Addresspage+i,Addressrow,Sizerow,str,fb);
}
return(Addressrow+Sizerow);
}
const unsigned char CharMode[10][16]=
{
//0
{
0x00,0xF0,0x08,0x04,0x04,0x08,0xF0,0x00,
0x00,0x07,0x08,0x10,0x10,0x08,0x07,0x00,
},
//1
{
0x00,0x08,0x08,0xFC,0x00,0x00,0x00,0x00,
0x00,0x10,0x10,0x1F,0x10,0x10,0x00,0x00,
},
//2
{
0x00,0x38,0x04,0x04,0x04,0xC4,0x38,0x00,
0x00,0x18,0x14,0x12,0x11,0x10,0x18,0x00,
},
//3
{
0x00,0x18,0x04,0x44,0x44,0xA4,0x18,0x00,
0x00,0x0C,0x10,0x10,0x10,0x08,0x07,0x00,
},
//4
{
0x00,0x80,0x60,0x10,0x08,0xFC,0x00,0x00,
0x00,0x03,0x02,0x12,0x12,0x1F,0x12,0x00,
},
//5
{
0x00,0xFC,0x84,0x44,0x44,0x84,0x04,0x00,
0x00,0x0C,0x10,0x10,0x10,0x08,0x07,0x00,
},
//6
{
0x00,0xF0,0x88,0x44,0x44,0x8C,0x00,0x00,
0x00,0x07,0x08,0x10,0x10,0x08,0x07,0x00,
},
//7
{
0x00,0x1C,0x04,0x84,0x64,0x1C,0x04,0x00,
0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,
},
//8
{
0x00,0x38,0x44,0x84,0x84,0x44,0x38,0x00,
0x00,0x0E,0x11,0x10,0x10,0x11,0x0E,0x00,
},
//9
{
0x00,0x70,0x88,0x04,0x04,0x88,0xF0,0x00,
0x00,0x00,0x18,0x11,0x11,0x08,0x07,0x00
}
};