#include "HT45F24A.h"
#define uchar unsigned char
/********************************************************
*********************************************************
#  Configuration: HIRC enable\Watchdog enable 1 instruction
# 				:AUD enable\SIM WCOL CSEN enable
#				:systemfreq 4Mhz
#  Compiler: Holtek C Compiler V3.10
#  ROM used: 318 words (total: 2046 words)
#  RAM used: 25 bytes (total: 128 bytes)
#  Function: Voice paly PCM12
#  Author: tony
#  Data: 2013/8/18
#  File: Voice_Play.c
/******************************************************/
/**************  User  registers**********************/
volatile unsigned  int AN7_AD_data;
volatile unsigned  char  Low_Baterry_Step,Timer1_50ms,Timer1_10s;
volatile unsigned  char  Timer1_2s,a_DebounceKey,a_DebounceKey_Long,a_II0_PFD0;
volatile uchar	An7_AD_H,An7_AD_L;
/******************************************************/
typedef volatile unsigned char FLAG0;
typedef struct {
volatile unsigned char FLAG0_b0:1;
volatile unsigned char FLAG0_b1:1;
volatile unsigned char FLAG0_b2:1;
volatile unsigned char FLAG0_b3:1;
volatile unsigned char FLAG0_b4:1;
volatile unsigned char FLAG0_b5:1;
volatile unsigned char FLAG0_b6:1;
volatile unsigned char FLAG0_b7:1;
} FLAG0_BIT;
typedef union {
FLAG0_BIT bits;
FLAG0 byte;
} R0;
R0 bitvar0;
/********************************************************/
/*********************  Flag ***************************/

//#define Fg_FirstPCM12Data 	 bitvar0.bits.FLAG0_b0
#define Fg_Test_mode		 bitvar0.bits.FLAG0_b1
#define Fg_Test_Voice	     bitvar0.bits.FLAG0_b2
#define Fg_Smoke		     bitvar0.bits.FLAG0_b3
#define Fg_Low_Baterry   	 bitvar0.bits.FLAG0_b4
#define fg_WaitReleaseKey	 bitvar0.bits.FLAG0_b5
#define Fg_50ms			     bitvar0.bits.FLAG0_b6
#define fg_WithKeyIn	   	 bitvar0.bits.FLAG0_b7  //short key

typedef volatile unsigned char FLAG1;
typedef struct{
volatile unsigned char FLAG1_b0 :1;
volatile unsigned char FLAG1_b1 :1;
volatile unsigned char FLAG1_b2 :1;
volatile unsigned char FLAG1_b3 :1;
volatile unsigned char FLAG1_b4 :1;
volatile unsigned char FLAG1_b5 :1;
volatile unsigned char FLAG1_b6 :1;
volatile unsigned char FLAG1_b7 :1;
} FLAG1_BIT;
typedef union {
FLAG1_BIT bits;
FLAG1 byte;
} R1;
R1 bitvar1;
//#define Fg_Long_Key			bitvar1.bits.FLAG1_b0
#define Fg_Key_Debounce		bitvar1.bits.FLAG1_b1
#define Fg_Key_Press		bitvar1.bits.FLAG1_b2
#define Fg_10s				bitvar1.bits.FLAG1_b3
#define Fg_2s				bitvar1.bits.FLAG1_b4
#define fg_WithLongKeyIn	bitvar1.bits.FLAG1_b5		//long key
#define fg_KEY				bitvar1.bits.FLAG1_b6
#define	fg_key_busy			bitvar1.bits.FLAG1_b7

/********************************************************/
/*****************Requires the user to set**************/
#define tmro_value 90;	  //tmro_value=SystemFrequency/(4*SAMPLINGRATE)			
#define lvd_value  03;    //0 2.0V/1 2.2V/2 2.4V/3 2.7V/4 3.0V/5 3.3V/6 3.6V/7 4.4V
#define Test_Key			_pa0
#define IR_tx				_pc6
#define LED					_pc4
#define Uart_TXD			_pb6
/********************************************************/
//********************Function void*********************/

void _ClearRam(void);
void delay500us(void);
void delay1000us(void);
void delay90us(void);
void Poweron_init(void);
void Baterry_check(void);
void _Smoke_Check(void);
void _eeprom_write(unsigned char eep_addr,unsigned char eep_data);
extern	void PLAY_VOICE(unsigned char voice_num);
void _PreparetoHalt(void);
void _Uart_WByte(uchar Uart_WData);
unsigned int _HEX2BCD(long R_HEX_Data_H);
void _Uart_Send(long Uart_WData_H);
                //====================//
				//=  PROGRAM ENTRY   = //                
                //====================//
void main()
{										
//POWER ON									//****************
	if(_pdf==0)								//Poweron init
	{										//
		_ClearRam();						//
		Poweron_init();						//
											//
		_Smoke_Check();						//
		_eeprom_write(00,An7_AD_H);			//
		_eeprom_write(01,An7_AD_L);			//
		goto Loop;
//		_PreparetoHalt();					//goto HALT
//		GCC_CLRWDT();						//
//		GCC_HALT();							//****************
	}
//WDT Time Out	
	
    if(_to==1)								
	{
		if(Fg_Test_mode==1)					//****************
		{									//Test Mode
			_Smoke_Check();					//
			_Uart_Send(AN7_AD_data);	//1: smoke check
			if(Fg_Test_Voice==0&Fg_Smoke==0)//
			{								//2:Sent ADC data 
				PLAY_VOICE(1);				//	to PC by UART				
				PLAY_VOICE(4);				//
				Fg_Test_Voice=0x01;			//	
			}	
									        
			_PreparetoHalt();				//
			GCC_HALT();						//*****************
		}
				
//Normal Mode		
		if(Fg_Low_Baterry==0)				//*****************
		Baterry_check();					//Normal Mode
		if(Fg_Low_Baterry==1)				//
		{									//
			Low_Baterry_Step++;				//
			if(Fg_Key_Press==1)				//
			{								//
				Low_Baterry_Step=0x04;		//
			}								//
			switch(Low_Baterry_Step)		//
			{								//
				case 1:						//
				case 2:						//1:Low Baterry Step 1
					PLAY_VOICE(0);			//	PLAY VOICE
					break;					//	請更換電池
				case 4:						//	PLAY twice in total
				case 6:						//
				case 8:						//
				case 10:					//
				case 12:					//
				case 14:					//
				case 16:					//
				case 18:					//
					Fg_50ms=0x00;			//2:Low Baterry Step 2 
					_emi=1;					//	LED Flash
					_et1i=1;				//	3 Times for 50ms
					_t1on=1;				//	per 8S. 8 cycles
					LED=0x00;				//
					while(!Fg_50ms);		//
					LED=0x01;				//
					break;					//
				case 19:					//
				case 20:					//
				case 21:					//3:Low Baterry Step 3
					PLAY_VOICE(0X02);		//	PLAY VOICE
					break;					//	"Bi" 3 times per4S
				default:					//
					GCC_CLRWDT();			//******************
			}
		}
		
		_Smoke_Check();						//******************
		if(Fg_Smoke==1)						//Smoke Check
		{									//
			Fg_10s=0;						//1:Smoke Check
			_emi=1;							//
			_et1i=1;						//
			_t1on=1;						//
			while(!Fg_10s)					//
			{								//2:When have smoke 
				PLAY_VOICE(0X01);			//	PLAY VOICE
				GCC_CLRWDT();				//	"Alarm""
											//	火災發生
				PLAY_VOICE(0x04);			//	10S
			}								//
			Fg_Smoke=0x00;					//******************			
		}	
Loop:					
		_PreparetoHalt();		
		GCC_HALT();							//WDT Wake up task FINISH goto HALT
	}
	delay1000us();
	delay1000us();
	fg_WithKeyIn=1;
	GCC_NOP();
	GCC_NOP();
//**************按鍵喚醒************//
Key_Wake:
		if(Test_Key==0)	
		{						  	
			if(fg_WithLongKeyIn==1)	
			{						  	
				goto long_key;
			}			
			goto Key_Wake;
		}
		
		
			          
long_key:
		while(fg_WithLongKeyIn==1)
		{								
			fg_WithKeyIn=0;				//Long Key
			fg_WithLongKeyIn=0;			//1:PLAY VOICE	Alarm
			PLAY_VOICE(0x01);				
//		    GCC_CLRWDT();
  			fg_WithLongKeyIn=0;    
		    fg_WithKeyIn=0;
		}		
			
			while(fg_WithKeyIn==1)
		    {	
		    	fg_WithKeyIn=0;			//	系統正常
		    	PLAY_VOICE(0x03);           //	PLAY VOICE					    
		    	fg_WithKeyIn=0;
				fg_WithLongKeyIn=0;
			}	


			goto Loop;
				
}			


void __attribute((interrupt(0x10))) Timer1_ISR(void)
{
	if(--a_II0_PFD0>0)
	{
		goto I_Timer1_END;	
	}
	a_II0_PFD0=2;
    key_check:
				if(Test_Key==1)
				{
					goto Rst_Chk_Key;
				}	
//				fg_KEY==1;								
					if(fg_key_busy==1)
					{
						goto Rst_Chk_Key_END;	
					}							
					if(--a_DebounceKey>0)
					{
					 	goto Rst_Chk_Key_END;	
					}
					if(fg_WaitReleaseKey==1)
					{
					 	goto Chk_LongKey;			
					}								
					fg_WaitReleaseKey=1;
										
	Chk_LongKey:				
					if(--a_DebounceKey_Long>0)
					{
					 	goto Rst_Chk_Key_1;	
					}
					fg_WithLongKeyIn=1;				// Long_key
					fg_WaitReleaseKey=0;
					fg_key_busy=1;	
					a_DebounceKey_Long=150;
					goto Rst_Chk_Key_1;			
	Rst_Chk_Key:			
	            	if(fg_key_busy==1)
	            	{	            	
	            		fg_key_busy==0;	
	            	}		
	                 
	                if(fg_WaitReleaseKey==1)		
	            	{
	            		fg_WithKeyIn=1;
	            	}	
	            	fg_WaitReleaseKey=0;
               		fg_key_busy=0;
               		a_DebounceKey_Long=150;
    Rst_Chk_Key_1:
               		a_DebounceKey=20;
                 
  	Rst_Chk_Key_END:  
  		
	Timer1_50ms--;
	if(Timer1_50ms==0)
	{
		Timer1_50ms=50;
		Fg_50ms=1;		
		Timer1_2s--;
		if(Timer1_2s==0)
		{
			Timer1_2s=40;					
			Fg_2s=1;	
				
			Timer1_10s--;
			if(Timer1_10s==0)
			{
				Timer1_10s=5;
				Fg_10s=1;	
			}
		}
	}
 I_Timer1_END:
 				GCC_NOP();	
}	


/************************Power on init**********************************/
void Poweron_init(void)
{
//ININ_IO_PORT
        //		 ;;+--------- PA7: A2E           [O]
        //		 ;;|+-------- PA6: A2N           [I]
        //		 ;;||+------- PA5: A2P           [I]
        //		 ;;|||+------ PA4: A1E           [O]
        //		 ;;||||+----- PA3: A1N           [I]
        //		 ;;|||||+---- PA2: A1P           [I]
        //		 ;;||||||+--- PA1: Bias          [O]
        //		 ;;|||||||+-- PA0: Test Key      [I]
		//		 ;;||||||||
		_pac	=0b00011101;
		_pa		=0b00011100;
		_papu	=0b00000001;
		_pawu	=0b00000001;
        //	     ;;+--------- PB7: No Pin	     [x]
        //       ;;|+-------- PB6: UART_output   [O]
        //       ;;||+------- PB5: No use		 [O]
        //       ;;|||+------ PB4: AUD		     [O]
        //       ;;||||+----- PB3: SIM (/SCS)    [O]
        //       ;;|||||+---- PB2: SIM (SCK)     [O]
        //       ;;||||||+--- PB1: SIM (SDI)     [O]
        //       ;;|||||||+-- PB0: SIM (SDO)     [I]
        //		 ;;||||||||
		_pbc	=0b00000001;
		_pb		=0b00001101;
        _pbpu	=0b00000000;
		//       ;;+---------  PC7: No Pin out   [x]
		//       ;;|+--------- PC6: IR_TX        [x]
		//       ;;||+-------- PC5: No Use       [x]
		//       ;;|||+------- PC4: LED		     [x]
		//       ;;||||+------ PC3: No Use       [x]
		//       ;;|||||+----- PC2: No Use       [x]
		//       ;;||||||+---- PC1: Flash_control[x]
		//       ;;|||||||+--- PC0: apm_control  [x]
		//       ;;||||||||
		_pcc	=0b00000000;
		_pc		=0b00010011;
		_pcpu	=0b00000000;										  
//ADC
	//		ACSR(xxh) register //
	// Bit7		+----------- Unused
	// Bit6		|+---------- ADONB : ADC module power on/off control bit
	// 			||          > 0 : ADC power on
	//			||            1 : ADC power off
	// Bit5		||+--------- VRSEL : Select ADC reference voltage
	//			|||           0 : Internal ADC power (Vdd)
	//			|||           1 : VREF pin
	// Bit4,3	|||++------- Unused
	// Bit2,1,0 |||||+++---- ADCS 2,1,0 : Select ADC clock source
	//			||||||||          0 0 0 : Fsys / 2
	//			||||||||          0 0 1 : Fsys / 8
	//      	||||||||          0 1 0 : Fsys / 32
	//      	||||||||          0 1 1 : Undefined
	//      	||||||||          1 0 0 : Fsys
	//      	||||||||    >>    1 0 1 : Fsys / 4
	//      	||||||||          1 1 0 : Fsys / 16
	//			||||||||          1 1 1 : Undefined
		_acsr=0B01000101;
	//		ADPCR(xxh) register //
	// Bit7,6	++---------- Unused
	// 			||
	// Bit5		||+--------- PCR5 : Define PC1 is A/D input or mot
	//			|||           0 : Not A/D input (PC1)
	//			|||           1 : A/D input, AN5
	// Bit4		|||+-------- PCR4 : Define PC0 is A/D input or mot
	//			||||          0 : Not A/D input (PC0)
	//			||||          1 : A/D input, AN4
	// Bit3		||||+------- PCR3 : Define PB6 is A/D input or mot
	//			|||||         0 : Not A/D input (PB6)
	//			|||||         1 : A/D input, AN3
	// Bit2		|||||+------ PCR2 : Define PB5 is A/D input or mot
	//			||||||        0 : Not A/D input (PB5)
	//			||||||        1 : A/D input, AN2
	// Bit1		||||||+----- PCR1 : Define PB4 is A/D input or mot
	//			|||||||       0 : Not A/D input (PB4)
	//			|||||||       1 : A/D input, AN1
	// Bit0		|||||||+---- PCR0 : Define PB3 is A/D input or mot
	//			||||||||      0 : Not A/D input (PB3)
	//			||||||||      1 : A/D input, AN0
	//			||||||||
	   _adpcr=0B00000000;
	//		ADCR0(xxh) register	//
	// Bit7		+----------- START Start the A/D conversion
	//			|           > 0 -> 1 -> 0 : Start ADC
	//			|             0 -> 1      : Reset ADC and set EOCB to 1
	// Bit6		|+---------- EOCB End of A/D Conversion bit
	//			||          > 0 : End of ADC
	//			||            1 : not End of ADC
	// Bit5,4,3 ||+++------- Unused
	//			|||||
	// Bit2,1,0	|||||+++---- ACS 2,1,0 Select ADC clock source
	//			||||||||      000 : AN0
	//			||||||||      001 : AN1
	//			||||||||      010 : AN2
	//			||||||||      011 : AN3
	//			||||||||      100 : AN4
	//			||||||||      101 : AN5
	//			||||||||      110 : A1E OPA 1 output
	//			||||||||    > 111 : A2E OPA 2 output
		_adcr=0B00000111;
//Interrupt
	//--- Set INTC0 Register ---
	//			 +------------- For Test Only Must be writen as 0
	//			 |+------------ T0F,   Timer0-Int request Flag (1=Act)
	//			 ||+----------- EIF1,  Ext-Int 1 request Flag  (1=Act)
	//			 |||+---------- EIF0,  Ext-Int 0 request Flag  (1=Act)
	//			 ||||+--------- ET0I,  Timer 0-Int Control     (1=En )
	//			 |||||+-------- EEI0,  Ext-Int 1 Control       (1=En )
	//			 ||||||+------- EEI0,  Ext-Int 0 Control       (1=En )
	//			 |||||||+------ EMI,   Control Int             (1=En )
	//			 ||||||||
		_intc0=0B00001000;
	//--- Set INTC1 Register ---
	//			 +------------- Unused, read as 0
	//			 |+------------ MFF,   Multi-function request Flag (1=Act)
	//			 ||+----------- SIMF,  SPI/I2C request Flag        (1=Act)
	//			 |||+---------- T1F,   Timer0-Int request Flag (1=Act)
	//			 ||||+--------- Unused, read as 0
	//			 |||||+-------- EMFI,  Control the Multi-function interrupt (1=En)
	//			 ||||||+------- ESIM,  Control SPI/I2C Interrupt (1=En)
	//			 |||||||+------ ET1I,  Timer 1-Int Control      (1=En))
	//           ||||||||
		_intc1=0B00000101;

	//--- Set MFIC0 Register ---
	//			 +------------- PEF,  External peripheral interrupt request flag
	//			 |+------------ TB1F, Time Base 1 interrupt request Flag
	//			 ||+----------- TB0F, Time Base 0 interrupt request Flag
	//			 |||+---------- ADF,  A/D converter interrupt request Flag
	//			 ||||+--------- EPI,  External peripheral interrupt Enable
	//			 |||||+-------- TB1E, Time Base 1 interrupt Enable
	//			 ||||||+------- TB0E, Time Base 0 interrupt Enable
	//			 |||||||+------ EADI, A/D converter interrupt Enable
	//           ||||||||
		_mfic0=0B00000000;

	//--- Set MFIC1 Register ---
	//;;       +------------- Unused, read as 0
 	//;;       |+------------ LVDF,  LVD interrupt request Flag
	//;;       ||+----------- E2F,   EEPROM interrupt request Flag
	//;;       |||+---------- CF,    Comparator interrupt request Flag
	//;;       ||||+--------- Unused, read as 0
	//;;       |||||+-------- ELVDI, LVD interrupt Enable
	//;;       ||||||+------- EE2I,  EEPROM interrupt Enable
	//;;       |||||||+------ ECI,   Comparator interrupt Enable
	//         ||||||||
		_mfic1=0B00000000;

	//--- Set INTEDGE Register ---
	//;;       ++++---------- Unused, read as 0
	//;;       ||||++-------- INT1S1,INT1S0 : INT1 Edge select
	//;;       ||||||              0 0 : disable
	//;;       ||||||              0 1 : rising edge tigger
	//;;       ||||||              1 0 : falling edge tigger
	//;;       ||||||              1 1 : dual  edge tigger
	//;;       ||||||
	//;;       ||||||++------ INT0S1,INT0S0 : INT0 Edge select
	//;;       ||||||              0 0 : disable
	//;;       ||||||              0 1 : rising edge tigger
	//;;       ||||||              1 0 : falling edge tigger
	//;;       ||||||              1 1 : dual  edge tigger
	//         ||||||||
	_intedge=0B00000000;
	//--- Set Timer0 Control register TMR0C ---
	// Bit7,6	 ++---------- T0M1,0 ( Defines the Operating Mode )
	//			 ||            0 0 : Unused
	//			 ||            0 1 : Event count mode ( External Clock )
	//			 ||          > 1 0 : Timer Mode ( Internal Clock )
	//			 ||            1 1 : Pulse Width measurement mode ( External Pulse )
	// Bit5		 ||+--------- Unused
	// Bit4		 |||+-------- T0ON ( Enable/Disable Timer 0 )
	//			 ||||        > 0 : Disable
	//			 ||||          1 : Enable
	// Bit3 	 ||||+------- T0E ( Define Timer0 active edge of the timer/event counter )
	//			 |||||       > 0 : rising edge
	//			 |||||         1 : falling edge
	// Bit2,1,0	 |||||+++---- T0PSC2,1,0 ( Define PreScaler Stages )
	//			 ||||||||      0 0 0 : Fint=Fsys       0 0 1 : Fint=Fsys/2
	//			 ||||||||     >0 1 0 : Fint=Fsys/4     0 1 1 : Fint=Fsys/8
	//			 ||||||||      1 0 0 : Fint=Fsys/16    1 0 1 : Fint=Fsys/32
	//			 ||||||||      1 1 0 : Fint=Fsys/64    1 1 1 : Fint=Fsys/128
		_tmr0c=0B10000010;
		_tmr0=0xff-tmro_value;
	//--- Set Timer1 Control register TMR1C ---
	//;; Bit7,6  ++---------- T1M1,0 ( Defines the Operating Mode )
	//           ||            0 0 : Unused
	//           ||            0 1 : Event count mode ( External Clock )
	//           ||          > 1 0 : Timer Mode ( Internal Clock )
	//           ||            1 1 : Pulse Width measurement mode ( External Pulse )
	// Bit5      ||+--------- T1S ( Define Internal Clock Source )
	//           |||         > 0 : Fsys/4
	//           |||           1 : Fsub, 32K RC OSC
	// Bit4      |||+-------- T1ON ( Enable/Disable Timer 0 )
	//           ||||        > 0 : Disable
	//           ||||          1 : Enable
	// Bit3      ||||+------- T1E ( Define Timer0 active edge of the timer/event counter )
	//           |||||         0 : rising edge
	//			 |||||       > 1 : falling edge
	// Bit2,1,0	 |||||+++---- Unused
	//			 ||||||||
		_tmr1c=0b10000000;
		_tmr1l=0x0b;		// Overflow time 500us
		_tmr1h=0xfe;
		_et1i=1;		   //
		_t1on=1;		   //
				 
//WDT
	if(Test_Key==1)
	{

	//--- Set WDT Control register WDTC ---
	// Bit7     +----------- FSYSON : Fsys control in IDLE Mode
	//          |           >  0 : Disable
	//          |              1 : Enable
	// Bit6,5,4 |+++-------- WS2,1,0 ( Defines the Operating Mode )
	//          ||||           0 0 0 : 2^13 / Fs
	//          ||||           0 0 1 : 2^14 / Fs
	//          ||||           0 1 0 : 2^15 / Fs     Fs=32K  1sec
	//          ||||           0 1 1 : 2^16 / Fs             2sec
	//          ||||        >  1 0 0 : 2^17 / Fs             4sec
	//          ||||           1 0 1 : 2^18 / Fs
	//          ||||           1 1 0 : 2^19 / Fs
	//          ||||           1 1 1 : 2^20 / Fs
	// Bit3~0   ||||++++---- TWDTEN3,2,1,0 : WDT Software Control
	//          ||||||||       1010 : Disable
	//          ||||||||    >  Other: Enable
		_wdtc=0B11000101;			//Normal mode 4sec
	}
	else
	{
		_wdtc=0B10100101;			//Test mode 1sec
		Fg_Test_mode=1;
	}
//OPA
	//--- Set OPA1C1 (xx) register ---
	// Bit 7      +------------- A1O2CIN --> OPA1 output to comparator control bit
	//            |
	// Bit 6      |+------------ A1O2N   --> OPA1 output to OPA1 inverting input control bit
	// Bit 5,4    ||++---------- A1PSEL1,0 --> OPA1 Non-inverting input selection bit
	//            ||||              >  0 0 : no connection
	//            ||||                 0 1 : 0.9 VDD
	//            ||||                 1 0 : VM ( 0.5 VDD or 0.5 VLDO )
	//            ||||                 1 1 : 0.1 VDD
	// Bit 3      ||||+--------- A1PS --> A1P to OPA1 + input control bit
	//            |||||
	// Bit 2      |||||+-------- A1NS --> A1N to OPA1 - input control bit
	//            ||||||
	// Bit 1      ||||||+------- A1OEN--> OPA1 output Enable
	//            |||||||
	// Bit 0      |||||||+------ A1EN --> OPA1 Enable
	//            ||||||||
		_opa1c1=0B00001101;
	//--- Set OPA2C0 (xxh) Register ---
	// Bit 7      +------------- A2OP  --> Operational amplifier output (read only)
	// Bit 6      |+------------ A2OFM --> OPA1 Mode Select.
	//            ||            >  0 : OPA mode
	//            ||               1 : Input offset voltage cancellation mode
	// Bit 5      ||+----------- A2RS  --> Input offset voltage cancellation reference selection.
	//            |||              0 : Select A1N
	//            |||           >  1 : Select A1P
	// Bit 4 ~ 0  |||+++++------ A2OF4,3,4,1,0 --> Input offset voltage cancellation control bits.
	//            ||||||||
		_opa2c0=0B00010000;

	//--- Set OPA2C1 (xx) register ---
	// Bit 7      +------------- A2O2CIN --> OPA1 output to comparator control bit
	//            |
	// Bit 6      |+------------ A2O2N   --> OPA1 output to OPA1 inverting input control bit
	// Bit 5,4    ||++---------- A2PSEL1,0 --> OPA1 Non-inverting input selection bit
	//            ||||              >  0 0 : no connection
	//            ||||                 0 1 : 0.9 VDD
	//            ||||                 1 0 : VM ( 0.5 VDD or 0.5 VLDO )
	//            ||||                 1 1 : 0.1 VDD
	// Bit 3      ||||+--------- A2PS --> A1P to OPA1 + input control bit
	//            |||||
	// Bit 2      |||||+-------- A2NS --> A1N to OPA1 - input control bit
	//            ||||||
	// Bit 1      ||||||+------- A2OEN--> OPA1 output Enable
	//            |||||||
	// Bit 0      |||||||+------ A2EN --> OPA1 Enable
	//            ||||||||
		_opa2c1=0B00001111;

	//--- Set OPA2C2 (xx) register ---
	// Bit 7      +------------- A1O2A2N ---> OPA1 Out to OPA2 (-) inverting input control bit
	// Bit 6      |+------------ A1O2A2P ---> OPA1 Out to OPA2 (+) non-inverting input control bit
	// Bit 5,4    ||++---------- Unused
	// Bit 3      ||||+--------- PGAEN   ---> OPA2 PGA gain enable
	// Bit 2 ~ 0  |||||+++------ PGA2,1,0 --> OPA2 PGA gain control bits.
	//            ||||||||          0 0 0 : 1
	//            ||||||||          0 0 1 : 8
	//            ||||||||          0 1 0 : 16
	//            ||||||||          0 1 1 : 24
	//            ||||||||          1 0 0 : 32
	//            ||||||||          1 0 1 : 40
	//            ||||||||          1 1 0 : 48
	//            ||||||||      >   1 1 1 : 56
	//            ||||||||
		_opa2c2=0B00000111;
//LVDC
	//==============LVDC==============//
	// Bit 7,6++---------------unimplemented,read as"0"
	// Bit 5  ||+--------------LVDO:LVD Output Flag
	//		  |||				  ->0:no Low Voltage Detect
	//		  |||					1:Low Voltage Detect
	// Bit 4  |||+-------------LVDEN:Low Voltage Detector Control
	//		  ||||				  ->0:disable
	//		  ||||					1:enable
	// Bit 3  ||||+------------unimplemented,read as"0"
	// Bit 2~0|||||+++---------LVD2~LVD0:Select LVD Voltage
	//		  ||||||||			  ->000:2.0V			
	//		  ||||||||				001:2.2V			
	//		  ||||||||				010:2.4V			
	//		  ||||||||				011:2.7V			
	//		  ||||||||				100:3.0V			
	//		  ||||||||				101:3.3V			
	//		  ||||||||				110:3.6V			
	//		  ||||||||				111:4.4V
	// POR	  --00-000
		_lvdc=lvd_value;		
//DEFAULT_RAM				
	Timer1_10s=5;
	Timer1_50ms=50;
	Timer1_2s=40;					
	a_II0_PFD0=2;
	a_DebounceKey_Long=150;
    a_DebounceKey=20;
//	Debounce_count=50;
}

/************************Baterry check**********************************/
void Baterry_check(void)
{
	Fg_Low_Baterry=0;
	Low_Baterry_Step=0;
	_lvden=1;
	delay90us();
	if(_lvdo==1)
		Fg_Low_Baterry=1;
	else
		Fg_Low_Baterry=0;			
		_lvden		  =0;
}
/************************Prepare to Halt*******************************/	
void _PreparetoHalt(void)
{
	LED		=1;
	_lvden	=0;		//LVD  OFF
	_cmp1en	=0;		//CMP1 OFF
	_cmp2en =0;		//CMP2 OFF
	_a1en	=0;		//OPA1 OFF
	_a2en	=0;		//OPA2 OFF
	_lcden	=0;		//LCD  OFF
	_ldoen	=0;		//LDO  OFF
	_ren1	=0;
	_simen	=0;		//SPI  OFF
	_adonb	=1;		//ADC  OFF
	_dacen	=0;		//DAC  0FF
	_pckp1	=0;		//pckpsc1 OFF
	_pckp0	=0;		
	_pcken	=0;
	_idlen	=0;
//	_t1on	=0;
//	_t0on	=0;
	_emi	=1;
	_pawu	=0x01;
}


void _Smoke_Check(void)
{
volatile	uchar	An7_AD_H,An7_AD_L;
volatile	unsigned  int Smoke_value=500;
	GCC_CLRWDT();
		
	_pac1		=0;
	_pa1 		=1;				//set opa input DC bias source voltage
	_a1en		=1;
	delay500us();
	_a2en		=1;
	delay500us();
	delay500us();
	_emi		=0;
	IR_tx		=1;					//Set IR LED On
	delay90us();
//Read ADC	
	_adonb=0;
		
	GCC_NOP();
	GCC_NOP();
	_adcr=0b00000111;
	_start=0;					//START ADC Converter ADCR.7 								
	_start=1;					//(ADC START bit) 0 -> 1 -> 0
	_start=0;
	GCC_NOP();
	GCC_NOP();					//4 Tad for ADC Sampling Time
	GCC_NOP();
	GCC_NOP();
	IR_tx=0;					//Clr IR LED Off
	_a2en=0;
	_a1en=0;
	_pa1=0;
	while(_eocb);
	GCC_CLRWDT();

	An7_AD_H=_adrh;
	An7_AD_L=_adrl;
	_adonb=1;					//Disable ADC
	_c=0;
	
	GCC_RRC(An7_AD_H);
	GCC_RRC(An7_AD_L);	

	GCC_RRC(An7_AD_H);
	GCC_RRC(An7_AD_L);
	
	GCC_RRC(An7_AD_H);
	GCC_RRC(An7_AD_L);

	GCC_RRC(An7_AD_H);
	GCC_RRC(An7_AD_L);
	

			
	AN7_AD_data=An7_AD_H;
	AN7_AD_data<<=8;
	AN7_AD_data=AN7_AD_data+An7_AD_L;

	if(AN7_AD_data>=Smoke_value)
		Fg_Smoke=0x01;
	else
		Fg_Smoke=0x00;
}

void _Uart_Send(long Uart_WData_H)
{
	volatile uchar i,j,temp;
	volatile unsigned int data;
	data=_HEX2BCD(Uart_WData_H);
	i=data;
	data>>=8;
	j=data;

	_Uart_WByte(0x02);	
	_Uart_WByte(0x30);
	_Uart_WByte(0x30);

	temp=j&0xf0;
	GCC_SWAP(temp);
	temp=temp+0x30;
	_Uart_WByte(temp);

	temp=j&0x0f;
	temp=temp+0x30;
	_Uart_WByte(temp);
	
	temp=i&0xf0;
	GCC_SWAP(temp);
	temp=temp+0x30;
	_Uart_WByte(temp);

	temp=i&0x0f;
	temp=temp+0x30;
	_Uart_WByte(temp);	
	
	_Uart_WByte(0x03);
	
}




unsigned int _HEX2BCD(long R_HEX_Data_H)
{
	volatile unsigned int temp=0,output=0;
	volatile uchar i;

	GCC_CLRWDT();	

	temp=R_HEX_Data_H;
 
    i=0;
	while(temp>=1000)
	{
		temp=temp-1000;
		i++;
	}
	GCC_SWAP(i);
	while(temp>=100)
	{
		temp=temp-100;
		i++;
	}
	output=i;
	output<<=8;

	i=0;
	while(temp>=10)
	{
		temp=temp-10;
		i++;
	}
	GCC_SWAP(i);
	while(temp>=1)
	{
		temp=temp-1;
		i++;
	}
	output=output+i;
	return output;
}

void _Uart_WByte(uchar Uart_WData)
{
/*	_pbc6=0;*/
	volatile uchar i=8;
	Uart_TXD=0;					//發送起始位
	GCC_CLRWDT();
	GCC_CLRWDT();
	_c=0;
	
asm("WByte_LOOP:");	
	asm("rrc %0" : "=m" (Uart_WData));

	asm("SZ  	c");
	asm("JMP	WByte_H");
	
	GCC_CLRWDT();
	GCC_CLRWDT();
	asm("clr 	[14H].6");
	asm("SDZ %0" :"=m" 	(i));
	asm("JMP	WByte_LOOP");
	asm("JMP	WByte_END");		
asm("WByte_H:");
	GCC_CLRWDT();
	asm("set 	[14H].6");
	asm("SDZ %0" :"=m" 	(i));
	asm("JMP	WByte_LOOP");	

asm("WByte_END:");
	Uart_TXD=1;
	GCC_NOP();
	GCC_NOP();
	GCC_NOP();
	GCC_NOP();
	GCC_NOP();
	GCC_NOP();
	GCC_NOP();
}



/*************************_ClearRam***********************************/
void _ClearRam(void)
{
//	_bp = 0;		//bank0:0x40~0x0ff
	for(_mp1 = 0x40;_mp1 <0x0BF;_mp1++)
	{
		_iar1 = 0;
	}
	
}

void _eeprom_write(unsigned char eep_addr,unsigned char eep_data)
{
	_eea=eep_addr;
	_eed=eep_data;
	_bp=0x01;
	_mp1=0x40;
	_wren=1;
	_wr=1;
	while(_wr);				
	_iar1=0x00;					//Write Once. 4ms Max.
	_bp=0x00;
	GCC_CLRWDT();
}
void delay500us(void)
{
	GCC_NOP();
	uchar i;
	for(i=0;i<124;i++)
	
	GCC_NOP();
}

void delay1000us(void)
{
	uchar i;
	for(i=0;i<99;i++)	
	GCC_NOP();
	GCC_NOP();
}

void delay90us(void)
{
	uchar i;
	for(i=0;i<21;i++)	
	GCC_NOP();
	GCC_NOP();
}

