keil编写单片机日历,为什么用函数调用就不行了?

wingwqr 2013-01-29 11:19:16
源程序:没有使用函数调用时,用硬件测试,可以实现加秒,但是将红色部分换成change_sfm(10,7,60,miao);就加不了秒了。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#include"24c02.h"
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^3;
sbit rd=P3^7;
sbit beep=P2^3;
uchar shi,fen,miao;
uchar yue,ri,nianH,nianL;
uint nian;
uchar count,s,dayp;//dayp用于记录星期几,然后调用数组
uchar code week[7][3]={
"MON","TUE","WEN",
"THU","FRI","STA","SUN"};
void delayms(uint xms);
void write_com(uchar com);
void write_date(uchar date);
void write_nyr(uchar add,uint date);
void write_nyr(uchar add,uint date);
void write_day(uchar a[3]);
void init();
void di();
void change_sfm(uchar add,uchar place,uchar max,uchar date);
void change_day();
void change_ri();
void change_yue();
void change_nian();
void changed_sfm(uchar add,uchar place,uchar min,uchar date);
void changed_day();
void changed_ri();
void changed_yue();
void changed_nian();
void keyscan();
//延时
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}

//写液晶命令
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}

//写液晶数据
void write_date(uchar date)
{
lcdrs=1;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}

//写年月日函数显示于液晶屏
void write_nian()
{
uchar qian,bai,shi,ge;
qian=(uchar)(nian/1000);
bai=(uchar)(nian%1000/100);
shi=(uchar)(nian%1000%100/10);
ge=(uchar)(nian%1000%100%10);
write_com(0x80+1);
write_date(0x30+qian);
write_date(0x30+bai);
write_date(0x30+shi);
write_date(0x30+ge);
}

void write_yue()
{
uchar shi,ge;
shi=0;
if(yue>9)
shi=yue/10;
ge=yue%10;
write_com(0x80+6);
write_date(0x30+shi);
write_date(0x30+ge);
}


void write_ri()
{
uchar shi,ge;
shi=0;
if(ri>9)
shi=ri/10;
ge=ri%10;
write_com(0x80+9);
write_date(0x30+shi);
write_date(0x30+ge);
}

//写时分秒显示与液晶屏
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}

//写星期几显示于液晶屏
void write_day(uchar a[3])
{
uchar n;
write_com(0x80+12);
for(n=0;n<3;n++)
{
write_date(a[n]);
delayms(5);
}
}

//初始化
void init()
{
rd=0;
dula=0;
wela=0;
nianH=20;
nianL=0;
nian=((int)(nianH))*100+((int)(nianL));
yue=1;
ri=1;
shi=0;
fen=0;
miao=0;
dayp=0;
count=0;
s=0;
lcden=0;
init_24c02();
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);

write_com(0x80+5);
write_date('-');
delayms(5);
write_com(0x80+8);
write_date('-');
delayms(5);
write_com(0x80+11);
write_date('-');
delayms(5);
write_com(0x80+0x40+6);
write_date(':');
delayms(5);
write_com(0x80+0x40+9);
write_date(':');
delayms(5);

nianH=read_add(1);
if((nianH<19)|(nianH>21))
nianH=19;
nianL=read_add(2);
if((nianL<0)|(nianL>99))
nianL=91;
nian=((int)(nianH))*100+((int)(nianL));
yue=read_add(3);
if((yue<1)|(yue>12))
yue=4;
ri=read_add(4);
if((ri<1)|(ri>31))
ri=23;
shi=read_add(5);
if((shi<0)|(shi>23))
shi=0;
fen=read_add(6);
if((fen<0)|(fen>59))
fen=0;
miao=read_add(7);
if((miao<0)|(miao>59))
miao=0;
dayp=read_add(8);
if((dayp<0)|(dayp>6))
dayp=0;

write_nian();
delayms(5);
write_yue();
delayms(5);
write_ri();
delayms(5);
write_sfm(4,shi);
delayms(5);
write_sfm(7,fen);
delayms(5);
write_sfm(10,miao);
delayms(5);
write_day(week[dayp]);
delayms(5);

TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
}

//蜂鸣器
void di()
{
beep=0;
delayms(100);
beep=1;
}

//加时函数
void change_sfm(uchar add,uchar place,uchar max,uchar date)
{
date++;
if(date==max)
{
date=0;
}
write_sfm(add,date);
write_com(0x80+0x40+add);
write_add(place,date);
}


void change_day()
{
dayp++;
if(dayp==7)
dayp=0;
write_day(week[dayp]);
write_com(0x80+12);
write_add(8,dayp);
}

void change_ri()
{
ri++;
if((nian%4==0&&nian%100!=0)||(nian%400==0))
{
if((yue==1)|(yue==3)|(yue==5)|(yue==7)|(yue==8)|(yue==10)|(yue==12))
{
if(ri==32)
ri=1;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}else if(yue==2)
{
if(ri==30)
ri=1;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}else
{
if(ri==31)
ri=1;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}
}else
{
if((yue==1)|(yue==3)|(yue==5)|(yue==7)|(yue==8)|(yue==10)|(yue==12))
{
if(ri==32)
ri=1;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}else if(yue==2)
{
if(ri==29)
ri=1;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}else
{
if(ri==31)
ri=1;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}
}
}

void change_yue()
{
yue++;
if(yue==13)
yue=1;
write_yue();
write_com(0x80+6);
write_add(3,yue);
}

void change_nian()
{
nian++;
if(nian==2100)
nian=1991;
nianH=nian/100;
nianL=nian%100;
write_nian();
write_com(0x80+1);
write_add(1,nianH);
delayms(5);
write_add(2,nianL);
}

//减时函数
void changed_day()
{
dayp--;
if(dayp==0)
dayp=6;
write_day(week[dayp]);
write_com(0x80+12);
write_add(8,dayp);
}

void changed_ri()
{
ri--;
if((nian%4==0&&nian%100!=0)||(nian%400==0))
{
if((yue==1)|(yue==3)|(yue==5)|(yue==7)|(yue==8)|(yue==10)|(yue==12))
{
if(ri==0)
ri=31;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}else if(yue==2)
{
if(ri==0)
ri=29;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}else
{
if(ri==0)
ri=30;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}
}else
{

if((yue==1)|(yue==3)|(yue==5)|(yue==7)|(yue==8)|(yue==10)|(yue==12))
{
if(ri==0)
ri=31;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}else if(yue==2)
{
if(ri==0)
ri=28;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}else
{
if(ri==0)
ri=30;
write_ri();
write_com(0x80+9);
write_add(4,ri);
}
}
}

void changed_yue()
{
yue--;
if(yue==0)
yue=12;
write_yue();
write_com(0x80+6);
write_add(3,yue);
}

void changed_nian()
{
nian--;
if(nian==1990)
nian=2100;
nianH=nian/100;
nianL=nian%100;
write_nian();
write_com(0x80+1);
write_add(1,nianH);
delayms(5);
write_add(2,nianL);
}

//键盘扫描
void keyscan()
{
if(s1==0)
{
delayms(5);
if(s1==0)
{
s++;
while(!s1);
di();
if(s==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(s==2)
{
write_com(0x80+0x40+7);
}
if(s==3)
{
write_com(0x80+0x40+4);
}
if(s==4)
{
write_com(0x80+12);
}
if(s==5)
{
write_com(0x80+9);
}
if(s==6)
{
write_com(0x80+6);
}
if(s==7)
{
write_com(0x80+1);
}
if(s==8)
{
s=0;
write_com(0x0c);
TR0=1;
}
}
}
if(s!=0)
{
if(s2==0)
{
delayms(5);
if(s2==0)
{
while(!s2);
di();
if(s==1)
{
miao++;
if(miao==60)
{
miao=0;
}
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_add(7,miao);

}
//将红色部分换成change_sfm(10,7,60,miao);就不行了。
if(s==2)
{
fen++;
if(fen==60)
{
fen=0;
}
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_add(6,fen);
}
if(s==3)
{
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_add(5,shi);
}
if(s==4)
change_day();
if(s==5)
change_ri();
if(s==6)
change_yue();
if(s==7)
change_nian();
}
}
if(s3==0)
{
delayms(5);
if(s3==0)
{
while(!s3);
di();
if(s==1)
{
miao--;
if(miao==-1)
{
miao=59;
}
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_add(7,miao);
}
if(s==2)
{
fen--;
if(fen==-1)
{
fen=59;
}
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_add(6,fen);
}
if(s==3)
{
shi--;
if(shi==-1)
{
shi=23;
}
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_add(5,shi);
}
if(s==4)
changed_day();
if(s==5)
changed_ri();
if(s==6)
changed_yue();
if(s==7)
changed_nian();
}
}
}
}

//主函数
void main()
{
init();
while(1)
{
keyscan();
}
}

//中断
void timer0() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
count++;
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
dayp++;
{
if(dayp==7)
{
dayp=0;
}
write_day(week[dayp]);
write_add(8,dayp);
}
ri++;
if((nian%4==0&&nian%100!=0)||(nian%400==0))
{
if((yue==1)|(yue==3)|(yue==5)|(yue==7)|(yue==8)|(yue==10)|(yue==12))
{
if(ri==32)
{
ri=1;
yue++;
}
}else if(yue==2)
{
if(ri==30)
{
ri=1;
yue++;
};
}else
{
if(ri==31)
{
ri=1;
yue++;
}
}
}else
{

if((yue==1)|(yue==3)|(yue==5)|(yue==7)|(yue==8)|(yue==10)|(yue==12))
{
if(ri==32)
{
ri=1;
yue++;
}
}else if(yue==2)
{
if(ri==29)
{
ri=1;
yue++;
}
}else
{
if(ri==31)
{
ri=1;
yue++;
}
}
}
if(yue==13)
{
yue=0;
nian++;
if(nian==2100)
{
nian=1991;
}
write_nian();
write_add(1,nianH);
write_add(2,nianL);
}
write_yue();
write_add(2,yue);
}
write_ri();
write_add(3,ri);
}
write_sfm(4,shi);
write_add(5,shi);
}
write_sfm(7,fen);
write_add(6,fen);
}
write_sfm(10,miao);
write_add(7,miao);
}
...全文
517 1 点赞 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wingwqr 2013-01-29
不好意思啊,犯了低级错误,原因是函数的调用,只能实参给形参,不能形参给实参。
  • 打赏
  • 举报
回复
相关推荐
发帖
单片机/工控
加入

2.6w+

社区成员

硬件/嵌入开发 单片机/工控
申请成为版主
帖子事件
创建了帖子
2013-01-29 11:19
社区公告
暂无公告