#include <msp430x16x.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define LCD_databus P5OUT //LCD8 位数据口
#define CLR_RS P4OUT&=~BIT3; //RS = P4.3
#define SET_RS P4OUT|=BIT3;
#define CLR_RW P4OUT&=~BIT4; //RW = P4.4
#define SET_RW P4OUT|=BIT4;
#define CLR_EN P4OUT&=~BIT5; //EN = P4.5
#define SET_EN P4OUT|=BIT5;
#define CLR_CS1 P4OUT&=~BIT6; //CS1 = P4.6
#define SET_CS1 P4OUT|=BIT6;
#define CLR_CS2 P4OUT&=~BIT7; //CS2 = P4.7
#define SET_CS2 P4OUT|=BIT7;
uchar huan[32]={0x00,0x80,0x00,0x80,0xFC,0x80,0x05,0xFE,0x85,0x04,0x4A,0x48,0x28,0x40,0x10,0x40,
0x18,0x40,0x18,0x60,0x24,0xA0,0x24,0x90,0x41,0x18,0x86,0x0E,0x38,0x04,0x00,0x00};
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void delay_50ms(uint t)
{
uint j;
for(;t>0;t--)
for(j=6245;j>0;j--);
}
void Read_busy()//读“忙”函数-----数据线的最高位DB71 则busy
{
LCD_databus=0x00;
CLR_RS;
SET_RW;
SET_EN;
while(LCD_databus & 0x80);
CLR_EN;
}
void write_LCD_command(uchar value)
{
Read_busy();//每次读写都要忙判断
CLR_RS; //选择命令
CLR_RW; //读操作
delay_50us(1);
LCD_databus=value;
SET_EN; //EN 由1----0 锁存有效数据
delay_50us(10);
CLR_EN;
delay_50us(2);
}
void write_LCD_data(uchar value)//写数据函数
{
Read_busy();
SET_RS; //选择数据
CLR_RW;
delay_50us(1);
LCD_databus=value;
SET_EN; //EN 由1----0 锁存有效数据
delay_50us(10);
CLR_EN;
delay_50us(2);
}
void Set_page(uchar page)//设置“页”LCD12864 共8 页,一页是8 行点阵点
{
page=0x03 & page;
page=0xb8|page; //页的首地址为0xB8
write_LCD_command(page);
}
void Set_line(uchar startline) //设置显示的起始行
{
startline=0x3f & startline;
startline=0xC0|startline; //起始行地址为0xC0
write_LCD_command(startline); //设置从哪行开始:共0--63;一般从0 行开始显示
}
void Set_column(uchar column) //设置显示的列
{
column=column &0x3f; //列的最大值为64
column= 0x40|column; //列的首地址为0x40
write_LCD_command(column);//列位置
}
void SetOnOff(uchar onoff) //显示开关函数:0x3E 是关显示,0x3F 是开显示
{
onoff=0x3e|onoff; //0011 111x,onoff只能为0 或者1
write_LCD_command(onoff);
}
void SelectScreen(uchar screen) //选屏
{
switch(screen)
{
case 0: CLR_CS1;CLR_CS2;break; //全屏
case 1: CLR_CS1;SET_CS2;break;//左半屏
case 2: SET_CS1;CLR_CS2;break;//右半屏
default:break;
}
}
void ClearScreen(uchar screen) //清屏
{
uchar i,j;
SelectScreen(screen);
for(i=0;i<8;i++)//控制页数0-7,共8 页
{
Set_page(i);
Set_column(0);
for(j=0;j<64;j++) //控制列数0-63,共64 列
{
write_LCD_data(0x00); //写入0,地址指针自加1
}
}
}
void InitLCD() //LCD 的初始化
{
P5DIR = 0xff;
P5OUT = 0x00;
P4DIR |= 0xf8;
P4OUT = 0x00;
Read_busy();
SelectScreen(0);
SetOnOff(0); //关显示
SelectScreen(0);
SetOnOff(1); //开显示
SelectScreen(0);
ClearScreen(0);//清屏
Set_line(0); //开始行:0
}
void Display(uchar ss,uchar page,uchar column,uchar *p) //显示汉字
{
uchar i;
SelectScreen(ss);
Set_page(page); //写上半页
Set_column(column); //控制列
for(i=0;i<16;i++) //控制16 列的数据输出
{
write_LCD_data(p[i]); //汉字的上半部分
}
Set_page(page+1); //写下半页
Set_column(column); //控制列
for(i=0;i<16;i++) //控制16 列的数据输出
{
write_LCD_data(p[i+16]);//汉字的下半部分
}
}
void main()
{
WDTCTL = WDTPW + WDTHOLD;
InitLCD(); //初始化
ClearScreen(0); //清屏
Set_line(0); //显示开始行
Display(1,0,2*16,huan);//欢
while(1);
}
|