Demo entry 6638249

c

   

Submitted by anonymous on Sep 07, 2017 at 05:14
Language: C. Code size: 9.8 kB.

#include<reg51.h>
#include"lcd.h"
#include"ds1302.h"
#include"28BYJ48.h"
#include"ds18b20.h"

sbit K1=P3^4;		//设置
sbit K2=P3^5;		//加
sbit K3=P3^6;		//减
sbit K4=P3^7;	 	//确认

#define DAY 	1
#define NIGHT 	0
#define BRIGHT 	0
#define DARK	1
#define CLOSE	0
#define OPEN	1
#define ENABLE	1
#define DISABLE 0

sbit sensor=P1^6;      //光传感器接口	   高电平:光线暗     低电平:光线强

unsigned char key1_values=0;
unsigned char i;
unsigned int display_status=0;
char TIME_H=0;
char TIME_L=0;
int temp=0;

bit curtains_status=0;	//默认开机状态下窗帘为关闭下拉状态
bit timer_status=0;	   	
bit sensor_status=0;
bit motor_status=0;
bit setting_status=0;

void SYS_Init(void);  
void KEY_Scan(void);  //按键扫描
void SET_Time(void);  //时间设置
void Waitting_Display();//电机等待时间LCD显示
void LcdDisplay();	  //LCD显示
void Sensor_Scan(void);//传感器扫描
void Motor_drive(void);//电机控制
void delay_ms(unsigned char time);
/*******************************************************************************
* 函数名         : main
* 函数功能		 : 主函数
* 输入           : 无
* 输出         	 : 无
*******************************************************************************/

void main()
{
	LcdInit();
	SYS_Init();
	temp=Ds18b20ReadTemp();
	Ds1302Init();
	delay_ms(100);
	while(1)
	{	
		Sensor_Scan();
		if(motor_status==DISABLE)
		{
			KEY_Scan();
			SET_Time();		
		}
	   	else
		{	
			if(setting_status==DISABLE)
			  Motor_drive();
			setting_status=ENABLE;
			LcdDisplay();			  
			Ds1302ReadTime();
			temp=Ds18b20ReadTemp();
		}
	}
}

/*******************************************************************************
* 函数名         : SYS_Init()
* 函数功能		 : 系统初始化
* 输入           : 无
* 输出         	 : 无
*******************************************************************************/

void SYS_Init(void)
{
	Ds1302Write(0x8E,0X00);		 //禁止写保护,就是关闭写保护功能
	for (i=0; i<7; i++)
	{
		Ds1302Write(WRITE_RTC_ADDR[i], TIME[i]);	
	}
	Ds1302Write(0x8E,0x80);		 //打开写保护功能
}


/*******************************************************************************
* 函数名         : KEY_Scan()
* 函数功能		 : 按键扫描函数
* 输入           : 无
* 输出         	 : 无
*******************************************************************************/

void KEY_Scan(void)
{
	if(K1==0)	   	//设置按键
	{
		delay_ms(3);
		if(K1==0)
			key1_values++;//0、退出状态;1、年;2、月;3、日;4、时;5、分;6、秒
		if(key1_values>6) //由K1控制确认退出
			key1_values=0;
		while(K1==0);
	}
	if(key1_values==0)
	setting_status=DISABLE;
	else
		setting_status=ENABLE;
}

/*******************************************************************************
* 函数名         : SET_Time()
* 函数功能		 : 时间设置
* 输入           : 无
* 输出         	 : 无
*******************************************************************************/
void SET_Time(void)
{
		switch(key1_values){
		case 0:Ds1302ReadTime();temp = Ds18b20ReadTemp();LcdDisplay();break;
		case 1:	//年份设置
				{	
					TIME_H=TIME[6]/16;
					TIME_L=TIME[6]&0x0f;
					if(K2==0)
					{
						delay_ms(3);
						if(K2==0)
						TIME_L+=1;
						if(TIME_L>9)
						{
						 	TIME_H++;
							TIME_L=0;
							if(TIME_H>9)
							TIME_H=0;
						}
						while(K2==0);	
					}
					if(K3==0)
					{
						delay_ms(3);
						if(K3==0)
						TIME_L--;
						if(TIME_L<0)
						{
						 	TIME_H--;
							TIME_L=9;
							if(TIME_H<0)
							TIME_H=9;
						}
						while(K3==0);
					}						
					TIME[6]=TIME_H*16+TIME_L;
					SYS_Init();	
				}break;
		case 2:		//月
				{
					TIME_H=TIME[4]/16;
					TIME_L=TIME[4]&0x0f;
					if(K2==0)
					{
						delay_ms(3);
						if(K2==0)
						TIME_L+=1;
						if(TIME_L>9)
						{
						 	TIME_H++;
							TIME_L=0;
						}
						while(K2==0);	
					}
					if(K3==0)
					{
						delay_ms(3);
						if(K3==0)
						TIME_L-=1;
						if(TIME_L<0)
						{
							TIME_H--;
							TIME_L=9;
							if(TIME_H<0)
							{
							  TIME_H=1;
							  TIME_L=2;
							  }
						}
						while(K3==0);
					}
					TIME[4]=TIME_H*16+TIME_L;
					if(TIME[4]>0x12)
					TIME[4]=0x01;
					SYS_Init();			
				}break;
		case 3:											   //日
				{
					TIME_H=TIME[3]/16;
					TIME_L=TIME[3]&0x0f;
					if(K2==0)
					{
						delay_ms(3);
						if(K2==0)
						TIME_L++;
						if(TIME_L>9)
						{
						 	TIME_H++;
							TIME_L=0;
						}
						while(K2==0);	
					}
					if(K3==0)
					{
						delay_ms(3);
						if(K3==0)
						TIME_L-=1;
						if(TIME_L<0)
						{
							TIME_H--;
							TIME_L=9;
							if(TIME_H<0)
							{
								TIME_H=3;
								TIME_L=1;	
							}	
						}
						while(K3==0);
					}
					TIME[3]=TIME_H*16+TIME_L;
					if(TIME[3]>0x31)
					TIME[3]=0x01;
					SYS_Init();			
				}break;
		case 4:		//h
				{
					TIME_H=TIME[2]/16;
					TIME_L=TIME[2]&0x0f;
					if(K2==0)
					{
						delay_ms(3);
						if(K2==0)
						TIME_L++;
						if(TIME_L>9)
						{
						 	TIME_L=0;
							TIME_H++;
						}
						while(K2==0);
					}
					if(K3==0)
					{
						delay_ms(3);
						if(K3==0)
						TIME_L-=1;
						if(TIME_L<0)
						{
							TIME_L=9;
							TIME_H--;
							if(TIME_H<0)
							{
							TIME_H=2;
							TIME_L=3;	
							}
						}
						while(K3==0);
					}
					TIME[2]=TIME_H*16+TIME_L;
					if(TIME[2]>0x23)
					TIME[2]=0x00;
					SYS_Init();			
				}break;
		case 5:							  	//MIN
				{
					TIME_H=TIME[1]/16;
					TIME_L=TIME[1]&0x0f;					
					if(K2==0)
					{
						delay_ms(3);
						if(K2==0)
						TIME_L++;
						if(TIME_L>9)
						{
						  TIME_L=0;
						  TIME_H++;
						}
						while(K2==0);	
					}
					if(K3==0)
					{
						delay_ms(3);
						if(K3==0)
						TIME_L--;
						if(TIME_L<0)
						{
						   TIME_L=9;
						   TIME_H--;
						   if(TIME_H<0)
							 TIME_H=5;
						}
						while(K3==0);
					}
					TIME[1]=TIME_H*16+TIME_L;
					if(TIME[1]>0x59)
					TIME[1]=0x00;
					SYS_Init();			
				}break;
		case 6:
				{
					TIME_H=TIME[0]/16;
					TIME_L=TIME[0]&0x0f;					
					if(K2==0)
					{
						delay_ms(3);
						if(K2==0)
						TIME_L++;
						if(TIME_L>9)
						{
						  TIME_L=0;
						  TIME_H++;
						}
						while(K2==0);	
					}
					if(K3==0)
					{
						delay_ms(3);
						if(K3==0)
						TIME_L--;
						if(TIME_L<0)
						{
						   TIME_L=9;
						   TIME_H--;
						   if(TIME_H<0)
							 TIME_H=5;
						}
						while(K3==0);
					}
					TIME[0]=TIME_H*16+TIME_L;
					if(TIME[0]>0x59)
					TIME[0]=0x00;
					SYS_Init();			
				}break;
		default :break;
		}
}
/*******************************************************************************
* 函数名         : LcdDisplay()
* 函数功能		   : 显示函数
* 输入           : 无
* 输出         	 : 无
*******************************************************************************/

void Waitting_Display()
{
	 //LcdInit();
	 LcdWriteCom(0x01);  //清屏
	 LcdWriteCom(0x80);
	 LcdWriteData('W');
	 LcdWriteData('A');
	 LcdWriteData('I');
	 LcdWriteData('T');
	 LcdWriteData('T');
	 LcdWriteData('I');
	 LcdWriteData('N');
	 LcdWriteData('G');
	 LcdWriteData('.');
	 LcdWriteData('.');
	 LcdWriteData('.');
}

void LcdDisplay()
{
	unsigned char datas[] = {0, 0, 0, 0, 0}; 
	datas[0] = temp / 1000;
	datas[1] = temp / 100;
	datas[2] = temp % 100 / 10;
	datas[3] = temp % 100;
	datas[4] = temp % 10;

	LcdWriteCom(0x80+0X40);
	LcdWriteData('0'+TIME[2]/16);	//时
	LcdWriteData('0'+(TIME[2]&0x0f));				 
	LcdWriteData('-');
	LcdWriteData('0'+TIME[1]/16);	//分
	LcdWriteData('0'+(TIME[1]&0x0f));	
	LcdWriteData('-');
	LcdWriteData('0'+TIME[0]/16);	//秒
	LcdWriteData('0'+(TIME[0]&0x0f));


	LcdWriteCom(0x80);
	LcdWriteData('2');
	LcdWriteData('0');
	LcdWriteData('0'+TIME[6]/16);//年
	LcdWriteData('0'+(TIME[6]&0x0f));
	LcdWriteData('-');
	LcdWriteData('0'+TIME[4]/16);//月
	LcdWriteData('0'+(TIME[4]&0x0f));
	LcdWriteData('-');
	LcdWriteData('0'+TIME[3]/16);//日
	LcdWriteData('0'+(TIME[3]&0x0f));
	
		
	LcdWriteCom(0x80+0X40+0x0b);
	LcdWriteData('T');
	LcdWriteCom(0x80+0X40+0x0c);
	LcdWriteData('0'+datas[1]);
	LcdWriteCom(0x80+0X40+0x0d);
	LcdWriteData('0'+datas[2]); //十位
	LcdWriteCom(0x80+0X40+0x0e);
	LcdWriteData('.'); //个位 
	LcdWriteCom(0x80+0X40+0x0f);
	LcdWriteData('0'+datas[4]); //显示小数点 		 
}

void delay_ms(unsigned char time)
{
	unsigned int i,j;
	for(i=time;i>0;i--)
		for(j=0;j<100;j++) ;
}

void Sensor_Scan(void)
{
	if(K4==0)		//K4按下代表窗帘关上了
		curtains_status=CLOSE;
	else
		curtains_status=OPEN;
	if(TIME[2]>0x07&&TIME[2]<0x20)
	timer_status=DAY;
	else
	timer_status=NIGHT;
	if(sensor==1)					   //高电平
	sensor_status=DARK;				   //无光
	else
	sensor_status=BRIGHT;	//有光
	if(timer_status==DAY)	//白天
	{
		if(sensor_status==DARK)//传感无光
		{
			if(curtains_status==OPEN)		  //key4不按 窗帘开
			motor_status=DISABLE;			  //电机不动
			else if(curtains_status==CLOSE)			  //key4按下 窗帘关
			motor_status=ENABLE;			   // 电机动
		}
		else		//有光
		{
			if(curtains_status==CLOSE)			  // 按下 关
			motor_status=ENABLE;				   // 转
			else								   
			motor_status=DISABLE;				   //不按  开 不转
		}
	}
	else//黑夜
	{
		if(curtains_status==OPEN)	// 不按  开
		motor_status=ENABLE;		//	转动
		else
		motor_status=DISABLE;		//	按下 关 不转
	}	
}

void Motor_drive(void)
{
	if(timer_status==DAY)
	{
		if(sensor_status==DARK)   //白天,黑,关
		while(K4==1)
		{	
			Waitting_Display();
			Motor_Run(CCW, 100, Angle(90));
			LcdWriteCom(0x01);  //清屏
			}
		else
		while(K4==0)	//白天,亮,开
		{
			Waitting_Display();
			Motor_Run(CW, 100, Angle(90));
			LcdWriteCom(0x01);  //清屏	
			}
	}
	else 	//夜里。关
	while(K4==1)
	{
			Waitting_Display();
		 	Motor_Run(CCW, 100, Angle(90));
			LcdWriteCom(0x01);  //清屏
		 }
	Motor_Step(0);	
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).