#include "drive_1602.h"
#include "string.h"
#include "stdio.h"
#include "dht11.h"

#define uint unsigned int 
#define uchar unsigned char
	
/*
����ʵ���˻���������Ҫ��ʽ����ʪ�ȴ��������Ըв⻷���¶�ʪ�ȣ�
���Ҳɼ����ź�����Ƭ��������ģ�����Ƭ���ṩ���ܱ�֤ϵͳ�ܹ��������У�
ˮλ�������ܹ����ˮ��ˮλ��֤һ����ˮλ��
��ʾ��������ʾ��ʪ����Ϣ��ˮλ��Ϣ��
��������ģ���ܹ����ɼ����źŶ��������ֻ�APP����ʵ��ͼ�λ���ʾ��
ʵ�ֵĹ��ܣ�
1��ͨ�������ܹ���⵽ˮ���ˮλ���¶ȡ�ʪ�ȡ�
2��ͨ��USB +5V ����
3���ܹ�������ֵ�����Ĺ���
4����ֵ���Խ��е��ڡ�
5��ʵ��APP����ʪ����Ϣ��⣬����APP��ͼ�λ�ʵʱ����¶�ʪ�ȱ仯��
*/
sbit SW1=P3^7;
sbit SW2=P3^6;
sbit SW3=P3^5;
sbit SW4=P3^4;

sbit JR_LED=P2^2;
sbit JW_LED=P2^4;

sbit buzz=P3^3;

uchar temp,humi;
int sw_val=0;

void delay_xms(int xms)
{
    char ix=0;
	  for(;xms>0;xms--)
	   for(ix=110;ix>0;ix--);
}
uchar SET=0;
char WdL=10;
char WdH=30;

char SdL=30;
char SdH=80;

char SWL=2;
char SWH=6;

char keyflag=0;
uchar bj_flag=0;
uchar Bj_CNt=0;
uchar JCount=0;

uchar clear_flag=0;

uchar bj_clear=0;
uint clear_cnt=0;

void KeyRead(void)
{
    if(!SW1)
		{
       if(!SW1&&keyflag==0)	
			 {
				 keyflag=1;
				 if(++SET>6)SET=0;
			   while(!SW1);
			 }
		}
    else if(!SW2)
		{
       if(!SW2&&keyflag==0)	
			 {
				 keyflag=1;
				 switch(SET)
				 {
				   case 1:
						  if(++WdH>50)WdH=0;
					 break;
					 
					 case 2:
						  if(++WdL>50)WdL=0;
					 break;
					 
					 
					 case 3:
						  if(++SdH>90)SdH=0;
					 break;
					 
					 case 4:
						  if(++SdL>90)SdH=0;
					 break;
					 
			   	 case 5:
						  if(++SWH>8)SWH=0;
					 break;
					 
					 case 6:
						  if(++SWL>8)SWL=0;
					 break;	 
				 }
			   while(!SW2);
			 }
		}
		
	  else if(!SW3)
		{
       if(!SW3&&keyflag==0)	
			 {
				 keyflag=1;
				 switch(SET)
				 {
				   case 1:
						 if(--WdH<0)WdH=50;
					 break;
					 
					 case 2:
						 if(--WdL<0)WdL=50;
					 break;
					
					 case 3:
						 if(--SdH<0)SdH=90;
					 break;
					 
					 case 4:
						 if(--SdL<0)SdL=90;
					 break;
					 
					 case 5:
						 if(--SWH<0)SWH=8;
					 break;
					 
					 case 6:
						 if(--SWL<0)SWL=8;
					 break;
					 
				 }
			   while(!SW3);
			 }
		}
		else if(!SW4)
		{
		  if(!SW4&&keyflag==0)	
			 {

				   bj_clear^=1;
					
				 
				  while(!SW4);
			 }
		}
		else
		{
		  keyflag=0;
		  clear_cnt=0;
		}


}

void Timer0Init(void)		//10����@11.0592MHz
{
	
	TMOD &= 0xF0;		//���ö�ʱ��ģʽ
	TMOD |= 0x01;		//���ö�ʱ��ģʽ
	TL0 = 0x00;		//���ö�ʱ��ֵ
	TH0 = 0xDC;		//���ö�ʱ��ֵ
	TF0 = 0;		//���TF0��־
	ET0=1;
	TR0 = 1;		//��ʱ��0��ʼ��ʱ
	EA=1;
	
}

void TimeISR()interrupt 1
{
  TL0 = 0x00;		//���ö�ʱ��ֵ
	TH0 = 0xDC;		//���ö�ʱ��ֵ
	 KeyRead();
	if((bj_flag==1)&&(bj_clear==0))
	{
	   if(++Bj_CNt>30)
		{
					Bj_CNt=0;
					buzz=~buzz; 
		}
	}else
	{
	   buzz=1;	Bj_CNt=0;
	}
	JCount++;
}

void UartInit(void)		//9600bps@11.0592MHz
{
	PCON &= 0x7F;		//�����ʲ�����
	SCON = 0x50;		//8λ����,�ɱ䲨����
	TMOD &= 0x0F;		//�����ʱ��1ģʽλ
	TMOD |= 0x20;		//�趨��ʱ��1Ϊ8λ�Զ���װ��ʽ
	TL1 = 0xFD;		//�趨��ʱ��ֵ
	TH1 = 0xFD;		//�趨��ʱ����װֵ
	ET1 = 0;		//��ֹ��ʱ��1�ж�
	TR1 = 1;		//������ʱ��1
	EA=1;
	ES=0;
}


void sendData(char *p,unsigned  char n)
{
  if( p == 0) return ;
	 ES = 0;
	 if(n > 0)
	 {
		 while(n --)
		 {
			 SBUF = *p++ ;
			 while(!TI)  ;
			 TI = 0      ;
		 }
	 }
	// ES = 1;
}



//   �����ڷ����ַ���
void sendString(char *p)
{
   if(p == 0) return ;
   sendData(p,strlen(p));
}
//  �����жϺ���
void usart() interrupt 4
{
  if(RI == 1)
	{  
    // setUsartRxData(SBUF); 
	}			 
	RI = 0;
	TI = 0;
}

//char xdata showbuff[16]={0};
char data send_buff[17]={0};

sbit IN_PL = P2^0;  //SHIFT/!LOA����
sbit IN_Data = P2^2; // QH�����������
sbit SCK = P2^1;  //CLOCK����

unsigned char Read74HC165(void)
{
   unsigned char indata;
   unsigned char i;
   IN_PL = 0;//װ��8λ��������
   _nop_();
   IN_PL = 1;//ת��8λ��������Ϊ��������
   _nop_();
 
   indata = 0;
   for(i = 0; i < 8; i ++) //��8���������ݴ�������indata��
   {
    indata = indata << 1;//��ʼʱ��indata����������һλ���Ժ�ÿ��ѭ������һ��
		SCK = 0;//ʱ�ӵ͵�ƽ�����󣬲������ݿ�ʼת����������
		_nop_();
    
		indata |= IN_Data; //��ת����ɵĴ�������һλλ��������
		SCK = 1;  //ʱ�ӱ�Ϊ�ߵ�ƽ���ٴα�Ϊ�͵�ƽʱ����ʼ������һλ��������
   }
		return indata;
}


char find_bit(unsigned char dat)
{
	 char i=0;
	 char cnt=0;
   for(i=0;i<8;i++)
	 {
	    if(!(dat&0x01))
			{
			  cnt++;
			}
		  dat>>=1;
	 }
	 return cnt;
}
void main()
{
	lcd1602_init();
	UartInit();
  dht11_value(&temp,&humi,DHT11_UINT8);
	buzz=1;
	delay_xms(200);
	dht11_value(&temp,&humi,DHT11_UINT8);
	Timer0Init();
  while(1)
	{
		
	 	if(JCount>50)//500ms�ɼ�һ��
		{
	  JCount=0;
		if(SET==0)
		{ 
		     if(clear_flag==1)
					{
						clear_flag=0;
				      	
						Write_1602_com(0x01);//����ʾ 
						delay_1ms(100);				 
					}
			    sw_val = find_bit(Read74HC165());
			    dht11_value(&temp,&humi,DHT11_UINT8);
				  L1602_printf(0,0,"temp:%2d",(int)temp);
				  L1602_printf(0,1,"humi:%2d",(int)humi);
				  L1602_printf(12,0,"%s","SW");
				  L1602_printf(12,1,"%2dcm",(int)sw_val);
					if((temp<WdL||temp>WdH)||(humi<SdL||humi>SdH)||(sw_val<SWL||sw_val>SWH))
					{
					   bj_flag=1;
					}else
					{
					  bj_flag=0;
					}
						sprintf(send_buff,"X%dH%dD%dZ\r\n",(int)temp,(int)humi,(int)sw_val);
					 sendString(send_buff);
		}
		else 
		{
			 if(clear_flag==0)
			 {
			    clear_flag=1;
				      	
				  Write_1602_com(0x01);//����ʾ 
          delay_1ms(100);				 
			 }
			 switch(SET)
			 {
			   case 1:
				 case 2:
					 WriteLcd1602_String(0,0,"Set temp");
				   L1602_printf(0,1,"H:%2d  L:%2d",(int)WdH,(int)WdL);
				 break;
				 
				 case 3:
				 case 4:
					 WriteLcd1602_String(0,0,"Set humi");
				   L1602_printf(0,1,"H:%2d  L:%2d",(int)SdH,(int)SdL);
				 break;
				 
				 case 5:
				 case 6:
					 WriteLcd1602_String(0,0,"Set SW  ");
				   L1602_printf(0,1,"H:%2d  L:%2d",(int)SWH,(int)SWL);
				 break;
			 }
			 
		}
  }
	
	}

}