#include <msp430x14x.h>
#define P4_0_RS 0x01
#define P4_1_RW 0x02
#define P4_2_EN 0x04
#define P4_3_CS1 0x08
#define P4_4_CS2 0x10
#define P4_5_RST 0x20
#define P2_0_DB0 0x01
#define P2_1_DB1 0x02
#define P2_2_DB2 0x04
#define P2_3_DB3 0x08
#define P2_4_DB4 0x10
#define P2_5_DB5 0x20
#define P2_6_DB6 0x40
#define P2_7_DB7 0x80
#define uint unsigned int
#define uchar unsigned char
uchar hz[]={0x20};
void display_hz( uchar *hz, uchar nRow, uchar nCol );
void delay(uint v)
{
while(v>0)
{
v--;
}
}
void port_ini( void )
{
P2DIR = 0xFF;
P2OUT = 0x00;
P4SEL = 0XFF;
P4DIR = 0xff;
P4OUT |= P4_5_RST;
P4OUT &=~P4_5_RST;
_NOP();
_NOP();
P4OUT |= P4_2_EN;
_NOP();
_NOP();
P4OUT &=~P4_2_EN;
_NOP();
_NOP();
P4OUT |= P4_5_RST;
_NOP();
_NOP();
delay(400);
}
void write_command(uchar nByte)//写指令
{
//P4DIR = 0xFF;
P4OUT |= P4_3_CS1+P4_4_CS2;
P4OUT &=~(P4_0_RS+P4_1_RW);
P2OUT = nByte;
P4OUT |= P4_2_EN;
delay(10);
P4OUT &=~P4_2_EN;
/* delay(400);*/
}
void write_char( uchar nByte, uchar CS1, uchar CS2 )//写数据
{
//P2DIR = 0xFF;
if(CS1) P4OUT |= P4_3_CS1;
else P4OUT &=~P4_3_CS1;
if(CS2) P4OUT |= P4_4_CS2;
else P4OUT &=~P4_4_CS2;
P4OUT |= P4_0_RS; //H,显示数据。L,写指令
P4OUT &=~P4_1_RW; //H,读数据。L,写数据
P2OUT = nByte;
P4OUT |= P4_2_EN; //下降沿锁存数据。结合RW为L时,数据写到数据总线
delay(3);
P4OUT &=~P4_2_EN;
}
void clear_lcd( void )
{
uchar i=0;
for(i=0;i<8;i++)//8行
{
write_command(i|0xB8);
//for (uchar x = 0 ; x<128; x++)
//{ uchar y = x | 0X40;
// write_command(y);
//}
//write_command(0x40);
for(int l=0; l<128; l++)//128列
{ write_command(0x40 | l);
if (l <= 63)
write_char(0,1,0);
else
write_char(0,0,1);
_NOP();
}
}
}
void display_hz( uchar *hz, uchar nRow, uchar nCol )//nROW为X地址,nCol为Y地址,液晶显示一行十六位,128列,64行
{
uchar i,tmpCol;
write_command(0xB8|nRow);//设置X地址,第一行
tmpCol=nCol;
for(i=0;i<8;i++)//数组元素
{
if(tmpCol<=63)
{
write_command(0x40|tmpCol);
write_char(hz[i],1,0);
}
else
{
write_command(0x40|(tmpCol-64));
write_char(hz[i],0,1);
}
tmpCol++;
}
}
void int_clk()
{
uchar i;
BCSCTL1&=~XT2OFF;// 打开晶振XT2
BCSCTL2|=SELM1+SELS;//MCLK为8MHz,SMLK为1MHz
do
{
IFG1&=~OFIFG;
for(i=0;i<100;i++)
_NOP();
}
while((IFG1&OFIFG)!=0);
IFG1&=~OFIFG;
}
void main()
{
WDTCTL = WDTPW + WDTHOLD;
port_ini();
int_clk();
clear_lcd();
while(1)
{
display_hz(hz,0,0);
}
} |