📄 main.c
字号:
/*****************************************************************************
Copyright(c)2010, GUILIN UNIVERSITY OF ELECTRONIC TECHNOLOGY
All rights reserved
文件名称:main.c
实验任务:
当前版本:1.0
作 者:davecheng
完成日期:2010.06.04
本次使用的DS2438的64位ROM为 26H 77H 22H 15H 00H 00H 00H FEH
实验用的DS18B20的64位ROM为 28H 9EH 9EH 7BH 02H 00H 00H DAH
没有用到CRC校验!
NOTES:
1. Temperature conversion takes up to 10 ms.
2.
3. EEPROM writes take up to 10 ms.
********************************************************************************/
#include <reg52.h>
#include <intrins.h>
//#include "PIN.H"
//#include "CH452CMD.H"
#include "CH452W4.C"
typedef unsigned char uchar;
typedef unsigned int uint;
uchar bdata status;
sbit DQ=P3^4;//单总线数据线
sbit TB=status^4;//温度转换标志位
sbit ADB=status^6;//AD转换标志位
uchar temperatureLsb=0xFF, temperatureMsb=0xFF, voltageLsb=0xFF, voltageMsb=0xFF;//8位的温度,湿度数据存储单元
//uchar status1=0xFF, status2, status3, status4, status5, status6, status7;//测试程序时使用的变量
uint temperature, humidity;//16位的温湿度存储单元
uchar bit0, bit1, bit2, bit3, bit4, bit5;//LED0,1,2,3,4,5显示位
float temperatureTemp, humidityTemp;//温湿度数据处理缓冲单元
uchar code sectionTable[]= //LED显示时的数字数组。
{
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
/******************************************************************************
延时函数
函数功能:根据用户的需要产生延时时间。
******************************************************************************/
void Delay(uchar num) //*********us精确延时,num*2+5us.
{
while (--num);
}
void Del(uint num) //长时间延时。
{
while (--num);
}
/******************************************************************************
单总线驱动函数/1-Wire Communication Through Software
函数功能:单总线器件的初始化,读写BIT,读写字节。
******************************************************************************/
void ResetDS2438(void) //复位单总线器件
{
do
{
DQ=0;
Delay(240);//标准480us,H
DQ=1;
Delay(35);//标准70us,I
}
while (DQ);
Delay(205);//标准410us,J
DQ=1;
}//复位程序调试OK
uchar ReadBit(void) //读一BIT并返回
{
uchar result;
DQ=0;
Delay(1);//标准6us,A
DQ=1;
Delay(2);//标准9us,E
result=DQ;
Delay(25);//标准55us,F
//DQ=1;需要释放总线?
return result;
}
uchar ReadByte(void) //读一BYTE并返回
{
uchar i, dataTemp=0x00;
for (i=0; i<8; i++)
{
dataTemp>>=1;
if (ReadBit())
{
dataTemp|=0x80;
}
}
//DQ=1;需要释放总线?
return dataTemp;
}//调试OK
void WriteBit(uchar Bit) //写一位BIT
{
if (Bit)
{ //写BIT 1
DQ=0;
Delay(1);//标准6us,A
DQ=1;
Delay(32);//标准64us,B
}
else
{//写BIT 0
DQ=0;
Delay(30);//标准60us,C
DQ=1;
Delay(3);//标准10us,D
}
}
void WriteByte(uchar dataTemp)//写一BYTE
{
int i;
for (i=0; i<8; i++)
{
WriteBit(dataTemp&0x01);
dataTemp>>=1;
}
//DQ=1;
}//调试OK
/******************************************************************************
主函数
函数功能:处理数据,并在LED上显示温湿度。
******************************************************************************/
void main (void)
{
DQ=1;
CH452_Write(CH452_RESET);
CH452_Write(0x401);
CH452_Write(0x56d);//CH452的初始化
CH452_Write(0x83f);
CH452_Write(0x9bf);
CH452_Write(0xa3f);//温度位显示00.0
Del(50000);//0.8s
CH452_Write(0xb3f);
CH452_Write(0xcbf);
CH452_Write(0xd3f);//湿度位显示00.0
Del(50000);
while (1)
{ //每隔2.5s刷新LED,读取DS2438的数据,并处理,最后在LED上显示。
ResetDS2438();
WriteByte(0xCC);
WriteByte(0x44);//CONVERT T,Temperature conversion takes up to 10 ms
DQ=1;
ResetDS2438();
WriteByte(0xCC);
WriteByte(0xB4);//CONVERT V,A/D conversion takes up to 4 ms.
DQ=1;
Del(50000);//0.8s,保证温湿度转换的时间。
ResetDS2438();
WriteByte(0xCC);
WriteByte(0xB8);
WriteByte(0x00);//Issue Recall Memory page 00h command
ResetDS2438();
WriteByte(0xCC);
WriteByte(0xBE);
WriteByte(0x00);//Issue Read SP 00h command,这两句的顺序不能变!
status=ReadByte();//读状态寄存器
temperatureLsb=ReadByte();
temperatureMsb=ReadByte();//读温度的值
voltageLsb=ReadByte();
voltageMsb=ReadByte();//读AD的值
//温度数据处理
temperature=temperatureMsb;
temperature<<=8;
temperature+=temperatureLsb;
temperature>>=3;
if (temperature&0x1000)
{//0下温度处理
temperature&=0x0FFF;
temperature=0x0FFF-temperature+1;
temperatureTemp=temperature;
temperature=temperatureTemp*0.3125;
bit0=temperature%10;
bit1=temperature/10%10;
CH452_Write(0x800|sectionTable[bit0]);
CH452_Write(0x980|sectionTable[bit1]);
CH452_Write(0xa40);//显示0下温度,位数位个位和小数点后一位。
}
else
{//0上温度处理
temperatureTemp=temperature;
temperature=temperatureTemp*0.3125;
bit0=temperature%10;
bit1=temperature/10%10;
bit2=temperature/100;
CH452_Write(0x800|sectionTable[bit0]);
CH452_Write(0x980|sectionTable[bit1]);
CH452_Write(0xa00|sectionTable[bit2]);//温度显示OK
}
//湿度数据处理,Vout=Vsupply(0.0062(Sensor RH)+0.16), typcial@25°C. 湿度补偿Ture RH=(Sensor RH)/(1.0546-0.00216T),T in °C.
//实际测得HI3605电源脚Vsupply=4.4V。
humidity=voltageMsb;
humidity<<=8;
humidity+=voltageLsb;
humidityTemp=humidity;
humidity=(humidityTemp/4.4-16)/0.06/(1.05406-0.00216*temperatureTemp*0.03125);
//humidity+=5;
bit3=humidity%10;
bit4=humidity/10%10;
bit5=humidity/100;
CH452_Write(0xb00|sectionTable[bit3]);
CH452_Write(0xc80|sectionTable[bit4]);
CH452_Write(0xd00|sectionTable[bit5]);
Del(50000);
Del(50000);
Del(50000);//约2.5s刷新显示。
}
/* WriteByte(0x33);
status=ReadByte();
status1=ReadByte();
status2=ReadByte();
status3=ReadByte();
status4=ReadByte();
status5=ReadByte();
status6=ReadByte();
status7=ReadByte();
CH452_Write(0x800|status);//通过这两句读出了DS2438的族码0x26,用18B20读出族码0x28。调试正确,说明单总线的驱动程序已经OK了。
CH452_Write(0x900|status1);
CH452_Write(0xa00|status2);
CH452_Write(0xb00|status3);
CH452_Write(0xc00|status4);
CH452_Write(0xd00|status5);
CH452_Write(0x800|status6);
CH452_Write(0x900|status7);
status=ReadByte();
status1=ReadByte();
status2=ReadByte();
status3=ReadByte();
status4=ReadByte();
CH452_Write(0x800|status);//把转换温度后的STATUS状态读出。怎么读出的都是00H啊!默认的是XXXX1111B?
CH452_Write(0x900|status1);
CH452_Write(0xa00|status2);
CH452_Write(0xb00|status3);//把转换温度后的STATUS状态读出。怎么读出的都是00H啊!默认的是XXXX1111B?
CH452_Write(0xc00|status4);}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -