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

📄 fre89s51.c

📁 基于AT89S51单片机和HS1101做的相对湿度检测。需要的可以下载参考。
💻 C
字号:
#include<reg51.h>
#define u_int  unsigned int
#define u_char unsigned char
/* 函数声明 */
void time0_over_int(void); /* 定时器0中断服务程序 ,使用第1组寄存器  */
void ext1_int_proc(void);  /* 外部中断1中断服务程序,使用第2组寄存器 */
void system_init(void);    /* 系统上电初始化 */
/* 声明结束 */
#define Disdata P1
#define discan	P0
sbit DIN=P1^5;
u_char code dis_8[11]={
    0xa0,		// 0
	0xbb,		// 1
	0x62,		// 2
	0x2a,		// 3
	0x39,		// 4
	0x2c,		// 5
	0x24,		// 6
	0xba,		// 7
	0x20,		// 8
	0x28,		// 9
	0xFF,	//"灭"
				   };/*0~9的数码管段码*/
/***********us延时函数*******************/
//u_char code ditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x80,0x08,0x09,0x09};
u_char code scan_con[4]={0xfe,0xfd,0xfb,0xf7};
//u_char data wet_data[1]={0x00};
//u_char data display[4]={0x00,0x00,0x00,0x00};//显示单元数据,共4个数据,一个运算暂存用
u_char  display[4]={0x00,0x00,0x00,0x00};
u_int fhz_tlc555;    /* tlc555输出频率 */
u_char timer_count;  /* 定时计数       */
bit timer_int;       /* 定时标志       */
float hum_const;     /* 计算湿度的常数 */
float real_hum=0.0;
/* 定时器0中断服务程序 ,使用第1组寄存器 */
void time0_over_int(void) interrupt 1 using 1
{
     TF0=0;
     timer_count++;
     TH0=0x3c;
	 TL0=0xb0;
	 
}
/* 外部中断1中断服务程序,使用第2组寄存器 */
void ext1_int_proc(void) interrupt 2 using 2
{
    fhz_tlc555++;
}
/* 系统上电初始化 */
void system_init(void)
{
    EA = 0;
    timer_int = 0;
    timer_count = 0;
    TMOD = 0x01;           /*定时器0工作于方式1*/
    TCON = 0x04;           /*外部中断下降沿触发*/
	TH0=0x3c;              /*定时50毫秒*/
	TL0=0xb0;
    PT0 = 1;               /*定时器0高优先级*/
    TR0 = 1;               /*定时器0启动计数*/
    IE = 0x86;             /*定时器0,外部中断1,CPU开中断*/
}
/* 计算当前湿度 */
float get_humidity(u_int fhz) /* 输入fhz为tlc555的频率计数 */
{
    float humidity = 0;
    
    if ( (fhz > 7400) || (fhz < 6000) )
    {
        return 0;
    }
    else if (fhz >= 7000)
    {
        hum_const = 5123.7;
    }
    else if ( (fhz >= 6700) && (fhz < 7000) )
    {
        hum_const = 5083.7;
    }
    else if(fhz < 6700)
    {
        hum_const = 5103.7;
    }
    //humidity = (u_int)( (hum_const - 0.702*(fhz - 40)) / 10);
	humidity = (hum_const - 0.702*(fhz - 36)) / 10.0;
	humidity =humidity+0.9;
	if(humidity>100.0||humidity<0.0)
	 {
	 return humidity=0;
	 }
	  else
    return (humidity);//
}
void delay(u_int t)
{
	for(;t>0;t--);
}

/*显示扫描***********************/
void scan()
{
	char k;
	for(k=0;k<4;k++)
		{
			Disdata=dis_8[display[k]];
			if(k==1)
				{
					DIN=0;
				}
				discan=scan_con[k];
			   delay(90);
				//delay(40);
				discan=0xff;
		}
}

/***************湿度数据处理函数*************/
wetdata_process()
{	
    display[3]=real_hum/100;
	display[2]=((real_hum-display[3]*100)/10);
	display[1]=(real_hum-display[3]*100-display[2]*10);
    display[0]=(int)((real_hum-(int)real_hum)*10);
//	display[0]=(real_hum-display[3]*100-display[2]*10-display[1])*10;
	if(!display[3])
	{
		display[3]=0x0a;
		if(!display[2])
			{
				display[2]=0x0a;
			}
	}	
	
}
main()
{
   int h;
    system_init();
    while(1)
    {
           if (timer_count ==20)  /* 1秒定时到 100*/
           {  			   
               timer_count = 0;			   
               fhz_tlc555 -=2;			   
			   real_hum= get_humidity(fhz_tlc555);
			   wetdata_process();
			  for(h=0;h<200;h++)
 			  scan();

               fhz_tlc555 = 0;
			   system_init();
            }

    }
}

⌨️ 快捷键说明

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