標(biāo)題: 基于單片機(jī)的智能風(fēng)扇設(shè)計(jì) 程序有問(wèn)題 [打印本頁(yè)]
作者: 燈下黑吖 時(shí)間: 2020-4-22 14:17
標(biāo)題: 基于單片機(jī)的智能風(fēng)扇設(shè)計(jì) 程序有問(wèn)題
按鍵不能工作,數(shù)碼管不亮。
設(shè)計(jì)要求是按鍵控制預(yù)設(shè)溫度,但在仿真里面無(wú)法控制。數(shù)碼管應(yīng)該顯示環(huán)境溫度和預(yù)設(shè)溫度,也無(wú)法顯示。
程序如下:
#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; //電機(jī)控制端口
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[]={//帶小數(shù)點(diǎn)的段碼
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)//延時(shí)函數(shù)
{
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延時(shí)
while(count--)
{
for(i=0;i<125;i++){}
}
}
void tmrese(void)
{
DQ=0;
Delay(90);//精確延時(shí) 大于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)//讀一個(gè)比特
{
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)//寫(xiě)一個(gè)比特
{
unit i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;//從低位開(kāi)始
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開(kāi)始轉(zhuǎn)換
{
dmsec(1);
tmrese();
tmpre();
dmsec(1);
tmwbyte(0xcc);//skip rom
tmwbyte(0x44);//轉(zhuǎn)換
}
uchar tmrtemp(void)//讀取溫度
{
uchar a,b;
tmreset();
tmpre();
dmsec(1);
tmwbyte(0xcc);//skip rom
tmwbyte(0xbe);//轉(zhuǎn)換
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()//電機(jī)控制
{
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開(kāi)始轉(zhuǎn)換
dmsec(2);
last=tmrtemp()+256;//讀取溫度
shi=last/100;
ge=(last%100)/10
xiaoshu=(last%100)%10;
kayscan();
dmsec(2);
deal(last/10);
dianjik();
}
}
-
仿真.png
(12.81 KB, 下載次數(shù): 50)
下載附件
2020-4-22 14:14 上傳
| 歡迎光臨 (http://m.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |