按键不能工作,数码管不亮。
设计要求是按键控制预设温度,但在仿真里面无法控制。数码管应该显示环境温度和预设温度,也无法显示。
程序如下:
#include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit DQ=P1^7; //温度传感器接口 sbit key1=P1^3; sbit key2=P1^4; sbit dianji=P3^1; //电机控制端口 float ff; unit y3; uchar shi,ge,xiaoshu,sheding=20,gaonum,dinum; uchar code dispcode[]={//段码 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; uchar code tablel[]={//带小数点的段码 0xbf,0x86,0xdb,0xcf, 0xe6,0xed,0xfd,0x87, 0xff,0xef}; uchar dispbitcode[]={//位选 0xfe,0xfd,0xfb,0xf7, 0xef,0xdf,0xbf,0x7f}; uchar dispbuf[8]={0,0,0,0,0,0,0,0}; void Delay(unit num)//延时函数 { while(--num); } void digitalshow(uchar a4,uchar a3,uchar a2,uchar a1,uchar a0) { dispbuf[0]=a0; dispbuf[1]=a1; dispbuf[2]=a2; dispbuf[3]=a3; dispbuf[4]=a4;
P2=0xff; P0=dispcode[dispbuf[0]]; P2=dispbitcode[5]; Delay(1);
P2=0xff; P0=dispcode[dispuf[1]]; P2=dispbitcode[4]; Delay(1);
P2=0xff; P0=dispcode[dispuf[2]]; P2=dispbitcode[2]; Delay(1);
P2=0xff; P0=dispcode[dispuf[3]]; P2=dispbitcode[1]; Delay(1);
P2=0xff; P0=dispcode[dispuf[4]]; P2=dispbitcode[0]; Delay(1); } void dmsec(unit count) {
unit i; //1ms延时 while(count--) { for(i=0;i<125;i++){} } } void tmrese(void) { DQ=0; Delay(90);//精确延时 大于480us DQ=1; Delay(4); //90,4可以小范围变化 } void tmpre(void) { while(DQ); while(~DQ); Delay(4); } bit tmrbit(void) { unit i; bit dat; DQ=0; i++; //i++;大概1us DQ=1; i++; i++; dat=DQ; Delay(8); return(dat); }
uchar tmrbyte(void)//读一个比特 { uchar i,j,dat; dat=0; for(i=1;i<=8;i++) { j=tmrbit(); dat=(j<<7)|(dat>>1); } return(dat); }
void tmwbyte(uchar dat)//写一个比特 { unit i; uchar j; bit testb; for(j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1;//从低位开始 if(testb)//write 1 { DQ=0;//先拉低 i++; i++;//>1us DQ=1; Delay(4); } else//write 0 { DQ=0; Delay(4); DQ=1; i++; i++;//再拉高 } } } void tmstart(void)//ds1820开始转换 { dmsec(1); tmrese(); tmpre(); dmsec(1); tmwbyte(0xcc);//skip rom tmwbyte(0x44);//转换 } uchar tmrtemp(void)//读取温度 { uchar a,b; tmreset(); tmpre(); dmsec(1); tmwbyte(0xcc);//skip rom tmwbyte(0xbe);//转换 a=tmrbyte();//LSB低8位 b=tmrbyte();//MSB高8位 y3=b; y3<<=8; y3=y3|a; ff=y3*0.0625; y3=ff*10+0.5; return(y3); } void keyscan(void) { if(key1==0) { dmsec(5); if(key1==0) { sheding++; if(sheding==100) sheding=20; } while(!key1); } else if(key2==0) { dmsec(5); if(key2==0) { sheding--; if(sheding==0) sheding=20; } while (!key2); } } void deal(unit tmp)//温度处理 { if(tmp<=sheidng) { gaonum=0; dinum=4; } else if((tmp>sheding)&&(tmp<=(sheding+5))) { gaonum=1; dinum=3; } else if((tmp>(sheding+5))&&(tmp<=(sheding+10))) { gaonum=2; dinum=2; } else if((()tmp>(sheding+10))&&(tmp<=(sheding+15))) { gaonum=3; dinum=1; } else { gaonum=4; dinum=0; } } void dianjik()//电机控制 { uchar q,i; for(q=0;q<dinum;q++) { dianji=0; digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10); for(i=255;i>0;i--) { digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10); } } for(q=0;q<gaonum;q++) { dianji=1; digitalsow(shi,ge,xiaoshu,sheidng/10,sheding&10); for(i=255;i>0;i--) { digitalshow(shi,ge,xiaoshu,sheding/10,sheding&10); } } } void main(void) { unit last; dianji=0; tmstart(); dmsec(450);//初始化ds18b20 while(1) { tmstar();//ds1820开始转换 dmsec(2); last=tmrtemp()+256;//读取温度 shi=last/100; ge=(last%100)/10 xiaoshu=(last%100)%10; kayscan(); dmsec(2); deal(last/10); dianjik(); } }
|