#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define Coil_A1 {A1=1;B1=0;C1=0;D1=0;}
#define Coil_B1 {A1=0;B1=1;C1=0;D1=0;}
#define Coil_C1 {A1=0;B1=0;C1=1;D1=0;}
#define Coil_D1 {A1=0;B1=0;C1=0;D1=1;}
#define Coil_AB1 {A1=1;B1=1;C1=0;D1=0;}
#define Coil_BC1 {A1=0;B1=1;C1=1;D1=0;}
#define Coil_CD1 {A1=0;B1=0;C1=1;D1=1;
#define Coil_DA1 {A1=1;B1=0;C1=0;D1=1;}
#define Coil_OFF {A1=0;B1=0;C1=0;D1=0;}
sbit DSPORT=P3^7;
sbit A1=P1^0;
sbit B1=P1^1;
sbit C1=P1^2;
sbit D1=P1^3;
uchar Speed;
void Delay1ms(uint y);
void Delay1ms(uint y)
{
uint x;
for(y;y>0;y--)
for(x=110;x>0;x--);
}
unsigned char Ds18b20Init()
{
unsigned int i;
DSPORT=0;
i=70;
while(i--);
DSPORT=1;
i=0;
while(DSPORT)
{
i++;
if(i>5000)
return 0;
}
return 1;
}
void Ds18b20WriteByte(unsigned char dat)
{
unsigned int i,j;
for(j=0;j<8;j++)
{
DSPORT=0;
i++;
DSPORT=dat&0x01;
i=6;
while(i--);
DSPORT=1;
dat>>=1;
}
}
unsigned char Ds18b20ReadByte()
{
unsigned char byte,bi;
unsigned int i,j;
for(j=8;j>0;j--)
{
DSPORT=0;
i++;
DSPORT=1;
i++;
i++;
bi=DSPORT;
byte=(byte>>1)|(bi<<7);
i=4;
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0x44);
}
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0xbe);
}
int Ds18b20ReadTemp()
{
int temp=0;
unsigned char tmh,tml;
Ds18b20ChangTemp();
Ds18b20ReadTempCom();
tml=Ds18b20ReadByte();
tmh=Ds18b20ReadByte();
temp=tmh;
temp<<=8;
temp|=tml;
return temp;
}
void main()
{
unsigned int tp,j,i;
tp=Ds18b20ReadTemp;
Speed=2;
while(1)
{
if(tp>=20)
{
Coil_A1
Delay1ms(Speed);
Coil_AB1
Delay1ms(Speed);
Coil_B1
Delay1ms(Speed);
Coil_BC1
Delay1ms(Speed);
Coil_C1
Delay1ms(Speed);
Coil_CD1
Delay1ms(Speed);
Coil_D1
Delay1ms(Speed);
Coil_DA1
Delay1ms(Speed);
Coil_OFF;
for(i=0;i<10;i++)
{
for(j=0;j<60;j++)
{
Delay1ms(1000);
}
}
}
}
}
}
|