📄 3_ds.c
字号:
while(P0==ds_num); // 检测到应答脉冲
i = 4;
while (i>0) i--;
}
/* 读取数据的一位,满足读时隙要求 */
uchar RdBit(void)
{
uint i;
uchar b;
P0 = 0x00;
i++;
P0 = ds_num;
i++;i++; // 延时15us以上,读时隙下降沿后15us,DS18B20输出数据才有效
b = P0;
i = 8;
while(i>0) i--;
return (b);
}
/* 读取数据的一个字节 */
uchar RdByte(void)
{
//float k;
unsigned int xdata uiData[8];
unsigned char xdata Mask; //OS the resoult of Temperature
unsigned char xdata p_tem[16];
uint i,j,b;
//uchar i,j,b;
//b = 0;
for (i=0;i<=15;i++)
{
p_tem[i] = RdBit();
//b = (j<<7)|(b>>1);
}
//return(b);
for(i=16;i>0;--i)
{
b=i-1;
Mask = 0x01;
for(j=0;j<8;j++)
{
uiData[j] = uiData[j]<<1;
if(p_tem[b]&Mask) uiData[j]++;
Mask = Mask<<1;
}
}
//k=uiData[0]*0.0625;
ds1.ds1_temp=uiData[0]*6.25;
ds2.ds2_temp=uiData[1]*6.25;
ds3.ds3_temp=uiData[2]*6.25;
}
/* 写数据的一个字节,满足写1和写0的时隙要求 */
void WrByte(uchar b)
{
uint i;
uchar j;
bit btmp;
for(j=1;j<=8;j++)
{
btmp = b&0x01;
b = b>>1; // 取下一位(由低位向高位)
if (btmp)
{
/* 写1 */
P0 = 0;
i++;i++; // 延时,使得15us以内拉高
P0 = ds_num;
i = 8;
while(i>0) i--; // 整个写1时隙不低于60us
}
else
{
/* 写0 */
P0 = 0;
i = 8;
while(i>0) i--; // 保持低在60us到120us之间
P0 = ds_num;
i++;
i++;
}
}
}
/* 启动温度转换 */
void convert(void)
{
TxReset(); // 产生复位脉冲,初始化DS18B20
RxWait(); // 等待DS18B20给出应答脉冲
delay(1); // 延时
WrByte(0xcc); // skip rom 命令
WrByte(0x44); // convert T 命令
}
/* 读取温度值 */
void RdTemp(void)
{
TxReset(); // 产生复位脉冲,初始化DS18B20
RxWait(); // 等待DS18B20给出应答脉冲
delay(1); // 延时
WrByte(0xcc); // skip rom 命令
WrByte(0xbe); // read scratchpad 命令
//tplsb = RdByte(); // 温度值低位字节(其中低4位为二进制的“小数”部分)
//tpmsb = RdByte(); // 高位值高位字节(其中高5位为符号位)
RdByte();
}
/* 主程序,读取的温度值最终存放在tplsb和tpmsb变量中。
tplsb其中低4位为二进制的“小数”部分;tpmsb其中高
5位为符号位。真正通过数码管输出时,需要进行到十进
制有符号实数(包括小数部分)的转换。 */
/*void main(void)
{
do
{
delay(1); // 延时1ms
convert(); // 启动温度转换,需要750ms
delay(1000); // 延时1s
RdTemp(); // 读取温度
}
while(1);
}
*/
/***************************校验CRC16,CRC8************************************/
unsigned char crc16(unsigned char *chcmd,unsigned char lenth,unsigned char num) //校验码crc16计算
{
unsigned char crc16_h=0xff,crc16_l=0xff;
unsigned char save_h=0x00,save_l=0x00;
unsigned char ch=0xa0,cl=0x01;
unsigned char i,j;
unsigned char dat;
for(i=0;i<lenth;i++)
{
dat=*chcmd;
chcmd++;
crc16_h=crc16_h^dat;
for(j=0;j<8;j++)
{
save_h=crc16_h;
save_l=crc16_l;
crc16_l=crc16_l>>1;
crc16_h=crc16_h>>1;
if((save_l&0x01)==0x01) crc16_h=crc16_h|0x80;
if((save_h&0x01)==0x01)
{
crc16_l=crc16_l^ch;
crc16_h=crc16_h^cl;
}
}
}
if(num==1) return crc16_h;
if(num==0) return crc16_l;
}
void serial() interrupt 4
{
unsigned char p;
unsigned char a;
if (RI)
{
a=SBUF;
redata[0]=redata[1];
redata[1]=redata[2];
redata[2]=redata[3];
redata[3]=redata[4];
redata[4]=redata[5];
redata[5]=redata[6];
redata[6]=redata[7];
redata[7]=a;
RI=0;
}
if((redata[0]==0x01))
{
if(redata[1]==0x03)
{
//int485=1;
EA=0; //关总中断
sedata[0]=0x01;
sedata[1]=0x03;
sedata[2]=0x04;
sedata[3]=s.s_t[0];
sedata[4]=s.s_t[1];
sedata[5]=ds1.ds1_t[0];
sedata[6]=ds1.ds1_t[1];
sedata[7]=ds2.ds2_t[0];
sedata[8]=ds2.ds2_t[1];
sedata[9]=ds3.ds3_t[0];
sedata[10]=ds3.ds3_t[1];
sedata[11]=crc16(sedata,11,1);
sedata[12]=crc16(sedata,11,0);
for(p=0;p<13;p++)
{
SBUF=sedata[p];
while(!TI);TI=0;
}
ds1.ds1_t[0]=0;
ds2.ds2_t[0]=0;
ds3.ds3_t[0]=0;
ds1.ds1_t[1]=0;
ds2.ds2_t[1]=0;
ds3.ds3_t[1]=0;
EA=1; //开总中断
}
}
RI=0;
TI=0;
}
void init(void)
{
TMOD = 0x20; //自动再装入8位计数器
PCON = 0X00; //波特率倍率为零
SCON = 0x50; //采用方式一进行串口数据通信
TH1 = 0xfd;
TL1 = 0xfd;
PS = 1; //若PS=1,则串行口指定为高中断优先级,否则为低中断优先级
ES = 1; //当ES=1 允许串行中断
EA = 1; //当EA=1时,中断总允许
TR1 = 1; //启动定时器,产生波特率
RI = 0; //清接收中断标志位
TI = 0; //清发送中断标志位
// int485=0; // RE低电平有效的接收允许,DE高电平有效的发送允许
}
//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [癈]
// 4. calculate dew point [癈]
// 5. print temperature, humidity, dew point
{ value humi_val,temp_val;
//value temp_val;
//float dew_point;
unsigned char error,checksum;
// unsigned char checksum;
// unsigned int i;
//unsigned char tplsb,tpmsb;
ds_num=0x07;
// int485=1;
// init_uart();
init();
s_connectionreset();
while(1)
{
delay(1); // 延时1ms
convert(); // 启动温度转换,需要750ms
delay(1000); // 延时1s
RdTemp(); // 读取温度
//ds1.ds1_temp=(tpmsb*256+tplsb)*6.25;
//ds1.ds1_temp=ds1_tem*100;
error=0;
//error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
//s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
//if(error!=0) s_connectionreset(); //in case of an error: connection reset
//else
//{ //humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
//calc_sth11(&temp_val.f,&temp_val.f);
//dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
//printf("temp:%dC humi:%d%%\n",temp_val.i,humi_val.i);
//printf("temp:%5.2fC humi:%5.2f%%\n",temp_val.f,humi_val.f);
//printf("%5.1f,%5.1f\n",temp_val.f,ds_temp);
// printf("%d,%d\n",temp_val.i,humi_val.i);
// while(!TI);TI=0;
//tm=&temp_val.f;
//SBUF=*(tm+1);
//s_temph=temp_val.f;
//s_temph=s_temph*100;
//s_templ=(temp_val.f-s_temph)*100+1;
//s_templ=s_temp*100;
//ds_temph=ds_temp;
//ds_templ=(ds_temp-ds_temph)*100+1;
//SBUF=s_temph;
//while(!TI);TI=0;
//SBUF=*tm;
//SBUF=s_templ;
//while(!TI);TI=0;
s.s_temp=temp_val.f*100;
//}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
//for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -