#include
#include
#include "LCD1602.h"
#define uint unsigned int
#define uchar unsigned char
#define noACK 0
#define ACK 1
#define STATUS_REG_W 0x06
#define STATUS_REG_R 0x07
#define MEASURE_TEMP 0x03
#define MEASURE_HUMI 0x05
#define RESET 0x1e
enum { TEMP, HUMI };
typedef union //定義共用同類型
{
unsigned int i;
float f;
} value;
sbit SCK = P1 ^ 0;
sbit DATA = P1 ^ 1;
uchar wendu[6];
uchar shidu[6];
void delay_50us(uint t)
{
uint j;
for (; t>0; t--)
for (j = 19; j>0; j--);
}
void displaywendu(void)
{
uchar i;
write_com(0x80+0x40);
write_data('T');
write_data(':');
for (i = 0; i<3; i++)
{
write_data(wendu[i]);
delay_50us(1);
}
for (i = 0; i<1; i++)
{
write_data('.');
delay_50us(1);
}
for (i = 4; i<5; i++)
{
write_data(wendu[i]);
delay_50us(1);
}
write_data(0xDF);
write_data('C');
}
void displayshidu(void)
{
uchar i;
write_com(0x80);
write_data('H');
write_data(':');
for (i = 0; i<3; i++)
{
write_data(shidu[i]);
delay_50us(1);
}
for (i = 0; i<1; i++)
{
write_data('.');
delay_50us(1);
}
for (i = 4; i<5; i++)
{
write_data(shidu[i]);
delay_50us(1);
}
write_data('%');
write_data('R');
write_data('H');
}
char s_write_byte(unsigned char value)
{
unsigned char i, error = 0;
for (i = 0x80; i>0; i >>= 1) //高位為1,循環右移
{
if (i&value) DATA = 1; //和要發送的數相與,結果為發送的位
else DATA = 0;
SCK = 1;
_nop_(); _nop_(); _nop_(); //延時3us
SCK = 0;
}
DATA = 1; //釋放資料線
SCK = 1;
error = DATA; //檢查應答信号,确認通訊正常
_nop_(); _nop_(); _nop_();
SCK = 0;
DATA = 1;
return error; //error=1 通訊錯誤
}
char s_read_byte(unsigned char ack)
{
unsigned char i, val = 0;
DATA = 1; //釋放資料線
for (i = 0x80; i>0; i >>= 1) //高位為1,循環右移
{
SCK = 1;
if (DATA) val = (val | i); //讀一位資料線的值
SCK = 0;
}
DATA = !ack; //如果是校驗,讀取完後結束通訊;
SCK = 1;
_nop_(); _nop_(); _nop_(); //延時3us
SCK = 0;
_nop_(); _nop_(); _nop_();
DATA = 1; //釋放資料線
return val;
}
void s_transstart(void)
{
DATA = 1; SCK = 0; //準備
_nop_();
SCK = 1;
_nop_();
DATA = 0;
_nop_();
SCK = 0;
_nop_(); _nop_(); _nop_();
SCK = 1;
_nop_();
DATA = 1;
_nop_();
SCK = 0;
}
void s_connectionreset(void)
{
unsigned char i;
DATA = 1; SCK = 0; //準備
for (i = 0; i<9; i++) //DATA保持高,SCK時鐘觸發9次,發送啟動傳輸,通迅即複位
{
SCK = 1;
SCK = 0;
}
s_transstart(); //啟動傳輸
}
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error = 0;
unsigned int i;
s_transstart(); //啟動傳輸
switch (mode) //選擇發送指令
{
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; //等待測量結束
if (DATA) error += 1; // 如果長時間資料線沒有拉低,說明測量錯誤
*(p_value) = s_read_byte(ACK); //讀第一個位元組,高位元組 (MSB)
*(p_value + 1) = s_read_byte(ACK); //讀第二個位元組,低位元組 (LSB)
*p_checksum = s_read_byte(noACK); //read CRC校驗碼
return error; // error=1 通訊錯誤
}
void calc_sth10(float *p_humidity, float *p_temperature)
{
const float C1 = -4.0; // 12位濕度精度 修正公式
const float C2 = +0.0405; // 12位濕度精度 修正公式
const float C3 = -0.0000028; // 12位濕度精度 修正公式
const float T1 = +0.01; // 14位溫度精度 5V條件 修正公式
const float T2 = +0.00008; // 14位溫度精度 5V條件 修正公式
float rh = *p_humidity; // rh: 12位 濕度
float t = *p_temperature; // t: 14位 溫度
float rh_lin; // rh_lin: 濕度 linear值
float rh_true; // rh_true: 濕度 ture值
float t_C; // t_C : 溫度 ℃
t_C = t*0.01 - 40; //補償溫度
rh_lin = C3*rh*rh + C2*rh + C1; //相對濕度非線性補償
rh_true = (t_C - 25)*(T1 + T2*rh) + rh_lin; //相對濕度對于溫度依賴性補償
if (rh_true>100)rh_true = 100; //濕度最大修正
if (rh_true<0.1)rh_true = 0.1; //濕度最小修正
*p_temperature = t_C; //傳回溫度結果
*p_humidity = rh_true; //傳回濕度結果
}
void main(void)
{
unsigned int temp, humi;
value humi_val, temp_val; //定義兩個共同體,一個用于濕度,一個用于溫度
unsigned char error; //用于檢驗是否出現錯誤
unsigned char checksum; //CRC
LcdInitiate();
……………………
…………限于本文篇幅 餘下代碼請從51黑下載下傳附件…………