#include
#define uint unsigned int
#define uchar unsigned char
#define PSB BIT0
#define E BIT1
#define SID BIT2
#define CS BIT3
#define PORT P1OUT
void writebyte_12864(unsigned char byte) //发送一个字节
{
unsigned char i,a;
for(i=0;i<8;i++)
{
a = byte&0x80; //取出最高位
if(a == 0x80)
{
PORT |= SID;
}
else
{
PORT &=~ SID;
}
PORT |= E;
PORT &=~ E;
byte<<=1; //左移
}
}
void write_12864(uchar start, unsigned char date) //start=0写指令,start=1写数据
{
unsigned char com,hdate,ldate;
if(start == 1)
com=0xfa; //写数据
else
com=0xf8; //写指令
hdate=date&0xf0; //取高四位
ldate=(date<<4)&0xf0; //取低四位
writebyte_12864(com); //发送起始信号/发数据或命令
//__delay_cycles(5000);
writebyte_12864(hdate); //发送高四位
//__delay_cycles(5000);
writebyte_12864(ldate); //发送低四位
//__delay_cycles(5000);
}
void init_12864(void) //初始化LCD
{
__delay_cycles(20000); //等待进入状态
PORT &=~ PSB; //串口驱动模式
PORT |= CS; //片选 RS高电平有效 单片LCD使用时可固定高电平
write_12864(0,0x30); //8 位介面,基本指令集
__delay_cycles(5000);
write_12864(0,0x02); //清DDRAM
__delay_cycles(5000);
write_12864(0,0x06); //清屏,将DDRAM的地址计数器归零
__delay_cycles(5000);
write_12864(0,0x0c); //显示打开,光标关,反白关
__delay_cycles(5000);
write_12864(0,0x01);//写入空格清屏幕
}
void displaystr_12864(uchar x,uchar y,uchar *str) //写入字符串
{
write_12864(0,0x80+(x/2+(x-1)%2)*8+y-1); //x行,y列
while(*str!='\0')
{
write_12864(1,*str);
str++;
}
}
void displaychar_12864(uchar x,uchar y,uchar z) //写入字符
{
write_12864(0,0x80+(x/2+(x-1)%2)*8+y-1); //x行,y列
write_12864(1,z);
}
void main(void) {
WDTCTL = WDTPW + WDTHOLD;
P1DIR = 0xff;
init_12864();
P1OUT |= BIT4;
P1OUT |= BIT5;
while(1)
{
displaystr_12864(1,1,"一江之水可以望月");
displaystr_12864(2,1,"一江之水可以望月");
displaystr_12864(3,1,"一江之水可以望月");
displaystr_12864(4,1,"一江之水可以望月");
}
} |