急求STC89C52的数字钟温度计(液晶显示1602 DS1302 DS18B20)的程序 C语言和汇编都行啊 !
跪求各位大仙帮忙啊!急用!若是能用,重金感谢啊!另补充:有四个按键,蜂鸣器在温度大于或小于一定值时要可以报警的...
跪求各位大仙帮忙啊 !急用!若是能用,重金感谢啊!另补充: 有四个按键 ,蜂鸣器在温度大于或小于一定值时要可以报警的
展开
展开全部
程序功能:将DS18B20所采集到的温度实时显示到1602上。
*****************************************************************/
#include <reg52.H>
#include <intrins.H>
#include <math.H>
#define uchar unsigned char
#define uint unsigned int
sbit dula = P2^6;
sbit wela = P2^7;
sbit RS = P3^5;
sbit LCDEN = P3^4;
void delayUs()
{
_nop_();
}
void delayMs(uint a)
{
uint i, j;
for(i = a; i > 0; i--)
for(j = 100; j > 0; j--);
}
void writeComm(uchar comm)
{
RS = 0;
P0 = comm;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}
//写数据:RS=1, RW=0;
void writeData(uchar dat)
{
RS = 1;
P0 = dat;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}
void init()
{
dula = wela = 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*******************************/
sbit ds = P2^2;
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;
}
void display(int v)
{
unsigned char count;
unsigned char datas[] = {0, 0, 0, 0, 0};
unsigned int 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('.');
}
}
}
/**//*****************************DS18B20*******************************/
void main()
{
uchar table[] = " xianzaiwendu: ";
sendChangeCmd();
init();
writeComm(0x80);
writeString(table, 16);
while(1)
{
delayMs(1000); //温度转换时间需要750ms以上
writeComm(0xc0);
display(getTmpValue());
sendChangeCmd();
}
}
*****************************************************************/
#include <reg52.H>
#include <intrins.H>
#include <math.H>
#define uchar unsigned char
#define uint unsigned int
sbit dula = P2^6;
sbit wela = P2^7;
sbit RS = P3^5;
sbit LCDEN = P3^4;
void delayUs()
{
_nop_();
}
void delayMs(uint a)
{
uint i, j;
for(i = a; i > 0; i--)
for(j = 100; j > 0; j--);
}
void writeComm(uchar comm)
{
RS = 0;
P0 = comm;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}
//写数据:RS=1, RW=0;
void writeData(uchar dat)
{
RS = 1;
P0 = dat;
LCDEN = 1;
delayUs();
LCDEN = 0;
delayMs(1);
}
void init()
{
dula = wela = 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*******************************/
sbit ds = P2^2;
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;
}
void display(int v)
{
unsigned char count;
unsigned char datas[] = {0, 0, 0, 0, 0};
unsigned int 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('.');
}
}
}
/**//*****************************DS18B20*******************************/
void main()
{
uchar table[] = " xianzaiwendu: ";
sendChangeCmd();
init();
writeComm(0x80);
writeString(table, 16);
while(1)
{
delayMs(1000); //温度转换时间需要750ms以上
writeComm(0xc0);
display(getTmpValue());
sendChangeCmd();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
leipole
2024-11-29 广告
2024-11-29 广告
上海雷普电气有限公司的数字温度控制仪,采用高精度传感器与先进算法,实现对温度的精确测量与智能控制。该控制仪具有易操作、稳定性强的特点,广泛应用于工业自动化、暖通空调、冷藏保鲜等领域。用户可通过显示屏直观读取温度数值,并根据需求设定目标温度,...
点击进入详情页
本回答由leipole提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询