本帖最后由 苯甲醇 于 2020-6-10 15:42 編輯
#include<reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define noACK 0 //繼續傳輸數據,用于判斷是否結束通訊
#define ACK 1 //結束數據傳輸;
//地址 命令 讀/寫
#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
enum {TEMP,HUMI};
sbit DATA = P2^5;
sbit SCK = P2^4;
sbit RS = P2^0;
sbit RW = P2^1;
sbit E = P2^2;
sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.數據端口
/******** DS1602函數聲明 ********/
void LCD_Initial();
void GotoXY(unsigned char x, unsigned char y);
void Print(unsigned char *str);
void LCD_Write(bit style, unsigned char input);
/******** SHT10函數聲明 ********/
void s_connectionreset(void);
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
void calc_sth10(float *p_humidity ,float *p_temperature);
//float calc_dewpoint(float h,float t);
/****************************************************************/
//寫字節程序
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)
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
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)
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DATA=1; SCK=0; //準備
for(i=0;i<9;i++) //DATA保持高,SCK時鐘觸發9次,發送啟動傳輸,通迅即復位
{
SCK=1;
SCK=0;
}
s_transstart(); //啟動傳輸
}
//軟復位程序
char s_softreset(void)
// resets the sensor by a softreset
{
unsigned char error=0;
s_connectionreset(); //啟動連接復位
error+=s_write_byte(RESET); //發送復位命令
return error; //error=1 通訊錯誤
}
/*讀狀態寄存器
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//寫狀態寄存器
char s_write_statusreg(unsigned char *p_value)
// writes the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
} */
//溫濕度測量
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
// 進行溫度或者濕度轉換,由參數mode決定轉換內容;
{
// enum {TEMP,HUMI}; //已經在頭文件中定義
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; //返回濕度結果
}
//從相對溫度和濕度計算露點
/*float calc_dewpoint(float h,float t)
{
float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
} */
/***********************************************************************************************************************************************************/
//DS1602程序(1602.c):
//#include<tou.h>
//內部等待函數**************************************************************
unsigned char LCD_Wait(void)
{
RS=0;
RW=1; _nop_();
E=1; _nop_();
E=0;
return DBPort;
}
//向LCD寫入命令或數據********************************************************
#define LCD_COMMAND 0 // Command
#define LCD_DATA 1 // Data
#define LCD_CLEAR_SCREEN 0x01 // 清屏
#define LCD_HOMING 0x02 // 光標返回原點
void LCD_Write(bit style, unsigned char input)
{
E=0;
RS=style;
RW=0; _nop_();
DBPort=input; _nop_();//注意順序
E=1; _nop_();//注意順序
E=0; _nop_();
LCD_Wait();
}
//設置顯示模式************************************************************
#define LCD_SHOW 0x04 //顯示開
#define LCD_HIDE 0x00 //顯示關
#define LCD_CURSOR 0x02 //顯示光標
#define LCD_NO_CURSOR 0x00 //無光標
#define LCD_FLASH 0x01 //光標閃動
#define LCD_NO_FLASH 0x00 //光標不閃動
void LCD_SetDisplay(unsigned char DisplayMode)
{
LCD_Write(LCD_COMMAND, 0x08|DisplayMode);
}
//設置輸入模式************************************************************
#define LCD_AC_UP 0x02
#define LCD_AC_DOWN 0x00 // default
#define LCD_MOVE 0x01 // 畫面可平移
#define LCD_NO_MOVE 0x00 //default
void LCD_SetInput(unsigned char InputMode)
{
LCD_Write(LCD_COMMAND, 0x04|InputMode);
}
//初始化LCD************************************************************
void LCD_Initial()
{
E=0;
LCD_Write(LCD_COMMAND,0x38); //8位數據端口,2行顯示,5*7點陣
LCD_Write(LCD_COMMAND,0x38);
LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //開啟顯示, 無光標
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC遞增, 畫面不動
}
//液晶字符輸入的位置************************
void GotoXY(unsigned char x, unsigned char y)
{
if(y==0)
LCD_Write(LCD_COMMAND,0x80|x);
if(y==1)
LCD_Write(LCD_COMMAND,0x80|(x-0x40));
}
//將字符輸出到液晶顯示
void Print(unsigned char *str)
{
while(*str!='\0')
{
LCD_Write(LCD_DATA,*str);
str++;
}
}
/***********************************************************************************************************************************************************/
//主函數(main.c):
//#include<tou.h>
typedef union //定義共用同類型
{
unsigned int i;
float f;
} value;
//延時函數
void delay(int z) //z為毫秒數
{
int x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
void main()
{
unsigned int temp,humi;
value humi_val,temp_val; //定義兩個共同體,一個用于濕度,一個用于溫度
// float dew_point; //用于記錄露點值
unsigned char error; //用于檢驗是否出現錯誤
unsigned char checksum; //CRC
uchar wendu[6]; //用于記錄溫度
uchar shidu[6]; //用于記錄濕度
LCD_Initial(); //初始化液晶
GotoXY(0,0); //選擇溫度顯示位置
Print("TEMP: %C"); //5格空格
GotoXY(0,1); //選擇濕度顯示位置
Print("HUMI: %RH"); //5格空格
s_connectionreset(); //啟動連接復位
while(1)
{
error=0; //初始化error=0,即沒有錯誤
error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //溫度測量
error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //濕度測量
if(error!=0) s_connectionreset(); ////如果發生錯誤,系統復位
else
{
humi_val.f=(float)humi_val.i; //轉換為浮點數
temp_val.f=(float)temp_val.i; //轉換為浮點數
calc_sth10(&humi_val.f,&temp_val.f); //修正相對濕度及溫度
// dew_point=calc_dewpoint(humi_val.f,temp_val.f); //計算e dew_point
temp=temp_val.f*10;
humi=humi_val.f*10;
GotoXY(5,0); //設置溫度顯示位置
wendu[0]=temp/1000+'0'; //溫度百位
wendu[1]=temp%1000/100+'0'; //溫度十位
wendu[2]=temp%100/10+'0'; //溫度個位
wendu[3]=0x2E; //小數點
wendu[4]=temp%10+'0'; //溫度小數點后第一位
Print(wendu); //輸出溫度
GotoXY(5,1); //設置濕度顯示位置
shidu[0]=humi/1000+'0'; //濕度百位
shidu[1]=humi%1000/100+'0'; //濕度十位
shidu[2]=humi%100/10+'0'; //濕度個位
shidu[3]=0x2E; //小數點
shidu[4]=humi%10+'0'; //濕度小數點后第一位
Print(shidu); //輸出濕度
}
delay(800); //等待足夠長的時間,以現行下一次轉換
}
自己想加進去的報警電路編程:
sbit led = P3^7; //超限指示燈
sbit led1 = P3^2; //正常指示燈
sbit buzz = P2^3; //蜂鳴器
sbit key_set = P1^5; //設置鍵
void Delay_1ms(uint i);
void display1(void)
{
if(SHT10_Check()==1) //判斷校驗和是否正確
{
if(temp<=16||temp>35||humi<=40||humi>80) //判斷是否超出設置的溫濕度上下限
{
buzz=0; //超出的話蜂鳴器響起
led=0; //點亮報警燈
led1=1;
}
else //否者溫濕度都處于限值范圍內
{
buzz=1; //關閉蜂鳴器
led=1;
led1=0; //點亮正常燈
}
}
}
添加了自己的那個代碼后編譯一下出現下面這樣的情況
|