单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #include<absacc.h> //头文件
- #define uc unsigned char
- #define ui unsigned int
- sbit in_go = P1^3;
- sbit in_back = P1^2;//手动前进后退按键
- sbit out_go = P3^0;
- sbit out_back = P3^5;//电机控制输出
- sbit limit_go = P3^4;
- sbit limit_back = P3^6;//前进后退限位开关
- sbit led_go = P1^4;
- sbit led_back = P1^5;//前进后退限位指示灯
- sbit light = P1^0;//光线传感器
- sbit change = P1^1;//手动自动切换按键(默认自动)
- sbit led_zhishi = P1^7;
- sbit buzz = P1^6;
- sbit W1=P2^2;
- sbit W2=P2^1;
- //定义DHT11数据口
- sbit DATA = P2^0;
- uc U8FLAG,U8temp,U8comdata,U8RH_data_H_temp,U8RH_data_L_temp,U8T_data_H_temp,U8T_data_L_temp,U8checkdata_temp;
- uc U8RH_data_H,U8RH_data_L,U8T_data_H,U8T_data_L,U8checkdata;
- uc Mode,humidity;
- bit bdata FlagStartRH,hot,cold,humid,dry;
- bit bdata flag1,flag2,change_flag,flag_shan,flag_shan1,flag_shan2,flag_HH;
- uc m,n,shan;
- uc code table[11]={ //共阴极字型码
- 0x3f, //--0
- 0x06, //--1
- 0x5b, //--2
- 0x4f, //--3
- 0x66, //--4
- 0x6d, //--5
- 0x7d, //--6
- 0x07, //--7
- 0x7f, //--8
- 0x6f, //--9
- 0x00 //--NULL
- };
- //定时器0初始化
- void Timer0_Init()
- {
- ET0 = 1; //允许定时器0中断
- TMOD = 1; //定时器工作方式选择
- TL0 = 0xb0;
- TH0 = 0x3c; //定时器赋予初值
- TR0 = 1; //启动定时器
- EA=1;
- }
- void Delay1(ui j)
- {
- uc i;
- for(;j>0;j--)
- {
- for(i=0;i<27;i++);
- }
- }
- void Delay_10us(void)
- {
- uc i;
- i--;
- i--;
- i--;
- i--;
- i--;
- i--;
- }
- void COM(void)
- {
- uc i;
- for(i=0;i<8;i++)
- {
- U8FLAG=2;
- while((!DATA)&&U8FLAG++);
- Delay_10us();
- Delay_10us();
- Delay_10us();
- U8temp=0;
- if(DATA)U8temp=1;
- U8FLAG=2;
- while((DATA)&&U8FLAG++);
- //超时则跳出for循环
- if(U8FLAG==1)break;
- //判断数据位是0还是1
-
- // 如果高电平高过预定0高电平值则数据位为 1
-
- U8comdata<<=1;
- U8comdata|=U8temp; //0
- }//rof
- }
- //--------------------------------
- //-----温湿度读取子程序 ------------
- //--------------------------------
- //----以下变量均为全局变量--------
- //----温度高8位== U8T_data_H------
- //----温度低8位== U8T_data_L------
- //----湿度高8位== U8RH_data_H-----
- //----湿度低8位== U8RH_data_L-----
- //----校验 8位 == U8checkdata-----
- //----调用相关子程序如下----------
- //---- Delay();, Delay_10us();,COM();
- //--------------------------------
- uc RH(void)
- {
- //主机拉低18ms
- DATA=0;
- Delay1(180); //原来为5
- DATA=1;
- //总线由上拉电阻拉高 主机延时20us
- Delay_10us();
- Delay_10us();
- Delay_10us();
- Delay_10us();
- //主机设为输入 判断从机响应信号
- DATA=1;
- //判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行
- if(!DATA) //T !
- {
- U8FLAG=2;
- //判断从机是否发出 80us 的低电平响应信号是否结束
- while((!DATA)&&U8FLAG++);
- U8FLAG=2;
- //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
- while((DATA)&&U8FLAG++);
- //数据接收状态
- COM();
- U8RH_data_H_temp=U8comdata;
- COM();
- U8RH_data_L_temp=U8comdata;
- COM();
- U8T_data_H_temp=U8comdata;
- COM();
- U8T_data_L_temp=U8comdata;
- COM();
- U8checkdata_temp=U8comdata;
- DATA=1;
- //数据校验
- U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
- if(U8temp==U8checkdata_temp)
- {
- U8RH_data_H=U8RH_data_H_temp;
- U8RH_data_L=U8RH_data_L_temp;
- U8T_data_H=U8T_data_H_temp;
- U8T_data_L=U8T_data_L_temp;
- U8checkdata=U8checkdata_temp;
- }
- return 1;
- }
- else //传感器不响应
- {
- return 0;
- }
- }
- void delay(ui x)//延时函数
- {
- ui i,j;
- for(i=0;i<x;i++)
- for(j=0;j<110;j++);
- }
- void display()
- {
- P0=table[humidity/10];
- W1=0;
- delay(1); //延时
- W1=1; //关显示
-
- P0=table[humidity%10];
- W2=0;
- delay(1); //延时
- W2=1; //关显示
- }
- void work()//工作函数
- {
- if(change==0)//判断切换是否按下
- {
- buzz=0;
- delay(50);
- if(change==0)
- change_flag=~change_flag;//手动,自动切换
- led_zhishi=change_flag;
- if(change_flag==1)
- {
- out_go=1;
- out_back=1;
- led_go=1;
- led_back=1;
- }
- buzz=1;
- while(!change);//释放
- buzz=1;
- }
-
- if(change_flag==0)//自动模式
- {
- if(light==0||flag_HH==1)//夜间(无光)时拉回衣架 flag_HH==1湿度高
- {
- delay(20);
- if(light==0||flag_HH==1)
- {
- if((limit_go==0)&&(flag_shan==0))
- {
- out_go=1;
- out_back=1;
- led_go=1;
-
- for(shan=0;shan<6;shan++)
- {
- led_go=~led_go;
- buzz=led_go;
- delay(500);
- }
- flag_shan=1;
- }
- else if(limit_go==1)
- {
- out_go=1;
- out_back=0;
-
- led_back=1;
- led_go=0;
- flag_shan=0;
- }
- }
- }
- else if(light==1&&flag_HH==0)//白天(有光)时伸出衣架
- {
- delay(20);
- if(light==1&&flag_HH==0)
- {
- if((limit_back==0)&&(flag_shan==0))
- {
- out_go=1;
- out_back=1;
-
- led_back=1;
- for(shan=0;shan<6;shan++)
- {
- led_back=~led_back;
- buzz=led_back;
- delay(500);
- }
- flag_shan=1;
- }
- else if(limit_back==1)
- {
- out_go=0;
- out_back=1;
-
- led_back=0;
- led_go=1;
- flag_shan=0;
- }
- }
- }
- }
- else if(change_flag==1)//手动模式
- {
- if((in_go==0)&&(limit_go==1))//关闭是否按下
- {
- buzz=0;
- delay(5);
- if((in_go==0)&&(limit_go==1))
- {
- buzz=1;
- out_go=1;
- out_back=~out_back;//按一下伸出,再按一下,停止
-
- buzz=1;
- led_back=1;
- led_go=out_back;
- flag_shan1=0;
-
- }
- while(!in_go);//释放
- buzz=1;
- }
- else if((in_back==0)&&(limit_back==1))//打开是否按下
- {
- buzz=0;
- delay(5);
- if((in_back==0)&&(limit_back==1))
- {
- buzz=1;
- out_back=1;
- out_go=~out_go;//按一下收回,再按一下,停止
-
- buzz=1;
- led_go=1;
- led_back=out_go;
- flag_shan2=0;
-
- }
- while(!in_back);//按键释放
- buzz=1;
- }
- if((limit_go==0)&&(flag_shan1==0))
- {
- delay(5);
- if((limit_go==0)&&(flag_shan1==0))
- {
-
- out_back=1;
- led_go=1;
- for(shan=0;shan<6;shan++)
- {
- led_go=~led_go;
- buzz=led_go;
- delay(500);
- }
- flag_shan1=1;
- }
- }
- if((limit_back==0)&&(flag_shan2==0))
- {
- delay(5);
- if((limit_back==0)&&(flag_shan2==0))
- {
-
- out_go=1;
- led_back=1;
- for(shan=0;shan<6;shan++)
- {
- led_back=~led_back;
- buzz=led_back;
- delay(500);
- }
- flag_shan2=1;
- }
- }
- }
- }
- void main()//主函数
- {
- uc testnum;
- light=0;
- buzz=1;
- led_zhishi=0;
- Timer0_Init();
- while(1)//进入死循环
- {
- work();//调用工作函数
- display();
- if (FlagStartRH == 1) //温湿度转换标志检查
- {
- testnum = RH();
- FlagStartRH = 0;
- humidity = U8RH_data_H; //读出湿度,只取整数部分
- if(humidity>=80)
- flag_HH=1;
- else
- flag_HH=0;
- TR0 = 1;
- }
- display();
- }
- }
- //定时器0中断
- void Timer0_ISR (void) interrupt 1 using 0
- {
- ui RHCounter;
- TL0 = 0xb0;
- TH0 = 0x3c; //定时器赋予初值
- //每5秒钟启动一次温湿度转换
- RHCounter ++;
- if (RHCounter >= 20)
- {
- FlagStartRH = 1;
- RHCounter = 0;
- TR0=0;
- }
- }
复制代码
所有资料51hei提供下载:
晾衣架.rar
(235.43 KB, 下载次数: 41)
|