#ifndef __SPARROW_H__
#define	__SPARROW_H__
#include <intrins.h>
typedef signed       char           int8_t;
typedef unsigned     char           uint8_t;
typedef signed       short int      int16_t;
typedef unsigned     short int      uint16_t;
typedef unsigned     int            uint_t;
typedef signed       long           int32_t;
typedef unsigned     long           uint32_t;

#define NOP                                 _nop_()                                 // Keil 51  中的NOP命令




//-设置IO为输入[P0.1=(P0, 1)]：
#define GPIO_INPUT(Px, pin,pcr)                (##Px##CR &= ~(1 << ##pin), pcr ? (##Px##PCR |=  (1 << ##pin)) : (##Px##PCR &= ~(1 << ##pin)))

//-设置IO为输出[P0.1=(P0, 1)]：
#define GPIO_OUTPUT(Px, pin, odr)               (##Px##CR |=  (1 << ##pin), odr ? (##Px##ODR |=  (1 << ##pin)) : (##Px##ODR &= ~(1 << ##pin)))

//-设置IO为低电平[P0.1=(P0, 1)]：
#define GPIO_LOW(Px, pin)                  (##Px##    &= ~(1 << ##pin))

//-设置IO为高电平[P0.1=(P0, 1)]：
#define GPIO_HIGH(Px, pin)                 (##Px##    |=  (1 << ##pin))

//-IO翻转[P0.1=(P0, 1)]：
#define GPIO_TOGGLE(Px, pin)               ((##Px##    &  (1 << ##pin)) ? (##Px##    &= ~(1 << ##pin)) : (##Px##    |=  (1 << ##pin)))

//-判断IO是否为高电平[P0.1=(P0, 1)]：
#define GPIO_IS_HIGH(Px, pin)              (##Px##    &  (1 << ##pin))

//-关闭IO上拉电阻[P0.1=(P0, 1)]：
#define GPIO_PUPD_NONE(Px, pin)            (##Px##PCR &= ~(1 << ##pin))

//-开启IO上拉电阻[P0.1=(P0, 1)]：
#define GPIO_PUPD_PU(Px, pin)              (##Px##PCR |=  (1 << ##pin))






#define IO_P0_0                 P0, 0
#define IO_P0_1                 P0, 1
#define IO_P0_2                 P0, 2
#define IO_P0_3                 P0, 3
#define IO_P0_4                 P0, 4
#define IO_P0_5                 P0, 5
#define IO_P0_6                 P0, 6
#define IO_P0_7                 P0, 7

#define IO_P1_0                 P1, 0
#define IO_P1_1                 P1, 1
#define IO_P1_2                 P1, 2
#define IO_P1_3                 P1, 3
#define IO_P1_4                 P1, 4
#define IO_P1_5                 P1, 5
#define IO_P1_6                 P1, 6
#define IO_P1_7                 P1, 7

#define IO_P2_0                 P2, 0
#define IO_P2_1                 P2, 1
#define IO_P2_2                 P2, 2
#define IO_P2_3                 P2, 3
#define IO_P2_4                 P2, 4
#define IO_P2_5                 P2, 5
#define IO_P2_6                 P2, 6
#define IO_P2_7                 P2, 7

#define IO_P3_0                 P3, 0
#define IO_P3_1                 P3, 1
#define IO_P3_2                 P3, 2
#define IO_P3_3                 P3, 3
#define IO_P3_4                 P3, 4
#define IO_P3_5                 P3, 5
#define IO_P3_6                 P3, 6
#define IO_P3_7                 P3, 7

#define IO_P4_0                 P4, 0
#define IO_P4_1                 P4, 1
#define IO_P4_2                 P4, 2
#define IO_P4_3                 P4, 3
#define IO_P4_4                 P4, 4
#define IO_P4_5                 P4, 5
#define IO_P4_6                 P4, 6
#define IO_P4_7                 P4, 7

#define IO_P5_0                 P5, 0
#define IO_P5_1                 P5, 1
#define IO_P5_2                 P5, 2
#define IO_P5_3                 P5, 3
#define IO_P5_4                 P5, 4
#define IO_P5_5                 P5, 5
#define IO_P5_6                 P5, 6
#define IO_P5_7                 P5, 7

#define NO_PULL  0
#define PULL_UP  1

#define OD_OUT 0
#define PUSH_PULL 1


/* ------------------- BYTE Register-------------------- */
/*BANK0*/
/**********CPU**********/
sfr BANK        = 0xF8;
sfr PSW         = 0xD0;
sfr ACC         = 0xE0;
sfr B           = 0xF0;
sfr RSTID       = 0xC7;
sfr SP          = 0x81;
sfr DPL         = 0x82;
sfr DPH         = 0x83;
sfr DPL1        = 0x84;
sfr DPH1        = 0x85;
sfr PCON        = 0x87;
sfr TCON        = 0x88;

/**********CLK**********/
sfr CKCON       = 0xD1;
sfr SYSCON      = 0xD2;
sfr CLKCFG      = 0xD3;
sfr CLKEN0    = 0xD4;
sfr CLKEN1    = 0xD5;
sfr BORCON      = 0xD6;
sfr CLKSTAT     = 0xD7;

/**********INT**********/
sfr IP00        = 0xB8;
sfr IP01        = 0xA5;
sfr IP10        = 0xA6;
sfr IP11        = 0xA7;
sfr IP20        = 0xCB;
sfr IP21        = 0xCC;
sfr IEN0          = 0xA8;
sfr IEN1        = 0xC0;
sfr IEN2        = 0xC8;
sfr IEN3        = 0xD8;
sfr IEN4        = 0xE8;
sfr INTEN       = 0xB1;
sfr INTCTRL     = 0xB2;
sfr INTFLG      = 0xB3;

/**********UART**********/
sfr SCON        = 0x98;
sfr SBUF        = 0x99;
sfr SADDR       = 0x9A;
sfr SADEN       = 0x9B;
sfr SBRTH       = 0xA1;
sfr SBRTL       = 0xA2;
sfr SFINE       = 0xA3;

/**********TIMER2**********/
sfr T2CON       = 0xB9;
sfr T2MOD       = 0xBA;
sfr T2CPL       = 0xBB;
sfr T2CPH       = 0xBC;
sfr TL2         = 0xBD;
sfr TH2         = 0xBE;

/**********TIMER3**********/
sfr T3CON       = 0x91;
sfr TL3         = 0x92;
sfr TH3         = 0x93;

/**********SPI**********/
sfr SPICON      = 0x94;
sfr SPIDAT      = 0x95;
sfr SPISTA      = 0x96;
               
/**********CRC**********/
sfr CRCCON      = 0x9C;
sfr CRCDAT      = 0x9D;
sfr CRCSD0      = 0x9E;
sfr CRCSD1      = 0x9F;
              
/**********FLASH**********/
sfr FSHCON       = 0xC1;
sfr FSHDATL      = 0xC2;
sfr FSHDATH      = 0xC3;
sfr FSHADRL     = 0xC4;
sfr FSHADRH     = 0xC5;
sfr FSDIV       = 0xC6;

sfr DPS         = 0xC9;
sfr DPC         = 0xCA;
/**********WD**********/
//sfr WDTREL      = 0xCD;
sfr WTCON       = 0xCD;
sfr WDTCON      = 0xCE;
sfr WDTWK       = 0xCF;
//sfr WDTCTRL     = 0x86;		//WDTRLD
sfr WDTRLD     = 0x86;	

/**********BUZ**********/
sfr BUZCON      = 0xDF;							
							
/**********PWM**********/               
sfr PWM0CON     = 0xF5;
sfr PWM0P       = 0xF6;
sfr PWM0D       = 0xF7;
sfr PWM1CON     = 0x8B;
sfr PWM1P       = 0x8C;
sfr PWM1D       = 0x8D;
sfr PWM2CON     = 0xB4;
sfr PWM2P       = 0xB5;
sfr PWM2D       = 0xB6;
sfr PWM3CON     = 0x8E;
sfr PWM3P       = 0xA4;
sfr PWM3D       = 0x97;

/**********REMOTE**********/	
sfr REMCON      = 0xF1;
sfr REMNUMH     = 0xF2;
sfr REMNUML     = 0xF3;

/**********ADC**********/
sfr ADCON       = 0xF9;
sfr ADT         = 0xFA;
sfr ADCH        = 0xFB;
sfr ADDL        = 0xFC;
sfr ADDH        = 0xFD;
sfr ADCON1      = 0xFE;

/**********IO**********/
sfr REMAP       = 0xFF;
sfr P0CR        = 0xE1;
sfr P1CR        = 0xE2;
sfr P2CR        = 0xE3;
sfr P3CR        = 0xE4;
sfr P4CR        = 0xE5;
sfr P5CR        = 0xE6;	

sfr P0PCR       = 0xE9;
sfr P1PCR       = 0xEA;
sfr P2PCR       = 0xEB;
sfr P3PCR       = 0xEC;
sfr P4PCR       = 0xED;
sfr P5PCR       = 0xEE;

sfr P0ODR       = 0xD9;
sfr P1ODR       = 0xDA;
sfr P2ODR       = 0xDB;
sfr P3ODR       = 0xDC;
sfr P4ODR       = 0xDD;
sfr P5ODR       = 0xDE;

sfr ALFSEL0     = 0x89;
sfr ALFSEL1     = 0x8A;

sfr P0          = 0x80;
sfr P1          = 0x90;
sfr P2          = 0xA0;
sfr P3          = 0xB0;
sfr P4          = 0xBF;
sfr P5          = 0xB7;							
							
/**********OTHER**********/							
							
sfr LCDCON1     = 0xA9;
sfr LCDCON      = 0xAA;
sfr SEGEN1      = 0xAB;
sfr SEGEN2      = 0xAC;
sfr SEGEN3      = 0xAD;
sfr SEGEN4      = 0xAE;
sfr COMEN       = 0xAF;
             
sfr SRST        = 0x8F;

sfr AENCON0     = 0xEF;
sfr AENCON1     = 0xE7;



/*********************bit***************************/	
//IEN0
sbit EX0     	= 0xA8;
sbit EX1        = 0xAA;
sbit ES       	= 0xAC;
sbit EA			= 0xAF;

//SCON
sbit RI         = 0x98;
sbit TI         = 0x99;
sbit RB8        = 0x9A;
sbit TB8        = 0x9B;
sbit REN        = 0x9C;
sbit SM2_TXCOL	= 0x9D;
sbit SM1_RXOV		= 0x9E;
sbit SM0_FE			= 0x9F;





//---------------------------定义端口↓
sbit P0_0           = P0 ^ 0;
sbit P0_1           = P0 ^ 1;
sbit P0_2           = P0 ^ 2;
sbit P0_3           = P0 ^ 3;
sbit P0_4           = P0 ^ 4;
sbit P0_5           = P0 ^ 5;
sbit P0_6           = P0 ^ 6;
sbit P0_7           = P0 ^ 7;

sbit P1_0           = P1 ^ 0;
sbit P1_1           = P1 ^ 1;
sbit P1_2           = P1 ^ 2;
sbit P1_3           = P1 ^ 3;
sbit P1_4           = P1 ^ 4;
sbit P1_5           = P1 ^ 5;
sbit P1_6           = P1 ^ 6;
sbit P1_7           = P1 ^ 7;

sbit P2_0           = P2 ^ 0;
sbit P2_1           = P2 ^ 1;
sbit P2_2           = P2 ^ 2;
sbit P2_3           = P2 ^ 3;
sbit P2_4           = P2 ^ 4;
sbit P2_5           = P2 ^ 5;
sbit P2_6           = P2 ^ 6;
sbit P2_7           = P2 ^ 7;

sbit P3_0           = P3 ^ 0;
sbit P3_1           = P3 ^ 1;
sbit P3_2           = P3 ^ 2;
sbit P3_3           = P3 ^ 3;
sbit P3_4           = P3 ^ 4;
sbit P3_5           = P3 ^ 5;
sbit P3_6           = P3 ^ 6;
sbit P3_7           = P3 ^ 7;

#endif
