//==============================================================================
//==============================================================================
//==============================================================================
//INTERRUPT 入口
//==============================================================================
#ifndef __STM8S103_H
#define __STM8S103_H
typedef unsigned char uint8;
typedef unsigned int uint16;
#define BIT0 (0x01)
#define BIT1 (0x02)
#define BIT2 (0x04)
#define BIT3 (0x08)
#define BIT4 (0x10)
#define BIT5 (0x20)
#define BIT6 (0x40)
#define BIT7 (0x80)
#define _INT_RESET 0x00
#define _INT_TRAP 0x01
#define _INT_TLI 0x02
#define _INT_AWU 0x03
#define _INT_CLK 0x04
#define _INT_PA 0x05
#define _INT_PB 0x06
#define _INT_PC 0x07
#define _INT_PD 0x08
#define _INT_PE 0x09
#define _INT_CAN_TX 0x0A
#define _INT_CAN_RX 0x0B
#define _INT_SPI 0x0C
#define _INT_TIM1 0x0D
#define _INT_TIM1CC 0x0E
#define _INT_TIM2 0x0F
#define _INT_TIM2CC 0x10
#define _INT_TIM3 0x11
#define _INT_TIM3CC 0x12
#define _INT_UART1_TX 0x13
#define _INT_UART1_RX 0x14
#define _INT_IIC 0x15
#define _INT_UART2_TX 0x16
#define _INT_UART2_RX 0x17
#define _INT_ADC1 0x18
#define _INT_TIM14 0x19
#define _INT_FLASH 0x1A
//==============================================================================
//
//==============================================================================
#define PA_ODR (*(volatile unsigned char*)0x5000)
#define PA_IDR (*(volatile unsigned char*)0x5001)
#define PA_DDR (*(volatile unsigned char*)0x5002)
#define PA_CR1 (*(volatile unsigned char*)0x5003)
#define PA_CR2 (*(volatile unsigned char*)0x5004)
#define PB_ODR (*(volatile unsigned char*)0x5005)
#define PB_IDR (*(volatile unsigned char*)0x5006)
#define PB_DDR (*(volatile unsigned char*)0x5007)
#define PB_CR1 (*(volatile unsigned char*)0x5008)
#define PB_CR2 (*(volatile unsigned char*)0x5009)
#define PC_ODR (*(volatile unsigned char*)0x500a)
#define PC_IDR (*(volatile unsigned char*)0x500b)
#define PC_DDR (*(volatile unsigned char*)0x500c)
#define PC_CR1 (*(volatile unsigned char*)0x500d)
#define PC_CR2 (*(volatile unsigned char*)0x500e)
#define PD_ODR (*(volatile unsigned char*)0x500f)
#define PD_IDR (*(volatile unsigned char*)0x5010)
#define PD_DDR (*(volatile unsigned char*)0x5011)
#define PD_CR1 (*(volatile unsigned char*)0x5012)
#define PD_CR2 (*(volatile unsigned char*)0x5013)
#define PE_ODR (*(volatile unsigned char*)0x5014)
#define PE_IDR (*(volatile unsigned char*)0x5015)
#define PE_DDR (*(volatile unsigned char*)0x5016)
#define PE_CR1 (*(volatile unsigned char*)0x5017)
#define PE_CR2 (*(volatile unsigned char*)0x5018)
#define PF_ODR (*(volatile unsigned char*)0x5019)
#define PF_IDR (*(volatile unsigned char*)0x501a)
#define PF_DDR (*(volatile unsigned char*)0x501b)
#define PF_CR1 (*(volatile unsigned char*)0x501c)
#define PF_CR2 (*(volatile unsigned char*)0x501d)
#define PG_ODR (*(volatile unsigned char*)0x501e)
#define PG_IDR (*(volatile unsigned char*)0x501f)
#define PG_DDR (*(volatile unsigned char*)0x5020)
#define PG_CR1 (*(volatile unsigned char*)0x5021)
#define PG_CR2 (*(volatile unsigned char*)0x5022)
#define PH_ODR (*(volatile unsigned char*)0x5023)
#define PH_IDR (*(volatile unsigned char*)0x5024)
#define PH_DDR (*(volatile unsigned char*)0x5025)
#define PH_CR1 (*(volatile unsigned char*)0x5026)
#define PH_CR2 (*(volatile unsigned char*)0x5027)
#define PI_ODR (*(volatile unsigned char*)0x5028)
#define PI_IDR (*(volatile unsigned char*)0x5029)
#define PI_DDR (*(volatile unsigned char*)0x502a)
#define PI_CR1 (*(volatile unsigned char*)0x502b)
#define PI_CR2 (*(volatile unsigned char*)0x502c)
#define FLASH_CR1 (*(volatile unsigned char*)0x505A)
#define FLASH_CR2 (*(volatile unsigned char*)0x505B)
#define FLASH_NCR2 (*(volatile unsigned char*)0x505C)
#define FLASH_FPR (*(volatile unsigned char*)0x505D)
#define FLASH_NFPR (*(volatile unsigned char*)0x505E)
#define FLASH_IAPSR (*(volatile unsigned char*)0x505F)
#define FLASH_PUKR (*(volatile unsigned char*)0x5062)
#define FLASH_DUKR (*(volatile unsigned char*)0x5064)
#define EXTI_CR1 (*(volatile unsigned char*)0x50A0)
#define EXTI_CR2 (*(volatile unsigned char*)0x50A1)
#define RST_SR (*(volatile unsigned char*)0x50B3)
#define CLK_ICKR (*(volatile unsigned char*)0x50C0)
#define CLK_ECKR (*(volatile unsigned char*)0x50C1)
#define CLK_CMSR (*(volatile unsigned char*)0x50C3)
#define CLK_SWR (*(volatile unsigned char*)0x50C4)
#define CLK_SWCR (*(volatile unsigned char*)0x50C5)
#define CLK_CKDIVR (*(volatile unsigned char*)0x50C6)
#define CLK_PCKENR1 (*(volatile unsigned char*)0x50C7)
#define CLK_CSSR (*(volatile unsigned char*)0x50C8)
#define CLK_CCOR (*(volatile unsigned char*)0x50C9)
#define CLK_PCKENR2 (*(volatile unsigned char*)0x50CA)
#define CLK_CANCCR (*(volatile unsigned char*)0x50CB)
#define CLK_HSITRIMR (*(volatile unsigned char*)0x50CC)
#define CLK_SWIMCCR (*(volatile unsigned char*)0x50CD)
#define WWDG_CR (*(volatile unsigned char*)0x50D1)
#define WWDG_WR (*(volatile unsigned char*)0x50D2)
#define IWDG_KR (*(volatile unsigned char*)0x50E0)
#define IWDG_PR (*(volatile unsigned char*)0x50E1)
#define IWDG_RLR (*(volatile unsigned char*)0x50E2)
#define AWU_CSR1 (*(volatile unsigned char*)0x50F0)
#define AWU_ARP (*(volatile unsigned char*)0x50F1)
#define AWU_TBR (*(volatile unsigned char*)0x50F2)
#define BEEP_CSR (*(volatile unsigned char*)0x50F3)
#define SPI_CR1 (*(volatile unsigned char*)0x5200)
#define SPI_CR2 (*(volatile unsigned char*)0x5201)
#define SPI_ICR (*(volatile unsigned char*)0x5202)
#define SPI_SR (*(volatile unsigned char*)0x5203)
#define SPI_DR (*(volatile unsigned char*)0x5204)
#define SPI_CRCPR (*(volatile unsigned char*)0x5205)
#define SPI_RXCRCR (*(volatile unsigned char*)0x5206)
#define SPI_TXCRCR (*(volatile unsigned char*)0x5207)
#define I2C_CR1 (*(volatile unsigned char*)0x5210)
#define I2C_CR2 (*(volatile unsigned char*)0x5211)
#define I2C_FREQR (*(volatile unsigned char*)0x5212)
#define I2C_OARL (*(volatile unsigned char*)0x5213)
#define I2C_OARH (*(volatile unsigned char*)0x5214)
#define I2C_DR (*(volatile unsigned char*)0x5216)
#define I2C_SR1 (*(volatile unsigned char*)0x5217)
#define I2C_SR2 (*(volatile unsigned char*)0x5218)
#define I2C_SR3 (*(volatile unsigned char*)0x5219)
#define I2C_ITR (*(volatile unsigned char*)0x521A)
#define I2C_CCRL (*(volatile unsigned char*)0x521B)
#define I2C_CCRH (*(volatile unsigned char*)0x521C)
#define I2C_TRISER (*(volatile unsigned char*)0x521D)
#define I2C_PECR (*(volatile unsigned char*)0x521E)
#define UART1_SR (*(volatile unsigned char*)0x5230)
#define UART1_DR (*(volatile unsigned char*)0x5231)
#define UART1_BRR1 (*(volatile unsigned char*)0x5232)
#define UART1_BRR2 (*(volatile unsigned char*)0x5233)
#define UART1_CR1 (*(volatile unsigned char*)0x5234)
#define UART1_CR2 (*(volatile unsigned char*)0x5235)
#define UART1_CR3 (*(volatile unsigned char*)0x5236)
#define UART1_CR4 (*(volatile unsigned char*)0x5237)
#define UART1_CR5 (*(volatile unsigned char*)0x5238)
#define UART1_GTR (*(volatile unsigned char*)0x5239)
#define UART1_PSCR (*(volatile unsigned char*)0x523a)
#define TIM1_CR1 (*(volatile unsigned char*)0x5250)
#define TIM1_CR2 (*(volatile unsigned char*)0x5251)
#define TIM1_SMCR (*(volatile unsigned char*)0x5252)
#define TIM1_ETR (*(volatile unsigned char*)0x5253)
#define TIM1_IER (*(volatile unsigned char*)0x5254)
#define TIM1_SR1 (*(volatile unsigned char*)0x5255)
#define TIM1_SR2 (*(volatile unsigned char*)0x5256)
#define TIM1_EGR (*(volatile unsigned char*)0x5257)
#define TIM1_CCMR1 (*(volatile unsigned char*)0x5258)
#define TIM1_CCMR2 (*(volatile unsigned char*)0x5259)
#define TIM1_CCMR3 (*(volatile unsigned char*)0x525A)
#define TIM1_CCMR4 (*(volatile unsigned char*)0x525B)
#define TIM1_CCER1 (*(volatile unsigned char*)0x525C)
#define TIM1_CCER2 (*(volatile unsigned char*)0x525D)
#define TIM1_CNTRH (*(volatile unsigned char*)0x525E)
#define TIM1_CNTRL (*(volatile unsigned char*)0x525F)
#define TIM1_PSCRH (*(volatile unsigned char*)0x5260)
#define TIM1_PSCRL (*(volatile unsigned char*)0x5261)
#define TIM1_ARRH (*(volatile unsigned char*)0x5262)
#define TIM1_ARRL (*(volatile unsigned char*)0x5263)
#define TIM1_RCR (*(volatile unsigned char*)0x5264)
#define TIM1_CCR1H (*(volatile unsigned char*)0x5265)
#define TIM1_CCR1L (*(volatile unsigned char*)0x5266)
#define TIM1_CCR2H (*(volatile unsigned char*)0x5267)
#define TIM1_CCR2L (*(volatile unsigned char*)0x5268)
#define TIM1_CCR3H (*(volatile unsigned char*)0x5269)
#define TIM1_CCR3L (*(volatile unsigned char*)0x526A)
#define TIM1_CCR4H (*(volatile unsigned char*)0x526B)
#define TIM1_CCR4L (*(volatile unsigned char*)0x526C)
#define TIM1_BKR (*(volatile unsigned char*)0x526D)
#define TIM1_DTR (*(volatile unsigned char*)0x526E)
#define TIM1_OISR (*(volatile unsigned char*)0x526F)
#define TIM2_CR1 (*(volatile unsigned char*)0x5300)
#define TIM2_IER (*(volatile unsigned char*)0x5303)
#define TIM2_SR1 (*(volatile unsigned char*)0x5304)
#define TIM2_SR2 (*(volatile unsigned char*)0x5305)
#define TIM2_EGR (*(volatile unsigned char*)0x5306)
#define TIM2_CCMR1 (*(volatile unsigned char*)0x5307)
#define TIM2_CCMR2 (*(volatile unsigned char*)0x5308)
#define TIM2_CCMR3 (*(volatile unsigned char*)0x5309)
#define TIM2_CCER1 (*(volatile unsigned char*)0x530A)
#define TIM2_CCER2 (*(volatile unsigned char*)0x530B)
#define TIM2_CNTRH (*(volatile unsigned char*)0x530C)
#define TIM2_CNTRL (*(volatile unsigned char*)0x530D)
#define TIM2_PSCR (*(volatile unsigned char*)0x530E)
#define TIM2_ARRH (*(volatile unsigned char*)0x530F)
#define TIM2_ARRL (*(volatile unsigned char*)0x5310)
#define TIM2_CCR1H (*(volatile unsigned char*)0x5311)
#define TIM2_CCR1L (*(volatile unsigned char*)0x5312)
#define TIM2_CCR2H (*(volatile unsigned char*)0x5313)
#define TIM2_CCR2L (*(volatile unsigned char*)0x5314)
#define TIM2_CCR3H (*(volatile unsigned char*)0x5315)
#define TIM2_CCR3L (*(volatile unsigned char*)0x5316)
#define TIM4_CR1 (*(volatile unsigned char*)0x5340)
#define TIM4_IER (*(volatile unsigned char*)0x5343)
#define TIM4_SR (*(volatile unsigned char*)0x5344)
#define TIM4_EGR (*(volatile unsigned char*)0x5345)
#define TIM4_CNTR (*(volatile unsigned char*)0x5346)
#define TIM4_PSCR (*(volatile unsigned char*)0x5347)
#define TIM4_ARR (*(volatile unsigned char*)0x5348)
#define ADC_DB0R (*(volatile unsigned char*)0x53E0)
#define ADC_DB1R (*(volatile unsigned char*)0x53E1)
#define ADC_DB2R (*(volatile unsigned char*)0x53E2)
#define ADC_DB3R (*(volatile unsigned char*)0x53E3)
#define ADC_DB4R (*(volatile unsigned char*)0x53E4)
#define ADC_DB5R (*(volatile unsigned char*)0x53E5)
#define ADC_DB6R (*(volatile unsigned char*)0x53E6)
#define ADC_DB7R (*(volatile unsigned char*)0x53E7)
#define ADC_DB8R (*(volatile unsigned char*)0x53E8)
#define ADC_DB9R (*(volatile unsigned char*)0x53E9)
#define ADC_DB10R (*(volatile unsigned char*)0x53EA)
#define ADC_DB11R (*(volatile unsigned char*)0x53EB)
#define ADC_DB12R (*(volatile unsigned char*)0x53EC)
#define ADC_DB13R (*(volatile unsigned char*)0x53ED)
#define ADC_DB14R (*(volatile unsigned char*)0x53EE)
#define ADC_DB15R (*(volatile unsigned char*)0x53EF)
#define ADC_DB16R (*(volatile unsigned char*)0x53F0)
#define ADC_DB17R (*(volatile unsigned char*)0x53F1)
#define ADC_DB18R (*(volatile unsigned char*)0x53F2)
#define ADC_DB19R (*(volatile unsigned char*)0x53F3)
#define ADC_CSR (*(volatile unsigned char*)0x5400)
#define ADC_CR1 (*(volatile unsigned char*)0x5401)
#define ADC_CR2 (*(volatile unsigned char*)0x5402)
#define ADC_CR3 (*(volatile unsigned char*)0x5403)
#define ADC_DRH (*(volatile unsigned char*)0x5404)
#define ADC_DRL (*(volatile unsigned char*)0x5405)
#define ADC_TDRH (*(volatile unsigned char*)0x5406)
#define ADC_TDRL (*(volatile unsigned char*)0x5407)
#define ADC_HTRH (*(volatile unsigned char*)0x5408)
#define ADC_HTRL (*(volatile unsigned char*)0x5409)
#define ADC_LTRH (*(volatile unsigned char*)0x540A)
#define ADC_LTRL (*(volatile unsigned char*)0x540B)
#define ADC_AWSRH (*(volatile unsigned char*)0x540C)
#define ADC_AWSRL (*(volatile unsigned char*)0x540D)
#define ADC_AWCRH (*(volatile unsigned char*)0x540E)
#define ADC_AWCRL (*(volatile unsigned char*)0x540F)
#define ADD_ID_S 0x4865 //ID号起始地址
#define ADD_ID_E 0X4870 //ID号结束地址
#define ADD_FLASH_S 0X8000 //flash起始地址
#define ADD_FLASH_E 0X9fff //flash结束地址
#define CFG_GCR (*(volatile unsigned char*)0x7F60)
//==============================================================================
#define SBIT(var,bit) (var|=(1<<(bit))) //位设置
#define CBIT(var,bit) (var&=(~(1<<(bit)))) //位清零
#define RBIT(var,bit) (var^=(1<<(bit))) //位取反
#define GBIT(var,bit) (var&(1<<bit)) //取位数据
#define _IO_OH(P,B) (SBIT(P##_ODR,B)) //将端口P 的B位置1
#define _IO_OL(P,B) (CBIT(P##_ODR,B)) //将端口P 的B位置0
#define _IO_OR(P,B) (RBIT(P##_ODR,B)) //将端口P 的B位取反
#define _IO_DO(P,B) (GBIT(P##_ODR,B)) //取端口P的B位输出值
#define _IO_MO(P,B) (SBIT(P##_DDR,B)) //端口P的B位设置为输出口
#define _IO_MI(P,B) (CBIT(P##_DDR,B)) //端口P的B位设置为输入口
#define _IO_DI(P,B) (GBIT(P##_IDR,B)) //取端口P的B位输入值
#define _IO_IF(P,B) (CBIT(P##_CR1,B)) //输入口悬空
#define _IO_IR(P,B) (SBIT(P##_CR1,B)) //输入口上拉电阻
#define _IO_OC(P,B) (CBIT(P##_CR1,B)) //输出口开漏
#define _IO_OP(P,B) (SBIT(P##_CR1,B)) //输出口推挽输出
#define _IO_IE(P,B) (SBIT(P##_CR2,B)) //输入口中断允许
#define _IO_ID(P,B) (CBIT(P##_CR2,B)) //输入口中断禁止
#define _IO_CH(P,B) (SBIT(P##_CR2,B)) //输出口高速输出
#define _IO_CL(P,B) (CBIT(P##_CR2,B)) //输出口低速输出
//==============================================================================
//==============================================================================
#define E_IWDG IWDG_KR=0XCC //看门狗允许
#define R_IWDG(x) IWDG_KR=0X55;IWDG_RLR=x;IWDG_KR=0XAA //看门狗装载值
#define D_IWDG(x) IWDG_KR=0X55;IWDG_PR=x;IWDG_KR=0XAA //看门狗时钟分频值
//==============================================================================
#define E_INT asm("rim") //开中断
#define D_INT asm("sim") //关中断
//==============================================================================
#define CE_AD CBIT(ADC_CSR,7) //清除AD转换结束标志位
#define END_AD GBIT(ADC_CSR,7) //AD转换结束标志位
#define CH0_AD ADC_CSR&=0Xf0 //AD0通道
#define CH1_AD ADC_CSR&=0Xf0;ADC_CSR|=0x01 //AD1通道
#define CH2_AD ADC_CSR&=0Xf0;ADC_CSR|=0x02 //AD2通道
#define CH3_AD ADC_CSR&=0Xf0;ADC_CSR|=0x03 //AD3通道
#define CH4_AD ADC_CSR&=0Xf0;ADC_CSR|=0x04 //AD4通道
#define CH5_AD ADC_CSR&=0Xf0;ADC_CSR|=0x05 //AD5通道
#define CH6_AD ADC_CSR&=0Xf0;ADC_CSR|=0x06 //AD6通道
#define CH7_AD ADC_CSR&=0Xf0;ADC_CSR|=0x07 //AD7通道
#define CH8_AD ADC_CSR&=0Xf0;ADC_CSR|=0x08 //AD8通道
#define CH9_AD ADC_CSR&=0Xf0;ADC_CSR|=0x09 //AD9通道
#define CH10_AD ADC_CSR&=0Xf0;ADC_CSR|=0x0a //AD10通道
#define CH11_AD ADC_CSR&=0Xf0;ADC_CSR|=0x0b //AD11通道
#define CH12_AD ADC_CSR&=0Xf0;ADC_CSR|=0x0c //AD12通道
#define CH13_AD ADC_CSR&=0Xf0;ADC_CSR|=0x0d //AD13通道
#define CH14_AD ADC_CSR&=0Xf0;ADC_CSR|=0x0e //AD14通道
#define CH15_AD ADC_CSR&=0Xf0;ADC_CSR|=0x0f //AD15通道
#define ON_AD SBIT(ADC_CR1,0) //打开AD电源
#define OFF_AD CBIT(ADC_CR1,0) //关闭AD
#define STR_AD SBIT(ADC_CR1,0) //开始AD转换
//==============================================================================
#define DENB_WREEPROM GBIT(FLASH_IAPSR,3) //允许写EE标志位数据
#define DIS_WREEPROM CBIT(FLASH_IAPSR,3) //EE写保护
#define DEND_WREEPROM GBIT(FLASH_IAPSR,2) //写EE完成标志位数据
//==============================================================================
#define TIM1_IEN SBIT(TIM1_IER,0) //定时器1中断开
#define TIM1_IDIS CBIT(TIM1_IER,0) //定时器1中断关
#define TIM1_INT GBIT(TIM1_IER,0) //定时器1中断状态
//==============================================================================
#define BEEP_ENB BEEP_CSR|=0x20
#define BEEP_DIS BEEP_CSR&=(~0x20)
#define G_BEEP (BEEP_CSR&0X20)
//==============================================================================
#define SPI_ENB SPI_CR1|=0x40
#define SPI_DIS SPI_CR1&=(~0x40)
#endif