#include <msp430g2553.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
unsigned char a[]={"波天神终于把12864学会了!"};
#define SID BIT1
#define SCLK BIT2
#define CS BIT3
#define LCDPORT P1OUT
#define SID_1 LCDPORT |= SID
#define SID_0 LCDPORT &= ~SID
#define SCLK_1 LCDPORT |= SCLK
#define SCLK_0 LCDPORT &= ~SCLK
#define CS_1 LCDPORT |= CS
#define CS_0 LCDPORT &= ~CS
void delay(unsigned char ms)
{
unsigned char i,j;
for(i=ms;i>0;i--)
for(j=120;j>0;j--);
}
/***********************************************************
*名 称:LCD_Write_cmd()
*功 能:写一个命令到LCD12864
*入口参数:cmd:待写入的命令,无符号字节形式
*出口参数:无
*说 明:写入命令时,RW=0,RS=0 扩展成24位串行发送
*格 式:11111 RW0 RS 0 xxxx0000 xxxx0000
* |最高的字节 |命令的bit7~4|命令的bit3~0|
***********************************************************/
void write_cmd(uchar cmd)
{
uchar i,high4bits,low4bits;
ulong lcdcmd;
high4bits = cmd & 0xf0;
low4bits = cmd & 0x0f;
lcdcmd=((ulong)0xf8<<16)+((ulong)high4bits<<8)+((ulong)low4bits<<4);
CS_1;
SCLK_0;
for(i=0;i<24;i++)
{
SID_0;
if(lcdcmd & 0x00800000) SID_1;
lcdcmd <<=1;
delay(3);
SCLK_1;
delay(3);
SCLK_0;
}
CS_0;
}
}
/***********************************************************
*名 称:LCD_Write_Byte()
*功 能:向LCD12864写入一个字节数据
*入口参数:byte:待写入的字符,无符号形式
*出口参数:无
*范 例:LCD_Write_Byte('F') //写入字符'F'
***********************************************************/
void write_dat(uchar dat)
{
uchar i,high4bits,low4bits;
ulong lcddat;
high4bits = dat & 0xf0;
low4bits = dat & 0x0f;
lcddat=((ulong)0xfa<<16)+((ulong)high4bits<<8)+((ulong)low4bits<<4);
CS_1;
SCLK_0;
for(i=0;i<24;i++)
{
SID_0;
if(lcddat & 0x00800000) SID_1;
lcddat <<=1;
delay(3);
SCLK_1;
delay(3);
SCLK_0;
}
CS_0;
}
/***********************************************************
*名 称:LCD_pos()
*功 能:设置液晶的显示位置
*入口参数:x:第几行,1~4对应第1行~第4行
* y:第几列,0~15对应第1列~第16列
*出口参数:无
*范 例:LCD_pos(2,3) //第二行,第四列
***********************************************************/
void lcd_pos(uchar x,uchar y)
{
uchar pos;
switch(x)
{
case 1:pos=0x80;break;
case 2:pos=0x90;break;
case 3:pos=0x88;break;
case 4:pos=0x98;break;
default:pos=0x80;
}
pos += y;
write_cmd(pos);
}
/****************************************************/
//LCD12864初始化
void LCD_init(void)
{
write_cmd(0x30);
delay(5);
write_cmd(0x0C);
delay(5);
write_cmd(0x01);
delay(5);
write_cmd(0x02);
delay(5);
}
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1SEL=0x00;
P1DIR = BIT1 + BIT2 + BIT3;
LCD_init();
delay(40);
lcd_pos(1,0);
write_dat(a[0]);
write_dat(a[1]);
write_dat(a[2]);
write_dat(a[3]);
write_dat(a[4]);
write_dat(a[5]);
write_dat(a[6]);
write_dat(a[7]);
write_dat(a[8]);
write_dat(a[9]);
write_dat(a[10]);
write_dat(a[11]);
lcd_pos(2,0);
write_dat(a[12]);
write_dat(a[13]);
write_dat(a[14]);
write_dat(a[15]);
write_dat(a[16]);
write_dat(' ');
write_dat(a[17]);
write_dat(a[18]);
write_dat(a[19]);
write_dat(a[20]);
write_dat(a[21]);
write_dat(a[22]);
write_dat(a[23]);
write_dat(a[24]);
while(1)
{;}
//return 0;
}