282 基于单片机的智能窗帘控制系统设计(自动 手动 光控 远程 温湿度)

(1)仿真原理图和硬件原理图会有着些许区别,做硬件请参照硬件原理图;
(2)仿真时一定要记得烧录程序(.hex)(具体请看仿真视频);
(3)仿真请打开“.pdsprj”工程文件或“.DSN”文件;
1可通过按键切换:手动模式、定时模式、光控模式;
2手动模式:通过开窗帘和关窗帘键对窗帘进行控制;
3定时模式:通过按键设置开窗帘和关窗帘的时间;
4光控模式:光照强度大于设置值时开启窗帘,否则关闭;
5步进电机正转半圈,模拟开窗,红色LED灯点亮;电机反转半圈,模拟关窗,红色LED灯熄灭。
6实时显示温湿度
按键说明:  
按键1:切换模式(在手动模式、定时模式、光控模式循环切换)
按键2:进入当前时间的设置(年、月、日、时、分的设置)
按键3:进入定时时间和光控阈值大小的设置
按键4:减(手动关闭窗帘)
按键5:加(手动开启窗帘)
按键6:远程关闭窗帘
按键7:远程开启窗帘
 

完整版 电路图和程序代码 下载地址

https://pan.baidu.com/s/1C_QrDaGo0Z9UB9czspvZ4Q?pwd=8888

部分代码展示
#include <reg52.h>
#include <intrins.h>

#define uchar unsigned char		// 以后unsigned char就可以用uchar代替
#define uint  unsigned int		// 以后unsigned int 就可以用uint 代替


sbit ADC_CS     = P1^6; 			// ADC0832的CS引脚
sbit ADC_CLK    = P1^7; 			// ADC0832的CLK引脚
sbit ADC_DAT    = P3^2; 			// ADC0832的DI/DO引脚
sbit SCK_P      = P1^0;				// 时钟芯片DS1302的SCK管脚
sbit SDA_P      = P1^1;				// 时钟芯片DS1302的SDA管脚
sbit RST_P      = P1^2;				// 时钟芯片DS1302的RST管脚
sbit LcdRs_P    = P1^3;       // 1602液晶的RS管脚       
sbit LcdRw_P    = P1^4;       // 1602液晶的RW管脚 
sbit LcdEn_P    = P1^5;       // 1602液晶的EN管脚
sbit KeyMode_P  = P3^3;				// 模式切换
sbit KeySet_P   = P3^4;				// 设置时间按键
sbit KeySet2_P  = P3^5;				// 设置时间模式的开关时间和光照控制强度
sbit KeyDown_P  = P3^6;				// 减按键
sbit KeyUp_P    = P3^7;				// 加按键
sbit Led_P      = P2^0;				// 指示灯

sbit KeyDown_P1  = P3^0;				// 远程
sbit KeyUp_P1    = P3^1;				// 远程

sbit Data_P    = P2^2;			// SHT11传感器的数据管脚
sbit Sck_P     = P2^1;			// SHT11传感器的时钟管脚
uchar tmpe,h;
unsigned char temp;							// 保存温度
unsigned char humi;				  		// 保存湿度
enum { TEMP,HUMI };
typedef union              		//定义共用同类型
{
	unsigned int i;
	float f;
}value;

uchar gMode=1;								// 1是手动模式,2是时间自动模式,3是亮度自动模式
uchar OpenHour    = 18;				// 开启窗帘的小时
uchar OpenMinute  = 20;				// 开启窗帘的分钟
uchar CloseHour   = 10;				// 关闭窗帘的小时
uchar CloseMinute = 30;				// 关闭窗帘的分钟
uchar gLight      = 40;				// 窗帘开关的阈值

uchar code Clock[]={0x10,0x20,0x40,0x80}; 			// 步进电机顺时针旋转数组
uchar code AntiClock[]={0x80,0x40,0x20,0x10};		// 步进电机逆时针旋转数组

uchar TimeBuff[7]={17,9,1,6,18,30,40};					// 时间数组,默认2017年9月1日,星期五,18:30:40
// TimeBuff[0] 代表年份,范围00-99
// TimeBuff[1] 代表月份,范围1-12
// TimeBuff[2] 代表日期,范围1-31
// TimeBuff[3] 代表星期,范围1-7
// TimeBuff[4] 代表小时,范围00-23
// TimeBuff[5] 代表分钟,范围00-59
// TimeBuff[6] 代表秒钟,范围00-59


 // 温湿度采集
 char ShtWriteByte(unsigned char value)
{
	unsigned char i,error=0;
	for(i=128;i>0;i>>=1)  // 高位为1,循环右移
	{
		if (i&value)
			Data_P=1;       	// 和要发送的数相与,结果为发送的位
		else
			Data_P=0;
		Sck_P=1;
		_nop_();						// 延时3us
		_nop_();
		_nop_();
		Sck_P=0;
	}
	Data_P=1;    					// 释放数据线
	Sck_P=1;
	error=Data_P;  				// 检查应答信号,确认通讯正常
	_nop_();
	_nop_();
	_nop_();
	Sck_P=0;
	Data_P=1;
	return error; 				// error=1 通讯错误
}

char ShtReadByte(unsigned char ack)
{
	unsigned char i,val=0;
	Data_P=1; 						// 释放数据线
	for(i=0x80;i>0;i>>=1)	// 高位为1,循环右移
	{
		Sck_P=1;
		if(Data_P)
			val=(val|i);    	// 读一位数据线的值
		Sck_P=0;
	}
	Data_P=!ack;    			// 如果是校验,读取完后结束通讯
	Sck_P=1;
	_nop_();							// 延时3us
	_nop_();
	_nop_();
	Sck_P=0;
	_nop_();
	_nop_();
	_nop_();
	Data_P=1; 						// 释放数据线
	return val;
}


void ShtTransStart(void)
{
	Data_P=1;
	Sck_P=0;
	_nop_();
	Sck_P=1;
	_nop_();
	Data_P=0;
	_nop_();
	Sck_P=0;
	_nop_();
	_nop_();
	_nop_();
	Sck_P=1;
	_nop_();
	Data_P=1;
	_nop_();
	Sck_P=0;
}

void ShtConnectReset(void)
{
	unsigned char i;
	Data_P=1; 		   		//准备
	Sck_P=0;
	for(i=0;i<9;i++)  	//DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位
	{
		Sck_P=1;
		Sck_P=0;
	}
	ShtTransStart();   	//启动传输
}

char ShtMeasure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
	unsigned error=0;
	unsigned int i;
	ShtTransStart();  		// 启动传输
	switch(mode)       		// 选择发送命令
	{
		case 1 :   					// 测量温度
			error+=ShtWriteByte(0x03);
			break;
		case 2 :   					// 测量湿度
			error+=ShtWriteByte(0x05);
			break;
		default:
			break;
	}
	for(i=0;i<65535;i++)
		if(Data_P==0)
			break;  					// 等待测量结束
		if(Data_P)
			error+=1;   			// 如果长时间数据线没有拉低,说明测量错误
	*(p_value) =ShtReadByte(1);  		// 读第一个字节,高字节 (MSB)
	*(p_value+1)=ShtReadByte(1); 		// 读第二个字节,低字节 (LSB)
	*p_checksum =ShtReadByte(0);  	// read CRC校验码
	return error;  									// error=1 通讯错误
}

void CalcSHT11(float *p_humidity ,float *p_temperature)
{
	const float C1=-4.0;	 			// 12位湿度精度 修正公式
	const float C2=+0.0405;			// 12位湿度精度 修正公式
	const float C3=-0.0000028;	// 12位湿度精度 修正公式
	const float T1=+0.01;	 			// 14位温度精度 5V条件 修正公式
	const float T2=+0.00008;	 	// 14位温度精度 5V条件 修正公式
	float rh=*p_humidity;	 			// rh: 12位 湿度
	float t=*p_temperature;			// t:  14位 温度
	float rh_lin;								// rh_lin: 湿度 linear值
	float rh_true;							// rh_true: 湿度 ture值
	float t_C;	 								// t_C : 温度 ℃
	t_C=t*0.01 - 40;	 					//补偿温度
	rh_lin=C3*rh*rh + C2*rh + C1;					//相对湿度非线性补偿
	rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;		//相对湿度对于温度依赖性补偿
	*p_temperature=t_C;	 				//返回温度结果
	*p_humidity=rh_true;	 			//返回湿度结果
}

unsigned char TempCorrect(int temp)
{
	if(temp<0)	temp=0;
	if(temp>970)  temp=970;
	if(temp>235)  temp=temp+10;
	if(temp>555)  temp=temp+10;
	if(temp>875)  temp=temp+10;
	temp=(temp%1000)/10;
	return temp;
}

unsigned char HumiCorrect(unsigned int humi)
{
	if(humi>999)  humi=999;
	if((humi>490)&&(humi<951))  humi=humi-10;
	humi=(humi%1000)/10;
	return humi+4;
}

void ReadShtData()
{
	value humi_val,temp_val;  	// 定义两个共同体,一个用于湿度,一个用于温度
	unsigned char error;  							// 用于检验是否出现错误
	unsigned char checksum;  						// CRC
	unsigned int temp1,humi1;						// 临时读取到的温湿度数据

	error=0; 										//初始化error=0,即没有错误
	error+=ShtMeasure((unsigned char*)&temp_val.i,&checksum,1); 	//温度测量
	error+=ShtMeasure((unsigned char*)&humi_val.i,&checksum,2); 	//湿度测量

	if(error!=0) 		  					//如果发生错误,系统复位
		ShtConnectReset();
	else
	{
		humi_val.f=(float)humi_val.i; 				//转换为浮点数
		temp_val.f=(float)temp_val.i;  				//转换为浮点数
		CalcSHT11(&humi_val.f,&temp_val.f);  	//修正相对湿度及温度
		temp1=temp_val.f*10;
		temp=TempCorrect(temp1);
		humi1=humi_val.f*10-50;
		humi=HumiCorrect(humi1);
		humi1=humi1-1;
	}

}



/*********************************************************/
// 毫秒级的延时函数,time是要延时的毫秒数
/*********************************************************/
void DelayMs(uint time)
{
	uint i,j;
	for(i=0;i<time;i++)
		for(j=0;j<112;j++);
}


/*********************************************************/
// 1602液晶写命令函数,cmd就是要写入的命令
/*********************************************************/
void LcdWriteCmd(uchar cmd)
{ 
	LcdRs_P = 0;
	LcdRw_P = 0;
	LcdEn_P = 0;
	P0=cmd;
	DelayMs(2);
	LcdEn_P = 1;    
	DelayMs(2);
	LcdEn_P = 0;	
}


/*********************************************************/
// 1602液晶写数据函数,dat就是要写入的数据
/*********************************************************/
void LcdWriteData(uchar dat)
{
	LcdRs_P = 1; 
	LcdRw_P = 0;
	LcdEn_P = 0;
	P0=dat;
	DelayMs(2);
	LcdEn_P = 1;    
	DelayMs(2);
	LcdEn_P = 0;
}


/*********************************************************/
// 1602液晶初始化函数
/*********************************************************/
void LcdInit()
{
	LcdWriteCmd(0x38);        // 16*2显示,5*7点阵,8位数据口
	LcdWriteCmd(0x0C);        // 开显示,不显示光标
	LcdWriteCmd(0x06);        // 地址加1,当写入数据后光标右移
	LcdWriteCmd(0x01);        // 清屏
}


/*********************************************************/
// 液晶光标定位函数
/*********************************************************/
void LcdGotoXY(uchar line,uchar column)
{
	// 第一行
	if(line==0)        
		LcdWriteCmd(0x80+column); 
	// 第二行
	if(line==1)        
		LcdWriteCmd(0x80+0x40+column); 
}


/*********************************************************/
// 液晶输出字符串函数
/*********************************************************/
void LcdPrintStr(uchar *str)
{
	while(*str!='\0')
			LcdWriteData(*str++);
}


/*********************************************************/
// 液晶输出数字(0-99)
/*********************************************************/
void LcdPrintNum(uchar num)
{
	LcdWriteData(num/10+48);		// 十位
	LcdWriteData(num%10+48); 		// 个位
}


/*********************************************************/
// 显示模式
/*********************************************************/
void LcdPrintMode(uchar num)
{
	switch(num)			
	{
		case 1: LcdPrintStr("M");	break;
		case 2: LcdPrintStr("T");	break;
		case 3: LcdPrintStr("L");	break;
		default:												break;
	}
}


/*********************************************************/
// 液晶显示内容的初始化
/*********************************************************/
void LcdShowInit()
{
	LcdGotoXY(0,0);
	LcdPrintStr("20  -  -     :  ");
	LcdGotoXY(1,0);
	LcdPrintStr("  H:   T:   G:  ");
	LcdGotoXY(1,0);
	LcdPrintMode(gMode);
}

/*********************************************************/
// 刷新时间显示
/*********************************************************/
void FlashTime()
{
	LcdGotoXY(0,2);										// 年份
	LcdPrintNum(TimeBuff[0]);
	LcdGotoXY(0,5);										// 月份
	LcdPrintNum(TimeBuff[1]);
	LcdGotoXY(0,8);										// 日期
	LcdPrintNum(TimeBuff[2]);
	LcdGotoXY(0,11);									// 小时
	LcdPrintNum(TimeBuff[4]);
	LcdGotoXY(0,14);									// 分钟
	LcdPrintNum(TimeBuff[5]);
	LcdGotoXY(0,13);									// 秒钟
	if(TimeBuff[6]%2==0)							// 秒钟是偶数显示冒号
		LcdWriteData(':');
	else															// 秒钟是奇数显示空格
		LcdWriteData(' ');
}

/*********************************************************/
// 初始化DS1302
/*********************************************************/
void DS1302_Init(void)
{
	RST_P=0;			// RST脚置低
	SCK_P=0;			// SCK脚置低
	SDA_P=0;			// SDA脚置低				
}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值