#include<pic.h>
#include<pic168xa.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned char
#define RST RB1
#define PSB RB2//PSB=0串行模式=1并行模式
//#define E RB3//使能信号SCLK
//#define RW RB4//与RS配合做选择功能,STD
//#define RS RB5//CS
#define SCLK RB3
#define STD RB4
#define CS RB5
//延时程序(短)
void delay_us(int i)
{
while(--i);
}
void delay(int x)//延时子程序
{
uint i;
uchar j;
for(i=0;i<x;i++)
{
for(j=0;j<120;j++);
}
}
void write_data(uchar dis_data)//数据输入子程序
{
uint i;
uchar i_data1,i_data2;
// uchar fro_sig=0xfd;
TRISB=0X00;
delay_us(5);
CS=1;
delay_us(5);
/* for(i=0;i<8;i++)
{
if(fro_sig&0x80){STD=1;}
else{STD=0;}
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(10);
fro_sig<<=1;
delay_us(10);
}*/
for(i=0;i<5;i++)//五位信号零输入
{
STD=1;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
STD=0;
SCLK=1;
delay_us(10);//RW
SCLK=0;
delay_us(5);
STD=1;
SCLK=1;
delay_us(10);//RS
SCLK=0;
delay_us(5);
STD=0;
SCLK=1;
delay_us(10);//0
SCLK=0;
delay_us(5);
delay(10);
i_data1=dis_data&0xf0;//高4位输入
for(i=0;i<4;i++)
{
if(i_data1&0x80){
STD=1;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
else{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
delay_us(10);
i_data1<<=1;
}
for(i=0;i<4;i++)
{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay(5);
}
delay(10);
i_data2=dis_data&0x0f;//低4位输入
for(i=0;i<4;i++)
{
if(i_data2&0x08){
STD=1;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
else{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay(5);
}
delay_us(10);
i_data2<<=1;
}
for(i=0;i<4;i++)
{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
delay(10);
delay_us(5);
STD=1;
delay_us(10);
}
void write_command(uchar dis_cmd)//写命令子程序
{
uint i;
uchar i_cmd1,i_cmd2;
TRISB=0X00;
delay_us(5);
CS=1;
delay_us(5);
for(i=0;i<5;i++)//五位信号零输入
{
STD=1;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
for(i=0;i<3;i++)//RW,RS,O输入
{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
delay(10);
i_cmd1=dis_cmd&0xf0;//高4位输入
for(i=0;i<4;i++)
{
if(i_cmd1&0x80){
STD=1;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
else{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
delay_us(10);
i_cmd1<<=1;
}
for(i=0;i<4;i++)
{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay(5);
}
delay(10);
i_cmd2=dis_cmd&0x0f;//低4位输入
for(i=0;i<4;i++)
{
if(i_cmd2&0x08){
STD=1;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
else{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay(5);
}
delay_us(10);
i_cmd2<<=1;
}
for(i=0;i<4;i++)
{
STD=0;
SCLK=1;
delay_us(10);
SCLK=0;
delay_us(5);
}
delay(10);
delay_us(5);
STD=1;
delay_us(10);
}
//初始化液晶屏幕
void lcdinit()
{
delay(200);
write_command(0x30);
delay(10); //选择基本指令集
write_command(0x30); //选择8bit数据流
delay(50);
write_command(0x0F); //开显示
delay(100);
write_command(0x01); //清显示并设地址为00H
delay(100);
write_command(0x01); //指定在资料的读取与写入时设定游标的移动
//方向及指定显示的移位
delay(10);
}
//主函数
main()
{
delay_us(10000);
TRISB=0X00;
// CS=1;
TRISB=0X00;
RB1=1;
RB2=0;
RB0=1;
// TRISC=0X00;
delay(10);
while(1)
{
lcdinit();
write_command(0x80);
delay(50);
write_data(0xcc);
delay(50);
write_data(0xec);
delay(100);//天
write_data(0xbd);
delay(50);
write_data(0xf2);
delay(100);//津
write_data(0xbd);
delay(50);
write_data(0xf2);
delay(100); //津
write_data(0xd3);
delay(50);
write_data(0xed);
delay(100);//禹
write_data(0xbf);
delay(50);
write_data(0xc6);
delay(100);//科
write_data(0xbc);
delay(50);
write_data(0xbc);
delay(100);//技
write_data(0xd3);
delay(50);
write_data(0xd0);
delay(100);//有
write_data(0xcf);
delay(50);
write_data(0xde);
delay(100);//限
write_command(0X93);
write_data(0xb9);
delay(50);
write_data(0xab);
delay(100);//公
write_data(0xcb);
delay(50);
write_data(0xbe);
delay(100);//司
delay(100);
lcdinit();
write_command(0x88);
delay(50);
write_data(0xcc);
delay(50);
write_data(0xec);
delay(100);//天
write_data(0xbd);
delay(50);
write_data(0xf2);
delay(100);//津
write_data(0xbd);
delay(50);
write_data(0xf2);
delay(100); //津
write_data(0xd3);
delay(50);
write_data(0xed);
delay(100);//禹
write_data(0xbf);
delay(50);
write_data(0xc6);
delay(100);//科
write_data(0xbc);
delay(50);
write_data(0xbc);
delay(100);//技
write_data(0xd3);
delay(50);
write_data(0xd0);
delay(100);//有
write_data(0xcf);
delay(50);
write_data(0xde);
delay(100);//限
write_command(0X9b);
write_data(0xb9);
delay(50);
write_data(0xab);
delay(100);//公
write_data(0xcb);
delay(50);
write_data(0xbe);
delay(100); //司
}
}