?? mysht10.c
字號(hào):
/***********************************************************************************
;功能說(shuō)明:SHT10-LED溫濕度顯示KeilC程序
;文件名稱:mysht10.c
;微處理器:AT89C52
;編譯環(huán)境:Keil uVision V3.53 、uv2.38a均通過(guò),proteus7.4sp3仿真正常。
;作 者:Huang qianjiang
;創(chuàng)建日期:2009.04.15
;版 本:V1.0
;修改日期:
;修改說(shuō)明:
;***********************************************************************************/
/*************定義接口********************
P0------ (高位led段)
P2------ (低位led段)
P1.1------SCK (SHT10時(shí)鐘線)
P1.0------DATA (SHT10數(shù)據(jù)線)
P1.6------(溫度顯示片選)
P1.7------(濕度顯示片選)
P1.5-------(蜂鳴器控制)
P1.4-------(控制繼電器開(kāi)關(guān))
P3.0、P3.1-------(串行口)
*****************************************/
#include <reg52.h>
#include <intrins.h>
#include <math.h> //Keil library
#include <stdio.h> //Keil library
//*********************第一部分LED設(shè)置****************************************
unsigned int wendu,shidu;
unsigned char code table_h[20]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char code table_l[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit Tc = P1^7; //溫度顯示控制,1=關(guān),0=開(kāi)
sbit Hc = P1^6; //濕度顯示控制,1=關(guān),0=開(kāi)
/*sbit Yc = P1^5; //蜂鳴器控制
sbit Jc = P1.4 //開(kāi)關(guān)輸出
static unsigned char bdata StateREG;//可位尋址的輸出狀態(tài)寄存器
sbit YC1 = StateREG^0; //是否有告警1
sbit YC2 = StateREG^1; //是否告警2
sbit YC3 = StateREG^2; //是否告警3
sbit JC1 = StateREG^3; //是否開(kāi)關(guān)
sbit QM = StateREG^4; //P1.4輸出一定時(shí)間后(5分鐘),溫度上升小于3度告警4(缺煤)
*/
/******定義函數(shù)****************/
#define uchar unsigned char
#define uint unsigned int
void LED_init(void); //LED初始化函數(shù)
void LED_xianshi(void); //LED顯示函數(shù)
void delay_n10us(uint n); //延時(shí)10uS函數(shù)
//void shuchu_kz(void); //輸出控制函數(shù)
//中斷函數(shù)
//串口初始化函數(shù)
//void com_data(void); //串口發(fā)送數(shù)據(jù)函數(shù)
//void GJ_sheng(void); //告警聲音函數(shù)
/*--------------------------------------
;模塊名稱:LED_init();
;功 能:初始化LED、及端口
;參數(shù)說(shuō)明:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
void LED_init(void)
{
Tc = 1;
Hc = 1;
//Yc = 1;
// Jc = 1;
//bdata StateREG = 0x00;
P0=0xff ;//data_h = 0x80;
P2=0xff ;//data_l = 0x00;
}
/*--------------------------------------
;模塊名稱:LED_xianshi();
;功 能:LED 顯示溫度、濕度
;參數(shù)說(shuō)明:
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
void LED_xianshi(void)
{
uint i;
uchar a,b,c,v,w;
for(i=0;i<51;i++)
{
Hc = 1;
P0 = 0xff;
P2 = 0xff;
delay_n10us(200);
Tc = 0;
a = (wendu/1000); //溫度百位
b=((wendu%1000)/100); //溫度十位
c=((wendu%100)/10); //溫度個(gè)位
a = a*10+b;
P0 = table_h[a];
P2 = table_l[c];
delay_n10us(1000);//延時(shí)10ms
Tc = 1;
P0 = 0xff;
delay_n10us(200);
Hc = 0;
v=((shidu%1000)/100); //濕度十位
w=((shidu%100)/10); //濕度個(gè)位
P0 = table_h[v];
P2 = table_l[w];
delay_n10us(1000);
Hc = 1;
}
}
/*--------------------------------------
;模塊名稱:delay_n10us(uint n);
;功 能:延時(shí)10uS函數(shù)
;參數(shù)說(shuō)明:延時(shí)n個(gè)10us
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
void delay_n10us(uint n) //延時(shí)n個(gè)10us@12M晶振
{
uint i;
for(i=n;i>0;i--)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
}
/*--------------------------------------
;模塊名稱:shuchu_kz(void);
;功 能:輸出控制函數(shù)
;參數(shù)說(shuō)明:
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
//void shuchu_kz(void)
/*--------------------------------------
;模塊名稱:com_data(void);
;功 能:通過(guò)232向PC發(fā)送當(dāng)前溫度、濕度、輸出控制狀態(tài)寄存器值等數(shù)據(jù)函數(shù)
;參數(shù)說(shuō)明:固定地址01,波特率9600
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
//void com_data(void)
//*********************第二部分SHT10設(shè)置 ****************************************
sbit SCK = P1^1; //定義通訊時(shí)鐘端口
sbit DATA = P1^0; //定義通訊數(shù)據(jù)端口
typedef union
{ unsigned int i; //定義了兩個(gè)共用體
float f;
} value;
enum {TEMP,HUMI}; //TEMP=0,HUMI=1
#define noACK 0 //用于判斷是否結(jié)束SHT10通訊
#define ACK 1 //結(jié)束數(shù)據(jù)傳輸
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
/****************定義函數(shù)****************/
void s_transstart(void); //啟動(dòng)傳輸函數(shù)
void s_connectionreset(void); //連接復(fù)位函數(shù)
char s_write_byte(unsigned char value);//SHT10寫(xiě)函數(shù)
char s_read_byte(unsigned char ack); //SHT10讀函數(shù)
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//測(cè)量溫濕度函數(shù)
void calc_dht10(float *p_humidity ,float *p_temperature);//溫濕度補(bǔ)償
/*--------------------------------------
;模塊名稱:s_transstart();
;功 能:啟動(dòng)傳輸函數(shù)
;參數(shù)說(shuō)明:
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
void s_transstart(void)
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA=1; //初始狀態(tài)
SCK=0;
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
/*--------------------------------------
;模塊名稱:s_connectionreset();
;功 能:連接復(fù)位函數(shù)
;參數(shù)說(shuō)明:
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
void s_connectionreset(void)
// 通訊復(fù)位: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i; //初始狀態(tài)
DATA=1;
SCK=0;
for(i=0;i<9;i++) //9 SCK cycles
{
SCK=1;
SCK=0;
}
s_transstart(); //啟動(dòng)傳輸
}
/*--------------------------------------
;模塊名稱:s_write_byte();
;功 能:SHT10寫(xiě)函數(shù)
;參數(shù)說(shuō)明:
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & value) DATA=1; //masking value with i , write to SENSI-BUS
else DATA=0;
SCK=1; //clk for SENSI-BUS
_nop_();_nop_();_nop_(); //pulswith approx. 3 us
SCK=0;
}
DATA=1; //release DATA-line
SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by SHT10),DATA在第9個(gè)上升沿將被SHT10自動(dòng)下拉為低電平。
_nop_();_nop_();_nop_();
SCK=0;
DATA=1; //release DATA-line
return error; //error=1 in case of no acknowledge //返回:0成功,1失敗
}
/*--------------------------------------
;模塊名稱:s_read_byte();
;功 能:SHT10讀函數(shù)
;參數(shù)說(shuō)明:
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
char s_read_byte(unsigned char ack)
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK=1; //clk for SENSI-BUS
if (DATA) val=(val | i); //read bit
_nop_();_nop_();_nop_(); //pulswith approx. 3 us
SCK=0;
}
if(ack==1)DATA=0; //in case of "ack==1" pull down DATA-Line
else DATA=1; //如果是校驗(yàn)(ack==0),讀取完后結(jié)束通訊
_nop_();_nop_();_nop_(); //pulswith approx. 3 us
SCK=1; //clk #9 for ack
_nop_();_nop_();_nop_(); //pulswith approx. 3 us
SCK=0;
_nop_();_nop_();_nop_(); //pulswith approx. 3 us
DATA=1; //release DATA-line
return val;
}
/*--------------------------------------
;模塊名稱:s_measure();
;功 能:測(cè)量溫濕度函數(shù)
;參數(shù)說(shuō)明:
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
s_transstart(); //啟動(dòng)傳輸
switch(mode){ //發(fā)送命令
case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
for (i=0;i<65535;i++) if(DATA==0) break; //等待完成測(cè)量
if(DATA) error+=1; // 或超時(shí) (約2 sec.)
*(p_value) =s_read_byte(ACK); //讀首字節(jié) (MSB)
*(p_value+1)=s_read_byte(ACK); //讀第二字節(jié) (LSB)
*p_checksum =s_read_byte(noACK); //讀檢查和
return error;
}
/*--------------------------------------
;模塊名稱:calc_dht10();
;功 能:溫濕度補(bǔ)償計(jì)算函數(shù)
;參數(shù)說(shuō)明:
;創(chuàng)建日期:
;版 本:
;修改日期:
;修改說(shuō)明:
;-------------------------------------*/
void calc_dht10(float *p_humidity ,float *p_temperature)
// calculates temperature [C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [C]
{ const float C1=-4.0; // for 12 Bit (-4.0)
const float C2=+0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit(-0.0000028)
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [C]
t_C=t*0.01 - 40; //calc. temperature from ticks to [C]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>99)rh_true=99; //cut if the value is outside of//100
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [C]
*p_humidity=rh_true; //return humidity[%RH]
}
//*********主函數(shù)*****************
void main(void)
{
value humi_val,temp_val;
unsigned char error,checksum;
LED_init(); //LED初始化
s_connectionreset(); //連接復(fù)位SHT10
while(1)
{
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
if(error!=0)
{
P0 = 0x86; //如果出錯(cuò),溫度LED顯示“E0”
P2 = 0xc0;
Tc = 0;
s_connectionreset();
} //重新連接復(fù)位
else
{
humi_val.f=(float)humi_val.i; //轉(zhuǎn)換濕度為浮點(diǎn)
temp_val.f=(float)temp_val.i; //轉(zhuǎn)換溫度為浮點(diǎn)
calc_dht10(&humi_val.f,&temp_val.f); //計(jì)算濕度、溫度
wendu=10*temp_val.f + 5; //+5為小數(shù)位4舍5入
shidu=10*humi_val.f;
/*if(wendu>70 || shidu<40)
{
YC3 = 1;
}
else if(wendu>60 || shidu<30)
{
YC2 = 1;
}
else if(wendu>50 || shidu<20)
{
YC1 = 1;
}
else if(wendu<70 || shidu>50)
{
JC1 = 1;
}
else if(bdata StateREG != 0)
{
shuchu_kz(); //輸出控制函數(shù)
}
else //bdata StateREG = 0x00;
com_data(); //232發(fā)送數(shù)據(jù)函數(shù)
*/
LED_xianshi(); //LED顯示
}
delay_n10us(100000); //延時(shí)約1s
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -