本帖最后由 上天的果子 于 2012-11-20 14:56 编辑
程序输入后1602啥显示也没有,位的定义是对的。本人是单片机初学者,希望各位高手能帮我解决问题,谢谢了。
程序实现功能1602显示时分秒年月日星期附带温度传感:
#include
#include
#define uchar unsigned char
#define uint unsigned int
***it shuzhi=P2^6;
***it duxie=P2^5;
***it shineng=P2^7;
***it ds=P3^7;
***it key1=P1^1;
***it key2=P1^2;
***it key3=P1^3;
***it beep=P1^5;
uint nian;
uchar yue,ri,shi,fen,miao,num,num1,num2,flag;
uchar code tab_yue[]={31,28,31,30,31,30,31,31,30,31,30,31};
uchar code xingqi[7][3]={"MON","TUE","WEN","THU","FRI","SAT","SUN"};
uint wd;
float f_wd;
void delay(uint xms) //延时
{
uint i,j;
for(i=xms;i>0;i--)
for(j=100;j>0;j--);
}
void di() //蜂鸣
{
beep=0;
delay(100);
beep=1;
}
void dshq() //定时器初始化
{
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void fun_xiezhi(uchar a)
{
shineng=0;
shuzhi=0;
delay(1);
duxie=0;
delay(1);
P0=a;
delay(5);
shineng=1;
delay(5);
shineng=0;
delay(5);
}
void fun_xieshu(uchar b)
{
shineng=0;
shuzhi=1;
delay(1);
duxie=0;
delay(1);
P0=b;
delay(5);
shineng=1;
delay(5);
shineng=0;
delay(5);
}
void fun_1602() //1602初始化
{
fun_xiezhi(0x38);
fun_xiezhi(0x0c);
fun_xiezhi(0x06);
fun_xiezhi(0x01);
shineng=0;
}
void fun_displayxq(uchar d) //1602显示星期
{
fun_xiezhi(0x80+12);
fun_xieshu(xingqi[d][0]);
fun_xieshu(xingqi[d][1]);
fun_xieshu(xingqi[d][2]);
}
void fun_displaysfm(uchar address1,uchar dat1) //1602显示时分秒
{
uchar si,ge;
si=dat1/10;
ge=dat1%10;
fun_xiezhi(0x80+0x40+address1);
fun_xieshu(si);
fun_xieshu(ge);
}
void fun_displayn(uint g)
{
uchar qian,bai,si,ge;
qian=g/1000;
bai=g%1000/100;
si=g%1000%100/10;
ge=g%1000%100%10;
fun_xiezhi(0x80+1);
fun_xieshu(qian);
fun_xieshu(bai);
fun_xieshu(si);
fun_xieshu(ge);
}
void fun_displaynyr(uchar address2,uchar dat2) //1602显示年月日
{
uchar ge,shi;
shi=dat2/10;
ge=dat2%10;
fun_xiezhi(0x80+address2);
fun_xieshu(shi);
fun_xieshu(ge);
}
uchar fun1()
{
if(yue=2&&((nian%4==0&&nian%100!=0)||nian%400==0))
return 29;
else
{
return tab_yue[yue-1];
}
}
void nyr()
{
if(flag==1)
{
flag=0;
ri++;
if(yue==2)
{
if((nian%4==0&&nian%100!=0)||nian%400==0)
if(ri>29)
{
ri=1;
yue++;
fun_displaynyr(6,yue);
}
else
if(ri>28)
{
ri=1;
yue++;
fun_displaynyr(6,yue);
}
}
else
{
if(ri>tab_yue[yue-1])
ri=1;
yue++;
if(yue>12)
{
yue=1;
nian++;
fun_displayn(nian);
}
fun_displaynyr(6,yue);
}
fun_displaynyr(9,ri);
}
}
void keyscan() //按键检测
{
if(key1==0)
{
delay(5);
if(key1==0)
{
num2++;
while(key1==0);
di();
if(num2==1)
{
TR0=0;
fun_xiezhi(0x80+0x40+9);
fun_xiezhi(0x0f);
}
if(num2==2)
{
fun_xiezhi(0x80+0x40+6);
}
if(num2==3)
{
fun_xiezhi(0x80+0x40+3);
}
if(num2==4)
{
fun_xiezhi(0x80+4);
}
if(num2==5)
{
fun_xiezhi(0x80+7);
}
if(num2==6)
{
fun_xiezhi(0x80+10);
}
if(num2==7)
{
fun_xiezhi(0x80+14);
}
if(num2==8)
{
num2=0;
fun_xiezhi(0x0c);
TR0=1;
}
}
}
if(num2!=0)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(key2==0);
di();
if(num2==1)
{
miao++;
if(miao==60)
miao=0;
fun_displaysfm(8,miao);
fun_xiezhi(0x80+0x40+9);
}
if(num2==2)
{
fen++;
if(fen==60)
fen=0;
fun_displaysfm(5,fen);
fun_xiezhi(0x80+0x40+6);
}
if(num2==3)
{
shi++;
if(shi==24)
shi=0;
fun_displaysfm(2,shi);
fun_xiezhi(0x80+0x40+3);
}
if(num2==4)
{
nian++;
fun_displayn(nian);
fun_xiezhi(0x80+4);
}
if(num2==5)
{
yue++;
if(yue==13)
yue=1;
fun_displaysfm(6,yue);
fun_xiezhi(0x80+7);
}
if(num2==6)
{
ri++;
if(ri>fun1())
ri=1;
fun_displaysfm(9,ri);
fun_xiezhi(0x80+10);
}
if(num2==7)
{
num1++;
if(num1==7)
num1=0;
fun_displayxq(num1);
fun_xiezhi(0x80+14);
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(key3==0);
di();
if(num2==1)
{
if(miao==0)
miao=60;
miao--;
fun_displaysfm(8,miao);
fun_xiezhi(0x80+0x40+9);
}
if(num2==2)
{
if(fen==0)
fen=60;
fen--;
fun_displaysfm(5,fen);
fun_xiezhi(0x80+0x40+6);
}
if(num2==3)
{
if(shi==0)
shi=24;
shi--;
fun_displaysfm(2,shi);
fun_xiezhi(0x80+0x40+3);
}
if(num2==4)
{
nian--;
fun_displayn(nian);
fun_xiezhi(0x80+4);
}
if(num2==5)
{
if(yue==1)
yue=13;
yue--;
fun_displaysfm(6,yue);
fun_xiezhi(0x80+7);
}
if(num2==6)
{
if(ri==1)
ri=fun1()+1;
ri--;
fun_displaysfm(9,ri);
fun_xiezhi(0x80+10);
}
if(num2==7)
{
if(num1==0)
shi=7;
num1--;
fun_displayxq(num1);
fun_xiezhi(0x80+14);
}
}
}
}
}
void dsreset() //DS18B20初始化
{
uint i;
ds=0;
i=80;while(i>0)i--;
ds=1;
i=4;while(i>0)i--;
}
void writezj(uchar e) //DS18B20写字节函数
{
bit wei;
uchar i;
uint j;
for(i=1;i<=8;i++)
{
wei=e&0x01;
e=e>>1;
if(wei)
{
ds=0;
_nop_();_nop_();
ds=1;
j=8;while(j>0)j--;
}
else
{
ds=0;
j=8;while(j>0)j--;
ds=1;
_nop_();_nop_();
}
}
}
uchar readzj() //DS18B20读字节函数
{
uchar i,j,k,h,dat;
bit wei;
for(i=0;i<8;i++)
{
ds=0;
h++;
ds=1;h++;h++;
wei=ds;
j=8;while(j>=0)j--;
k=wei;
dat=(dat>>1)|(k<<7);
}
return dat;
}
void tempchange() //温度转换
{
dsreset();
delay(1);
writezj(0xcc);
writezj(0x44);
}
uint gettemp() //获取温度
{
uchar a,b;
tempchange();
delay(1);
writezj(0xcc);
writezj(0xbe);
a=readzj();
b=readzj();
wd=b;
wd=wd<<8;
wd=wd|a;
f_wd=wd*0.0625;
wd=f_wd+0.5;
f_wd=f_wd+0.05;
return wd;
}
void dealdis_wd(uint f) //温度处理显示
{
uchar shi,ge;
shi=f/10;
ge=f%10;
fun_xiezhi(0x80+0x40+11);
delay(5);
fun_xieshu(shi);
delay(5);
fun_xieshu(ge);
delay(5);
fun_xieshu(0xdf);
delay(5);
fun_xieshu('C');
delay(5);
}
void mian()
{
uchar h;
nian=2010;yue=1;ri=1;shi=0;fen=0;miao=0;num1=4;
dshq();
fun_1602();
delay(20);
fun_displayn(nian);
delay(1);
fun_displaynyr(6,yue);
delay(1);
fun_displaynyr(9,ri);
delay(1);
fun_displayxq(num1);
delay(1);
fun_displaysfm(2,shi);
delay(1);
fun_displaysfm(5,fen);
delay(1);
fun_displaysfm(8,miao);
delay(1);
fun_xiezhi(0x80+5);
fun_xieshu('-');
delay(1);
fun_xiezhi(0x80+8);
fun_xieshu('-');
delay(1);
fun_xiezhi(0x80+0x40+4);
fun_xieshu(':');
delay(1);
fun_xiezhi(0x80+0x40+7);
fun_xieshu(':');
delay(1);
while(1)
{
tempchange();
for(h=10;h>0;h--)
{
dealdis_wd(gettemp());
}
keyscan();
nyr();
fun_displayxq(num1);
for(h=10;h>0;h--)
{
dealdis_wd(gettemp());
}
}
}
void T0_time() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
num++;
if(num==20)
{
num=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
flag=1;
num1++;
if(num1==7)
num1=0;
}
fun_displaysfm(2,shi);
}
fun_displaysfm(5,fen);
}
fun_displaysfm(8,miao);
}
}
|