#include "key.h"


uint8_t KEY = 0;
uint8_t TEMP = 26; //温度
extern uint8_t IDLE_FLG;
void key_init(void)
{
	GPIO_INPUT(P1, 0, 1);
	GPIO_INPUT(P1, 1, 1);
	GPIO_INPUT(P1, 2, 1);
	GPIO_INPUT(P1, 3, 1);
	EA = 1;
	IEN2 |= 0x01;
	INTCTRL |= 0x10; //INT4下降沿产生中断
	INTEN = 0x0F;    //P1.0 1 2 3外部中断使能
	
  KEY = 0;
  TEMP = 26; //温度
}


void KEY_Scan() interrupt 12
{
	
	switch(INTFLG)
	{
		case 0x01: //开机	
			IEN2 &= 0xFE; //禁止外部中断使能
			INTFLG = 0x00;
			delay(30);									//按键消抖
			if ((P1 & 0x01) == 0x00 && KEY == 0)
			{	
				KEY = 1;
				REM_Test();
			}	
			else 	
				IDLE_FLG = 1;
			break;
		case 0x02: //关机
			IEN2 &= 0xFE; //禁止外部中断使能
			INTFLG = 0x00;
			delay(30);	
			if ((P1 & 0x02) == 0x00 && KEY != 0)
			{
				KEY = 0;
				REM_Test();
			}		
			else 
				IDLE_FLG = 1;
			break;
		case 0x04: //加		
			IEN2 &= 0xFE; //禁止外部中断使能
			INTFLG = 0x00;	
			delay(30);		
			if ((P1 & 0x04) == 0x00 && (TEMP < 30)&& KEY != 0)
			{
				TEMP++;
				REM_Test();
			}
			else 
				IDLE_FLG = 1;
			break;
		case 0x08: //减
			INTFLG = 0x00;
			IEN2 &= 0xFE; //禁止外部中断使能
			delay(30);	
			if ((P1 & 0x08) == 0x00 && (TEMP > 17)&& KEY != 0)
			{
				TEMP--;
				REM_Test();
			}
			else 
				IDLE_FLG = 1;
			break;
		default:
			INTFLG = 0x00;	
			IDLE_FLG = 1;
			break;				
	}
	
}