⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 theprogramofsupervisesandcontrol.c

📁 本源码实现了在下位机上对室内温湿度的监控
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <reg51.h> //Microcontroller specific library, e.g. port definitions
#include <intrins.h> //Keil library (is used for _nop()_ operation)
#include <math.h>    //Keil library
#include <stdio.h>   //Keil library
#include<ABSACC.H>

#define  uchar unsigned char
#define  uint  unsigned int

typedef union
{ unsigned int i;
  float f;
} value;

uint idata da[22];
uint idata r_in,r_out,t_in,t_out,state_RT,R_LRC,state,humd_high,vol_RT,vol_low;
 
bit buf_empty,buf_full,done,w_r;
int temp_high,temp_low;



#define set8155 XBYTE[0xfef8]           //8155的控制命令字地址
#define pb8155  XBYTE[0xfefa]           //PB口地址
#define pa8155  XBYTE[0xfef9]	        //PA口地址
#define adcin0  XBYTE[0xfbff]	        //ADC0809的通道IN0的启动端口地址


#define TMPon() 	  pa8155&=0xfb		//电炉开
#define TMPoff()      pa8155|=0x04		//电炉关
#define RHAon()	      pa8155&=0xfe		//湿机开
#define RHAoff()      pa8155|=0x01		//湿机关
#define led2light()	  pb8155&=0xfb	    //火警指示灯亮
#define led2dark()	  pb8155|=0x04	    //火警指示灯灭


#define led0light()   pb8155&=0xfe     //电炉指示灯亮
#define led0dark()    pb8155|=0x01     //电炉指示灯灭
#define led1light()	  pb8155&=0xfd	    //湿机指示灯亮
#define led1dark()	  pb8155|=0x02	    //湿机指示灯灭

uchar real[4]={0,0,0,0};


uint value_humi;
uint value_temp;


enum {TEMP,HUMI};

sbit DAT=P2^6;
sbit SCK=P2^7;

sbit din=P1^0;
sbit load=P1^2;
sbit clk=P1^1;
sbit fire=P2^3;
sbit P1_4=P1^4;




#define noACK 0
#define ACK   1

#define MEASURE_TEMP 0x03
#define MEASURE_HUMI 0x05

void Delay(unsigned int count)		   //count*1ms延时程序
{
  unsigned int i,j;

  for (i=0;i<count;i++)
     for (j=0;j<120;j++);
}

void TmpCompare(void)
{
  if(value_temp<temp_low)    //实际温度是否小于设定值减1
     {
       state_RT|=0x28;
     }
  if (value_temp>temp_high)//实际温度大于设定值加1
    {
       state_RT|=0x10;
       state_RT&=0xD7;
    }
  if(w_r==0||state&0x80)
    {
       if(state&0x20)
         {
            led0light();     //电炉指示灯亮
              TMPon() ;      //开电炉
          }
       else
         {
             led0dark();   //电炉指示灯灭
              TMPoff() ;   //关闭电炉
         }
    }
  else
    {
       if(state_RT&0x20)
         {
            led0light();     //电炉指示灯亮
              TMPon() ;      //开电炉
          }
       else
         {
             led0dark();   //电炉指示灯灭
              TMPoff() ;   //关闭电炉
         }
    }
}

void HumCompare(void)
{
  if (value_humi>humd_high)	  //是否需要开加湿机
    {
      state_RT|=0x44;
    }
 else
    {
      state_RT&=0xBB;
    }
  if(w_r==1&&(!(state&0x80)))
   {
     if(state_RT&0x40)
       {
          led1light();   //湿机指示灯亮
          RHAon();       //湿机开启
       }
     else
       {
          led1dark();   //湿机指示灯灭
          RHAoff();      //湿机关闭
       }
   }
  else
   {
     if(state&0x40)
       {
          led1light();   //湿机指示灯亮
          RHAon();       //湿机开启
       }
     else
       {
          led1dark();   //湿机指示灯灭
          RHAoff();      //湿机关闭
       }
   }
}

void  AlarmDetect(void)
{
 if(fire==1)              //若有火警,即p2.3为高电平
    {                     //火警标志, 0--无火警,1--有火警
     state_RT|=0x2;          //置位火警标位为1
     led2light();
    }
 else
    {
      state_RT&=0xFD;                         //若无火警,置位火警标志为1
      led2dark();
    }
}

void VoltCompare(void)
{
   uint volt;
   adcin0=0;
   adcin0=1;
   adcin0=0;
   Delay(5);
   volt=adcin0;
   vol_RT=250*volt/255;
   if(vol_RT<vol_low)
     state_RT|=0x1;
   else
     state_RT&=0xFE;
}

char s_write_byte(unsigned char value)
{
  unsigned char i,error=0;
  for (i=0x80;i>0;i/=2)             //shift bit for masking
  { if (i & value) DAT=1;          //masking value with i , write to SENSI-BUS
    else DAT=0;
    SCK=1;                          //clk for SENSI-BUS
    _nop_();_nop_();_nop_();        //pulswith approx. 5 us
    SCK=0;
  }
  DAT=1;                           //release DATA-line
  SCK=1;                            //clk #9 for ack
  error=DAT;                       //check ack (DATA will be pulled down by SHT11)
  SCK=0;
  return error;                     //error=1 in case of no acknowledge
}

char s_read_byte(unsigned char ack)
{
  unsigned char i,val=0;
  DAT=1;                           //release DATA-line
  for (i=0x80;i>0;i/=2)             //shift bit for masking
  { SCK=1;                          //clk for SENSI-BUS
    if (DAT) val=(val | i);        //read bit
    SCK=0;
  }
  DAT=!ack;                        //in case of "ack==1" pull down DATA-Line
  SCK=1;                            //clk #9 for ack
  _nop_();_nop_();_nop_();          //pulswith approx. 5 us
  SCK=0;
  DAT=1;                           //release DATA-line
  return val;
}

void s_transstart(void)
{
   DAT=1; SCK=0;                   //Initial state
   _nop_();
   SCK=1;
   _nop_();
   DAT=0;
   _nop_();
   SCK=0;
   _nop_();_nop_();_nop_();
   SCK=1;
   _nop_();
   DAT=1;
   _nop_();
   SCK=0;
}

void s_connectionreset(void)
{
  unsigned char i;
  DAT=1; SCK=0;                    //Initial state
  for(i=0;i<9;i++)                  //9 SCK cycles
  { SCK=1;
    SCK=0;
  }
  s_transstart();                   //transmission start
}

char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
  unsigned int error=0;
  unsigned int i;

  s_transstart();                   //transmission start
  switch(mode){                     //send command to sensor
    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(DAT==0) break; //wait until sensor has finished the measurement
  if(DAT) error+=1;                // or timeout (~2 sec.) is reached
  *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB)
  *(p_value+1)=s_read_byte(ACK);    //read the second byte (LSB)
  *p_checksum =s_read_byte(noACK);  //read checksum
  return error;
}

void calc_sth11(float *p_humidity ,float *p_temperature)
{ 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 DataInput(uchar add,uchar dat)
{uchar ADS,i;
 load=0;
 ADS=add;

 for (i=1;i<=8;i++)			   //送7219地址
  {
   din=ADS&0x80;
   ADS=ADS<<1;
   clk=1;
   clk=0;
  }

 ADS=dat;

 for (i=1;i<=8;i++)			   //送7219数据
  {
   din=ADS&0x80;
   ADS=ADS<<1;
   clk=1;
   clk=0;
  }
 load=1;
}
 int error(int error_co,int error_num)
  {
    int m;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -