基于单片机智能电风扇设计
立即下载
资源介绍:
基于单片机智能电风扇设计
#include
#include
#include"lcd.h"
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit k1=P1^1;
sbit k2=P1^2;
sbit hongwai=P3^4;
sbit dq=P3^7;
sbit moto=P1^0; //直流电机
sbit qidong=P3^3;
sbit tingzhi=P3^2;
sbit moshi=P3^1;
sbit led=P3^5;
uint8 d[16]=" ZNDFS ";
uint8 num[10]="0123456789";
uint8 a,c,temp,ms;
uint8 k=0,kk=0;
uchar time=0,PWM=0;
void zidong();
void shoudong();
void keypros();
void delay(uint16 i)
{
while(i--);
}
void wrc(uint8 c)
{
LcdWriteCom(c);
}
void wrd(uint8 dat)
{
LcdWriteData(dat);
}
void lcd_init()
{
LcdInit();
}
void ds18b20init()
{
dq=1;
delay(1);
dq=0;
delay(80);
dq=1;
delay(5);
dq=0;
delay(20);
dq=1;
delay(35);
}
void ds18b20wr(uint8 dat)
{
uint8 i;
for(i=0;i<8;i++)
{
dq=0;
dq=dat&0x01;
dat>>=1;
delay(8);
dq=1;
delay(1);
}
}
uint8 ds18b20rd()
{
uint8 value,i;
for(i=0;i<8;i++)
{
dq=0;
value>>=1;
dq=1;
if(dq==1)value|=0x80;
delay(8);
}
return value;
}
uint8 readtemp()
{
uint8 b;
ds18b20init();
ds18b20wr(0xcc);
ds18b20wr(0x44);
delay(100);
ds18b20init();
ds18b20wr(0xcc);
ds18b20wr(0xbe);
a=ds18b20rd();
b=ds18b20rd();
b<<=4;
c=b&0x80;
b+=(a&0xf0)>>4;
a=a&0x0f;
return b;
}
void display()
{
uint16 i,k;
float dio;
dio=a*0.0625;
k=dio*10000;
wrc(0x80+0x00);
for(i=0;i<16;i++)
{
wrd(d[i]);
}
wrc(0x80+0x43);
if(c==0x80)
{
wrd('-');
temp=temp-1;
temp=(~temp)|0x80;
}
else
{
wrd('+');
temp=(temp);
}
wrd(num[temp/100]);
wrd(num[temp%100/10]);
wrd(num[temp%100%10]);
wrd('.');
wrd(num[k/1000]);
wrd(num[k%1000/100]);
wrd(0xdf);
wrd('C');
}
void shoudong() //手动
{
if(kk==0)
{ led=1;
if(qidong==0)
{
moto=1;
}
if(tingzhi==0)
{
moto=0;
}
}
}
void zidong() //自动
{ if(kk==1)
{ led=0;
if(temp>=23&&hongwai==0)
{
moto=1;
}
else
{
moto=0;
}
}
}
void main()
{
moto=0;
lcd_init();
while(1)
{
temp=readtemp();
display();
zidong();
shoudong();
if(moshi==0)
{
k++;
while(moshi==0);
switch(k)
{
case(1): //自动
kk=1;
break;
case(2): //手动
kk=0;k=0;moto=0;
break;
}
}
}
}