顶层文件 万年历.C #include
#include
#define uchar unsigned char
#define uint unsigned int
sbit e=P3^4; //1602液晶的使能
sbit rs=P3^5; //1602液晶的片选
sbit key1=P3^0; //第一按键功能:调节时间选择位
sbit key2=P3^1; //第二按键功能:加值键
sbit key3=P3^2; //第三按键功能:减值键
sbit rd=P3^7; //矩阵键盘一根线设置为0,用其中四个做独立按键
sbit dscs=P2^4;
sbit dsas=P2^5;
sbit dsrw=P2^6;
sbit dsds=P2^7;
sbit dsirq=P3^3;
sbit beep=P2^3;
uchar code table1[]=" 20 - - "; //初始1602液晶显示的内容
uchar code table2[]=" : : ";
uchar count,k,week,mon,day,year,flag,flag1; //定义年月日
char shi,fen,miao; //定义时分秒和星期(char型,因为要判断其值时出现-1)
void write_ds(uchar,uchar);
void set_alarm(uchar,uchar,uchar);
uchar read_ds(uchar);
void set_time();
void delay(uint z) //延时函数
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void didi()
{
beep=0;
delay(50);
beep=1;
delay(100);
beep=0;
delay(50);
beep=1;
}
void write_com(uchar com) //1602液晶写命令程序
{
rs=0;
P1=com;
e=0;
delay(5);
e=1;
delay(5);
e=0;
delay(5);
}
void write_dat(uchar dat) //1602液晶写数据程序
{
rs=1;
P1=dat;
e=0;
delay(5);
e=1;
delay(5);
e=0;
delay(5);
}
void write_sfm(uchar add,uchar dat) //写时分秒的程序
{
uchar sh,g;
sh=dat/10;
g=dat%10;
write_com(0x80+0x40+add);
write_dat(0x30+sh); //1602字符手册里的数字对应的编码值
write_dat(0x30+g);
}
void write_nyr(uchar add,uchar dat)//写年月日的程序
{
uchar sh,g;
sh=dat/10;
g=dat%10;
write_com(0x80+add);
write_dat(0x30+sh);
write_dat(0x30+g);
}
void write_week(char we)
{ //写液晶星期显示函数
write_com(0x80+12);
switch(we)
{
case 1: write_dat('M');delay(5);
write_dat('o');delay(5);
write_dat('n');
break;
case 2: write_dat('T');delay(5);
write_dat('u');delay(5);
write_dat('e');
break;
case 3: write_dat('W');delay(5);
write_dat('e');delay(5);
write_dat('d');
break;
case 4: write_dat('T');delay(5);
write_dat('h');delay(5);
write_dat('u');
break;
case 5: write_dat('F');delay(5);
write_dat('r');delay(5);
write_dat('i');
break;
case 6: write_dat('S');delay(5);
write_dat('a');delay(5);
write_dat('t');
break;
case 7: write_dat('S');delay(5);
write_dat('u');delay(5);
write_dat('n');
break;
}
}
void init() //初始化液晶和定时器
{
uchar num;
EA=1;
EX0=1;
IT0=1;
e=0;
set_alarm(11,13,10);
write_ds(0x0B,0x26);
read_ds(0x0c);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
delay(2);
write_com(0x80); //为什么不能放在主函数的while大循环内呢?
for(num=0;num<16;num++) //因为这是初始数据,不能不断循环执行写入,
{ //只需把部分必须改变的改变即可。
write_dat(table1[num]);
delay(2);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_dat(table2[num]);
delay(2);
}
}
/*void keyscan() //键盘扫描程序
{
rd=0; //键盘的列端口设置
if(flag1==1)
{
if((key1==0)||(key2==0)||(key3==0))
{
delay(5);
if((key1==0)||(key2==0)||(key3==0))
{
while(!(key1&&key2&&key3));
didi();
flag1=0;
}
}
}
if(key1==0)
{
delay(5); //延时消抖
if(key1==0)//确认按键是否按下
{
flag=1;
flag1=0;
k++;
while(!key1);//松手检测
if(k==1) //按下功能键停止计时并分别用光标指向时分秒年月日进行调整
{
TR0=0;
write_com(0x80+0x40+11); //设置光标为写命令后不加1
write_com(0x0f);//设置光标为闪烁状态
}
}
if(k==2)
{
write_com(0x80+0x40+8);
}
if(k==3)
{
write_com(0x80+0x40+5);
}
if(k==4)
{
write_com(0x80+10);
}
if(k==5)
{
write_com(0x80+7);
}
if(k==6)
{
write_com(0x80+4);
}
if(k==7)
{
write_com(0x80+14);
}
if(k==8) //最后再按下功能键确认开始重新计时
{
k=0;
write_com(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,week);
write_ds(7,day);
write_ds(8,mon);
write_ds(9,year);
}
}
if(k!=0) //在key1键按下的情况下才能使用 加值(key2)功能键
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(k==1) //秒加1(按下Key即秒加一)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(k==2) //分加1
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(k==3) //时加1
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(k==4) //day加1
{
day++;
if(day==32)
day=0;
write_nyr(9,day);
write_com(0x80+10);
}
if(k==5) //mon加1
{
mon++;
if(mon==13)
mon=0;
write_nyr(6,mon);
write_com(0x80+7);
}
if(k==6) //year加1
{
year++;
if(year==100)
year=0;
write_nyr(3,year);
write_com(0x80+4);
}
if(k==7) //week加1
{
week++;
if(week==8)
week=1;
write_week(week);
write_com(0x80+14);
}
}
}
if(key3==0)//在key1键按下的情况下才能使用 减值(key3)功能键
{
delay(5);
if(key3==0)
{
while(!key3);
if(k==1) //秒减1
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(k==2) //分减1
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(k==3) //时减1
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(k==4) //day减1
{day--;
if(day==0)
day=31;
write_nyr(9,day);
write_com(0x80+10);
}
if(k==5) //mon减1
{mon--;
if(mon==0)
mon=12;
write_nyr(6,mon);
write_com(0x80+7);
}
if(k==6) //year减1
{
year--;
if(year==0)
year=99;
write_nyr(3,year);
write_com(0x80+4);
}
if(k==7)//week减1
{week--;
if(week==0)
week=7;
write_week(week);
write_com(0x80+14);
}
}
}
}
} */
void write_ds(uchar add,uchar dat)
{dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsrw=0;
P0=dat;
dsrw=1;
dsas=1;
dscs=1;
}
uchar read_ds(uchar add)
{uchar ds_dat;
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsds=0;
P0=0xff;
ds_dat=P0;
dsds=1;
dsas=1;
dscs=1;
return ds_dat;
}
void set_time()
{write_ds(0,0);
write_ds(2,59);
write_ds(4,23);
write_ds(6,1);
write_ds(7,28);
write_ds(8,2);
write_ds(9,10);
}
void set_alarm(uchar ashi,uchar afen,uchar amiao)
{write_ds(1,amiao);
write_ds(3,afen);
write_ds(5,ashi);
}
void main()
{init();
while(1) //不停调用键盘扫描看是否有按键按下
{
//keyscan();
if(flag1==1)
didi();
if(flag==0)
{miao=read_ds(0);
fen=read_ds(2);
shi=read_ds(4);
week=read_ds(6);
day=read_ds(7);
mon=read_ds(8);
year=read_ds(9);
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
write_nyr(9,day);
write_nyr(6,mon);
write_nyr(3,year);
write_week(week);
}
}
}
void exter() interrupt 0
{uchar c;
flag1=1;
c=read_ds(0x0c);
}
北航的《PIC16系列单片机C程序设计与PROTEUS仿真》一书有这种LCD的仿真的线路图和程序,并有详细介绍使用,你看一下,会有好处的。
因为这种程序比较复杂,如果只给你程序,没搞懂其中的原理,今后要改都很困难。所以要认真看一下书,知道其原理后才变成真正是你的东东。
如果有人给你粘贴一大段的程序,会有用么?除了程序难看懂外,程序还与硬件有关!
所以建议你看一下书,搞清原理(硬件与软件)才是硬道理!