/*************************************************************************
> File Name:bingxiang.c
> Author: yduo
> Mail: 1587202069@qq.com
> Created Time: 2015
************************************************************************/
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit RS = P0^7; //1602
sbit LCDEN = P0^5;
sbit rw = P0^6;
sbit RELAY_1 = P1^0; //制冷片
sbit RELAY_2 = P1^1;
sbit RELAY_3 = P1^2;
sbit RELAY_4 = P1^3;
sbit RELAY_5 = P1^4;
sbit RELAY_6 = P1^5;
sbit RELAY_7 = P1^6;
sbit RELAY_8 = P1^7;
sbit RELAY_9 = P3^2; //风扇
sbit ds = P3^7;
uint tmp;
uchar table[] = "xian zai wen du:";
/**
*函数声明
*/
void delayUs();
void delayMs(uint a);
void writeComm(uchar comm);
void writeData(uchar dat);
void writeString(uchar * str, uchar length);
void init();
void dsInit();
void Delay1ms()?
void display(int v);
void sendChangeCmd();
int getTmpValue();
void Relay_init();
void Interrupt_init();
void main()
{
Interrupt_init();//中断初始化
Relay_init();
//继电器初始化
sendChangeCmd();
init();
writeComm(0x80);
writeString(table, 16);
while(1)
{
delayMs(1000); //温度转换时间需要750ms以上
writeComm(0xc0);
display(getTmpValue());
sendChangeCmd();
}
}
/**
*微秒延时
*/
void delayUs()
{
_nop_();
}
/**
*毫秒延时
*/
void delayMs(uint a)
{
while(a--)
{
Delay1ms();
}
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void writeComm(uchar comm)
{
RS = 0;
P2 = comm;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}
/**
*写数据:RS=1, RW=0;
*/
void writeData(uchar dat)
{
RS = 1;
P2 = dat;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}
void init()
{
rw=0;
writeComm(0x38);
writeComm(0x0c);
writeComm(0x06);
writeComm(0x01);
}
void writeString(uchar * str, uchar length)
{
uchar i;
for(i = 0; i < length; i++)
{
writeData(str[i]);
}
}
/**
*DS18B20初始化
*/
void dsInit()
{
unsigned int i;
ds = 0;
i = 100;
while(i>0) i--;
ds = 1;
i = 4;
while(i>0) i--;
}
void dsWait()
{
unsigned int i;
while(ds);
while(~ds);
i = 4;
while(i > 0)
i--;
}
bit readBit()
{
unsigned int i;
bit b;
ds = 0;
i++;
ds = 1;
i++;
i++;
b = ds;
i = 8;
while(i>0) i--;
return b;
}
unsigned char readByte()
{
unsigned int i;
unsigned char j, dat;
dat = 0;
for(i=0; i<8; i++)
{
j = readBit();
dat = (j << 7) | (dat >> 1);
}
return dat;
}
void writeByte(unsigned char dat)
{
unsigned int i;
unsigned char j;
bit b;
for(j = 0; j < 8; j++)
{
b = dat & 0x01;
dat >>= 1;
if(b)
{
ds = 0;
i++;
i++;
ds = 1;
i = 8;
while(i>0) i--;
}
else
{
ds = 0;
i = 8;
while(i>0) i--;
ds = 1;
i++;
i++;
}
}
}
void sendChangeCmd()
{
dsInit();
dsWait();
delayMs(1);
writeByte(0xcc);
writeByte(0x44);
}
void sendReadCmd()
{
dsInit();
dsWait();
delayMs(1);
writeByte(0xcc);
writeByte(0xbe);
}
int getTmpValue()
{
unsigned int tmpvalue;
int value;
float t;
unsigned char low, high;
sendReadCmd();
low = readByte();
high = readByte();
tmpvalue = high;
tmpvalue <<= 8;
tmpvalue |= low;
value = tmpvalue;
t = value * 0.0625;
value = t * 100 + (value > 0 ? 0.5 : -0.5); //大于0加0.5, 小于0减0.5
return value;
}
/**
* 1602屏幕显示
*/
void display(int v)
{
unsigned char count;
unsigned char datas[] = {0, 0, 0, 0, 0};
tmp = abs(v);
datas[0] = tmp / 10000;
datas[1] = tmp % 10000 / 1000;
datas[2] = tmp % 1000 / 100;
datas[3] = tmp % 100 / 10;
datas[4] = tmp % 10;
writeComm(0xc0+3);
if(v < 0)
{
writeString("- ", 2);
}
else
{
writeString("+ ", 2);
}
if(datas[0] != 0)
{
writeData('0'+datas[0]);
}
for(count = 1; count != 5; count++)
{
writeData('0'+datas[count]);
if(count == 2)
{
writeData('.');
}
}
}
/**
*中断服务函数
*/
void Time1(void) interrupt 3
{
TH1=0xd8; //10ms定时
TL1=0xf0;
if(tmp > 2000)//2000指的是20° 全开
{
RELAY_1 = 0;
RELAY_2 = 0;
RELAY_3 = 0;
RELAY_4 = 0;
RELAY_5 = 0;
RELAY_6 = 0;
RELAY_7 = 0;
RELAY_8 = 0;
RELAY_9 = 1;
}
else if(tmp < 2000 && tmp > 1500)
{
RELAY_1 = 0;
RELAY_2 = 1;
RELAY_3 = 0;
RELAY_4 = 1;
RELAY_5 = 0;
RELAY_6 = 1;
RELAY_7 = 0;
RELAY_8 = 1;
RELAY_9 = 1;
}
else
{
Relay_init();
}
}
/**
*继电器初始化
*/
void Relay_init()
{
RELAY_1 = 1;
RELAY_2 = 1;
RELAY_3 = 1;
RELAY_4 = 1;
RELAY_5 = 1;
RELAY_6 = 1;
RELAY_7 = 1;
RELAY_8 = 1;
RELAY_9 = 1;
}
/**
*中断初始化
*/
void Interrupt_init()
{
TMOD=0x10; //定时/计数器1工作于方式1
c语言
TH0=0xd8; //定时器初始值
TL0=0xf0; //10ms定时常数
EA=1; //开总中断
ET1=1; //允许定时/计数器1 中断
TR1=1; //启动定时/计数器1 中断
}