value humi_val,temp_val;
#define keyin (P1IN & 0xf0)
#define keyout (P1OUT & 0x0f)
unsigned temp1,temp2;
uchar p1[6]={0+48};
uchar p2[6]={0+48};
uchar p3[6]={0+48};
float P0,H0,T0;
void ClockInit(void)
{
/*BCSCTL1&=~XT2OFF; //打开XT振荡器
BCSCTL2|=SELM1+SELS+DIVS_2+DIVS_1;//MCLK 8M and SMCLK 1M 8分频
do
{
IFG1 &= ~OFIFG; //清除振荡错误标志
for(unsigned int i = 0; i < 100; i++)
_NOP(); //延时等待
}
while ((IFG1 & OFIFG) != 0); //如果标志为1继续循环等待*/
//port
P3DIR=BIT2;
P6DIR|= SDA+ SCK;
P1DIR=0x0f;
P1OUT=0x00;
}
void SysInit(void)
{
MS561101BA_Init(); //MS561101BA初始化
S_Init();
S_Connectionreset();
}
void T_P_Measure(void)
{
MS561101BA_getTemperature(MS561101BA_D2_OSR_4096);
MS561101BA_getPressure(MS561101BA_D1_OSR_4096);
Exchange_Number();
LCD_WriteStr(6,2,exchange_num);
}
void R_T_Exchange(float *H)
{
uchar exchange[7];
long h;
h=(long)(*H*10000);
exchange[0]=h/100000+0x30;
h=h%100000;
exchange[1]=h/10000+0x30;
h=h%10000;
exchange[2]='.';
exchange[3]=h/1000+0x30;
h=h%1000;
exchange[4]=h/100+0x30;
h=h%100;
exchange[5]=h/10+0x30;
exchange[6]='\0';
LCD_WriteStr(6,3,exchange);
/////////////////////////////////////////////////////////////////////////
h=(long)(TEMP*100);
exchange[0]=h/100000+0x30;
h=h%100000;
exchange[1]=h/10000+0x30;
h=h%10000;
exchange[2]='.';
exchange[3]=h/1000+0x30;
h=h%1000;
exchange[4]=h/100+0x30;
h=h%100;
exchange[5]=h/10+0x30;
exchange[6]=0;
LCD_WriteStr(6,4,exchange);
}
void R_T_Measure()
{
unsigned char error,checksum;
error=0;
error+=S_Measure((unsigned char*) &humi_val.i,&checksum,HUMIDITY); //measure humidity
error+=S_Measure((unsigned char*) &temp_val.i,&checksum,TEMPERATURE); //measure temperature
//humi_val.i=(humi_val.i & 4095);
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
S_Calculate(&humi_val.f,&temp_val.f); //calculate humidity, temperature
//dew_point=calc_dewpoint(humi_val.f,temp_val.f);
R_T_Exchange(&humi_val.f);
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
for (unsigned int i=0;i<40000;i++); //(be sure that the compiler doesn’t eliminate this line!)
//-----------------------------------------------------------------------------------
}
}
void xianshiInit(void)
{
//////////////////////////////////////
LCD_WriteStr(1,1,"Set:");
LCD_WriteStr(1,2,"P:");
LCD_WriteStr(2,2,p1);
LCD_WriteStr(1,3,"H:");
LCD_WriteStr(2,3,p2);
LCD_WriteStr(1,4,"T:");
LCD_WriteStr(2,4,p3);
LCD_WriteStr(5,2,"p:");
LCD_WriteStr(5,3,"h:");
LCD_WriteStr(5,4,"t:");
//////////////////////////////////////////
}
int jianpan()
{
unsigned int i,j=0,k=0,n,p=1;
do
{
for(i=1;i<=4;i++)
{
if(i==1) {P1OUT=0x01;}
if(i==2) {P1OUT=0x02;}
if(i==3) {P1OUT=0x04;}
if(i==4) {P1OUT=0x08;}
if(keyin!=0x00)
{
delay(150);
if(keyin!=0x00)
{
p=0;
temp1=keyin;
temp2=keyout;
switch(temp1)
{
case 0x10:k=1;break;
case 0x20:k=2;break;
case 0x40:k=3;break;
case 0x80:k=4;break;
}
switch(temp2)
{
case 0x01:j=1;break;
case 0x02:j=2;break;
case 0x04:j=3;break;
case 0x08:j=4;break;
}
n=4*(j-1)+k-1;
}
}
}
}while(p);
delay(1000);
return n;
}
double set_value(uchar i)
{
uchar w=0;
uint m=1;
uint f=0;
Clear_GDRAM();
if(i==1)
{
LCD_WriteStr(1,1,"P:");
P0=0;
}
if(i==2)
{
LCD_WriteStr(1,1,"H:");
H0=0;
}
if(i==3)
{
LCD_WriteStr(1,1,"T:");
T0=0;
}
int j=0,n=-1;
char p[6]={0+48,0+48,0+48,0+48,0+48,0+48};
while(1)
{
if((n=jianpan())+1)
{
if(n>=0&&n<=10&&j<5)
{
if(n==10)
{
*(p+j)=46;
f=1;
}
else
*(p+j)=n+48;
LCD_WriteDBC(j+1,i+1,p[j]);
j++;
}
else if(n==15||j==5)
{
if(f==0)
p[j]='.';
for(uchar k=0;k<5;k++)
{
if(i==1)
{
p1[k]=p[k];
if(p[k]=='.')
{
w=1;
continue;
}
if(w==1)
m=m*10;
P0=P0*10+p[k]-48;
}
if(i==2)
{
p2[k]=p[k];
if(p[k]=='.')
{
w=1;
continue;
}
if(w==1)
m=m*10;
H0=H0*10+p[k]-48;
}
if(i==3)
{
p3[k]=p[k];
if(p[k]=='.')
{
w=1;
continue;
}
if(w==1)
m=m*10;
T0=T0*10+p[k]-48;
}
}
if(i==1)
P0=P0/m;
if(i==2)
H0=H0/m;
if(i==3)
T0=T0/m;
xianshiInit();
return 0;
}
}
}
}
void T_control()
{
//---------------加热管控制
if( ((T0*100)-TEMP)>=1000)
{
P2OUT=~0x1f;
}
else if( ((T0*100)-TEMP)<1000&&((T0*100)-TEMP)>=500)
{
P2OUT=~0x0f;
}
else if( ((T0*100)-TEMP)<500&&((T0*100)-TEMP)>=300)
{
P2OUT=~0x07;
}
else if( ((T0*100)-TEMP)<300&&((T0*100)-TEMP)>=100)
{
P2OUT=~0x03;
}
else if( ((T0*100)-TEMP)<100&&((T0*100)-TEMP)>=0)
{
P2OUT=~0x01;
}
else if(((T0*100)-TEMP)<0)
{
P2OUT=~0x00;
}
//----------------加湿器控制
if( ((T0*100)-TEMP)>=300)
{
P2OUT|=BIT5+BIT6+BIT7;
}
else if(T0!=0)
{
if( humi_val.f<70 )
{
P2OUT&=~(BIT6+BIT5+BIT7);
}
else if( (H0-humi_val.f)>5&& humi_val.f>=70 )
{
P2OUT&=~(BIT6+BIT7);
P2OUT|=BIT5;
}
else if( (H0-humi_val.f)<5&& humi_val.f>=70 )
{
P2OUT&=~(BIT7);
P2OUT|=BIT5+BIT6;
}
else if( (H0-humi_val.f)<1&& humi_val.f>=70 )
{
P2OUT|=BIT5+BIT6+BIT7;
}
}
//-----------延时
for(int i = 0; i < 100; ++i)//延时
{
for(int i = 0; i < 200; ++i)
;
}
}