main.c
#include"stc15f2k60s2.h"
#include"onewire.h"
#include"iic.h"
unsigned char code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc0&0x7f,0xf9&0x7f,0xa4&0x7f,0xb0&0x7f,0x99&0x7f,0x92&0x7f,0x82&0x7f,0xf8&0x7f,0x80&0x7f,0x90&0x7f,~0x3E,~0x71,~0x39,~0x76,~0x5E,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt,wendutt,ADtt;
unsigned char Rb2;
unsigned char mode=0;
unsigned char moshi=0;
unsigned int flag,NE555tt,zheng,fan,zheng_1,fan_1,T,f;
void display();
void keyscan();
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xC4;
TH0 = 0xFF;
TF0 = 0;
TR0 = 0;
}
void Timer0() interrupt 1
{
if(P34==0)
{
fan++;
if(flag==0){flag=1; zheng_1=zheng; zheng=0;}
}
if(P34==1)
{
zheng++;
if(flag==1){flag=0; fan_1=fan; fan=0;}
}
}
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0x20;
TH1 = 0xD1;
TF1 = 0;
TR1 = 1;
ET1=1;
EA=1;
}
void Timer1() interrupt 3
{
unsigned char P0_keeper=0,P2_keeper=0;
P0_keeper=P0; P2_keeper=P2; P2=P2&0x1f;
wendutt++;
ADtt++;
keytt++; if(keytt==10){keyscan(); keytt=0;}
display();
NE555tt++;
{
if(NE555tt==969){TR0=1; ET0=1;}
if(NE555tt==999){NE555tt=0; TR0=0; ET0=0; T=(zheng_1+fan_1)*5; f=1000000/T;}
}
P0=P0_keeper; P2=P2_keeper;
}
void display()
{
P0=0xff; P2=P2&0x1f|0xc0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0xe0; P2=P2&0x1f;
P0=1<<smgcom; P2=P2&0x1f|0xc0; P2=P2&0x1f;
P0=smgduan[smgbuffer[smgcom++]]; P2=P2&0x1f|0xe0; P2=P2&0x1f;
if(smgcom==8) smgcom=0;
}
void keyscan()
{
unsigned char dat,trg,cnt;
dat=P3^0xff;
trg=dat&(dat^cnt);
cnt=dat;
if(trg&0x01){P0=0; P2=P2&0x1f|0x80; P2=P2&0x1f;}
else if(trg&0x08){if(mode==0||mode==1) moshi++; if(moshi==3)moshi=0;}
}
void wenduwengding()
{
unsigned char Ti;
for(Ti=0;Ti<70;Ti++)
{
temp=tempget()*100+0.5;
}
}
void wenduhuoqu()
{
if(wendutt==200)
{
temp=tempget()*100+0.5;
wendutt=0;
}
}
void ADC()
{
if(ADtt==200)
{
Rb2=AD_read(0x03);
ADtt=0;
}
}
void xianshi()
{
if(mode==0)
{
if(moshi==0)
{
smgbuffer[0]=22;
smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=25;
smgbuffer[4]=temp/1000;
smgbuffer[5]=temp%1000/100+10;
smgbuffer[6]=temp%1000%100/10;
smgbuffer[7]=temp%10;
}
else if(moshi==1)
{
smgbuffer[0]=20;
smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=25;
smgbuffer[6]=(unsigned int)(Rb2*1.96+0.5)/100;
smgbuffer[7]=(unsigned int)(Rb2*1.96+0.5)%100/10;
}
else if(moshi==2)
{
unsigned char f1,f2,f3,f4,f5;
smgbuffer[0]=21;
smgbuffer[1]=smgbuffer[2]=25;
f1=f/10000; f2=f%10000/1000; f3=f%10000%1000/100; f4=f%10000%1000%100/10; f5=f%10;
if(f1==0){smgbuffer[3]=25; smgbuffer[4]=f2; smgbuffer[5]=f3; smgbuffer[6]=f4; smgbuffer[7]=f5;}
else if(f1==0 && f2==0){smgbuffer[3]=smgbuffer[4]=25; smgbuffer[5]=f3; smgbuffer[6]=f4; smgbuffer[7]=f5;}
else if(f1==0 && f2==0 && f3==0){smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=25; smgbuffer[6]=f4; smgbuffer[7]=f5;}
}
}
}
void main()
{
P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
config(0x3f);
wenduwengding();
Timer0Init();
Timer1Init();
while(1)
{
ADC();
wenduhuoqu();
xianshi();
}
}
onwire.c
#include "reg52.h"
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
void Delay_OneWire(unsigned int t)
{
t=t*12;
while(t--);
}
float tempget()
{
float temp;
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=(high<<8|low)*0.0625;
return temp;
}
void config(unsigned char jingdu)
{
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x4e);
Write_DS18B20(0x5f);
Write_DS18B20(0x00);
Write_DS18B20(jingdu);
}
iic.c
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
IIC_WaitAck();
}
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
IIC_WaitAck();
return da;
}
unsigned char AD_read(unsigned char add)
{
unsigned char AD_temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_SendByte(add);
IIC_Start();
IIC_SendByte(0x91);
AD_temp=IIC_RecByte();
IIC_Stop();
return AD_temp;
}
void EEPROM_write(unsigned char add,dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_SendByte(add);
IIC_SendByte(dat);
IIC_Stop();
}
unsigned char EEPROM_read(unsigned char add)
{
unsigned char EEPROM_temp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_SendByte(add);
IIC_Start();
IIC_SendByte(0xa1);
EEPROM_temp=IIC_RecByte();
IIC_Stop();
return EEPROM_temp;
}