?? sht11.c
字號:
/******************************************************************/
/* temperature模and humidity measure */
/* display the data on the 1602; */
/* first line display temprrature */
/* two line display humidity */
/******************************************************************/
#include<reg52.h>
#include"intrins.h" /*keil library for _nop() operation*/
#include"stdio.h"
#include"math.h"
#include"absacc.h" /*keil library*/
#define ACK 1
#define noACK 0
#define measure_temp 0x03 /*command to measure temperature*/
#define measure_humi 0x05 /*command to measure humidity*/
#define RESET 0x1e /*soft reset*/
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define LCD_DATA P0 /*LCD data import*/
sbit RS =P2^0;
sbit RW =P2^1;
sbit E =P2^2;
sbit DATA =P1^0;
sbit SCK =P1^1;
typedef union //reserve measured value for temperature or humidity
{
uint i;
float f;
}Value;
typedef struct
{
unsigned char gewei;
unsigned char shiwei;
unsigned char DATAstrins1[6];
unsigned char DATAstrins2[6];
}Systemtime;
char Write_Byte(uchar value) //write a byte on the sen_bus and checks the acknowledge
{
uchar i,error;
for(i=0x80;i>0;i/=2) // continuous right shift 8 bit
{
if(value&i)DATA =1; //get the DATA
else DATA =0;
SCK =1;
_nop_();
_nop_();
_nop_(); //delay 5 us
SCK =0;
// drop edge
}
DATA =1; //release DATA
SCK =1; //checks answer sequence
error =DATA; //checks the answer state ,if the acknowledge the DATA =0;
SCK =0;
return error;
}
char Read_Byte(uchar ack)
{
uchar i,val =0;
DATA =1; //release the DATA line
for(i=0x80;i>0;i/=2) // continuous right shift 8 bit
{
SCK =1;
if(DATA)
val =(val|i);
SCK =0;
}
DATA =!ack; //if acknowledge DATA =0;
SCK =1;
_nop_(); _nop_(); _nop_(); //delay 5 us
SCK =0;
DATA =1; //release the DATA line
return val;
}
void Transmit_Start(void) //transmit start
{
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 Connection_Reset(void) //if communication breaked , make use of the sequense start the commnication
{
unsigned char i;
DATA =1;
SCK =0;
for(i=9;i>0;i--) //continuous 9 drop edge for connection the transmit
{
SCK=1;
SCK=0;
}
Transmit_Start(); //transmit start
}
char Soft_Reset(void) //reset the SHT11 and clear the value of the register
{
uchar error =0;
Connection_Reset();
error += Write_Byte(RESET);
return error; //return the error for judgment answer
}
char Measure_Mode(uchar *P_Value, uchar *P_Checksum, uchar Mode) //make sure the style to measure
{
uchar error=0;
uchar i;
Transmit_Start();
switch(Mode)
{
case 0:error +=Write_Byte(measure_temp);break; // judgment the mode of measure
case 1:error +=Write_Byte(measure_humi);break;
default : break;
}
for(i=65535;i>0;i--)
if(DATA==0)break; //wait until sht11 complete the measure
if(DATA) error +=1; //time overflow 2second
*P_Value =Read_Byte(ACK); //get the high byte
*(P_Value+1) =Read_Byte(ACK); //get the low byte
*P_Checksum =Read_Byte(noACK); //get the checksum
return error;
}
void Calculate_SHT11(float *P_Temperature , float *P_Humidity) // calculate the temperature and humidity after compensated
{
const float C1=-4.0; //for 12 Bit
const float C2=+0.0405; //for 12 Bit
const float C3=-0.0000028; //for 12 Bit
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 [癈]*/
t_C=t*0.01 - 40; //calc. temperature from ticks to
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>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*P_Temperature=t_C; //return temperature
*P_Humidity=rh_true; //return humidity[%RH]
}
/***************************************************************************************/
void DelayUs(void) //delay 2 us;
{
_nop_();_nop_();
}
void DelayMs(uint a) //delay a*1Ms
{
uint i,j;
for(i=a;i>0;i--)
for(j=100;j>0;j--);
}
bit Check_Busy() //check the 1602 state ,if busing return 1, else return 0
{
bit Busy;
RS =0;
RW =1;
E=1;
DelayUs();
Busy =(bit)(LCD_DATA&0X80);
E=0;
return Busy;
}
void Wait(void) //wait the 1602 idle;
{
while(Check_Busy());
}
void Write_Command(uchar Command) //write the command
{
Wait();
RS =0;
RW =0;
E =0;
DelayUs();
LCD_DATA =Command;
DelayUs();
E =1;
DelayUs();
E =0;
}
void Write_Command_NO(uchar Command) //write the command but uncheck the busy
{
RS =0;
RW =0;
E =0;
DelayUs();
LCD_DATA =Command;
DelayUs();
E =1;
DelayUs();
E =0;
}
void Write_Data(uchar Data) //write data
{
Wait();
RS =1;
E =0;
RW =0;
DelayUs();
LCD_DATA =Data;
DelayUs();
E =1;
DelayUs();
E =0;
}
void Write_Data_NO(uchar Data) //write data but uncheck the busy
{
RS =1;
E =0;
RW =0;
DelayUs();
LCD_DATA =Data;
DelayUs();
E =1;
DelayUs();
E =0;
}
void Init_Lcd(void) //init the 1602
{
DelayMs(15);
Write_Command_NO(0X38);
DelayMs(5);
Write_Command_NO(0X38);
DelayMs(5);
Write_Command_NO(0X38);
Write_Command(0x38);
Write_Command(0x08); //display the cursor and the cursor flash
Write_Command(0x01);
Write_Command(0x06);
Write_Command(0x0c);
}
void Select_Position(uchar x, uchar y) //display on the 1602 x line ,y position
{
uchar P;
switch(x)
{
case 1: P=0X80;break;
case 2: P=0xc0;break;
}
P +=y;
Write_Command(P);
}
void Date_To_String(Systemtime *Time,float DATA,float DATA1)
{
uint i;
i=(int)DATA;
Time->shiwei=i/10;
Time->gewei =i%10;
Time->DATAstrins1[0] = Time->shiwei + '0';
Time->DATAstrins1[1] = Time->gewei + '0';
i=(int)((DATA-i)*10);
Time->DATAstrins1[2] ='.';
Time->DATAstrins1[3] = i + '0';
Time->DATAstrins1[4] = '\0';
i=(int)DATA1;
Time->shiwei=i/10;
Time->gewei =i%10;
Time->DATAstrins2[0] = Time->shiwei + '0';
Time->DATAstrins2[1] = Time->gewei + '0';
i=(int)((DATA1-i)*10);
Time->DATAstrins2[2] ='.';
Time->DATAstrins2[3] = i + '0';
Time->DATAstrins2[4] = '%';
Time->DATAstrins2[5] = '\0';
}
void Write_String(uchar *String)
{
unsigned char i;
while(String[i]!='0')
{
Write_Data(String[i++]);
}
}
/***************************************************************************************/
void main(void)
{
Systemtime S;
uchar time_str1[]={"Temp"};
uchar time_str2[]={"Humi"};
Value Temp_Value ,Humi_Value;
uchar error ,checksum;
uint i;
Connection_Reset();
Init_Lcd();
Select_Position(1, 0);
Write_String(time_str1);
Select_Position(2, 0);
Write_String(time_str2);
while(1)
{
error =0;
error+=Measure_Mode((unsigned char*) &Humi_Value.i,&checksum,1); /*measure humidity*/
error+=Measure_Mode((unsigned char*) &Temp_Value.i,&checksum,0); /*measure temperature*/
if(error!=0)
Connection_Reset(); /*in case of an error:connection reset*/
else
{
Humi_Value.f=(float)Humi_Value.i; /*converts integer to float*/
Temp_Value.f=(float)Temp_Value.i; /*converts integer to float*/
Calculate_SHT11(&Humi_Value.f,&Temp_Value.f); /*calculate humidity,temperature*/
Date_To_String(&S,Temp_Value.f,Humi_Value.f);
Select_Position(1,5);
Write_String(S.DATAstrins1);
Select_Position(2,5);
Write_String(S.DATAstrins2);
}
/*----------wait approx. 0.8s to avoid heating up SHT10------------------------------ */
for (i=0;i<10000;i++); //(be sure that the compiler doesn't eliminate this line!)
/*----------------------------------------------------------------------------------- */
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -