?? 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 + -